Skip to content

Commit d75f8cd

Browse files
committed
Also allow let chains in match guards
1 parent eb1639b commit d75f8cd

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
@@ -3431,15 +3431,15 @@ impl<'a> Parser<'a> {
34313431
fn parse_match_arm_guard(&mut self) -> PResult<'a, Option<P<Expr>>> {
34323432
// Used to check the `let_chains` and `if_let_guard` features mostly by scanning
34333433
// `&&` tokens.
3434-
fn check_let_expr(expr: &Expr) -> (bool, bool) {
3434+
fn has_let_expr(expr: &Expr) -> bool {
34353435
match &expr.kind {
34363436
ExprKind::Binary(BinOp { node: BinOpKind::And, .. }, lhs, rhs) => {
3437-
let lhs_rslt = check_let_expr(lhs);
3438-
let rhs_rslt = check_let_expr(rhs);
3439-
(lhs_rslt.0 || rhs_rslt.0, false)
3437+
let lhs_rslt = has_let_expr(lhs);
3438+
let rhs_rslt = has_let_expr(rhs);
3439+
lhs_rslt || rhs_rslt
34403440
}
3441-
ExprKind::Let(..) => (true, true),
3442-
_ => (false, true),
3441+
ExprKind::Let(..) => true,
3442+
_ => false,
34433443
}
34443444
}
34453445
if !self.eat_keyword(exp!(If)) {
@@ -3452,12 +3452,21 @@ impl<'a> Parser<'a> {
34523452

34533453
CondChecker::new(self).visit_expr(&mut cond);
34543454

3455-
let (has_let_expr, does_not_have_bin_op) = check_let_expr(&cond);
3456-
if has_let_expr {
3457-
if does_not_have_bin_op {
3458-
// Remove the last feature gating of a `let` expression since it's stable.
3459-
self.psess.gated_spans.ungate_last(sym::let_chains, cond.span);
3455+
if has_let_expr(&cond) {
3456+
// Let chains are allowed in match guards, but only there
3457+
fn ungate_let_exprs(this: &mut Parser<'_>, expr: &Expr) {
3458+
match &expr.kind {
3459+
ExprKind::Binary(BinOp { node: BinOpKind::And, .. }, lhs, rhs) => {
3460+
ungate_let_exprs(this, rhs);
3461+
ungate_let_exprs(this, lhs);
3462+
}
3463+
ExprKind::Let(..) => {
3464+
this.psess.gated_spans.ungate_last(sym::let_chains, expr.span)
3465+
}
3466+
_ => (),
3467+
}
34603468
}
3469+
ungate_let_exprs(self, &cond);
34613470
let span = if_span.to(cond.span);
34623471
self.psess.gated_spans.gate(sym::if_let_guard, span);
34633472
}

0 commit comments

Comments
 (0)