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