|
| 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 | + //~| ERROR must be annotated with `#[derive(PartialEq, Eq)]` |
| 43 | + |
| 44 | + const FIELD: OND = TrivialEq(Some(NoDerive)).0; |
| 45 | + match Some(NoDerive) { FIELD => dbg!(FIELD), _ => panic!("whoops"), }; |
| 46 | + //~^ ERROR must be annotated with `#[derive(PartialEq, Eq)]` |
| 47 | + //~| ERROR must be annotated with `#[derive(PartialEq, Eq)]` |
| 48 | + |
| 49 | + const NO_DERIVE_SOME: OND = Some(NoDerive); |
| 50 | + const INDIRECT: OND = NO_DERIVE_SOME; |
| 51 | + match Some(NoDerive) {INDIRECT => dbg!(INDIRECT), _ => panic!("whoops"), }; |
| 52 | + //~^ ERROR must be annotated with `#[derive(PartialEq, Eq)]` |
| 53 | + //~| ERROR must be annotated with `#[derive(PartialEq, Eq)]` |
| 54 | + |
| 55 | + const TUPLE: (OND, OND) = (None, Some(NoDerive)); |
| 56 | + match (None, Some(NoDerive)) { TUPLE => dbg!(TUPLE), _ => panic!("whoops"), }; |
| 57 | + //~^ ERROR must be annotated with `#[derive(PartialEq, Eq)]` |
| 58 | + //~| ERROR must be annotated with `#[derive(PartialEq, Eq)]` |
| 59 | + |
| 60 | + const TYPE: OND = Some(NoDerive): OND; |
| 61 | + match Some(NoDerive) { TYPE => dbg!(TYPE), _ => panic!("whoops"), }; |
| 62 | + //~^ ERROR must be annotated with `#[derive(PartialEq, Eq)]` |
| 63 | + //~| ERROR must be annotated with `#[derive(PartialEq, Eq)]` |
| 64 | + |
| 65 | + const ARRAY: [OND; 2] = [None, Some(NoDerive)]; |
| 66 | + match [None, Some(NoDerive)] { ARRAY => dbg!(ARRAY), _ => panic!("whoops"), }; |
| 67 | + //~^ ERROR must be annotated with `#[derive(PartialEq, Eq)]` |
| 68 | + //~| ERROR must be annotated with `#[derive(PartialEq, Eq)]` |
| 69 | + |
| 70 | + const REPEAT: [OND; 2] = [Some(NoDerive); 2]; |
| 71 | + match [Some(NoDerive); 2] { REPEAT => dbg!(REPEAT), _ => panic!("whoops"), }; |
| 72 | + //~^ ERROR must be annotated with `#[derive(PartialEq, Eq)]` |
| 73 | + //~| ERROR must be annotated with `#[derive(PartialEq, Eq)]` |
| 74 | + //~| ERROR must be annotated with `#[derive(PartialEq, Eq)]` |
| 75 | + //~| ERROR must be annotated with `#[derive(PartialEq, Eq)]` |
| 76 | + |
| 77 | + trait Trait: Sized { const ASSOC: Option<Self>; } |
| 78 | + impl Trait for NoDerive { const ASSOC: Option<NoDerive> = Some(NoDerive); } |
| 79 | + match Some(NoDerive) { NoDerive::ASSOC => dbg!(NoDerive::ASSOC), _ => panic!("whoops"), }; |
| 80 | + //~^ ERROR must be annotated with `#[derive(PartialEq, Eq)]` |
| 81 | + //~| ERROR must be annotated with `#[derive(PartialEq, Eq)]` |
| 82 | + |
| 83 | + const BLOCK: OND = { NoDerive; Some(NoDerive) }; |
| 84 | + match Some(NoDerive) { BLOCK => dbg!(BLOCK), _ => panic!("whoops"), }; |
| 85 | + //~^ ERROR must be annotated with `#[derive(PartialEq, Eq)]` |
| 86 | + //~| ERROR must be annotated with `#[derive(PartialEq, Eq)]` |
| 87 | + |
| 88 | + const ADDR_OF: &OND = &Some(NoDerive); |
| 89 | + match &Some(NoDerive) { ADDR_OF => dbg!(ADDR_OF), _ => panic!("whoops"), }; |
| 90 | + //~^ WARN must be annotated with `#[derive(PartialEq, Eq)]` |
| 91 | + //~| WARN previously accepted by the compiler but is being phased out |
| 92 | + //~| NOTE for more information, see issue #62411 |
| 93 | +} |
0 commit comments