Skip to content

Commit b4bd8b5

Browse files
committed
Don't call ungate_last
Also, stabilize while let chains on editions 2021 and before.
1 parent 162daaa commit b4bd8b5

File tree

7 files changed

+389
-454
lines changed

7 files changed

+389
-454
lines changed

Diff for: compiler/rustc_parse/src/parser/expr.rs

+44-54
Original file line numberDiff line numberDiff line change
@@ -2606,7 +2606,10 @@ impl<'a> Parser<'a> {
26062606
/// Parses an `if` expression (`if` token already eaten).
26072607
fn parse_expr_if(&mut self) -> PResult<'a, P<Expr>> {
26082608
let lo = self.prev_token.span;
2609-
let cond = self.parse_expr_cond(lo.edition())?;
2609+
// Scoping code checks the top level edition of the `if`; let's match it here.
2610+
// The CondChecker also checks the edition of the `let` itself, just to make sure.
2611+
let let_chains_policy = LetChainsPolicy::Edition(lo.edition());
2612+
let cond = self.parse_expr_cond(let_chains_policy)?;
26102613
self.parse_if_after_cond(lo, cond)
26112614
}
26122615

@@ -2716,41 +2719,16 @@ impl<'a> Parser<'a> {
27162719

27172720
/// Parses the condition of a `if` or `while` expression.
27182721
///
2719-
/// The specified `edition` should be that of the whole `if` or `while` construct: the same
2720-
/// span that we later decide the drop behaviour on (editions ..=2021 vs 2024..)
2722+
/// The specified `edition` in `let_chains_policy` should be that of the whole `if` construct,
2723+
/// i.e. the same span we use to later decide whether the drop behaviour should be that of
2724+
/// edition `..=2021` or that of `2024..`.
27212725
// Public because it is used in rustfmt forks such as https://github.com/tucant/rustfmt/blob/30c83df9e1db10007bdd16dafce8a86b404329b2/src/parse/macros/html.rs#L57 for custom if expressions.
2722-
pub fn parse_expr_cond(&mut self, edition: Edition) -> PResult<'a, P<Expr>> {
2726+
pub fn parse_expr_cond(&mut self, let_chains_policy: LetChainsPolicy) -> PResult<'a, P<Expr>> {
27232727
let attrs = self.parse_outer_attributes()?;
27242728
let (mut cond, _) =
27252729
self.parse_expr_res(Restrictions::NO_STRUCT_LITERAL | Restrictions::ALLOW_LET, attrs)?;
27262730

2727-
CondChecker::new(self).visit_expr(&mut cond);
2728-
2729-
if let ExprKind::Let(_, _, _, Recovered::No) = cond.kind {
2730-
// Remove the last feature gating of a `let` expression since it's stable.
2731-
self.psess.gated_spans.ungate_last(sym::let_chains, cond.span);
2732-
} else {
2733-
fn ungate_let_exprs(this: &mut Parser<'_>, expr: &Expr) {
2734-
if !expr.span.at_least_rust_2024() {
2735-
return;
2736-
}
2737-
match &expr.kind {
2738-
ExprKind::Binary(BinOp { node: BinOpKind::And, .. }, lhs, rhs) => {
2739-
ungate_let_exprs(this, rhs);
2740-
ungate_let_exprs(this, lhs);
2741-
}
2742-
ExprKind::Let(..) => {
2743-
this.psess.gated_spans.ungate_last(sym::let_chains, expr.span)
2744-
}
2745-
_ => (),
2746-
}
2747-
}
2748-
if edition.at_least_rust_2024() {
2749-
// Scoping code checks the top level edition of the `if`: let's match it here.
2750-
// Also check all editions in between, just to make sure.
2751-
ungate_let_exprs(self, &cond);
2752-
}
2753-
}
2731+
CondChecker::new(self, let_chains_policy).visit_expr(&mut cond);
27542732

27552733
Ok(cond)
27562734
}
@@ -3045,7 +3023,7 @@ impl<'a> Parser<'a> {
30453023

30463024
/// Parses a `while` or `while let` expression (`while` token already eaten).
30473025
fn parse_expr_while(&mut self, opt_label: Option<Label>, lo: Span) -> PResult<'a, P<Expr>> {
3048-
let cond = self.parse_expr_cond(lo.edition()).map_err(|mut err| {
3026+
let cond = self.parse_expr_cond(LetChainsPolicy::AlwaysAllowed).map_err(|mut err| {
30493027
err.span_label(lo, "while parsing the condition of this `while` expression");
30503028
err
30513029
})?;
@@ -3429,7 +3407,7 @@ impl<'a> Parser<'a> {
34293407
}
34303408

34313409
fn parse_match_arm_guard(&mut self) -> PResult<'a, Option<P<Expr>>> {
3432-
// Used to check the `let_chains` and `if_let_guard` features mostly by scanning
3410+
// Used to check the `if_let_guard` feature mostly by scanning
34333411
// `&&` tokens.
34343412
fn has_let_expr(expr: &Expr) -> bool {
34353413
match &expr.kind {
@@ -3450,23 +3428,9 @@ impl<'a> Parser<'a> {
34503428
let if_span = self.prev_token.span;
34513429
let mut cond = self.parse_match_guard_condition()?;
34523430

3453-
CondChecker::new(self).visit_expr(&mut cond);
3431+
CondChecker::new(self, LetChainsPolicy::AlwaysAllowed).visit_expr(&mut cond);
34543432

34553433
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-
}
3468-
}
3469-
ungate_let_exprs(self, &cond);
34703434
let span = if_span.to(cond.span);
34713435
self.psess.gated_spans.gate(sym::if_let_guard, span);
34723436
}
@@ -3493,7 +3457,7 @@ impl<'a> Parser<'a> {
34933457
unreachable!()
34943458
};
34953459
self.psess.gated_spans.ungate_last(sym::guard_patterns, cond.span);
3496-
CondChecker::new(self).visit_expr(&mut cond);
3460+
CondChecker::new(self, LetChainsPolicy::AlwaysAllowed).visit_expr(&mut cond);
34973461
let right = self.prev_token.span;
34983462
self.dcx().emit_err(errors::ParenthesesInMatchPat {
34993463
span: vec![left, right],
@@ -4072,7 +4036,14 @@ pub(crate) enum ForbiddenLetReason {
40724036
NotSupportedParentheses(#[primary_span] Span),
40734037
}
40744038

4075-
/// Visitor to check for invalid/unstable use of `ExprKind::Let` that can't
4039+
/// Whether let chains are allowed on all editions, or it's edition dependent.
4040+
/// In case of edition dependence, specify the currently present edition.
4041+
pub enum LetChainsPolicy {
4042+
AlwaysAllowed,
4043+
Edition(Edition),
4044+
}
4045+
4046+
/// Visitor to check for invalid use of `ExprKind::Let` that can't
40764047
/// easily be caught in parsing. For example:
40774048
///
40784049
/// ```rust,ignore (example)
@@ -4083,19 +4054,29 @@ pub(crate) enum ForbiddenLetReason {
40834054
/// ```
40844055
struct CondChecker<'a> {
40854056
parser: &'a Parser<'a>,
4057+
let_chains_policy: LetChainsPolicy,
4058+
depth: u32,
40864059
forbid_let_reason: Option<ForbiddenLetReason>,
40874060
missing_let: Option<errors::MaybeMissingLet>,
40884061
comparison: Option<errors::MaybeComparison>,
40894062
}
40904063

40914064
impl<'a> CondChecker<'a> {
4092-
fn new(parser: &'a Parser<'a>) -> Self {
4093-
CondChecker { parser, forbid_let_reason: None, missing_let: None, comparison: None }
4065+
fn new(parser: &'a Parser<'a>, let_chains_policy: LetChainsPolicy) -> Self {
4066+
CondChecker {
4067+
parser,
4068+
forbid_let_reason: None,
4069+
missing_let: None,
4070+
comparison: None,
4071+
let_chains_policy,
4072+
depth: 0,
4073+
}
40944074
}
40954075
}
40964076

40974077
impl MutVisitor for CondChecker<'_> {
40984078
fn visit_expr(&mut self, e: &mut P<Expr>) {
4079+
self.depth += 1;
40994080
use ForbiddenLetReason::*;
41004081

41014082
let span = e.span;
@@ -4110,8 +4091,16 @@ impl MutVisitor for CondChecker<'_> {
41104091
comparison: self.comparison,
41114092
},
41124093
));
4113-
} else {
4114-
self.parser.psess.gated_spans.gate(sym::let_chains, span);
4094+
} else if self.depth > 1 {
4095+
// Top level let is always allowed, only gate chains
4096+
match self.let_chains_policy {
4097+
LetChainsPolicy::AlwaysAllowed => (),
4098+
LetChainsPolicy::Edition(edition) => {
4099+
if !edition.at_least_rust_2024() || !span.at_least_rust_2024() {
4100+
self.parser.psess.gated_spans.gate(sym::let_chains, span);
4101+
}
4102+
}
4103+
}
41154104
}
41164105
}
41174106
ExprKind::Binary(Spanned { node: BinOpKind::And, .. }, _, _) => {
@@ -4213,5 +4202,6 @@ impl MutVisitor for CondChecker<'_> {
42134202
// These would forbid any let expressions they contain already.
42144203
}
42154204
}
4205+
self.depth -= 1;
42164206
}
42174207
}

0 commit comments

Comments
 (0)