]> git.proxmox.com Git - rustc.git/blob - src/test/run-pass/issue-13027.rs
Imported Upstream version 1.1.0+dfsg1
[rustc.git] / src / test / run-pass / issue-13027.rs
1 // Copyright 2012-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 // Tests that match expression handles overlapped literal and range
13 // properly in the presence of guard function.
14
15 #![feature(slice_patterns)]
16
17 fn val() -> usize { 1 }
18
19 static CONST: usize = 1;
20
21 pub fn main() {
22 lit_shadow_range();
23 range_shadow_lit();
24 range_shadow_range();
25 multi_pats_shadow_lit();
26 multi_pats_shadow_range();
27 lit_shadow_multi_pats();
28 range_shadow_multi_pats();
29 misc();
30 }
31
32 fn lit_shadow_range() {
33 assert_eq!(2, match 1 {
34 1 if false => 1,
35 1...2 => 2,
36 _ => 3
37 });
38
39 let x = 0;
40 assert_eq!(2, match x+1 {
41 0 => 0,
42 1 if false => 1,
43 1...2 => 2,
44 _ => 3
45 });
46
47 assert_eq!(2, match val() {
48 1 if false => 1,
49 1...2 => 2,
50 _ => 3
51 });
52
53 assert_eq!(2, match CONST {
54 0 => 0,
55 1 if false => 1,
56 1...2 => 2,
57 _ => 3
58 });
59
60 // value is out of the range of second arm, should match wildcard pattern
61 assert_eq!(3, match 3 {
62 1 if false => 1,
63 1...2 => 2,
64 _ => 3
65 });
66 }
67
68 fn range_shadow_lit() {
69 assert_eq!(2, match 1 {
70 1...2 if false => 1,
71 1 => 2,
72 _ => 3
73 });
74
75 let x = 0;
76 assert_eq!(2, match x+1 {
77 0 => 0,
78 1...2 if false => 1,
79 1 => 2,
80 _ => 3
81 });
82
83 assert_eq!(2, match val() {
84 1...2 if false => 1,
85 1 => 2,
86 _ => 3
87 });
88
89 assert_eq!(2, match CONST {
90 0 => 0,
91 1...2 if false => 1,
92 1 => 2,
93 _ => 3
94 });
95
96 // ditto
97 assert_eq!(3, match 3 {
98 1...2 if false => 1,
99 1 => 2,
100 _ => 3
101 });
102 }
103
104 fn range_shadow_range() {
105 assert_eq!(2, match 1 {
106 0...2 if false => 1,
107 1...3 => 2,
108 _ => 3,
109 });
110
111 let x = 0;
112 assert_eq!(2, match x+1 {
113 100 => 0,
114 0...2 if false => 1,
115 1...3 => 2,
116 _ => 3,
117 });
118
119 assert_eq!(2, match val() {
120 0...2 if false => 1,
121 1...3 => 2,
122 _ => 3,
123 });
124
125 assert_eq!(2, match CONST {
126 100 => 0,
127 0...2 if false => 1,
128 1...3 => 2,
129 _ => 3,
130 });
131
132 // ditto
133 assert_eq!(3, match 5 {
134 0...2 if false => 1,
135 1...3 => 2,
136 _ => 3,
137 });
138 }
139
140 fn multi_pats_shadow_lit() {
141 assert_eq!(2, match 1 {
142 100 => 0,
143 0 | 1...10 if false => 1,
144 1 => 2,
145 _ => 3,
146 });
147 }
148
149 fn multi_pats_shadow_range() {
150 assert_eq!(2, match 1 {
151 100 => 0,
152 0 | 1...10 if false => 1,
153 1...3 => 2,
154 _ => 3,
155 });
156 }
157
158 fn lit_shadow_multi_pats() {
159 assert_eq!(2, match 1 {
160 100 => 0,
161 1 if false => 1,
162 0 | 1...10 => 2,
163 _ => 3,
164 });
165 }
166
167 fn range_shadow_multi_pats() {
168 assert_eq!(2, match 1 {
169 100 => 0,
170 1...3 if false => 1,
171 0 | 1...10 => 2,
172 _ => 3,
173 });
174 }
175
176 fn misc() {
177 enum Foo {
178 Bar(usize, bool)
179 }
180 // This test basically mimics how trace_macros! macro is implemented,
181 // which is a rare combination of vector patterns, multiple wild-card
182 // patterns and guard functions.
183 let r = match [Foo::Bar(0, false)] {
184 [Foo::Bar(_, pred)] if pred => 1,
185 [Foo::Bar(_, pred)] if !pred => 2,
186 _ => 0,
187 };
188 assert_eq!(2, r);
189 }