]> git.proxmox.com Git - rustc.git/blame - src/test/run-pass/match-arm-statics.rs
Imported Upstream version 1.9.0+dfsg1
[rustc.git] / src / test / run-pass / match-arm-statics.rs
CommitLineData
1a4d82fc
JJ
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
c34b1796 11
54a0048b 12#[derive(PartialEq, Eq)]
1a4d82fc
JJ
13struct NewBool(bool);
14
54a0048b 15#[derive(PartialEq, Eq)]
1a4d82fc
JJ
16enum Direction {
17 North,
18 East,
19 South,
20 West
21}
54a0048b
SL
22
23#[derive(PartialEq, Eq)]
1a4d82fc
JJ
24struct Foo {
25 bar: Option<Direction>,
26 baz: NewBool
27}
54a0048b
SL
28
29#[derive(PartialEq, Eq)]
1a4d82fc
JJ
30enum EnumWithStructVariants {
31 Variant1(bool),
32 Variant2 {
33 dir: Direction
34 }
35}
36
37const TRUE_TRUE: (bool, bool) = (true, true);
38const NONE: Option<Direction> = None;
39const EAST: Direction = Direction::East;
40const NEW_FALSE: NewBool = NewBool(false);
41const STATIC_FOO: Foo = Foo { bar: Some(Direction::South), baz: NEW_FALSE };
42const VARIANT2_NORTH: EnumWithStructVariants = EnumWithStructVariants::Variant2 {
43 dir: Direction::North };
44
45pub mod glfw {
54a0048b 46 #[derive(Copy, Clone, PartialEq, Eq)]
c34b1796 47 pub struct InputState(usize);
1a4d82fc 48
1a4d82fc
JJ
49 pub const RELEASE : InputState = InputState(0);
50 pub const PRESS : InputState = InputState(1);
51 pub const REPEAT : InputState = InputState(2);
52}
53
54fn 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
72fn issue_13626() {
73 const VAL: [u8; 1] = [0];
74 match [1] {
75 VAL => unreachable!(),
76 _ => ()
77 }
78}
79
80fn 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
54a0048b 91 #[derive(PartialEq, Eq)]
1a4d82fc
JJ
92 enum C { D = 3, E = 4 }
93 const F : C = C::D;
94
85aaf69f 95 assert_eq!(match C::D { F => 1, _ => 2, }, 1);
1a4d82fc
JJ
96}
97
98fn issue_13731() {
54a0048b 99 #[derive(PartialEq, Eq)]
1a4d82fc
JJ
100 enum A { AA(()) }
101 const B: A = A::AA(());
102
103 match A::AA(()) {
104 B => ()
105 }
106}
107
108fn issue_15393() {
109 #![allow(dead_code)]
54a0048b 110 #[derive(PartialEq, Eq)]
1a4d82fc 111 struct Flags {
c34b1796 112 bits: usize
1a4d82fc
JJ
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
124fn main() {
125 assert_eq!(match (true, false) {
85aaf69f 126 TRUE_TRUE => 1,
1a4d82fc
JJ
127 (false, false) => 2,
128 (false, true) => 3,
129 (true, false) => 4
130 }, 4);
131
132 assert_eq!(match Some(Some(Direction::North)) {
85aaf69f 133 Some(NONE) => 1,
1a4d82fc
JJ
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) }) {
85aaf69f 142 Foo { bar: None, baz: NewBool(true) } => 1,
1a4d82fc
JJ
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 }) {
85aaf69f 153 EnumWithStructVariants::Variant1(true) => 1,
1a4d82fc
JJ
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}