Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 5204067

Browse files
committedMar 16, 2025
Also allow let chains in match guards
1 parent 17f4621 commit 5204067

File tree

1 file changed

+20
-11
lines changed
  • compiler/rustc_parse/src/parser

1 file changed

+20
-11
lines changed
 

‎compiler/rustc_parse/src/parser/expr.rs

+20-11
Original file line numberDiff line numberDiff line change
@@ -3291,15 +3291,15 @@ impl<'a> Parser<'a> {
32913291
fn parse_match_arm_guard(&mut self) -> PResult<'a, Option<P<Expr>>> {
32923292
// Used to check the `let_chains` and `if_let_guard` features mostly by scanning
32933293
// `&&` tokens.
3294-
fn check_let_expr(expr: &Expr) -> (bool, bool) {
3294+
fn has_let_expr(expr: &Expr) -> bool {
32953295
match &expr.kind {
32963296
ExprKind::Binary(BinOp { node: BinOpKind::And, .. }, lhs, rhs) => {
3297-
let lhs_rslt = check_let_expr(lhs);
3298-
let rhs_rslt = check_let_expr(rhs);
3299-
(lhs_rslt.0 || rhs_rslt.0, false)
3297+
let lhs_rslt = has_let_expr(lhs);
3298+
let rhs_rslt = has_let_expr(rhs);
3299+
lhs_rslt || rhs_rslt
33003300
}
3301-
ExprKind::Let(..) => (true, true),
3302-
_ => (false, true),
3301+
ExprKind::Let(..) => true,
3302+
_ => false,
33033303
}
33043304
}
33053305
if !self.eat_keyword(exp!(If)) {
@@ -3312,12 +3312,21 @@ impl<'a> Parser<'a> {
33123312

33133313
CondChecker::new(self).visit_expr(&mut cond);
33143314

3315-
let (has_let_expr, does_not_have_bin_op) = check_let_expr(&cond);
3316-
if has_let_expr {
3317-
if does_not_have_bin_op {
3318-
// Remove the last feature gating of a `let` expression since it's stable.
3319-
self.psess.gated_spans.ungate_last(sym::let_chains, cond.span);
3315+
if has_let_expr(&cond) {
3316+
// Let chains are allowed in match guards, but only there
3317+
fn ungate_let_exprs(this: &mut Parser<'_>, expr: &Expr) {
3318+
match &expr.kind {
3319+
ExprKind::Binary(BinOp { node: BinOpKind::And, .. }, lhs, rhs) => {
3320+
ungate_let_exprs(this, rhs);
3321+
ungate_let_exprs(this, lhs);
3322+
}
3323+
ExprKind::Let(..) => {
3324+
this.psess.gated_spans.ungate_last(sym::let_chains, expr.span)
3325+
}
3326+
_ => (),
3327+
}
33203328
}
3329+
ungate_let_exprs(self, &cond);
33213330
let span = if_span.to(cond.span);
33223331
self.psess.gated_spans.gate(sym::if_let_guard, span);
33233332
}

0 commit comments

Comments
 (0)
Please sign in to comment.