Auto merge of #145146 - fee1-dead-contrib:push-zmqrkurlzrxy, r=nnethercote
remove `P` Previous work: rust-lang/rust#141603 MCP: https://github.com/rust-lang/compiler-team/issues/878 cc `@nnethercote`
This commit is contained in:
@@ -6,7 +6,6 @@ use core::ops::{Bound, ControlFlow};
|
||||
use ast::mut_visit::{self, MutVisitor};
|
||||
use ast::token::IdentIsRaw;
|
||||
use ast::{CoroutineKind, ForLoopKind, GenBlockKind, MatchKind, Pat, Path, PathSegment, Recovered};
|
||||
use rustc_ast::ptr::P;
|
||||
use rustc_ast::token::{self, Delimiter, InvisibleOrigin, MetaVarKind, Token, TokenKind};
|
||||
use rustc_ast::tokenstream::TokenTree;
|
||||
use rustc_ast::util::case::Case;
|
||||
@@ -56,7 +55,7 @@ pub(super) enum DestructuredFloat {
|
||||
impl<'a> Parser<'a> {
|
||||
/// Parses an expression.
|
||||
#[inline]
|
||||
pub fn parse_expr(&mut self) -> PResult<'a, P<Expr>> {
|
||||
pub fn parse_expr(&mut self) -> PResult<'a, Box<Expr>> {
|
||||
self.current_closure.take();
|
||||
|
||||
let attrs = self.parse_outer_attributes()?;
|
||||
@@ -64,7 +63,7 @@ impl<'a> Parser<'a> {
|
||||
}
|
||||
|
||||
/// Parses an expression, forcing tokens to be collected.
|
||||
pub fn parse_expr_force_collect(&mut self) -> PResult<'a, P<Expr>> {
|
||||
pub fn parse_expr_force_collect(&mut self) -> PResult<'a, Box<Expr>> {
|
||||
self.current_closure.take();
|
||||
|
||||
// If the expression is associative (e.g. `1 + 2`), then any preceding
|
||||
@@ -90,7 +89,10 @@ impl<'a> Parser<'a> {
|
||||
self.parse_expr().map(|value| AnonConst { id: DUMMY_NODE_ID, value })
|
||||
}
|
||||
|
||||
fn parse_expr_catch_underscore(&mut self, restrictions: Restrictions) -> PResult<'a, P<Expr>> {
|
||||
fn parse_expr_catch_underscore(
|
||||
&mut self,
|
||||
restrictions: Restrictions,
|
||||
) -> PResult<'a, Box<Expr>> {
|
||||
let attrs = self.parse_outer_attributes()?;
|
||||
match self.parse_expr_res(restrictions, attrs) {
|
||||
Ok((expr, _)) => Ok(expr),
|
||||
@@ -109,7 +111,7 @@ impl<'a> Parser<'a> {
|
||||
}
|
||||
|
||||
/// Parses a sequence of expressions delimited by parentheses.
|
||||
fn parse_expr_paren_seq(&mut self) -> PResult<'a, ThinVec<P<Expr>>> {
|
||||
fn parse_expr_paren_seq(&mut self) -> PResult<'a, ThinVec<Box<Expr>>> {
|
||||
self.parse_paren_comma_seq(|p| p.parse_expr_catch_underscore(Restrictions::empty()))
|
||||
.map(|(r, _)| r)
|
||||
}
|
||||
@@ -120,7 +122,7 @@ impl<'a> Parser<'a> {
|
||||
&mut self,
|
||||
r: Restrictions,
|
||||
attrs: AttrWrapper,
|
||||
) -> PResult<'a, (P<Expr>, bool)> {
|
||||
) -> PResult<'a, (Box<Expr>, bool)> {
|
||||
self.with_res(r, |this| this.parse_expr_assoc_with(Bound::Unbounded, attrs))
|
||||
}
|
||||
|
||||
@@ -131,7 +133,7 @@ impl<'a> Parser<'a> {
|
||||
&mut self,
|
||||
min_prec: Bound<ExprPrecedence>,
|
||||
attrs: AttrWrapper,
|
||||
) -> PResult<'a, (P<Expr>, bool)> {
|
||||
) -> PResult<'a, (Box<Expr>, bool)> {
|
||||
let lhs = if self.token.is_range_separator() {
|
||||
return self.parse_expr_prefix_range(attrs).map(|res| (res, false));
|
||||
} else {
|
||||
@@ -147,8 +149,8 @@ impl<'a> Parser<'a> {
|
||||
&mut self,
|
||||
min_prec: Bound<ExprPrecedence>,
|
||||
starts_stmt: bool,
|
||||
mut lhs: P<Expr>,
|
||||
) -> PResult<'a, (P<Expr>, bool)> {
|
||||
mut lhs: Box<Expr>,
|
||||
) -> PResult<'a, (Box<Expr>, bool)> {
|
||||
let mut parsed_something = false;
|
||||
if !self.should_continue_as_assoc_expr(&lhs) {
|
||||
return Ok((lhs, parsed_something));
|
||||
@@ -414,10 +416,10 @@ impl<'a> Parser<'a> {
|
||||
fn parse_expr_range(
|
||||
&mut self,
|
||||
prec: ExprPrecedence,
|
||||
lhs: P<Expr>,
|
||||
lhs: Box<Expr>,
|
||||
limits: RangeLimits,
|
||||
cur_op_span: Span,
|
||||
) -> PResult<'a, P<Expr>> {
|
||||
) -> PResult<'a, Box<Expr>> {
|
||||
let rhs = if self.is_at_start_of_range_notation_rhs() {
|
||||
let maybe_lt = self.token;
|
||||
let attrs = self.parse_outer_attributes()?;
|
||||
@@ -448,7 +450,7 @@ impl<'a> Parser<'a> {
|
||||
}
|
||||
|
||||
/// Parses prefix-forms of range notation: `..expr`, `..`, `..=expr`.
|
||||
fn parse_expr_prefix_range(&mut self, attrs: AttrWrapper) -> PResult<'a, P<Expr>> {
|
||||
fn parse_expr_prefix_range(&mut self, attrs: AttrWrapper) -> PResult<'a, Box<Expr>> {
|
||||
if !attrs.is_empty() {
|
||||
let err = errors::DotDotRangeAttribute { span: self.token.span };
|
||||
self.dcx().emit_err(err);
|
||||
@@ -490,7 +492,7 @@ impl<'a> Parser<'a> {
|
||||
}
|
||||
|
||||
/// Parses a prefix-unary-operator expr.
|
||||
fn parse_expr_prefix(&mut self, attrs: AttrWrapper) -> PResult<'a, P<Expr>> {
|
||||
fn parse_expr_prefix(&mut self, attrs: AttrWrapper) -> PResult<'a, Box<Expr>> {
|
||||
let lo = self.token.span;
|
||||
|
||||
macro_rules! make_it {
|
||||
@@ -564,7 +566,7 @@ impl<'a> Parser<'a> {
|
||||
}
|
||||
}
|
||||
|
||||
fn parse_expr_prefix_common(&mut self, lo: Span) -> PResult<'a, (Span, P<Expr>)> {
|
||||
fn parse_expr_prefix_common(&mut self, lo: Span) -> PResult<'a, (Span, Box<Expr>)> {
|
||||
self.bump();
|
||||
let attrs = self.parse_outer_attributes()?;
|
||||
let expr = if self.token.is_range_separator() {
|
||||
@@ -653,12 +655,12 @@ impl<'a> Parser<'a> {
|
||||
|
||||
fn parse_assoc_op_cast(
|
||||
&mut self,
|
||||
lhs: P<Expr>,
|
||||
lhs: Box<Expr>,
|
||||
lhs_span: Span,
|
||||
op_span: Span,
|
||||
expr_kind: fn(P<Expr>, P<Ty>) -> ExprKind,
|
||||
) -> PResult<'a, P<Expr>> {
|
||||
let mk_expr = |this: &mut Self, lhs: P<Expr>, rhs: P<Ty>| {
|
||||
expr_kind: fn(Box<Expr>, Box<Ty>) -> ExprKind,
|
||||
) -> PResult<'a, Box<Expr>> {
|
||||
let mk_expr = |this: &mut Self, lhs: Box<Expr>, rhs: Box<Ty>| {
|
||||
this.mk_expr(this.mk_expr_sp(&lhs, lhs_span, op_span, rhs.span), expr_kind(lhs, rhs))
|
||||
};
|
||||
|
||||
@@ -873,7 +875,7 @@ impl<'a> Parser<'a> {
|
||||
}
|
||||
|
||||
/// Parses `a.b` or `a(13)` or `a[4]` or just `a`.
|
||||
fn parse_expr_dot_or_call(&mut self, attrs: AttrWrapper) -> PResult<'a, P<Expr>> {
|
||||
fn parse_expr_dot_or_call(&mut self, attrs: AttrWrapper) -> PResult<'a, Box<Expr>> {
|
||||
self.collect_tokens_for_expr(attrs, |this, attrs| {
|
||||
let base = this.parse_expr_bottom()?;
|
||||
let span = this.interpolated_or_expr_span(&base);
|
||||
@@ -884,9 +886,9 @@ impl<'a> Parser<'a> {
|
||||
pub(super) fn parse_expr_dot_or_call_with(
|
||||
&mut self,
|
||||
mut attrs: ast::AttrVec,
|
||||
mut e: P<Expr>,
|
||||
mut e: Box<Expr>,
|
||||
lo: Span,
|
||||
) -> PResult<'a, P<Expr>> {
|
||||
) -> PResult<'a, Box<Expr>> {
|
||||
let mut res = ensure_sufficient_stack(|| {
|
||||
loop {
|
||||
let has_question =
|
||||
@@ -945,8 +947,8 @@ impl<'a> Parser<'a> {
|
||||
pub(super) fn parse_dot_suffix_expr(
|
||||
&mut self,
|
||||
lo: Span,
|
||||
base: P<Expr>,
|
||||
) -> PResult<'a, P<Expr>> {
|
||||
base: Box<Expr>,
|
||||
) -> PResult<'a, Box<Expr>> {
|
||||
// At this point we've consumed something like `expr.` and `self.token` holds the token
|
||||
// after the dot.
|
||||
match self.token.uninterpolate().kind {
|
||||
@@ -1232,10 +1234,10 @@ impl<'a> Parser<'a> {
|
||||
&self,
|
||||
lo: Span,
|
||||
ident_span: Span,
|
||||
base: P<Expr>,
|
||||
base: Box<Expr>,
|
||||
field: Symbol,
|
||||
suffix: Option<Symbol>,
|
||||
) -> P<Expr> {
|
||||
) -> Box<Expr> {
|
||||
if let Some(suffix) = suffix {
|
||||
self.expect_no_tuple_index_suffix(ident_span, suffix);
|
||||
}
|
||||
@@ -1243,7 +1245,7 @@ impl<'a> Parser<'a> {
|
||||
}
|
||||
|
||||
/// Parse a function call expression, `expr(...)`.
|
||||
fn parse_expr_fn_call(&mut self, lo: Span, fun: P<Expr>) -> P<Expr> {
|
||||
fn parse_expr_fn_call(&mut self, lo: Span, fun: Box<Expr>) -> Box<Expr> {
|
||||
let snapshot = if self.token == token::OpenParen {
|
||||
Some((self.create_snapshot_for_diagnostic(), fun.kind.clone()))
|
||||
} else {
|
||||
@@ -1267,9 +1269,9 @@ impl<'a> Parser<'a> {
|
||||
&mut self,
|
||||
lo: Span,
|
||||
open_paren: Span,
|
||||
seq: PResult<'a, P<Expr>>,
|
||||
seq: PResult<'a, Box<Expr>>,
|
||||
snapshot: Option<(SnapshotParser<'a>, ExprKind)>,
|
||||
) -> PResult<'a, P<Expr>> {
|
||||
) -> PResult<'a, Box<Expr>> {
|
||||
match (self.may_recover(), seq, snapshot) {
|
||||
(true, Err(err), Some((mut snapshot, ExprKind::Path(None, path)))) => {
|
||||
snapshot.bump(); // `(`
|
||||
@@ -1325,7 +1327,7 @@ impl<'a> Parser<'a> {
|
||||
}
|
||||
|
||||
/// Parse an indexing expression `expr[...]`.
|
||||
fn parse_expr_index(&mut self, lo: Span, base: P<Expr>) -> PResult<'a, P<Expr>> {
|
||||
fn parse_expr_index(&mut self, lo: Span, base: Box<Expr>) -> PResult<'a, Box<Expr>> {
|
||||
let prev_span = self.prev_token.span;
|
||||
let open_delim_span = self.token.span;
|
||||
self.bump(); // `[`
|
||||
@@ -1339,7 +1341,7 @@ impl<'a> Parser<'a> {
|
||||
}
|
||||
|
||||
/// Assuming we have just parsed `.`, continue parsing into an expression.
|
||||
fn parse_dot_suffix(&mut self, self_arg: P<Expr>, lo: Span) -> PResult<'a, P<Expr>> {
|
||||
fn parse_dot_suffix(&mut self, self_arg: Box<Expr>, lo: Span) -> PResult<'a, Box<Expr>> {
|
||||
if self.token_uninterpolated_span().at_least_rust_2018() && self.eat_keyword(exp!(Await)) {
|
||||
return Ok(self.mk_await_expr(self_arg, lo));
|
||||
}
|
||||
@@ -1404,7 +1406,7 @@ impl<'a> Parser<'a> {
|
||||
///
|
||||
/// N.B., this does not parse outer attributes, and is private because it only works
|
||||
/// correctly if called from `parse_expr_dot_or_call`.
|
||||
fn parse_expr_bottom(&mut self) -> PResult<'a, P<Expr>> {
|
||||
fn parse_expr_bottom(&mut self) -> PResult<'a, Box<Expr>> {
|
||||
maybe_recover_from_interpolated_ty_qpath!(self, true);
|
||||
|
||||
let span = self.token.span;
|
||||
@@ -1556,7 +1558,7 @@ impl<'a> Parser<'a> {
|
||||
})
|
||||
}
|
||||
|
||||
fn parse_expr_lit(&mut self) -> PResult<'a, P<Expr>> {
|
||||
fn parse_expr_lit(&mut self) -> PResult<'a, Box<Expr>> {
|
||||
let lo = self.token.span;
|
||||
match self.parse_opt_token_lit() {
|
||||
Some((token_lit, _)) => {
|
||||
@@ -1567,7 +1569,7 @@ impl<'a> Parser<'a> {
|
||||
}
|
||||
}
|
||||
|
||||
fn parse_expr_tuple_parens(&mut self, restrictions: Restrictions) -> PResult<'a, P<Expr>> {
|
||||
fn parse_expr_tuple_parens(&mut self, restrictions: Restrictions) -> PResult<'a, Box<Expr>> {
|
||||
let lo = self.token.span;
|
||||
self.expect(exp!(OpenParen))?;
|
||||
let (es, trailing_comma) = match self.parse_seq_to_end(
|
||||
@@ -1596,7 +1598,7 @@ impl<'a> Parser<'a> {
|
||||
self.maybe_recover_from_bad_qpath(expr)
|
||||
}
|
||||
|
||||
fn parse_expr_array_or_repeat(&mut self, close: ExpTokenPair<'_>) -> PResult<'a, P<Expr>> {
|
||||
fn parse_expr_array_or_repeat(&mut self, close: ExpTokenPair<'_>) -> PResult<'a, Box<Expr>> {
|
||||
let lo = self.token.span;
|
||||
self.bump(); // `[` or other open delim
|
||||
|
||||
@@ -1627,7 +1629,7 @@ impl<'a> Parser<'a> {
|
||||
self.maybe_recover_from_bad_qpath(expr)
|
||||
}
|
||||
|
||||
fn parse_expr_path_start(&mut self) -> PResult<'a, P<Expr>> {
|
||||
fn parse_expr_path_start(&mut self) -> PResult<'a, Box<Expr>> {
|
||||
let maybe_eq_tok = self.prev_token;
|
||||
let (qself, path) = if self.eat_lt() {
|
||||
let lt_span = self.prev_token.span;
|
||||
@@ -1653,7 +1655,7 @@ impl<'a> Parser<'a> {
|
||||
self.dcx().emit_err(errors::MacroInvocationWithQualifiedPath(path.span));
|
||||
}
|
||||
let lo = path.span;
|
||||
let mac = P(MacCall { path, args: self.parse_delim_args()? });
|
||||
let mac = Box::new(MacCall { path, args: self.parse_delim_args()? });
|
||||
(lo.to(self.prev_token.span), ExprKind::MacCall(mac))
|
||||
} else if self.check(exp!(OpenBrace))
|
||||
&& let Some(expr) = self.maybe_parse_struct_expr(&qself, &path)
|
||||
@@ -1675,7 +1677,7 @@ impl<'a> Parser<'a> {
|
||||
&mut self,
|
||||
label_: Label,
|
||||
mut consume_colon: bool,
|
||||
) -> PResult<'a, P<Expr>> {
|
||||
) -> PResult<'a, Box<Expr>> {
|
||||
let lo = label_.ident.span;
|
||||
let label = Some(label_);
|
||||
let ate_colon = self.eat(exp!(Colon));
|
||||
@@ -1810,7 +1812,7 @@ impl<'a> Parser<'a> {
|
||||
}
|
||||
|
||||
/// Recover on the syntax `do catch { ... }` suggesting `try { ... }` instead.
|
||||
fn recover_do_catch(&mut self) -> PResult<'a, P<Expr>> {
|
||||
fn recover_do_catch(&mut self) -> PResult<'a, Box<Expr>> {
|
||||
let lo = self.token.span;
|
||||
|
||||
self.bump(); // `do`
|
||||
@@ -1823,12 +1825,12 @@ impl<'a> Parser<'a> {
|
||||
}
|
||||
|
||||
/// Parse an expression if the token can begin one.
|
||||
fn parse_expr_opt(&mut self) -> PResult<'a, Option<P<Expr>>> {
|
||||
fn parse_expr_opt(&mut self) -> PResult<'a, Option<Box<Expr>>> {
|
||||
Ok(if self.token.can_begin_expr() { Some(self.parse_expr()?) } else { None })
|
||||
}
|
||||
|
||||
/// Parse `"return" expr?`.
|
||||
fn parse_expr_return(&mut self) -> PResult<'a, P<Expr>> {
|
||||
fn parse_expr_return(&mut self) -> PResult<'a, Box<Expr>> {
|
||||
let lo = self.prev_token.span;
|
||||
let kind = ExprKind::Ret(self.parse_expr_opt()?);
|
||||
let expr = self.mk_expr(lo.to(self.prev_token.span), kind);
|
||||
@@ -1836,7 +1838,7 @@ impl<'a> Parser<'a> {
|
||||
}
|
||||
|
||||
/// Parse `"do" "yeet" expr?`.
|
||||
fn parse_expr_yeet(&mut self) -> PResult<'a, P<Expr>> {
|
||||
fn parse_expr_yeet(&mut self) -> PResult<'a, Box<Expr>> {
|
||||
let lo = self.token.span;
|
||||
|
||||
self.bump(); // `do`
|
||||
@@ -1851,7 +1853,7 @@ impl<'a> Parser<'a> {
|
||||
}
|
||||
|
||||
/// Parse `"become" expr`, with `"become"` token already eaten.
|
||||
fn parse_expr_become(&mut self) -> PResult<'a, P<Expr>> {
|
||||
fn parse_expr_become(&mut self) -> PResult<'a, Box<Expr>> {
|
||||
let lo = self.prev_token.span;
|
||||
let kind = ExprKind::Become(self.parse_expr()?);
|
||||
let span = lo.to(self.prev_token.span);
|
||||
@@ -1868,7 +1870,7 @@ impl<'a> Parser<'a> {
|
||||
/// `break 'lbl: loop {}`); a labeled break with an unlabeled loop as its value
|
||||
/// expression only gets a warning for compatibility reasons; and a labeled break
|
||||
/// with a labeled loop does not even get a warning because there is no ambiguity.
|
||||
fn parse_expr_break(&mut self) -> PResult<'a, P<Expr>> {
|
||||
fn parse_expr_break(&mut self) -> PResult<'a, Box<Expr>> {
|
||||
let lo = self.prev_token.span;
|
||||
let mut label = self.eat_label();
|
||||
let kind = if self.token == token::Colon
|
||||
@@ -1930,7 +1932,7 @@ impl<'a> Parser<'a> {
|
||||
}
|
||||
|
||||
/// Parse `"continue" label?`.
|
||||
fn parse_expr_continue(&mut self, lo: Span) -> PResult<'a, P<Expr>> {
|
||||
fn parse_expr_continue(&mut self, lo: Span) -> PResult<'a, Box<Expr>> {
|
||||
let mut label = self.eat_label();
|
||||
|
||||
// Recover `continue label` -> `continue 'label`
|
||||
@@ -1947,7 +1949,7 @@ impl<'a> Parser<'a> {
|
||||
}
|
||||
|
||||
/// Parse `"yield" expr?`.
|
||||
fn parse_expr_yield(&mut self) -> PResult<'a, P<Expr>> {
|
||||
fn parse_expr_yield(&mut self) -> PResult<'a, Box<Expr>> {
|
||||
let lo = self.prev_token.span;
|
||||
let kind = ExprKind::Yield(YieldKind::Prefix(self.parse_expr_opt()?));
|
||||
let span = lo.to(self.prev_token.span);
|
||||
@@ -1957,7 +1959,7 @@ impl<'a> Parser<'a> {
|
||||
}
|
||||
|
||||
/// Parse `builtin # ident(args,*)`.
|
||||
fn parse_expr_builtin(&mut self) -> PResult<'a, P<Expr>> {
|
||||
fn parse_expr_builtin(&mut self) -> PResult<'a, Box<Expr>> {
|
||||
self.parse_builtin(|this, lo, ident| {
|
||||
Ok(match ident.name {
|
||||
sym::offset_of => Some(this.parse_expr_offset_of(lo)?),
|
||||
@@ -2005,7 +2007,7 @@ impl<'a> Parser<'a> {
|
||||
}
|
||||
|
||||
/// Built-in macro for `offset_of!` expressions.
|
||||
pub(crate) fn parse_expr_offset_of(&mut self, lo: Span) -> PResult<'a, P<Expr>> {
|
||||
pub(crate) fn parse_expr_offset_of(&mut self, lo: Span) -> PResult<'a, Box<Expr>> {
|
||||
let container = self.parse_ty()?;
|
||||
self.expect(exp!(Comma))?;
|
||||
|
||||
@@ -2033,7 +2035,7 @@ impl<'a> Parser<'a> {
|
||||
}
|
||||
|
||||
/// Built-in macro for type ascription expressions.
|
||||
pub(crate) fn parse_expr_type_ascribe(&mut self, lo: Span) -> PResult<'a, P<Expr>> {
|
||||
pub(crate) fn parse_expr_type_ascribe(&mut self, lo: Span) -> PResult<'a, Box<Expr>> {
|
||||
let expr = self.parse_expr()?;
|
||||
self.expect(exp!(Comma))?;
|
||||
let ty = self.parse_ty()?;
|
||||
@@ -2045,7 +2047,7 @@ impl<'a> Parser<'a> {
|
||||
&mut self,
|
||||
lo: Span,
|
||||
kind: UnsafeBinderCastKind,
|
||||
) -> PResult<'a, P<Expr>> {
|
||||
) -> PResult<'a, Box<Expr>> {
|
||||
let expr = self.parse_expr()?;
|
||||
let ty = if self.eat(exp!(Comma)) { Some(self.parse_ty()?) } else { None };
|
||||
let span = lo.to(self.token.span);
|
||||
@@ -2155,7 +2157,7 @@ impl<'a> Parser<'a> {
|
||||
/// Keep this in sync with `Token::can_begin_literal_maybe_minus` and
|
||||
/// `Lit::from_token` (excluding unary negation).
|
||||
fn eat_token_lit(&mut self) -> Option<token::Lit> {
|
||||
let check_expr = |expr: P<Expr>| {
|
||||
let check_expr = |expr: Box<Expr>| {
|
||||
if let ast::ExprKind::Lit(token_lit) = expr.kind {
|
||||
Some(token_lit)
|
||||
} else if let ast::ExprKind::Unary(UnOp::Neg, inner) = &expr.kind
|
||||
@@ -2243,7 +2245,7 @@ impl<'a> Parser<'a> {
|
||||
|
||||
/// Matches `'-' lit | lit` (cf. `ast_validation::AstValidator::check_expr_within_pat`).
|
||||
/// Keep this in sync with `Token::can_begin_literal_maybe_minus`.
|
||||
pub fn parse_literal_maybe_minus(&mut self) -> PResult<'a, P<Expr>> {
|
||||
pub fn parse_literal_maybe_minus(&mut self) -> PResult<'a, Box<Expr>> {
|
||||
if let Some(expr) = self.eat_metavar_seq_with_matcher(
|
||||
|mv_kind| matches!(mv_kind, MetaVarKind::Expr { .. }),
|
||||
|this| {
|
||||
@@ -2290,7 +2292,7 @@ impl<'a> Parser<'a> {
|
||||
/// Emits a suggestion if it looks like the user meant an array but
|
||||
/// accidentally used braces, causing the code to be interpreted as a block
|
||||
/// expression.
|
||||
fn maybe_suggest_brackets_instead_of_braces(&mut self, lo: Span) -> Option<P<Expr>> {
|
||||
fn maybe_suggest_brackets_instead_of_braces(&mut self, lo: Span) -> Option<Box<Expr>> {
|
||||
let mut snapshot = self.create_snapshot_for_diagnostic();
|
||||
match snapshot.parse_expr_array_or_repeat(exp!(CloseBrace)) {
|
||||
Ok(arr) => {
|
||||
@@ -2360,7 +2362,7 @@ impl<'a> Parser<'a> {
|
||||
opt_label: Option<Label>,
|
||||
lo: Span,
|
||||
blk_mode: BlockCheckMode,
|
||||
) -> PResult<'a, P<Expr>> {
|
||||
) -> PResult<'a, Box<Expr>> {
|
||||
if self.may_recover() && self.is_array_like_block() {
|
||||
if let Some(arr) = self.maybe_suggest_brackets_instead_of_braces(lo) {
|
||||
return Ok(arr);
|
||||
@@ -2383,13 +2385,13 @@ impl<'a> Parser<'a> {
|
||||
}
|
||||
|
||||
/// Parse a block which takes no attributes and has no label
|
||||
fn parse_simple_block(&mut self) -> PResult<'a, P<Expr>> {
|
||||
fn parse_simple_block(&mut self) -> PResult<'a, Box<Expr>> {
|
||||
let blk = self.parse_block()?;
|
||||
Ok(self.mk_expr(blk.span, ExprKind::Block(blk, None)))
|
||||
}
|
||||
|
||||
/// Parses a closure expression (e.g., `move |args| expr`).
|
||||
fn parse_expr_closure(&mut self) -> PResult<'a, P<Expr>> {
|
||||
fn parse_expr_closure(&mut self) -> PResult<'a, Box<Expr>> {
|
||||
let lo = self.token.span;
|
||||
|
||||
let before = self.prev_token;
|
||||
@@ -2494,7 +2496,7 @@ impl<'a> Parser<'a> {
|
||||
}
|
||||
|
||||
/// If an explicit return type is given, require a block to appear (RFC 968).
|
||||
fn parse_closure_block_body(&mut self, ret_span: Span) -> PResult<'a, P<Expr>> {
|
||||
fn parse_closure_block_body(&mut self, ret_span: Span) -> PResult<'a, Box<Expr>> {
|
||||
if self.may_recover()
|
||||
&& self.token.can_begin_expr()
|
||||
&& self.token.kind != TokenKind::OpenBrace
|
||||
@@ -2565,7 +2567,7 @@ impl<'a> Parser<'a> {
|
||||
}
|
||||
|
||||
/// Parses the `|arg, arg|` header of a closure.
|
||||
fn parse_fn_block_decl(&mut self) -> PResult<'a, (P<FnDecl>, Span)> {
|
||||
fn parse_fn_block_decl(&mut self) -> PResult<'a, (Box<FnDecl>, Span)> {
|
||||
let arg_start = self.token.span.lo();
|
||||
|
||||
let inputs = if self.eat(exp!(OrOr)) {
|
||||
@@ -2587,7 +2589,7 @@ impl<'a> Parser<'a> {
|
||||
let output =
|
||||
self.parse_ret_ty(AllowPlus::Yes, RecoverQPath::Yes, RecoverReturnSign::Yes)?;
|
||||
|
||||
Ok((P(FnDecl { inputs, output }), arg_span))
|
||||
Ok((Box::new(FnDecl { inputs, output }), arg_span))
|
||||
}
|
||||
|
||||
/// Parses a parameter in a closure header (e.g., `|arg, arg|`).
|
||||
@@ -2618,7 +2620,7 @@ impl<'a> Parser<'a> {
|
||||
}
|
||||
|
||||
/// Parses an `if` expression (`if` token already eaten).
|
||||
fn parse_expr_if(&mut self) -> PResult<'a, P<Expr>> {
|
||||
fn parse_expr_if(&mut self) -> PResult<'a, Box<Expr>> {
|
||||
let lo = self.prev_token.span;
|
||||
// Scoping code checks the top level edition of the `if`; let's match it here.
|
||||
// The `CondChecker` also checks the edition of the `let` itself, just to make sure.
|
||||
@@ -2627,7 +2629,7 @@ impl<'a> Parser<'a> {
|
||||
self.parse_if_after_cond(lo, cond)
|
||||
}
|
||||
|
||||
fn parse_if_after_cond(&mut self, lo: Span, mut cond: P<Expr>) -> PResult<'a, P<Expr>> {
|
||||
fn parse_if_after_cond(&mut self, lo: Span, mut cond: Box<Expr>) -> PResult<'a, Box<Expr>> {
|
||||
let cond_span = cond.span;
|
||||
// Tries to interpret `cond` as either a missing expression if it's a block,
|
||||
// or as an unfinished expression if it's a binop and the RHS is a block.
|
||||
@@ -2737,7 +2739,10 @@ impl<'a> Parser<'a> {
|
||||
/// i.e. the same span we use to later decide whether the drop behaviour should be that of
|
||||
/// edition `..=2021` or that of `2024..`.
|
||||
// Public because it is used in rustfmt forks such as https://github.com/tucant/rustfmt/blob/30c83df9e1db10007bdd16dafce8a86b404329b2/src/parse/macros/html.rs#L57 for custom if expressions.
|
||||
pub fn parse_expr_cond(&mut self, let_chains_policy: LetChainsPolicy) -> PResult<'a, P<Expr>> {
|
||||
pub fn parse_expr_cond(
|
||||
&mut self,
|
||||
let_chains_policy: LetChainsPolicy,
|
||||
) -> PResult<'a, Box<Expr>> {
|
||||
let attrs = self.parse_outer_attributes()?;
|
||||
let (mut cond, _) =
|
||||
self.parse_expr_res(Restrictions::NO_STRUCT_LITERAL | Restrictions::ALLOW_LET, attrs)?;
|
||||
@@ -2748,7 +2753,7 @@ impl<'a> Parser<'a> {
|
||||
}
|
||||
|
||||
/// Parses a `let $pat = $expr` pseudo-expression.
|
||||
fn parse_expr_let(&mut self, restrictions: Restrictions) -> PResult<'a, P<Expr>> {
|
||||
fn parse_expr_let(&mut self, restrictions: Restrictions) -> PResult<'a, Box<Expr>> {
|
||||
let recovered = if !restrictions.contains(Restrictions::ALLOW_LET) {
|
||||
let err = errors::ExpectedExpressionFoundLet {
|
||||
span: self.token.span,
|
||||
@@ -2790,7 +2795,7 @@ impl<'a> Parser<'a> {
|
||||
}
|
||||
|
||||
/// Parses an `else { ... }` expression (`else` token already eaten).
|
||||
fn parse_expr_else(&mut self) -> PResult<'a, P<Expr>> {
|
||||
fn parse_expr_else(&mut self) -> PResult<'a, Box<Expr>> {
|
||||
let else_span = self.prev_token.span; // `else`
|
||||
let attrs = self.parse_outer_attributes()?; // For recovery.
|
||||
let expr = if self.eat_keyword(exp!(If)) {
|
||||
@@ -2888,7 +2893,7 @@ impl<'a> Parser<'a> {
|
||||
}
|
||||
}
|
||||
|
||||
fn error_on_extra_if(&mut self, cond: &P<Expr>) -> PResult<'a, ()> {
|
||||
fn error_on_extra_if(&mut self, cond: &Box<Expr>) -> PResult<'a, ()> {
|
||||
if let ExprKind::Binary(Spanned { span: binop_span, node: binop }, _, right) = &cond.kind
|
||||
&& let BinOpKind::And = binop
|
||||
&& let ExprKind::If(cond, ..) = &right.kind
|
||||
@@ -2901,7 +2906,7 @@ impl<'a> Parser<'a> {
|
||||
}
|
||||
}
|
||||
|
||||
fn parse_for_head(&mut self) -> PResult<'a, (P<Pat>, P<Expr>)> {
|
||||
fn parse_for_head(&mut self) -> PResult<'a, (Box<Pat>, Box<Expr>)> {
|
||||
let begin_paren = if self.token == token::OpenParen {
|
||||
// Record whether we are about to parse `for (`.
|
||||
// This is used below for recovery in case of `for ( $stuff ) $block`
|
||||
@@ -2967,7 +2972,7 @@ impl<'a> Parser<'a> {
|
||||
}
|
||||
|
||||
/// Parses `for await? <src_pat> in <src_expr> <src_loop_block>` (`for` token already eaten).
|
||||
fn parse_expr_for(&mut self, opt_label: Option<Label>, lo: Span) -> PResult<'a, P<Expr>> {
|
||||
fn parse_expr_for(&mut self, opt_label: Option<Label>, lo: Span) -> PResult<'a, Box<Expr>> {
|
||||
let is_await =
|
||||
self.token_uninterpolated_span().at_least_rust_2018() && self.eat_keyword(exp!(Await));
|
||||
|
||||
@@ -3038,7 +3043,7 @@ impl<'a> Parser<'a> {
|
||||
}
|
||||
|
||||
/// Parses a `while` or `while let` expression (`while` token already eaten).
|
||||
fn parse_expr_while(&mut self, opt_label: Option<Label>, lo: Span) -> PResult<'a, P<Expr>> {
|
||||
fn parse_expr_while(&mut self, opt_label: Option<Label>, lo: Span) -> PResult<'a, Box<Expr>> {
|
||||
let policy = LetChainsPolicy::EditionDependent { current_edition: lo.edition() };
|
||||
let cond = self.parse_expr_cond(policy).map_err(|mut err| {
|
||||
err.span_label(lo, "while parsing the condition of this `while` expression");
|
||||
@@ -3066,7 +3071,7 @@ impl<'a> Parser<'a> {
|
||||
}
|
||||
|
||||
/// Parses `loop { ... }` (`loop` token already eaten).
|
||||
fn parse_expr_loop(&mut self, opt_label: Option<Label>, lo: Span) -> PResult<'a, P<Expr>> {
|
||||
fn parse_expr_loop(&mut self, opt_label: Option<Label>, lo: Span) -> PResult<'a, Box<Expr>> {
|
||||
let loop_span = self.prev_token.span;
|
||||
let (attrs, body) = self.parse_inner_attrs_and_block(
|
||||
// Only suggest moving erroneous block label to the loop header
|
||||
@@ -3096,7 +3101,7 @@ impl<'a> Parser<'a> {
|
||||
}
|
||||
|
||||
/// Parses a `match ... { ... }` expression (`match` token already eaten).
|
||||
fn parse_expr_match(&mut self) -> PResult<'a, P<Expr>> {
|
||||
fn parse_expr_match(&mut self) -> PResult<'a, Box<Expr>> {
|
||||
let match_span = self.prev_token.span;
|
||||
let attrs = self.parse_outer_attributes()?;
|
||||
let (scrutinee, _) = self.parse_expr_res(Restrictions::NO_STRUCT_LITERAL, attrs)?;
|
||||
@@ -3110,9 +3115,9 @@ impl<'a> Parser<'a> {
|
||||
&mut self,
|
||||
lo: Span,
|
||||
match_span: Span,
|
||||
scrutinee: P<Expr>,
|
||||
scrutinee: Box<Expr>,
|
||||
match_kind: MatchKind,
|
||||
) -> PResult<'a, P<Expr>> {
|
||||
) -> PResult<'a, Box<Expr>> {
|
||||
if let Err(mut e) = self.expect(exp!(OpenBrace)) {
|
||||
if self.token == token::Semi {
|
||||
e.span_suggestion_short(
|
||||
@@ -3169,7 +3174,7 @@ impl<'a> Parser<'a> {
|
||||
/// Attempt to recover from match arm body with statements and no surrounding braces.
|
||||
fn parse_arm_body_missing_braces(
|
||||
&mut self,
|
||||
first_expr: &P<Expr>,
|
||||
first_expr: &Box<Expr>,
|
||||
arrow_span: Span,
|
||||
) -> Option<(Span, ErrorGuaranteed)> {
|
||||
if self.token != token::Semi {
|
||||
@@ -3441,7 +3446,7 @@ impl<'a> Parser<'a> {
|
||||
})
|
||||
}
|
||||
|
||||
fn parse_match_arm_guard(&mut self) -> PResult<'a, Option<P<Expr>>> {
|
||||
fn parse_match_arm_guard(&mut self) -> PResult<'a, Option<Box<Expr>>> {
|
||||
// Used to check the `if_let_guard` feature mostly by scanning
|
||||
// `&&` tokens.
|
||||
fn has_let_expr(expr: &Expr) -> bool {
|
||||
@@ -3472,7 +3477,7 @@ impl<'a> Parser<'a> {
|
||||
Ok(Some(cond))
|
||||
}
|
||||
|
||||
fn parse_match_arm_pat_and_guard(&mut self) -> PResult<'a, (P<Pat>, Option<P<Expr>>)> {
|
||||
fn parse_match_arm_pat_and_guard(&mut self) -> PResult<'a, (Box<Pat>, Option<Box<Expr>>)> {
|
||||
if self.token == token::OpenParen {
|
||||
let left = self.token.span;
|
||||
let pat = self.parse_pat_no_top_guard(
|
||||
@@ -3512,7 +3517,7 @@ impl<'a> Parser<'a> {
|
||||
}
|
||||
}
|
||||
|
||||
fn parse_match_guard_condition(&mut self) -> PResult<'a, P<Expr>> {
|
||||
fn parse_match_guard_condition(&mut self) -> PResult<'a, Box<Expr>> {
|
||||
let attrs = self.parse_outer_attributes()?;
|
||||
match self.parse_expr_res(Restrictions::ALLOW_LET | Restrictions::IN_IF_GUARD, attrs) {
|
||||
Ok((expr, _)) => Ok(expr),
|
||||
@@ -3546,7 +3551,7 @@ impl<'a> Parser<'a> {
|
||||
}
|
||||
|
||||
/// Parses a `try {...}` expression (`try` token already eaten).
|
||||
fn parse_try_block(&mut self, span_lo: Span) -> PResult<'a, P<Expr>> {
|
||||
fn parse_try_block(&mut self, span_lo: Span) -> PResult<'a, Box<Expr>> {
|
||||
let (attrs, body) = self.parse_inner_attrs_and_block(None)?;
|
||||
if self.eat_keyword(exp!(Catch)) {
|
||||
Err(self.dcx().create_err(errors::CatchAfterTry { span: self.prev_token.span }))
|
||||
@@ -3575,7 +3580,7 @@ impl<'a> Parser<'a> {
|
||||
}
|
||||
|
||||
/// Parses an `async move? {...}` or `gen move? {...}` expression.
|
||||
fn parse_gen_block(&mut self) -> PResult<'a, P<Expr>> {
|
||||
fn parse_gen_block(&mut self) -> PResult<'a, Box<Expr>> {
|
||||
let lo = self.token.span;
|
||||
let kind = if self.eat_keyword(exp!(Async)) {
|
||||
if self.eat_keyword(exp!(Gen)) { GenBlockKind::AsyncGen } else { GenBlockKind::Async }
|
||||
@@ -3624,9 +3629,9 @@ impl<'a> Parser<'a> {
|
||||
|
||||
fn maybe_parse_struct_expr(
|
||||
&mut self,
|
||||
qself: &Option<P<ast::QSelf>>,
|
||||
qself: &Option<Box<ast::QSelf>>,
|
||||
path: &ast::Path,
|
||||
) -> Option<PResult<'a, P<Expr>>> {
|
||||
) -> Option<PResult<'a, Box<Expr>>> {
|
||||
let struct_allowed = !self.restrictions.contains(Restrictions::NO_STRUCT_LITERAL);
|
||||
if struct_allowed || self.is_certainly_not_a_block() {
|
||||
if let Err(err) = self.expect(exp!(OpenBrace)) {
|
||||
@@ -3820,10 +3825,10 @@ impl<'a> Parser<'a> {
|
||||
/// Precondition: already parsed the '{'.
|
||||
pub(super) fn parse_expr_struct(
|
||||
&mut self,
|
||||
qself: Option<P<ast::QSelf>>,
|
||||
qself: Option<Box<ast::QSelf>>,
|
||||
pth: ast::Path,
|
||||
recover: bool,
|
||||
) -> PResult<'a, P<Expr>> {
|
||||
) -> PResult<'a, Box<Expr>> {
|
||||
let lo = pth.span;
|
||||
let (fields, base, recovered_async) =
|
||||
self.parse_struct_fields(pth.clone(), recover, exp!(CloseBrace))?;
|
||||
@@ -3832,7 +3837,7 @@ impl<'a> Parser<'a> {
|
||||
let expr = if let Some(guar) = recovered_async {
|
||||
ExprKind::Err(guar)
|
||||
} else {
|
||||
ExprKind::Struct(P(ast::StructExpr { qself, path: pth, fields, rest: base }))
|
||||
ExprKind::Struct(Box::new(ast::StructExpr { qself, path: pth, fields, rest: base }))
|
||||
};
|
||||
Ok(self.mk_expr(span, expr))
|
||||
}
|
||||
@@ -3947,14 +3952,14 @@ impl<'a> Parser<'a> {
|
||||
self.dcx().emit_err(errors::LeftArrowOperator { span });
|
||||
}
|
||||
|
||||
fn mk_assign_op(&self, assign_op: AssignOp, lhs: P<Expr>, rhs: P<Expr>) -> ExprKind {
|
||||
fn mk_assign_op(&self, assign_op: AssignOp, lhs: Box<Expr>, rhs: Box<Expr>) -> ExprKind {
|
||||
ExprKind::AssignOp(assign_op, lhs, rhs)
|
||||
}
|
||||
|
||||
fn mk_range(
|
||||
&mut self,
|
||||
start: Option<P<Expr>>,
|
||||
end: Option<P<Expr>>,
|
||||
start: Option<Box<Expr>>,
|
||||
end: Option<Box<Expr>>,
|
||||
limits: RangeLimits,
|
||||
) -> ExprKind {
|
||||
if end.is_none() && limits == RangeLimits::Closed {
|
||||
@@ -3965,51 +3970,56 @@ impl<'a> Parser<'a> {
|
||||
}
|
||||
}
|
||||
|
||||
fn mk_unary(&self, unop: UnOp, expr: P<Expr>) -> ExprKind {
|
||||
fn mk_unary(&self, unop: UnOp, expr: Box<Expr>) -> ExprKind {
|
||||
ExprKind::Unary(unop, expr)
|
||||
}
|
||||
|
||||
fn mk_binary(&self, binop: BinOp, lhs: P<Expr>, rhs: P<Expr>) -> ExprKind {
|
||||
fn mk_binary(&self, binop: BinOp, lhs: Box<Expr>, rhs: Box<Expr>) -> ExprKind {
|
||||
ExprKind::Binary(binop, lhs, rhs)
|
||||
}
|
||||
|
||||
fn mk_index(&self, expr: P<Expr>, idx: P<Expr>, brackets_span: Span) -> ExprKind {
|
||||
fn mk_index(&self, expr: Box<Expr>, idx: Box<Expr>, brackets_span: Span) -> ExprKind {
|
||||
ExprKind::Index(expr, idx, brackets_span)
|
||||
}
|
||||
|
||||
fn mk_call(&self, f: P<Expr>, args: ThinVec<P<Expr>>) -> ExprKind {
|
||||
fn mk_call(&self, f: Box<Expr>, args: ThinVec<Box<Expr>>) -> ExprKind {
|
||||
ExprKind::Call(f, args)
|
||||
}
|
||||
|
||||
fn mk_await_expr(&mut self, self_arg: P<Expr>, lo: Span) -> P<Expr> {
|
||||
fn mk_await_expr(&mut self, self_arg: Box<Expr>, lo: Span) -> Box<Expr> {
|
||||
let span = lo.to(self.prev_token.span);
|
||||
let await_expr = self.mk_expr(span, ExprKind::Await(self_arg, self.prev_token.span));
|
||||
self.recover_from_await_method_call();
|
||||
await_expr
|
||||
}
|
||||
|
||||
fn mk_use_expr(&mut self, self_arg: P<Expr>, lo: Span) -> P<Expr> {
|
||||
fn mk_use_expr(&mut self, self_arg: Box<Expr>, lo: Span) -> Box<Expr> {
|
||||
let span = lo.to(self.prev_token.span);
|
||||
let use_expr = self.mk_expr(span, ExprKind::Use(self_arg, self.prev_token.span));
|
||||
self.recover_from_use();
|
||||
use_expr
|
||||
}
|
||||
|
||||
pub(crate) fn mk_expr_with_attrs(&self, span: Span, kind: ExprKind, attrs: AttrVec) -> P<Expr> {
|
||||
P(Expr { kind, span, attrs, id: DUMMY_NODE_ID, tokens: None })
|
||||
pub(crate) fn mk_expr_with_attrs(
|
||||
&self,
|
||||
span: Span,
|
||||
kind: ExprKind,
|
||||
attrs: AttrVec,
|
||||
) -> Box<Expr> {
|
||||
Box::new(Expr { kind, span, attrs, id: DUMMY_NODE_ID, tokens: None })
|
||||
}
|
||||
|
||||
pub(crate) fn mk_expr(&self, span: Span, kind: ExprKind) -> P<Expr> {
|
||||
pub(crate) fn mk_expr(&self, span: Span, kind: ExprKind) -> Box<Expr> {
|
||||
self.mk_expr_with_attrs(span, kind, AttrVec::new())
|
||||
}
|
||||
|
||||
pub(super) fn mk_expr_err(&self, span: Span, guar: ErrorGuaranteed) -> P<Expr> {
|
||||
pub(super) fn mk_expr_err(&self, span: Span, guar: ErrorGuaranteed) -> Box<Expr> {
|
||||
self.mk_expr(span, ExprKind::Err(guar))
|
||||
}
|
||||
|
||||
/// Create expression span ensuring the span of the parent node
|
||||
/// is larger than the span of lhs and rhs, including the attributes.
|
||||
fn mk_expr_sp(&self, lhs: &P<Expr>, lhs_span: Span, op_span: Span, rhs_span: Span) -> Span {
|
||||
fn mk_expr_sp(&self, lhs: &Box<Expr>, lhs_span: Span, op_span: Span, rhs_span: Span) -> Span {
|
||||
lhs.attrs
|
||||
.iter()
|
||||
.find(|a| a.style == AttrStyle::Outer)
|
||||
@@ -4021,8 +4031,8 @@ impl<'a> Parser<'a> {
|
||||
fn collect_tokens_for_expr(
|
||||
&mut self,
|
||||
attrs: AttrWrapper,
|
||||
f: impl FnOnce(&mut Self, ast::AttrVec) -> PResult<'a, P<Expr>>,
|
||||
) -> PResult<'a, P<Expr>> {
|
||||
f: impl FnOnce(&mut Self, ast::AttrVec) -> PResult<'a, Box<Expr>>,
|
||||
) -> PResult<'a, Box<Expr>> {
|
||||
self.collect_tokens(None, attrs, ForceCollect::No, |this, attrs| {
|
||||
let res = f(this, attrs)?;
|
||||
let trailing = Trailing::from(
|
||||
|
||||
Reference in New Issue
Block a user