Skip to content

Commit 25523ba

Browse files
committed
Refactor LhsExpr.
Combine `NotYetParsed` and `AttributesParsed` into a single variant, because (a) that reflects the structure of the code that consumes `LhsExpr`, and (b) because that variant will have the `Option` removed in a later commit.
1 parent 42e47df commit 25523ba

File tree

3 files changed

+38
-42
lines changed

3 files changed

+38
-42
lines changed

compiler/rustc_parse/src/parser/expr.rs

Lines changed: 34 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -70,9 +70,10 @@ macro_rules! maybe_whole_expr {
7070

7171
#[derive(Debug)]
7272
pub(super) enum LhsExpr {
73-
NotYetParsed,
74-
AttributesParsed(AttrWrapper),
75-
AlreadyParsed { expr: P<Expr>, starts_statement: bool },
73+
// Already parsed either (a) nothing or (b) just the outer attributes.
74+
Unparsed { attrs: Option<AttrWrapper> },
75+
// Already parsed the expression.
76+
Parsed { expr: P<Expr>, starts_statement: bool },
7677
}
7778

7879
#[derive(Debug)]
@@ -133,25 +134,18 @@ impl<'a> Parser<'a> {
133134
pub(super) fn parse_expr_res(
134135
&mut self,
135136
r: Restrictions,
136-
already_parsed_attrs: Option<AttrWrapper>,
137+
attrs: Option<AttrWrapper>,
137138
) -> PResult<'a, P<Expr>> {
138-
self.with_res(r, |this| this.parse_expr_assoc(already_parsed_attrs))
139+
self.with_res(r, |this| this.parse_expr_assoc(attrs))
139140
}
140141

141142
/// Parses an associative expression.
142143
///
143144
/// This parses an expression accounting for associativity and precedence of the operators in
144145
/// the expression.
145146
#[inline]
146-
fn parse_expr_assoc(
147-
&mut self,
148-
already_parsed_attrs: Option<AttrWrapper>,
149-
) -> PResult<'a, P<Expr>> {
150-
let lhs = match already_parsed_attrs {
151-
Some(attrs) => LhsExpr::AttributesParsed(attrs),
152-
None => LhsExpr::NotYetParsed,
153-
};
154-
self.parse_expr_assoc_with(0, lhs)
147+
fn parse_expr_assoc(&mut self, attrs: Option<AttrWrapper>) -> PResult<'a, P<Expr>> {
148+
self.parse_expr_assoc_with(0, LhsExpr::Unparsed { attrs })
155149
}
156150

157151
/// Parses an associative expression with operators of at least `min_prec` precedence.
@@ -161,18 +155,17 @@ impl<'a> Parser<'a> {
161155
lhs: LhsExpr,
162156
) -> PResult<'a, P<Expr>> {
163157
let mut starts_stmt = false;
164-
let mut lhs = if let LhsExpr::AlreadyParsed { expr, starts_statement } = lhs {
165-
starts_stmt = starts_statement;
166-
expr
167-
} else {
168-
let attrs = match lhs {
169-
LhsExpr::AttributesParsed(attrs) => Some(attrs),
170-
_ => None,
171-
};
172-
if self.token.is_range_separator() {
173-
return self.parse_expr_prefix_range(attrs);
174-
} else {
175-
self.parse_expr_prefix(attrs)?
158+
let mut lhs = match lhs {
159+
LhsExpr::Parsed { expr, starts_statement } => {
160+
starts_stmt = starts_statement;
161+
expr
162+
}
163+
LhsExpr::Unparsed { attrs } => {
164+
if self.token.is_range_separator() {
165+
return self.parse_expr_prefix_range(attrs);
166+
} else {
167+
self.parse_expr_prefix(attrs)?
168+
}
176169
}
177170
};
178171

@@ -310,7 +303,10 @@ impl<'a> Parser<'a> {
310303
Fixity::None => 1,
311304
};
312305
let rhs = self.with_res(restrictions - Restrictions::STMT_EXPR, |this| {
313-
this.parse_expr_assoc_with(prec + prec_adjustment, LhsExpr::NotYetParsed)
306+
this.parse_expr_assoc_with(
307+
prec + prec_adjustment,
308+
LhsExpr::Unparsed { attrs: None },
309+
)
314310
})?;
315311

316312
let span = self.mk_expr_sp(&lhs, lhs_span, rhs.span);
@@ -484,7 +480,7 @@ impl<'a> Parser<'a> {
484480
let rhs = if self.is_at_start_of_range_notation_rhs() {
485481
let maybe_lt = self.token.clone();
486482
Some(
487-
self.parse_expr_assoc_with(prec + 1, LhsExpr::NotYetParsed)
483+
self.parse_expr_assoc_with(prec + 1, LhsExpr::Unparsed { attrs: None })
488484
.map_err(|err| self.maybe_err_dotdotlt_syntax(maybe_lt, err))?,
489485
)
490486
} else {
@@ -539,9 +535,12 @@ impl<'a> Parser<'a> {
539535
this.bump();
540536
let (span, opt_end) = if this.is_at_start_of_range_notation_rhs() {
541537
// RHS must be parsed with more associativity than the dots.
542-
this.parse_expr_assoc_with(op.unwrap().precedence() + 1, LhsExpr::NotYetParsed)
543-
.map(|x| (lo.to(x.span), Some(x)))
544-
.map_err(|err| this.maybe_err_dotdotlt_syntax(maybe_lt, err))?
538+
this.parse_expr_assoc_with(
539+
op.unwrap().precedence() + 1,
540+
LhsExpr::Unparsed { attrs: None },
541+
)
542+
.map(|x| (lo.to(x.span), Some(x)))
543+
.map_err(|err| this.maybe_err_dotdotlt_syntax(maybe_lt, err))?
545544
} else {
546545
(lo, None)
547546
};
@@ -2645,8 +2644,10 @@ impl<'a> Parser<'a> {
26452644
} else {
26462645
self.expect(&token::Eq)?;
26472646
}
2648-
let expr =
2649-
self.parse_expr_assoc_with(1 + prec_let_scrutinee_needs_par(), LhsExpr::NotYetParsed)?;
2647+
let expr = self.parse_expr_assoc_with(
2648+
1 + prec_let_scrutinee_needs_par(),
2649+
LhsExpr::Unparsed { attrs: None },
2650+
)?;
26502651
let span = lo.to(expr.span);
26512652
Ok(self.mk_expr(span, ExprKind::Let(pat, expr, span, recovered)))
26522653
}

compiler/rustc_parse/src/parser/pat.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -398,7 +398,7 @@ impl<'a> Parser<'a> {
398398

399399
// Parse an associative expression such as `+ expr`, `% expr`, ...
400400
// Assignements, ranges and `|` are disabled by [`Restrictions::IS_PAT`].
401-
let lhs = LhsExpr::AlreadyParsed { expr, starts_statement: false };
401+
let lhs = LhsExpr::Parsed { expr, starts_statement: false };
402402
if let Ok(expr) = snapshot.parse_expr_assoc_with(0, lhs).map_err(|err| err.cancel()) {
403403
// We got a valid expression.
404404
self.restore_snapshot(snapshot);

compiler/rustc_parse/src/parser/stmt.rs

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -174,10 +174,7 @@ impl<'a> Parser<'a> {
174174
// Perform this outside of the `collect_tokens_trailing_token` closure,
175175
// since our outer attributes do not apply to this part of the expression
176176
let expr = self.with_res(Restrictions::STMT_EXPR, |this| {
177-
this.parse_expr_assoc_with(
178-
0,
179-
LhsExpr::AlreadyParsed { expr, starts_statement: true },
180-
)
177+
this.parse_expr_assoc_with(0, LhsExpr::Parsed { expr, starts_statement: true })
181178
})?;
182179
Ok(self.mk_stmt(lo.to(self.prev_token.span), StmtKind::Expr(expr)))
183180
} else {
@@ -210,10 +207,8 @@ impl<'a> Parser<'a> {
210207
let e = self.mk_expr(lo.to(hi), ExprKind::MacCall(mac));
211208
let e = self.maybe_recover_from_bad_qpath(e)?;
212209
let e = self.parse_expr_dot_or_call_with(e, lo, attrs)?;
213-
let e = self.parse_expr_assoc_with(
214-
0,
215-
LhsExpr::AlreadyParsed { expr: e, starts_statement: false },
216-
)?;
210+
let e = self
211+
.parse_expr_assoc_with(0, LhsExpr::Parsed { expr: e, starts_statement: false })?;
217212
StmtKind::Expr(e)
218213
};
219214
Ok(self.mk_stmt(lo.to(hi), kind))

0 commit comments

Comments
 (0)