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