]> git.lizzy.rs Git - rust.git/blob - src/test/ui/consts/const_in_pattern/reject_non_structural.rs
Merge commit 'b40ea209e7f14c8193ddfc98143967b6a2f4f5c9' into clippyup
[rust.git] / src / test / ui / consts / const_in_pattern / reject_non_structural.rs
1 // This test of structural match checking enumerates the different kinds of
2 // const definitions, collecting cases where the const pattern is rejected.
3 //
4 // Note: Even if a non-structural-match type is part of an expression in a
5 // const's definition, that does not necessarily disqualify the const from being
6 // a match pattern: in principle, we just need the types involved in the final
7 // value to be structurally matchable.
8
9 // See also RFC 1445
10
11 #![feature(type_ascription)]
12 #![warn(indirect_structural_match)]
13 //~^ NOTE lint level is defined here
14
15 #[derive(Copy, Clone, Debug)]
16 struct NoPartialEq;
17
18 #[derive(Copy, Clone, Debug)]
19 struct NoDerive;
20
21 // This impl makes `NoDerive` irreflexive.
22 impl PartialEq for NoDerive { fn eq(&self, _: &Self) -> bool { false } }
23
24 impl Eq for NoDerive { }
25
26 type OND = Option<NoDerive>;
27
28 struct TrivialEq(OND);
29
30 // This impl makes `TrivialEq` trivial.
31 impl PartialEq for TrivialEq { fn eq(&self, _: &Self) -> bool { true } }
32
33 impl Eq for TrivialEq { }
34
35 fn main() {
36     #[derive(PartialEq, Eq, Debug)]
37     enum Derive<X> { Some(X), None, }
38
39     const ENUM: Derive<NoDerive> = Derive::Some(NoDerive);
40     match Derive::Some(NoDerive) { ENUM => dbg!(ENUM), _ => panic!("whoops"), };
41     //~^ ERROR must be annotated with `#[derive(PartialEq, Eq)]`
42
43     const FIELD: OND = TrivialEq(Some(NoDerive)).0;
44     match Some(NoDerive) { FIELD => dbg!(FIELD), _ => panic!("whoops"), };
45     //~^ ERROR must be annotated with `#[derive(PartialEq, Eq)]`
46
47     const NO_DERIVE_SOME: OND = Some(NoDerive);
48     const INDIRECT: OND = NO_DERIVE_SOME;
49     match Some(NoDerive) {INDIRECT => dbg!(INDIRECT), _ => panic!("whoops"), };
50     //~^ ERROR must be annotated with `#[derive(PartialEq, Eq)]`
51
52     const TUPLE: (OND, OND) = (None, Some(NoDerive));
53     match (None, Some(NoDerive)) { TUPLE => dbg!(TUPLE), _ => panic!("whoops"), };
54     //~^ ERROR must be annotated with `#[derive(PartialEq, Eq)]`
55
56     const TYPE_ASCRIPTION: OND = Some(NoDerive): OND;
57     match Some(NoDerive) { TYPE_ASCRIPTION => dbg!(TYPE_ASCRIPTION), _ => panic!("whoops"), };
58     //~^ ERROR must be annotated with `#[derive(PartialEq, Eq)]`
59
60     const ARRAY: [OND; 2] = [None, Some(NoDerive)];
61     match [None, Some(NoDerive)] { ARRAY => dbg!(ARRAY), _ => panic!("whoops"), };
62     //~^ ERROR must be annotated with `#[derive(PartialEq, Eq)]`
63
64     const REPEAT: [OND; 2] = [Some(NoDerive); 2];
65     match [Some(NoDerive); 2] { REPEAT => dbg!(REPEAT), _ => panic!("whoops"), };
66     //~^ ERROR must be annotated with `#[derive(PartialEq, Eq)]`
67     //~| ERROR must be annotated with `#[derive(PartialEq, Eq)]`
68
69     trait Trait: Sized { const ASSOC: Option<Self>; }
70     impl Trait for NoDerive { const ASSOC: Option<NoDerive> = Some(NoDerive); }
71     match Some(NoDerive) { NoDerive::ASSOC => dbg!(NoDerive::ASSOC), _ => panic!("whoops"), };
72     //~^ ERROR must be annotated with `#[derive(PartialEq, Eq)]`
73
74     const BLOCK: OND = { NoDerive; Some(NoDerive) };
75     match Some(NoDerive) { BLOCK => dbg!(BLOCK), _ => panic!("whoops"), };
76     //~^ ERROR must be annotated with `#[derive(PartialEq, Eq)]`
77
78     const ADDR_OF: &OND = &Some(NoDerive);
79     match &Some(NoDerive) { ADDR_OF => dbg!(ADDR_OF), _ => panic!("whoops"), };
80     //~^ WARN must be annotated with `#[derive(PartialEq, Eq)]`
81     //~| WARN previously accepted by the compiler but is being phased out
82     //~| NOTE for more information, see issue #62411
83 }