Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit fe647f0

Browse files
committed
Remove LhsExpr.
`parse_expr_assoc_with` has an awkward structure -- sometimes the lhs is already parsed. This commit splits the post-lhs part into a new method `parse_expr_assoc_rest_with`, which makes everything shorter and simpler.
1 parent 281c2fd commit fe647f0

File tree

3 files changed

+28
-42
lines changed

3 files changed

+28
-42
lines changed

compiler/rustc_parse/src/parser/expr.rs

Lines changed: 22 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -40,14 +40,6 @@ use super::{
4040
};
4141
use crate::{errors, maybe_recover_from_interpolated_ty_qpath};
4242

43-
#[derive(Debug)]
44-
pub(super) enum LhsExpr {
45-
// Already parsed just the outer attributes.
46-
Unparsed { attrs: AttrWrapper },
47-
// Already parsed the expression.
48-
Parsed { expr: P<Expr>, starts_statement: bool },
49-
}
50-
5143
#[derive(Debug)]
5244
enum DestructuredFloat {
5345
/// 1e2
@@ -113,30 +105,31 @@ impl<'a> Parser<'a> {
113105
r: Restrictions,
114106
attrs: AttrWrapper,
115107
) -> PResult<'a, P<Expr>> {
116-
self.with_res(r, |this| this.parse_expr_assoc_with(0, LhsExpr::Unparsed { attrs }))
108+
self.with_res(r, |this| this.parse_expr_assoc_with(0, attrs))
117109
}
118110

119111
/// Parses an associative expression with operators of at least `min_prec` precedence.
120112
pub(super) fn parse_expr_assoc_with(
121113
&mut self,
122114
min_prec: usize,
123-
lhs: LhsExpr,
115+
attrs: AttrWrapper,
124116
) -> PResult<'a, P<Expr>> {
125-
let mut starts_stmt = false;
126-
let mut lhs = match lhs {
127-
LhsExpr::Parsed { expr, starts_statement } => {
128-
starts_stmt = starts_statement;
129-
expr
130-
}
131-
LhsExpr::Unparsed { attrs } => {
132-
if self.token.is_range_separator() {
133-
return self.parse_expr_prefix_range(attrs);
134-
} else {
135-
self.parse_expr_prefix(attrs)?
136-
}
137-
}
117+
let lhs = if self.token.is_range_separator() {
118+
return self.parse_expr_prefix_range(attrs);
119+
} else {
120+
self.parse_expr_prefix(attrs)?
138121
};
122+
self.parse_expr_assoc_rest_with(min_prec, false, lhs)
123+
}
139124

125+
/// Parses the rest of an associative expression (i.e. the part after the lhs) with operators
126+
/// of at least `min_prec` precedence.
127+
pub(super) fn parse_expr_assoc_rest_with(
128+
&mut self,
129+
min_prec: usize,
130+
starts_stmt: bool,
131+
mut lhs: P<Expr>,
132+
) -> PResult<'a, P<Expr>> {
140133
if !self.should_continue_as_assoc_expr(&lhs) {
141134
return Ok(lhs);
142135
}
@@ -272,7 +265,7 @@ impl<'a> Parser<'a> {
272265
};
273266
let rhs = self.with_res(restrictions - Restrictions::STMT_EXPR, |this| {
274267
let attrs = this.parse_outer_attributes()?;
275-
this.parse_expr_assoc_with(prec + prec_adjustment, LhsExpr::Unparsed { attrs })
268+
this.parse_expr_assoc_with(prec + prec_adjustment, attrs)
276269
})?;
277270

278271
let span = self.mk_expr_sp(&lhs, lhs_span, rhs.span);
@@ -447,7 +440,7 @@ impl<'a> Parser<'a> {
447440
let maybe_lt = self.token.clone();
448441
let attrs = self.parse_outer_attributes()?;
449442
Some(
450-
self.parse_expr_assoc_with(prec + 1, LhsExpr::Unparsed { attrs })
443+
self.parse_expr_assoc_with(prec + 1, attrs)
451444
.map_err(|err| self.maybe_err_dotdotlt_syntax(maybe_lt, err))?,
452445
)
453446
} else {
@@ -504,12 +497,9 @@ impl<'a> Parser<'a> {
504497
let (span, opt_end) = if this.is_at_start_of_range_notation_rhs() {
505498
// RHS must be parsed with more associativity than the dots.
506499
let attrs = this.parse_outer_attributes()?;
507-
this.parse_expr_assoc_with(
508-
op.unwrap().precedence() + 1,
509-
LhsExpr::Unparsed { attrs },
510-
)
511-
.map(|x| (lo.to(x.span), Some(x)))
512-
.map_err(|err| this.maybe_err_dotdotlt_syntax(maybe_lt, err))?
500+
this.parse_expr_assoc_with(op.unwrap().precedence() + 1, attrs)
501+
.map(|x| (lo.to(x.span), Some(x)))
502+
.map_err(|err| this.maybe_err_dotdotlt_syntax(maybe_lt, err))?
513503
} else {
514504
(lo, None)
515505
};
@@ -2645,10 +2635,7 @@ impl<'a> Parser<'a> {
26452635
self.expect(&token::Eq)?;
26462636
}
26472637
let attrs = self.parse_outer_attributes()?;
2648-
let expr = self.parse_expr_assoc_with(
2649-
1 + prec_let_scrutinee_needs_par(),
2650-
LhsExpr::Unparsed { attrs },
2651-
)?;
2638+
let expr = self.parse_expr_assoc_with(1 + prec_let_scrutinee_needs_par(), attrs)?;
26522639
let span = lo.to(expr.span);
26532640
Ok(self.mk_expr(span, ExprKind::Let(pat, expr, span, recovered)))
26542641
}

compiler/rustc_parse/src/parser/pat.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ use crate::errors::{
2525
UnexpectedParenInRangePat, UnexpectedParenInRangePatSugg,
2626
UnexpectedVertVertBeforeFunctionParam, UnexpectedVertVertInPattern, WrapInParens,
2727
};
28-
use crate::parser::expr::{could_be_unclosed_char_literal, LhsExpr};
28+
use crate::parser::expr::could_be_unclosed_char_literal;
2929
use crate::{maybe_recover_from_interpolated_ty_qpath, maybe_whole};
3030

3131
#[derive(PartialEq, Copy, Clone)]
@@ -403,8 +403,9 @@ impl<'a> Parser<'a> {
403403

404404
// Parse an associative expression such as `+ expr`, `% expr`, ...
405405
// Assignements, ranges and `|` are disabled by [`Restrictions::IS_PAT`].
406-
let lhs = LhsExpr::Parsed { expr, starts_statement: false };
407-
if let Ok(expr) = snapshot.parse_expr_assoc_with(0, lhs).map_err(|err| err.cancel()) {
406+
if let Ok(expr) =
407+
snapshot.parse_expr_assoc_rest_with(0, false, expr).map_err(|err| err.cancel())
408+
{
408409
// We got a valid expression.
409410
self.restore_snapshot(snapshot);
410411
self.restrictions.remove(Restrictions::IS_PAT);

compiler/rustc_parse/src/parser/stmt.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ use thin_vec::{thin_vec, ThinVec};
1717

1818
use super::attr::InnerAttrForbiddenReason;
1919
use super::diagnostics::AttemptLocalParseRecovery;
20-
use super::expr::LhsExpr;
2120
use super::pat::{PatternLocation, RecoverComma};
2221
use super::path::PathStyle;
2322
use super::{
@@ -178,7 +177,7 @@ impl<'a> Parser<'a> {
178177
// Perform this outside of the `collect_tokens_trailing_token` closure,
179178
// since our outer attributes do not apply to this part of the expression
180179
let expr = self.with_res(Restrictions::STMT_EXPR, |this| {
181-
this.parse_expr_assoc_with(0, LhsExpr::Parsed { expr, starts_statement: true })
180+
this.parse_expr_assoc_rest_with(0, true, expr)
182181
})?;
183182
Ok(self.mk_stmt(lo.to(self.prev_token.span), StmtKind::Expr(expr)))
184183
} else {
@@ -211,8 +210,7 @@ impl<'a> Parser<'a> {
211210
let e = self.mk_expr(lo.to(hi), ExprKind::MacCall(mac));
212211
let e = self.maybe_recover_from_bad_qpath(e)?;
213212
let e = self.parse_expr_dot_or_call_with(attrs, e, lo)?;
214-
let e = self
215-
.parse_expr_assoc_with(0, LhsExpr::Parsed { expr: e, starts_statement: false })?;
213+
let e = self.parse_expr_assoc_rest_with(0, false, e)?;
216214
StmtKind::Expr(e)
217215
};
218216
Ok(self.mk_stmt(lo.to(hi), kind))

0 commit comments

Comments
 (0)