1 // This test of structural match checking enumerates the different kinds of
2 // const definitions, collecting cases where the const pattern is rejected.
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.
11 #![feature(type_ascription)]
12 #![warn(indirect_structural_match)]
13 //~^ NOTE lint level is defined here
15 #[derive(Copy, Clone, Debug)]
18 #[derive(Copy, Clone, Debug)]
21 // This impl makes `NoDerive` irreflexive.
22 impl PartialEq for NoDerive { fn eq(&self, _: &Self) -> bool { false } }
24 impl Eq for NoDerive { }
26 type OND = Option<NoDerive>;
28 struct TrivialEq(OND);
30 // This impl makes `TrivialEq` trivial.
31 impl PartialEq for TrivialEq { fn eq(&self, _: &Self) -> bool { true } }
33 impl Eq for TrivialEq { }
36 #[derive(PartialEq, Eq, Debug)]
37 enum Derive<X> { Some(X), None, }
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)]`
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)]`
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)]`
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)]`
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)]`
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)]`
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)]`
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)]`
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)]`
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