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