Skip to content

Commit 16fc301

Browse files
committed
Lower guard pattern to THIR
1 parent 0626f33 commit 16fc301

3 files changed

Lines changed: 36 additions & 24 deletions

File tree

compiler/rustc_mir_build/src/thir/pattern/mod.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -447,8 +447,10 @@ impl<'tcx, 'ptcx> PatCtxt<'tcx, 'ptcx> {
447447

448448
hir::PatKind::Or(pats) => PatKind::Or { pats: self.lower_patterns(pats) },
449449

450-
// FIXME(guard_patterns): implement guard pattern lowering
451-
hir::PatKind::Guard(pat, _) => self.lower_pattern(pat).kind,
450+
hir::PatKind::Guard(pat, condition) => PatKind::Guard {
451+
subpattern: self.lower_pattern(pat),
452+
condition: self.upper.mirror_expr(condition),
453+
},
452454

453455
hir::PatKind::Err(guar) => PatKind::Error(guar),
454456
};

tests/ui/pattern/rfc-3637-guard-patterns/name-resolution.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#![expect(incomplete_features)]
77

88
fn good_fn_item(((x if x) | x): bool) -> bool { x }
9+
//~^ ERROR: used binding `x` is possibly-uninitialized [E0381]
910

1011
fn bad_fn_item_1(x: bool, ((y if x) | y): bool) {}
1112
//~^ ERROR cannot find value `x` in this scope

tests/ui/pattern/rfc-3637-guard-patterns/name-resolution.stderr

Lines changed: 31 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error[E0408]: variable `y` is not bound in all patterns
2-
--> $DIR/name-resolution.rs:37:10
2+
--> $DIR/name-resolution.rs:38:10
33
|
44
LL | ((Ok(x) if y) | (Err(y) if x),) => x && y,
55
| ^^^^^^^^^^^^ - variable not in all patterns
@@ -13,7 +13,7 @@ LL + ((Ok(x) if y) | (Err(x) if x),) => x && y,
1313
|
1414

1515
error[E0408]: variable `x` is not bound in all patterns
16-
--> $DIR/name-resolution.rs:37:25
16+
--> $DIR/name-resolution.rs:38:25
1717
|
1818
LL | ((Ok(x) if y) | (Err(y) if x),) => x && y,
1919
| - ^^^^^^^^^^^^^ pattern doesn't bind `x`
@@ -27,15 +27,15 @@ LL + ((Ok(y) if y) | (Err(y) if x),) => x && y,
2727
|
2828

2929
error[E0408]: variable `x` is not bound in all patterns
30-
--> $DIR/name-resolution.rs:63:28
30+
--> $DIR/name-resolution.rs:64:28
3131
|
3232
LL | Some(x if x > 0) | None => {}
3333
| - ^^^^ pattern doesn't bind `x`
3434
| |
3535
| variable not in all patterns
3636

3737
error[E0425]: cannot find value `x` in this scope
38-
--> $DIR/name-resolution.rs:10:34
38+
--> $DIR/name-resolution.rs:11:34
3939
|
4040
LL | fn bad_fn_item_1(x: bool, ((y if x) | y): bool) {}
4141
| ^
@@ -47,7 +47,7 @@ LL + fn bad_fn_item_1(x: bool, ((y if y) | y): bool) {}
4747
|
4848

4949
error[E0425]: cannot find value `y` in this scope
50-
--> $DIR/name-resolution.rs:12:25
50+
--> $DIR/name-resolution.rs:13:25
5151
|
5252
LL | fn bad_fn_item_2(((x if y) | x): bool, y: bool) {}
5353
| ^
@@ -59,7 +59,7 @@ LL + fn bad_fn_item_2(((x if x) | x): bool, y: bool) {}
5959
|
6060

6161
error[E0425]: cannot find value `x` in this scope
62-
--> $DIR/name-resolution.rs:20:18
62+
--> $DIR/name-resolution.rs:21:18
6363
|
6464
LL | (x, y if x) => x && y,
6565
| ^
@@ -71,7 +71,7 @@ LL + (x, y if y) => x && y,
7171
|
7272

7373
error[E0425]: cannot find value `y` in this scope
74-
--> $DIR/name-resolution.rs:22:15
74+
--> $DIR/name-resolution.rs:23:15
7575
|
7676
LL | (x if y, y) => x && y,
7777
| ^
@@ -83,7 +83,7 @@ LL + (x if x, y) => x && y,
8383
|
8484

8585
error[E0425]: cannot find value `x` in this scope
86-
--> $DIR/name-resolution.rs:29:20
86+
--> $DIR/name-resolution.rs:30:20
8787
|
8888
LL | (x @ (y if x),) => x && y,
8989
| ^
@@ -95,7 +95,7 @@ LL + (x @ (y if y),) => x && y,
9595
|
9696

9797
error[E0425]: cannot find value `y` in this scope
98-
--> $DIR/name-resolution.rs:37:20
98+
--> $DIR/name-resolution.rs:38:20
9999
|
100100
LL | ((Ok(x) if y) | (Err(y) if x),) => x && y,
101101
| ^
@@ -107,7 +107,7 @@ LL + ((Ok(x) if x) | (Err(y) if x),) => x && y,
107107
|
108108

109109
error[E0425]: cannot find value `x` in this scope
110-
--> $DIR/name-resolution.rs:37:36
110+
--> $DIR/name-resolution.rs:38:36
111111
|
112112
LL | ((Ok(x) if y) | (Err(y) if x),) => x && y,
113113
| ^
@@ -119,13 +119,13 @@ LL + ((Ok(x) if y) | (Err(y) if y),) => x && y,
119119
|
120120

121121
error[E0425]: cannot find value `nonexistent` in this scope
122-
--> $DIR/name-resolution.rs:44:15
122+
--> $DIR/name-resolution.rs:45:15
123123
|
124124
LL | let (_ if nonexistent) = true;
125125
| ^^^^^^^^^^^ not found in this scope
126126

127127
error[E0425]: cannot find value `x` in this scope
128-
--> $DIR/name-resolution.rs:46:22
128+
--> $DIR/name-resolution.rs:47:22
129129
|
130130
LL | if let ((x, y if x) | (x if y, y)) = (true, true) { x && y; }
131131
| ^
@@ -137,7 +137,7 @@ LL + if let ((x, y if y) | (x if y, y)) = (true, true) { x && y; }
137137
|
138138

139139
error[E0425]: cannot find value `y` in this scope
140-
--> $DIR/name-resolution.rs:46:33
140+
--> $DIR/name-resolution.rs:47:33
141141
|
142142
LL | if let ((x, y if x) | (x if y, y)) = (true, true) { x && y; }
143143
| ^
@@ -149,7 +149,7 @@ LL + if let ((x, y if x) | (x if x, y)) = (true, true) { x && y; }
149149
|
150150

151151
error[E0425]: cannot find value `x` in this scope
152-
--> $DIR/name-resolution.rs:49:25
152+
--> $DIR/name-resolution.rs:50:25
153153
|
154154
LL | while let ((x, y if x) | (x if y, y)) = (true, true) { x && y; }
155155
| ^
@@ -161,7 +161,7 @@ LL + while let ((x, y if y) | (x if y, y)) = (true, true) { x && y; }
161161
|
162162

163163
error[E0425]: cannot find value `y` in this scope
164-
--> $DIR/name-resolution.rs:49:36
164+
--> $DIR/name-resolution.rs:50:36
165165
|
166166
LL | while let ((x, y if x) | (x if y, y)) = (true, true) { x && y; }
167167
| ^
@@ -173,7 +173,7 @@ LL + while let ((x, y if x) | (x if x, y)) = (true, true) { x && y; }
173173
|
174174

175175
error[E0425]: cannot find value `x` in this scope
176-
--> $DIR/name-resolution.rs:52:19
176+
--> $DIR/name-resolution.rs:53:19
177177
|
178178
LL | for ((x, y if x) | (x if y, y)) in [(true, true)] { x && y; }
179179
| ^
@@ -185,7 +185,7 @@ LL + for ((x, y if y) | (x if y, y)) in [(true, true)] { x && y; }
185185
|
186186

187187
error[E0425]: cannot find value `y` in this scope
188-
--> $DIR/name-resolution.rs:52:30
188+
--> $DIR/name-resolution.rs:53:30
189189
|
190190
LL | for ((x, y if x) | (x if y, y)) in [(true, true)] { x && y; }
191191
| ^
@@ -197,7 +197,7 @@ LL + for ((x, y if x) | (x if x, y)) in [(true, true)] { x && y; }
197197
|
198198

199199
error[E0425]: cannot find value `y` in this scope
200-
--> $DIR/name-resolution.rs:57:13
200+
--> $DIR/name-resolution.rs:58:13
201201
|
202202
LL | (|(x if y), (y if x)| x && y)(true, true);
203203
| ^
@@ -209,7 +209,7 @@ LL + (|(x if x), (y if x)| x && y)(true, true);
209209
|
210210

211211
error[E0425]: cannot find value `x` in this scope
212-
--> $DIR/name-resolution.rs:57:23
212+
--> $DIR/name-resolution.rs:58:23
213213
|
214214
LL | (|(x if y), (y if x)| x && y)(true, true);
215215
| ^
@@ -221,15 +221,24 @@ LL + (|(x if y), (y if y)| x && y)(true, true);
221221
|
222222

223223
error[E0308]: mismatched types
224-
--> $DIR/name-resolution.rs:75:18
224+
--> $DIR/name-resolution.rs:76:18
225225
|
226226
LL | local if local => 0,
227227
| ^^^^^ expected `bool`, found `({integer}, {integer})`
228228
|
229229
= note: expected type `bool`
230230
found tuple `({integer}, {integer})`
231231

232-
error: aborting due to 20 previous errors
232+
error[E0381]: used binding `x` is possibly-uninitialized
233+
--> $DIR/name-resolution.rs:8:49
234+
|
235+
LL | fn good_fn_item(((x if x) | x): bool) -> bool { x }
236+
| - - ^ `x` used here but it is possibly-uninitialized
237+
| | |
238+
| | binding initialized here in some conditions
239+
| binding declared here but left uninitialized
240+
241+
error: aborting due to 21 previous errors
233242

234-
Some errors have detailed explanations: E0308, E0408, E0425.
243+
Some errors have detailed explanations: E0308, E0381, E0408, E0425.
235244
For more information about an error, try `rustc --explain E0308`.

0 commit comments

Comments
 (0)