• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // run-pass
2 
3 #![warn(indirect_structural_match)]
4 
5 // This test is checking our logic for structural match checking by enumerating
6 // the different kinds of const expressions. This test is collecting cases where
7 // we have accepted the const expression as a pattern in the past and wish to
8 // continue doing so.
9 //
10 // Even if a non-structural-match type is part of an expression in a const's
11 // definition, that does not necessarily disqualify the const from being a match
12 // pattern: in principle, we just need the types involved in the final value to
13 // be structurally matchable.
14 
15 // See also RFC 1445
16 
17 #![feature(type_ascription)]
18 
19 #[derive(Copy, Clone, Debug)]
20 struct NoPartialEq(u32);
21 
22 #[derive(Copy, Clone, Debug)]
23 struct NoDerive(u32);
24 
25 // This impl makes `NoDerive` irreflexive.
eq(&self, _: &Self) -> bool26 impl PartialEq for NoDerive { fn eq(&self, _: &Self) -> bool { false } }
27 impl Eq for NoDerive { }
28 
29 type OND = Option<NoDerive>;
30 
main()31 fn main() {
32     const FIELD1: u32 = NoPartialEq(1).0;
33     match 1 { FIELD1 => dbg!(FIELD1), _ => panic!("whoops"), };
34     const FIELD2: u32 = NoDerive(1).0;
35     match 1 { FIELD2 => dbg!(FIELD2), _ => panic!("whoops"), };
36 
37     enum CLike { One = 1, #[allow(dead_code)] Two = 2, }
38     const ONE_CAST: u32 = CLike::One as u32;
39     match 1 { ONE_CAST => dbg!(ONE_CAST), _ => panic!("whoops"), };
40 
41     const NO_DERIVE_NONE: OND = None;
42     const INDIRECT: OND = NO_DERIVE_NONE;
43     match None { INDIRECT => dbg!(INDIRECT), _ => panic!("whoops"), };
44 
45     const TUPLE: (OND, OND) = (None, None);
46     match (None, None) { TUPLE => dbg!(TUPLE), _ => panic!("whoops"), };
47 
48     const TYPE_ASCRIPTION: OND = type_ascribe!(None, OND);
49     match None { TYPE_ASCRIPTION => dbg!(TYPE_ASCRIPTION), _ => panic!("whoops"), };
50 
51     const ARRAY: [OND; 2] = [None, None];
52     match [None; 2] { ARRAY => dbg!(ARRAY), _ => panic!("whoops"), };
53 
54     const REPEAT: [OND; 2] = [None; 2];
55     match [None, None] { REPEAT => dbg!(REPEAT), _ => panic!("whoops"), };
56 
57     trait Trait: Sized { const ASSOC: Option<Self>; }
58     impl Trait for NoDerive { const ASSOC: Option<NoDerive> = None; }
59     match None { NoDerive::ASSOC => dbg!(NoDerive::ASSOC), _ => panic!("whoops"), };
60 
61     const BLOCK: OND = { NoDerive(10); None };
62     match None { BLOCK => dbg!(BLOCK), _ => panic!("whoops"), };
63 
64     const ADDR_OF: &OND = &None;
65     match &None { ADDR_OF => dbg!(ADDR_OF),  _ => panic!("whoops"), };
66 }
67