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