]> git.lizzy.rs Git - rust.git/blob - src/test/ui/binding/match-arm-statics.rs
Merge commit '5988bbd24aa87732bfa1d111ba00bcdaa22c481a' into sync_cg_clif-2020-11-27
[rust.git] / src / test / ui / binding / match-arm-statics.rs
1 // run-pass
2 #![allow(dead_code)]
3 // compile-flags: -g
4 // ignore-asmjs wasm2js does not support source maps yet
5
6 #[derive(PartialEq, Eq)]
7 struct NewBool(bool);
8
9 #[derive(PartialEq, Eq)]
10 enum Direction {
11     North,
12     East,
13     South,
14     West
15 }
16
17 #[derive(PartialEq, Eq)]
18 struct Foo {
19     bar: Option<Direction>,
20     baz: NewBool
21 }
22
23 #[derive(PartialEq, Eq)]
24 enum EnumWithStructVariants {
25     Variant1(bool),
26     Variant2 {
27         dir: Direction
28     }
29 }
30
31 const TRUE_TRUE: (bool, bool) = (true, true);
32 const NONE: Option<Direction> = None;
33 const EAST: Direction = Direction::East;
34 const NEW_FALSE: NewBool = NewBool(false);
35 const STATIC_FOO: Foo = Foo { bar: Some(Direction::South), baz: NEW_FALSE };
36 const VARIANT2_NORTH: EnumWithStructVariants = EnumWithStructVariants::Variant2 {
37     dir: Direction::North };
38
39 pub mod glfw {
40     #[derive(Copy, Clone, PartialEq, Eq)]
41     pub struct InputState(usize);
42
43     pub const RELEASE  : InputState = InputState(0);
44     pub const PRESS    : InputState = InputState(1);
45     pub const REPEAT   : InputState = InputState(2);
46 }
47
48 fn issue_6533() {
49     fn action_to_str(state: glfw::InputState) -> &'static str {
50         use glfw::{RELEASE, PRESS, REPEAT};
51         match state {
52             RELEASE => { "Released" }
53             PRESS   => { "Pressed"  }
54             REPEAT  => { "Repeated" }
55             _       => { "Unknown"  }
56         }
57     }
58
59     assert_eq!(action_to_str(glfw::RELEASE), "Released");
60     assert_eq!(action_to_str(glfw::PRESS), "Pressed");
61     assert_eq!(action_to_str(glfw::REPEAT), "Repeated");
62 }
63
64 fn issue_13626() {
65     const VAL: [u8; 1] = [0];
66     match [1] {
67         VAL => unreachable!(),
68         _ => ()
69     }
70 }
71
72 fn issue_14576() {
73     type Foo = (i32, i32);
74     const ON: Foo = (1, 1);
75     const OFF: Foo = (0, 0);
76
77     match (1, 1) {
78         OFF => unreachable!(),
79         ON => (),
80         _ => unreachable!()
81     }
82
83     #[derive(PartialEq, Eq)]
84     enum C { D = 3, E = 4 }
85     const F : C = C::D;
86
87     assert_eq!(match C::D { F => 1, _ => 2, }, 1);
88
89     // test gaps
90     #[derive(PartialEq, Eq)]
91     enum G { H = 3, I = 5 }
92     const K : G = G::I;
93
94     assert_eq!(match G::I { K => 1, _ => 2, }, 1);
95 }
96
97 fn issue_13731() {
98     #[derive(PartialEq, Eq)]
99     enum A { AA(()) }
100     const B: A = A::AA(());
101
102     match A::AA(()) {
103         B => ()
104     }
105 }
106
107 fn issue_15393() {
108     #![allow(dead_code)]
109     #[derive(PartialEq, Eq)]
110     struct Flags {
111         bits: usize
112     }
113
114     const FOO: Flags = Flags { bits: 0x01 };
115     const BAR: Flags = Flags { bits: 0x02 };
116     match (Flags { bits: 0x02 }) {
117         FOO => unreachable!(),
118         BAR => (),
119         _ => unreachable!()
120     }
121 }
122
123 fn main() {
124     assert_eq!(match (true, false) {
125         TRUE_TRUE => 1,
126         (false, false) => 2,
127         (false, true) => 3,
128         (true, false) => 4
129     }, 4);
130
131     assert_eq!(match Some(Some(Direction::North)) {
132         Some(NONE) => 1,
133         Some(Some(Direction::North)) => 2,
134         Some(Some(EAST)) => 3,
135         Some(Some(Direction::South)) => 4,
136         Some(Some(Direction::West)) => 5,
137         None => 6
138     }, 2);
139
140     assert_eq!(match (Foo { bar: Some(Direction::West), baz: NewBool(true) }) {
141         Foo { bar: None, baz: NewBool(true) } => 1,
142         Foo { bar: NONE, baz: NEW_FALSE } => 2,
143         STATIC_FOO => 3,
144         Foo { bar: _, baz: NEW_FALSE } => 4,
145         Foo { bar: Some(Direction::West), baz: NewBool(true) } => 5,
146         Foo { bar: Some(Direction::South), baz: NewBool(true) } => 6,
147         Foo { bar: Some(EAST), .. } => 7,
148         Foo { bar: Some(Direction::North), baz: NewBool(true) } => 8
149     }, 5);
150
151     assert_eq!(match (EnumWithStructVariants::Variant2 { dir: Direction::North }) {
152         EnumWithStructVariants::Variant1(true) => 1,
153         EnumWithStructVariants::Variant1(false) => 2,
154         EnumWithStructVariants::Variant2 { dir: Direction::West } => 3,
155         VARIANT2_NORTH => 4,
156         EnumWithStructVariants::Variant2 { dir: Direction::South } => 5,
157         EnumWithStructVariants::Variant2 { dir: Direction::East } => 6
158     }, 4);
159
160     issue_6533();
161     issue_13626();
162     issue_13731();
163     issue_14576();
164     issue_15393();
165 }