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