Skip to content

Commit 25283f4

Browse files
committed
Auto merge of rust-lang#115371 - matthewjasper:if-let-guard-parsing, r=cjgillot
Make if let guard parsing consistent with normal guards - Add tests that struct expressions are not allowed in `if let` and `while let` (no change, consistent with `if` and `while`) - Allow struct expressions in `if let` guards (consistent with `if` guards). r? `@cjgillot` Closes rust-lang#93817 cc rust-lang#51114
2 parents ff902ff + 89235fd commit 25283f4

File tree

6 files changed

+48
-5
lines changed

6 files changed

+48
-5
lines changed

compiler/rustc_parse/src/parser/expr.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -2477,9 +2477,7 @@ impl<'a> Parser<'a> {
24772477
} else {
24782478
self.expect(&token::Eq)?;
24792479
}
2480-
let expr = self.with_res(self.restrictions | Restrictions::NO_STRUCT_LITERAL, |this| {
2481-
this.parse_expr_assoc_with(1 + prec_let_scrutinee_needs_par(), None.into())
2482-
})?;
2480+
let expr = self.parse_expr_assoc_with(1 + prec_let_scrutinee_needs_par(), None.into())?;
24832481
let span = lo.to(expr.span);
24842482
self.sess.gated_spans.gate(sym::let_chains, span);
24852483
Ok(self.mk_expr(span, ExprKind::Let(pat, expr, span)))

tests/ui/parser/struct-literal-in-if.rs

+5
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,9 @@ fn main() {
1414
}.hi() {
1515
println!("yo");
1616
}
17+
if let true = Foo { //~ ERROR struct literals are not allowed here
18+
x: 3
19+
}.hi() {
20+
println!("yo");
21+
}
1722
}

tests/ui/parser/struct-literal-in-if.stderr

+17-1
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,21 @@ LL | x: 3
1414
LL ~ }).hi() {
1515
|
1616

17-
error: aborting due to previous error
17+
error: struct literals are not allowed here
18+
--> $DIR/struct-literal-in-if.rs:17:19
19+
|
20+
LL | if let true = Foo {
21+
| ___________________^
22+
LL | | x: 3
23+
LL | | }.hi() {
24+
| |_____^
25+
|
26+
help: surround the struct literal with parentheses
27+
|
28+
LL ~ if let true = (Foo {
29+
LL | x: 3
30+
LL ~ }).hi() {
31+
|
32+
33+
error: aborting due to 2 previous errors
1834

tests/ui/parser/struct-literal-in-match-guard.rs

+3
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
// Unlike `if` condition, `match` guards accept struct literals.
44
// This is detected in <https://github.com/rust-lang/rust/pull/74566#issuecomment-663613705>.
55

6+
#![feature(if_let_guard)]
7+
68
#[derive(PartialEq)]
79
struct Foo {
810
x: isize,
@@ -11,6 +13,7 @@ struct Foo {
1113
fn foo(f: Foo) {
1214
match () {
1315
() if f == Foo { x: 42 } => {}
16+
() if let Foo { x: 0.. } = Foo { x: 42 } => {}
1417
_ => {}
1518
}
1619
}

tests/ui/parser/struct-literal-in-while.rs

+5
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,9 @@ fn main() {
1414
}.hi() {
1515
println!("yo");
1616
}
17+
while let true = Foo { //~ ERROR struct literals are not allowed here
18+
x: 3
19+
}.hi() {
20+
println!("yo");
21+
}
1722
}

tests/ui/parser/struct-literal-in-while.stderr

+17-1
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,21 @@ LL | x: 3
1414
LL ~ }).hi() {
1515
|
1616

17-
error: aborting due to previous error
17+
error: struct literals are not allowed here
18+
--> $DIR/struct-literal-in-while.rs:17:22
19+
|
20+
LL | while let true = Foo {
21+
| ______________________^
22+
LL | | x: 3
23+
LL | | }.hi() {
24+
| |_____^
25+
|
26+
help: surround the struct literal with parentheses
27+
|
28+
LL ~ while let true = (Foo {
29+
LL | x: 3
30+
LL ~ }).hi() {
31+
|
32+
33+
error: aborting due to 2 previous errors
1834

0 commit comments

Comments
 (0)