Skip to content

Commit 81da2a1

Browse files
authored
Rollup merge of rust-lang#106846 - WaffleLapkin:pico_parse_ref, r=TaKO8Ki
Improve some comments and names in parser Just a tiny drive-by cleanup.
2 parents 2e30e52 + f7850c5 commit 81da2a1

File tree

2 files changed

+25
-15
lines changed

2 files changed

+25
-15
lines changed

compiler/rustc_parse/src/parser/expr.rs

+17-13
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ macro_rules! maybe_whole_expr {
8383
pub(super) enum LhsExpr {
8484
NotYetParsed,
8585
AttributesParsed(AttrWrapper),
86-
AlreadyParsed(P<Expr>, bool), // (expr, starts_statement)
86+
AlreadyParsed { expr: P<Expr>, starts_statement: bool },
8787
}
8888

8989
impl From<Option<AttrWrapper>> for LhsExpr {
@@ -97,11 +97,11 @@ impl From<Option<AttrWrapper>> for LhsExpr {
9797
}
9898

9999
impl From<P<Expr>> for LhsExpr {
100-
/// Converts the `expr: P<Expr>` into `LhsExpr::AlreadyParsed(expr)`.
100+
/// Converts the `expr: P<Expr>` into `LhsExpr::AlreadyParsed { expr, starts_statement: false }`.
101101
///
102102
/// This conversion does not allocate.
103103
fn from(expr: P<Expr>) -> Self {
104-
LhsExpr::AlreadyParsed(expr, false)
104+
LhsExpr::AlreadyParsed { expr, starts_statement: false }
105105
}
106106
}
107107

@@ -174,7 +174,7 @@ impl<'a> Parser<'a> {
174174
lhs: LhsExpr,
175175
) -> PResult<'a, P<Expr>> {
176176
let mut starts_stmt = false;
177-
let mut lhs = if let LhsExpr::AlreadyParsed(expr, starts_statement) = lhs {
177+
let mut lhs = if let LhsExpr::AlreadyParsed { expr, starts_statement } = lhs {
178178
starts_stmt = starts_statement;
179179
expr
180180
} else {
@@ -562,17 +562,23 @@ impl<'a> Parser<'a> {
562562

563563
// Note: when adding new unary operators, don't forget to adjust TokenKind::can_begin_expr()
564564
match this.token.uninterpolate().kind {
565-
token::Not => make_it!(this, attrs, |this, _| this.parse_unary_expr(lo, UnOp::Not)), // `!expr`
566-
token::Tilde => make_it!(this, attrs, |this, _| this.recover_tilde_expr(lo)), // `~expr`
565+
// `!expr`
566+
token::Not => make_it!(this, attrs, |this, _| this.parse_unary_expr(lo, UnOp::Not)),
567+
// `~expr`
568+
token::Tilde => make_it!(this, attrs, |this, _| this.recover_tilde_expr(lo)),
569+
// `-expr`
567570
token::BinOp(token::Minus) => {
568571
make_it!(this, attrs, |this, _| this.parse_unary_expr(lo, UnOp::Neg))
569-
} // `-expr`
572+
}
573+
// `*expr`
570574
token::BinOp(token::Star) => {
571575
make_it!(this, attrs, |this, _| this.parse_unary_expr(lo, UnOp::Deref))
572-
} // `*expr`
576+
}
577+
// `&expr` and `&&expr`
573578
token::BinOp(token::And) | token::AndAnd => {
574579
make_it!(this, attrs, |this, _| this.parse_borrow_expr(lo))
575580
}
581+
// `+lit`
576582
token::BinOp(token::Plus) if this.look_ahead(1, |tok| tok.is_numeric_lit()) => {
577583
let mut err =
578584
LeadingPlusNotSupported { span: lo, remove_plus: None, add_parentheses: None };
@@ -587,7 +593,7 @@ impl<'a> Parser<'a> {
587593

588594
this.bump();
589595
this.parse_prefix_expr(None)
590-
} // `+expr`
596+
}
591597
// Recover from `++x`:
592598
token::BinOp(token::Plus)
593599
if this.look_ahead(1, |t| *t == token::BinOp(token::Plus)) =>
@@ -624,7 +630,7 @@ impl<'a> Parser<'a> {
624630
Ok((span, self.mk_unary(op, expr)))
625631
}
626632

627-
// Recover on `!` suggesting for bitwise negation instead.
633+
/// Recover on `~expr` in favor of `!expr`.
628634
fn recover_tilde_expr(&mut self, lo: Span) -> PResult<'a, (Span, ExprKind)> {
629635
self.sess.emit_err(TildeAsUnaryOperator(lo));
630636

@@ -651,7 +657,6 @@ impl<'a> Parser<'a> {
651657

652658
/// Recover on `not expr` in favor of `!expr`.
653659
fn recover_not_expr(&mut self, lo: Span) -> PResult<'a, (Span, ExprKind)> {
654-
// Emit the error...
655660
let negated_token = self.look_ahead(1, |t| t.clone());
656661

657662
let sub_diag = if negated_token.is_numeric_lit() {
@@ -672,7 +677,6 @@ impl<'a> Parser<'a> {
672677
),
673678
});
674679

675-
// ...and recover!
676680
self.parse_unary_expr(lo, UnOp::Not)
677681
}
678682

@@ -1593,7 +1597,7 @@ impl<'a> Parser<'a> {
15931597
vis.0
15941598
};
15951599

1596-
// Suggestion involves adding a (as of time of writing this, unstable) labeled block.
1600+
// Suggestion involves adding a labeled block.
15971601
//
15981602
// If there are no breaks that may use this label, suggest removing the label and
15991603
// recover to the unmodified expression.

compiler/rustc_parse/src/parser/stmt.rs

+8-2
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,10 @@ impl<'a> Parser<'a> {
164164
// Perform this outside of the `collect_tokens_trailing_token` closure,
165165
// since our outer attributes do not apply to this part of the expression
166166
let expr = self.with_res(Restrictions::STMT_EXPR, |this| {
167-
this.parse_assoc_expr_with(0, LhsExpr::AlreadyParsed(expr, true))
167+
this.parse_assoc_expr_with(
168+
0,
169+
LhsExpr::AlreadyParsed { expr, starts_statement: true },
170+
)
168171
})?;
169172
Ok(self.mk_stmt(lo.to(self.prev_token.span), StmtKind::Expr(expr)))
170173
} else {
@@ -198,7 +201,10 @@ impl<'a> Parser<'a> {
198201
let e = self.mk_expr(lo.to(hi), ExprKind::MacCall(mac));
199202
let e = self.maybe_recover_from_bad_qpath(e)?;
200203
let e = self.parse_dot_or_call_expr_with(e, lo, attrs)?;
201-
let e = self.parse_assoc_expr_with(0, LhsExpr::AlreadyParsed(e, false))?;
204+
let e = self.parse_assoc_expr_with(
205+
0,
206+
LhsExpr::AlreadyParsed { expr: e, starts_statement: false },
207+
)?;
202208
StmtKind::Expr(e)
203209
};
204210
Ok(self.mk_stmt(lo.to(hi), kind))

0 commit comments

Comments
 (0)