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