]> git.lizzy.rs Git - rust.git/blob - src/test/run-pass/match-arm-statics.rs
Auto merge of #24072 - ebfull:explain_closure_type_err, r=pnkfelix
[rust.git] / src / test / run-pass / match-arm-statics.rs
1 // Copyright 2014 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10
11
12 struct NewBool(bool);
13
14 enum Direction {
15     North,
16     East,
17     South,
18     West
19 }
20 struct Foo {
21     bar: Option<Direction>,
22     baz: NewBool
23 }
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)]
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     use glfw;
50
51     fn action_to_str(state: glfw::InputState) -> &'static str {
52         use glfw::{RELEASE, PRESS, REPEAT};
53         match state {
54             RELEASE => { "Released" }
55             PRESS   => { "Pressed"  }
56             REPEAT  => { "Repeated" }
57             _       => { "Unknown"  }
58         }
59     }
60
61     assert_eq!(action_to_str(glfw::RELEASE), "Released");
62     assert_eq!(action_to_str(glfw::PRESS), "Pressed");
63     assert_eq!(action_to_str(glfw::REPEAT), "Repeated");
64 }
65
66 fn issue_13626() {
67     const VAL: [u8; 1] = [0];
68     match [1] {
69         VAL => unreachable!(),
70         _ => ()
71     }
72 }
73
74 fn issue_14576() {
75     type Foo = (i32, i32);
76     const ON: Foo = (1, 1);
77     const OFF: Foo = (0, 0);
78
79     match (1, 1) {
80         OFF => unreachable!(),
81         ON => (),
82         _ => unreachable!()
83     }
84
85     enum C { D = 3, E = 4 }
86     const F : C = C::D;
87
88     assert_eq!(match C::D { F => 1, _ => 2, }, 1);
89 }
90
91 fn issue_13731() {
92     enum A { AA(()) }
93     const B: A = A::AA(());
94
95     match A::AA(()) {
96         B => ()
97     }
98 }
99
100 fn issue_15393() {
101     #![allow(dead_code)]
102     struct Flags {
103         bits: usize
104     }
105
106     const FOO: Flags = Flags { bits: 0x01 };
107     const BAR: Flags = Flags { bits: 0x02 };
108     match (Flags { bits: 0x02 }) {
109         FOO => unreachable!(),
110         BAR => (),
111         _ => unreachable!()
112     }
113 }
114
115 fn main() {
116     assert_eq!(match (true, false) {
117         TRUE_TRUE => 1,
118         (false, false) => 2,
119         (false, true) => 3,
120         (true, false) => 4
121     }, 4);
122
123     assert_eq!(match Some(Some(Direction::North)) {
124         Some(NONE) => 1,
125         Some(Some(Direction::North)) => 2,
126         Some(Some(EAST)) => 3,
127         Some(Some(Direction::South)) => 4,
128         Some(Some(Direction::West)) => 5,
129         None => 6
130     }, 2);
131
132     assert_eq!(match (Foo { bar: Some(Direction::West), baz: NewBool(true) }) {
133         Foo { bar: None, baz: NewBool(true) } => 1,
134         Foo { bar: NONE, baz: NEW_FALSE } => 2,
135         STATIC_FOO => 3,
136         Foo { bar: _, baz: NEW_FALSE } => 4,
137         Foo { bar: Some(Direction::West), baz: NewBool(true) } => 5,
138         Foo { bar: Some(Direction::South), baz: NewBool(true) } => 6,
139         Foo { bar: Some(EAST), .. } => 7,
140         Foo { bar: Some(Direction::North), baz: NewBool(true) } => 8
141     }, 5);
142
143     assert_eq!(match (EnumWithStructVariants::Variant2 { dir: Direction::North }) {
144         EnumWithStructVariants::Variant1(true) => 1,
145         EnumWithStructVariants::Variant1(false) => 2,
146         EnumWithStructVariants::Variant2 { dir: Direction::West } => 3,
147         VARIANT2_NORTH => 4,
148         EnumWithStructVariants::Variant2 { dir: Direction::South } => 5,
149         EnumWithStructVariants::Variant2 { dir: Direction::East } => 6
150     }, 4);
151
152     issue_6533();
153     issue_13626();
154     issue_13731();
155     issue_14576();
156     issue_15393();
157 }