Skip to content

Commit 0ecd73a

Browse files
committed
Auto merge of #126571 - nnethercote:less-maybe_whole-expr-2, r=<try>
Less `maybe_whole_expr`, take 2 I first tried this in #107550. I now think it's worth doing again, as a precursor to #124141. r? `@petrochenkov`
2 parents 5b270e1 + 379b761 commit 0ecd73a

File tree

1 file changed

+41
-32
lines changed
  • compiler/rustc_parse/src/parser

1 file changed

+41
-32
lines changed

compiler/rustc_parse/src/parser/expr.rs

+41-32
Original file line numberDiff line numberDiff line change
@@ -39,36 +39,6 @@ use rustc_span::{BytePos, ErrorGuaranteed, Pos, Span};
3939
use thin_vec::{thin_vec, ThinVec};
4040
use tracing::instrument;
4141

42-
/// Possibly accepts an `token::Interpolated` expression (a pre-parsed expression
43-
/// dropped into the token stream, which happens while parsing the result of
44-
/// macro expansion). Placement of these is not as complex as I feared it would
45-
/// be. The important thing is to make sure that lookahead doesn't balk at
46-
/// `token::Interpolated` tokens.
47-
macro_rules! maybe_whole_expr {
48-
($p:expr) => {
49-
if let token::Interpolated(nt) = &$p.token.kind {
50-
match &**nt {
51-
token::NtExpr(e) | token::NtLiteral(e) => {
52-
let e = e.clone();
53-
$p.bump();
54-
return Ok(e);
55-
}
56-
token::NtPath(path) => {
57-
let path = (**path).clone();
58-
$p.bump();
59-
return Ok($p.mk_expr($p.prev_token.span, ExprKind::Path(None, path)));
60-
}
61-
token::NtBlock(block) => {
62-
let block = block.clone();
63-
$p.bump();
64-
return Ok($p.mk_expr($p.prev_token.span, ExprKind::Block(block, None)));
65-
}
66-
_ => {}
67-
};
68-
}
69-
};
70-
}
71-
7242
#[derive(Debug)]
7343
pub(super) enum LhsExpr {
7444
// Already parsed just the outer attributes.
@@ -1421,7 +1391,27 @@ impl<'a> Parser<'a> {
14211391
/// correctly if called from `parse_dot_or_call_expr()`.
14221392
fn parse_expr_bottom(&mut self) -> PResult<'a, P<Expr>> {
14231393
maybe_recover_from_interpolated_ty_qpath!(self, true);
1424-
maybe_whole_expr!(self);
1394+
1395+
if let token::Interpolated(nt) = &self.token.kind {
1396+
match &**nt {
1397+
token::NtExpr(e) | token::NtLiteral(e) => {
1398+
let e = e.clone();
1399+
self.bump();
1400+
return Ok(e);
1401+
}
1402+
token::NtPath(path) => {
1403+
let path = (**path).clone();
1404+
self.bump();
1405+
return Ok(self.mk_expr(self.prev_token.span, ExprKind::Path(None, path)));
1406+
}
1407+
token::NtBlock(block) => {
1408+
let block = block.clone();
1409+
self.bump();
1410+
return Ok(self.mk_expr(self.prev_token.span, ExprKind::Block(block, None)));
1411+
}
1412+
_ => {}
1413+
};
1414+
}
14251415

14261416
// Outer attributes are already parsed and will be
14271417
// added to the return value after the fact.
@@ -2190,7 +2180,26 @@ impl<'a> Parser<'a> {
21902180
/// Matches `'-' lit | lit` (cf. `ast_validation::AstValidator::check_expr_within_pat`).
21912181
/// Keep this in sync with `Token::can_begin_literal_maybe_minus`.
21922182
pub fn parse_literal_maybe_minus(&mut self) -> PResult<'a, P<Expr>> {
2193-
maybe_whole_expr!(self);
2183+
if let token::Interpolated(nt) = &self.token.kind {
2184+
match &**nt {
2185+
// FIXME(nnethercote) The `NtExpr` case should only match if
2186+
// `e` is an `ExprKind::Lit` or an `ExprKind::Unary` containing
2187+
// an `UnOp::Neg` and an `ExprKind::Lit`, like how
2188+
// `can_begin_literal_maybe_minus` works. But this method has
2189+
// been over-accepting for a long time, and to make that change
2190+
// here requires also changing some `parse_literal_maybe_minus`
2191+
// call sites to accept additional expression kinds. E.g.
2192+
// `ExprKind::Path` must be accepted when parsing range
2193+
// patterns. That requires some care. So for now, we continue
2194+
// being less strict here than we should be.
2195+
token::NtExpr(e) | token::NtLiteral(e) => {
2196+
let e = e.clone();
2197+
self.bump();
2198+
return Ok(e);
2199+
}
2200+
_ => {}
2201+
};
2202+
}
21942203

21952204
let lo = self.token.span;
21962205
let minus_present = self.eat(&token::BinOp(token::Minus));

0 commit comments

Comments
 (0)