Skip to content

Commit

Permalink
Rollup merge of rust-lang#115371 - matthewjasper:if-let-guard-parsing…
Browse files Browse the repository at this point in the history
…, 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
  • Loading branch information
Dylan-DPC authored Sep 5, 2023
2 parents cfa8466 + 89235fd commit f3ac73d
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 5 deletions.
4 changes: 1 addition & 3 deletions compiler/rustc_parse/src/parser/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2477,9 +2477,7 @@ impl<'a> Parser<'a> {
} else {
self.expect(&token::Eq)?;
}
let expr = self.with_res(self.restrictions | Restrictions::NO_STRUCT_LITERAL, |this| {
this.parse_expr_assoc_with(1 + prec_let_scrutinee_needs_par(), None.into())
})?;
let expr = self.parse_expr_assoc_with(1 + prec_let_scrutinee_needs_par(), None.into())?;
let span = lo.to(expr.span);
self.sess.gated_spans.gate(sym::let_chains, span);
Ok(self.mk_expr(span, ExprKind::Let(pat, expr, span)))
Expand Down
5 changes: 5 additions & 0 deletions tests/ui/parser/struct-literal-in-if.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,9 @@ fn main() {
}.hi() {
println!("yo");
}
if let true = Foo { //~ ERROR struct literals are not allowed here
x: 3
}.hi() {
println!("yo");
}
}
18 changes: 17 additions & 1 deletion tests/ui/parser/struct-literal-in-if.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,21 @@ LL | x: 3
LL ~ }).hi() {
|

error: aborting due to previous error
error: struct literals are not allowed here
--> $DIR/struct-literal-in-if.rs:17:19
|
LL | if let true = Foo {
| ___________________^
LL | | x: 3
LL | | }.hi() {
| |_____^
|
help: surround the struct literal with parentheses
|
LL ~ if let true = (Foo {
LL | x: 3
LL ~ }).hi() {
|

error: aborting due to 2 previous errors

3 changes: 3 additions & 0 deletions tests/ui/parser/struct-literal-in-match-guard.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
// Unlike `if` condition, `match` guards accept struct literals.
// This is detected in <https://github.com/rust-lang/rust/pull/74566#issuecomment-663613705>.

#![feature(if_let_guard)]

#[derive(PartialEq)]
struct Foo {
x: isize,
Expand All @@ -11,6 +13,7 @@ struct Foo {
fn foo(f: Foo) {
match () {
() if f == Foo { x: 42 } => {}
() if let Foo { x: 0.. } = Foo { x: 42 } => {}
_ => {}
}
}
Expand Down
5 changes: 5 additions & 0 deletions tests/ui/parser/struct-literal-in-while.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,9 @@ fn main() {
}.hi() {
println!("yo");
}
while let true = Foo { //~ ERROR struct literals are not allowed here
x: 3
}.hi() {
println!("yo");
}
}
18 changes: 17 additions & 1 deletion tests/ui/parser/struct-literal-in-while.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,21 @@ LL | x: 3
LL ~ }).hi() {
|

error: aborting due to previous error
error: struct literals are not allowed here
--> $DIR/struct-literal-in-while.rs:17:22
|
LL | while let true = Foo {
| ______________________^
LL | | x: 3
LL | | }.hi() {
| |_____^
|
help: surround the struct literal with parentheses
|
LL ~ while let true = (Foo {
LL | x: 3
LL ~ }).hi() {
|

error: aborting due to 2 previous errors

0 comments on commit f3ac73d

Please sign in to comment.