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