Skip to content

Commit 7465eb4

Browse files
committed
let_chains: Refactor parse_{if,while}_expr a bit.
1 parent 5ae5086 commit 7465eb4

File tree

1 file changed

+12
-9
lines changed

1 file changed

+12
-9
lines changed

src/libsyntax/parse/parser.rs

+12-9
Original file line numberDiff line numberDiff line change
@@ -3157,8 +3157,7 @@ impl<'a> Parser<'a> {
31573157
/// Parses an `if` expression (`if` token already eaten).
31583158
fn parse_if_expr(&mut self, attrs: ThinVec<Attribute>) -> PResult<'a, P<Expr>> {
31593159
let lo = self.prev_span;
3160-
let cond = self.parse_expr_res(Restrictions::NO_STRUCT_LITERAL, None)?;
3161-
self.ungate_prev_let_expr(&cond);
3160+
let cond = self.parse_cond_expr()?;
31623161

31633162
// Verify that the parsed `if` condition makes sense as a condition. If it is a block, then
31643163
// verify that the last statement is either an implicit return (no `;`) or an explicit
@@ -3188,12 +3187,17 @@ impl<'a> Parser<'a> {
31883187
Ok(self.mk_expr(lo.to(hi), ExprKind::If(cond, thn, els), attrs))
31893188
}
31903189

3191-
/// Remove the last feature gating of a `let` expression that must the one provided.
3192-
fn ungate_prev_let_expr(&mut self, expr: &Expr) {
3193-
if let ExprKind::Let(..) = expr.node {
3190+
/// Parse the condition of a `if`- or `while`-expression
3191+
fn parse_cond_expr(&mut self) -> PResult<'a, P<Expr>> {
3192+
let cond = self.parse_expr_res(Restrictions::NO_STRUCT_LITERAL, None)?;
3193+
3194+
if let ExprKind::Let(..) = cond.node {
3195+
// Remove the last feature gating of a `let` expression since it's stable.
31943196
let last = self.sess.let_chains_spans.borrow_mut().pop();
3195-
debug_assert_eq!(expr.span, last.unwrap());
3197+
debug_assert_eq!(cond.span, last.unwrap());
31963198
}
3199+
3200+
Ok(cond)
31973201
}
31983202

31993203
/// Parses a `let $pats = $expr` pseudo-expression.
@@ -3295,12 +3299,11 @@ impl<'a> Parser<'a> {
32953299
fn parse_while_expr(&mut self, opt_label: Option<Label>,
32963300
span_lo: Span,
32973301
mut attrs: ThinVec<Attribute>) -> PResult<'a, P<Expr>> {
3298-
let cond = self.parse_expr_res(Restrictions::NO_STRUCT_LITERAL, None)?;
3299-
self.ungate_prev_let_expr(&cond);
3302+
let cond = self.parse_cond_expr()?;
33003303
let (iattrs, body) = self.parse_inner_attrs_and_block()?;
33013304
attrs.extend(iattrs);
33023305
let span = span_lo.to(body.span);
3303-
return Ok(self.mk_expr(span, ExprKind::While(cond, body, opt_label), attrs));
3306+
Ok(self.mk_expr(span, ExprKind::While(cond, body, opt_label), attrs))
33043307
}
33053308

33063309
// parse `loop {...}`, `loop` token already eaten

0 commit comments

Comments
 (0)