Skip to content

Commit

Permalink
Allow stuct literals in if let guards
Browse files Browse the repository at this point in the history
This is consistent with normal match guards.
  • Loading branch information
matthewjasper committed Aug 28, 2023
1 parent 56c17dc commit 89235fd
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 3 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
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

0 comments on commit 89235fd

Please sign in to comment.