File: reject_non_structural.rs

package info (click to toggle)
rustc 1.87.0%2Bdfsg1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 925,564 kB
  • sloc: xml: 158,127; python: 36,039; javascript: 19,761; sh: 19,737; cpp: 18,981; ansic: 13,133; asm: 4,376; makefile: 710; perl: 29; lisp: 28; ruby: 19; sql: 11
file content (110 lines) | stat: -rw-r--r-- 5,062 bytes parent folder | download | duplicates (15)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
//@ compile-flags: -Zdeduplicate-diagnostics=yes

// This test of structural match checking enumerates the different kinds of
// const definitions, collecting cases where the const pattern is rejected.
//
// Note: Even if a non-structural-match type is part of an expression in a
// const's definition, that does not necessarily disqualify the const from being
// a match pattern: in principle, we just need the types involved in the final
// value to be structurally matchable.

// See also RFC 1445

#![feature(type_ascription)]

#[derive(Copy, Clone, Debug)]
struct NoPartialEq;

#[derive(Copy, Clone, Debug)]
struct NoDerive;
//~^ NOTE must be annotated with `#[derive(PartialEq)]`
//~| NOTE must be annotated with `#[derive(PartialEq)]`
//~| NOTE must be annotated with `#[derive(PartialEq)]`
//~| NOTE must be annotated with `#[derive(PartialEq)]`
//~| NOTE must be annotated with `#[derive(PartialEq)]`
//~| NOTE must be annotated with `#[derive(PartialEq)]`
//~| NOTE must be annotated with `#[derive(PartialEq)]`
//~| NOTE must be annotated with `#[derive(PartialEq)]`
//~| NOTE must be annotated with `#[derive(PartialEq)]`
//~| NOTE must be annotated with `#[derive(PartialEq)]`

// This impl makes `NoDerive` irreflexive.
impl PartialEq for NoDerive { fn eq(&self, _: &Self) -> bool { false } }
//~^ NOTE StructuralPartialEq.html for details
//~| NOTE StructuralPartialEq.html for details
//~| NOTE StructuralPartialEq.html for details
//~| NOTE StructuralPartialEq.html for details
//~| NOTE StructuralPartialEq.html for details
//~| NOTE StructuralPartialEq.html for details
//~| NOTE StructuralPartialEq.html for details
//~| NOTE StructuralPartialEq.html for details
//~| NOTE StructuralPartialEq.html for details
//~| NOTE StructuralPartialEq.html for details

impl Eq for NoDerive { }

type OND = Option<NoDerive>;

struct TrivialEq(OND);

// This impl makes `TrivialEq` trivial.
impl PartialEq for TrivialEq { fn eq(&self, _: &Self) -> bool { true } }

impl Eq for TrivialEq { }

fn main() {
    #[derive(PartialEq, Eq, Debug)]
    enum Derive<X> { Some(X), None, }

    const ENUM: Derive<NoDerive> = Derive::Some(NoDerive); //~ NOTE constant defined here
    match Derive::Some(NoDerive) { ENUM => dbg!(ENUM), _ => panic!("whoops"), };
    //~^ ERROR constant of non-structural type `NoDerive` in a pattern
    //~| NOTE constant of non-structural type

    const FIELD: OND = TrivialEq(Some(NoDerive)).0; //~ NOTE constant defined here
    match Some(NoDerive) { FIELD => dbg!(FIELD), _ => panic!("whoops"), };
    //~^ ERROR constant of non-structural type `NoDerive` in a pattern
    //~| NOTE constant of non-structural type

    const NO_DERIVE_SOME: OND = Some(NoDerive);
    const INDIRECT: OND = NO_DERIVE_SOME; //~ NOTE constant defined here
    match Some(NoDerive) {INDIRECT => dbg!(INDIRECT), _ => panic!("whoops"), };
    //~^ ERROR constant of non-structural type `NoDerive` in a pattern
    //~| NOTE constant of non-structural type

    const TUPLE: (OND, OND) = (None, Some(NoDerive)); //~ NOTE constant defined here
    match (None, Some(NoDerive)) { TUPLE => dbg!(TUPLE), _ => panic!("whoops"), };
    //~^ ERROR constant of non-structural type `NoDerive` in a pattern
    //~| NOTE constant of non-structural type

    const TYPE_ASCRIPTION: OND = type_ascribe!(Some(NoDerive), OND); //~ NOTE constant defined here
    match Some(NoDerive) { TYPE_ASCRIPTION => dbg!(TYPE_ASCRIPTION), _ => panic!("whoops"), };
    //~^ ERROR constant of non-structural type `NoDerive` in a pattern
    //~| NOTE constant of non-structural type

    const ARRAY: [OND; 2] = [None, Some(NoDerive)]; //~ NOTE constant defined here
    match [None, Some(NoDerive)] { ARRAY => dbg!(ARRAY), _ => panic!("whoops"), };
    //~^ ERROR constant of non-structural type `NoDerive` in a pattern
    //~| NOTE constant of non-structural type

    const REPEAT: [OND; 2] = [Some(NoDerive); 2]; //~ NOTE constant defined here
    match [Some(NoDerive); 2] { REPEAT => dbg!(REPEAT), _ => panic!("whoops"), };
    //~^ ERROR constant of non-structural type `NoDerive` in a pattern
    //~| NOTE constant of non-structural type

    trait Trait: Sized { const ASSOC: Option<Self>; } //~ NOTE constant defined here
    impl Trait for NoDerive { const ASSOC: Option<NoDerive> = Some(NoDerive); }
    match Some(NoDerive) { NoDerive::ASSOC => dbg!(NoDerive::ASSOC), _ => panic!("whoops"), };
    //~^ ERROR constant of non-structural type `NoDerive` in a pattern
    //~| NOTE constant of non-structural type

    const BLOCK: OND = { NoDerive; Some(NoDerive) }; //~ NOTE constant defined here
    match Some(NoDerive) { BLOCK => dbg!(BLOCK), _ => panic!("whoops"), };
    //~^ ERROR constant of non-structural type `NoDerive` in a pattern
    //~| NOTE constant of non-structural type

    const ADDR_OF: &OND = &Some(NoDerive); //~ NOTE constant defined here
    match &Some(NoDerive) { ADDR_OF => dbg!(ADDR_OF), _ => panic!("whoops"), };
    //~^ ERROR constant of non-structural type `NoDerive` in a pattern
    //~| NOTE constant of non-structural type
}