1 use super::diagnostics::SnapshotParser;
2 use super::pat::{CommaRecoveryMode, RecoverColon, RecoverComma, PARAM_EXPECTED};
3 use super::ty::{AllowPlus, RecoverQPath, RecoverReturnSign};
5 AttrWrapper, BlockMode, ClosureSpans, ForceCollect, Parser, PathStyle, Restrictions,
6 SemiColonMode, SeqSep, TokenExpectType, TokenType, TrailingToken,
9 ArrayBracketsInsteadOfSpaces, ArrayBracketsInsteadOfSpacesSugg, AsyncMoveOrderIncorrect,
10 BracesForStructLiteral, CatchAfterTry, CommaAfterBaseStruct, ComparisonInterpretedAsGeneric,
11 ComparisonOrShiftInterpretedAsGenericSugg, DoCatchSyntaxRemoved, DotDotDot, EqFieldInit,
12 ExpectedElseBlock, ExpectedEqForLetExpr, ExpectedExpressionFoundLet,
13 FieldExpressionWithGeneric, FloatLiteralRequiresIntegerPart, FoundExprWouldBeStmt,
14 IfExpressionMissingCondition, IfExpressionMissingThenBlock, IfExpressionMissingThenBlockSub,
15 InvalidBlockMacroSegment, InvalidComparisonOperator, InvalidComparisonOperatorSub,
16 InvalidInterpolatedExpression, InvalidLiteralSuffixOnTupleIndex, InvalidLogicalOperator,
17 InvalidLogicalOperatorSub, LabeledLoopInBreak, LeadingPlusNotSupported, LeftArrowOperator,
18 LifetimeInBorrowExpression, MacroInvocationWithQualifiedPath, MalformedLoopLabel,
19 MatchArmBodyWithoutBraces, MatchArmBodyWithoutBracesSugg, MissingCommaAfterMatchArm,
20 MissingDotDot, MissingInInForLoop, MissingInInForLoopSub, MissingSemicolonBeforeArray,
21 NoFieldsForFnCall, NotAsNegationOperator, NotAsNegationOperatorSub,
22 OuterAttributeNotAllowedOnIfElse, ParenthesesWithStructFields,
23 RequireColonAfterLabeledExpression, ShiftInterpretedAsGeneric, StructLiteralNotAllowedHere,
24 StructLiteralNotAllowedHereSugg, TildeAsUnaryOperator, UnexpectedIfWithIf,
25 UnexpectedTokenAfterLabel, UnexpectedTokenAfterLabelSugg, WrapExpressionInParentheses,
27 use crate::maybe_recover_from_interpolated_ty_qpath;
29 use rustc_ast::ptr::P;
30 use rustc_ast::token::{self, Delimiter, Token, TokenKind};
31 use rustc_ast::tokenstream::Spacing;
32 use rustc_ast::util::case::Case;
33 use rustc_ast::util::classify;
34 use rustc_ast::util::parser::{prec_let_scrutinee_needs_par, AssocOp, Fixity};
35 use rustc_ast::visit::Visitor;
36 use rustc_ast::{self as ast, AttrStyle, AttrVec, CaptureBy, ExprField, Lit, UnOp, DUMMY_NODE_ID};
37 use rustc_ast::{AnonConst, BinOp, BinOpKind, FnDecl, FnRetTy, MacCall, Param, Ty, TyKind};
38 use rustc_ast::{Arm, Async, BlockCheckMode, Expr, ExprKind, Label, Movability, RangeLimits};
39 use rustc_ast::{ClosureBinder, StmtKind};
40 use rustc_ast_pretty::pprust;
42 Applicability, Diagnostic, DiagnosticBuilder, ErrorGuaranteed, IntoDiagnostic, PResult,
45 use rustc_session::errors::{report_lit_error, ExprParenthesesNeeded};
46 use rustc_session::lint::builtin::BREAK_WITH_LABEL_AND_LOOP;
47 use rustc_session::lint::BuiltinLintDiagnostics;
48 use rustc_span::source_map::{self, Span, Spanned};
49 use rustc_span::symbol::{kw, sym, Ident, Symbol};
50 use rustc_span::{BytePos, Pos};
52 /// Possibly accepts an `token::Interpolated` expression (a pre-parsed expression
53 /// dropped into the token stream, which happens while parsing the result of
54 /// macro expansion). Placement of these is not as complex as I feared it would
55 /// be. The important thing is to make sure that lookahead doesn't balk at
56 /// `token::Interpolated` tokens.
57 macro_rules! maybe_whole_expr {
59 if let token::Interpolated(nt) = &$p.token.kind {
61 token::NtExpr(e) | token::NtLiteral(e) => {
66 token::NtPath(path) => {
67 let path = (**path).clone();
69 return Ok($p.mk_expr($p.prev_token.span, ExprKind::Path(None, path)));
71 token::NtBlock(block) => {
72 let block = block.clone();
74 return Ok($p.mk_expr($p.prev_token.span, ExprKind::Block(block, None)));
83 pub(super) enum LhsExpr {
85 AttributesParsed(AttrWrapper),
86 AlreadyParsed(P<Expr>),
89 impl From<Option<AttrWrapper>> for LhsExpr {
90 /// Converts `Some(attrs)` into `LhsExpr::AttributesParsed(attrs)`
91 /// and `None` into `LhsExpr::NotYetParsed`.
93 /// This conversion does not allocate.
94 fn from(o: Option<AttrWrapper>) -> Self {
95 if let Some(attrs) = o { LhsExpr::AttributesParsed(attrs) } else { LhsExpr::NotYetParsed }
99 impl From<P<Expr>> for LhsExpr {
100 /// Converts the `expr: P<Expr>` into `LhsExpr::AlreadyParsed(expr)`.
102 /// This conversion does not allocate.
103 fn from(expr: P<Expr>) -> Self {
104 LhsExpr::AlreadyParsed(expr)
108 impl<'a> Parser<'a> {
109 /// Parses an expression.
111 pub fn parse_expr(&mut self) -> PResult<'a, P<Expr>> {
112 self.current_closure.take();
114 self.parse_expr_res(Restrictions::empty(), None)
117 /// Parses an expression, forcing tokens to be collected
118 pub fn parse_expr_force_collect(&mut self) -> PResult<'a, P<Expr>> {
119 self.collect_tokens_no_attrs(|this| this.parse_expr())
122 pub fn parse_anon_const_expr(&mut self) -> PResult<'a, AnonConst> {
123 self.parse_expr().map(|value| AnonConst { id: DUMMY_NODE_ID, value })
126 fn parse_expr_catch_underscore(&mut self) -> PResult<'a, P<Expr>> {
127 match self.parse_expr() {
128 Ok(expr) => Ok(expr),
129 Err(mut err) => match self.token.ident() {
130 Some((Ident { name: kw::Underscore, .. }, false))
131 if self.may_recover() && self.look_ahead(1, |t| t == &token::Comma) =>
133 // Special-case handling of `foo(_, _, _)`
136 Ok(self.mk_expr(self.prev_token.span, ExprKind::Err))
143 /// Parses a sequence of expressions delimited by parentheses.
144 fn parse_paren_expr_seq(&mut self) -> PResult<'a, Vec<P<Expr>>> {
145 self.parse_paren_comma_seq(|p| p.parse_expr_catch_underscore()).map(|(r, _)| r)
148 /// Parses an expression, subject to the given restrictions.
150 pub(super) fn parse_expr_res(
153 already_parsed_attrs: Option<AttrWrapper>,
154 ) -> PResult<'a, P<Expr>> {
155 self.with_res(r, |this| this.parse_assoc_expr(already_parsed_attrs))
158 /// Parses an associative expression.
160 /// This parses an expression accounting for associativity and precedence of the operators in
165 already_parsed_attrs: Option<AttrWrapper>,
166 ) -> PResult<'a, P<Expr>> {
167 self.parse_assoc_expr_with(0, already_parsed_attrs.into())
170 /// Parses an associative expression with operators of at least `min_prec` precedence.
171 pub(super) fn parse_assoc_expr_with(
175 ) -> PResult<'a, P<Expr>> {
176 let mut lhs = if let LhsExpr::AlreadyParsed(expr) = lhs {
179 let attrs = match lhs {
180 LhsExpr::AttributesParsed(attrs) => Some(attrs),
183 if [token::DotDot, token::DotDotDot, token::DotDotEq].contains(&self.token.kind) {
184 return self.parse_prefix_range_expr(attrs);
186 self.parse_prefix_expr(attrs)?
189 let last_type_ascription_set = self.last_type_ascription.is_some();
191 if !self.should_continue_as_assoc_expr(&lhs) {
192 self.last_type_ascription = None;
196 self.expected_tokens.push(TokenType::Operator);
197 while let Some(op) = self.check_assoc_op() {
198 // Adjust the span for interpolated LHS to point to the `$lhs` token
199 // and not to what it refers to.
200 let lhs_span = match self.prev_token.kind {
201 TokenKind::Interpolated(..) => self.prev_token.span,
205 let cur_op_span = self.token.span;
206 let restrictions = if op.node.is_assign_like() {
207 self.restrictions & Restrictions::NO_STRUCT_LITERAL
211 let prec = op.node.precedence();
215 // Check for deprecated `...` syntax
216 if self.token == token::DotDotDot && op.node == AssocOp::DotDotEq {
217 self.err_dotdotdot_syntax(self.token.span);
220 if self.token == token::LArrow {
221 self.err_larrow_operator(self.token.span);
225 if op.node.is_comparison() {
226 if let Some(expr) = self.check_no_chained_comparison(&lhs, &op)? {
231 // Look for JS' `===` and `!==` and recover
232 if (op.node == AssocOp::Equal || op.node == AssocOp::NotEqual)
233 && self.token.kind == token::Eq
234 && self.prev_token.span.hi() == self.token.span.lo()
236 let sp = op.span.to(self.token.span);
237 let sugg = match op.node {
238 AssocOp::Equal => "==",
239 AssocOp::NotEqual => "!=",
243 let invalid = format!("{}=", &sugg);
244 self.sess.emit_err(InvalidComparisonOperator {
246 invalid: invalid.clone(),
247 sub: InvalidComparisonOperatorSub::Correctable {
256 // Look for PHP's `<>` and recover
257 if op.node == AssocOp::Less
258 && self.token.kind == token::Gt
259 && self.prev_token.span.hi() == self.token.span.lo()
261 let sp = op.span.to(self.token.span);
262 self.sess.emit_err(InvalidComparisonOperator {
264 invalid: "<>".into(),
265 sub: InvalidComparisonOperatorSub::Correctable {
267 invalid: "<>".into(),
268 correct: "!=".into(),
274 // Look for C++'s `<=>` and recover
275 if op.node == AssocOp::LessEqual
276 && self.token.kind == token::Gt
277 && self.prev_token.span.hi() == self.token.span.lo()
279 let sp = op.span.to(self.token.span);
280 self.sess.emit_err(InvalidComparisonOperator {
282 invalid: "<=>".into(),
283 sub: InvalidComparisonOperatorSub::Spaceship(sp),
288 if self.prev_token == token::BinOp(token::Plus)
289 && self.token == token::BinOp(token::Plus)
290 && self.prev_token.span.between(self.token.span).is_empty()
292 let op_span = self.prev_token.span.to(self.token.span);
293 // Eat the second `+`
295 lhs = self.recover_from_postfix_increment(lhs, op_span)?;
301 if op == AssocOp::As {
302 lhs = self.parse_assoc_op_cast(lhs, lhs_span, ExprKind::Cast)?;
304 } else if op == AssocOp::Colon {
305 lhs = self.parse_assoc_op_ascribe(lhs, lhs_span)?;
307 } else if op == AssocOp::DotDot || op == AssocOp::DotDotEq {
308 // If we didn't have to handle `x..`/`x..=`, it would be pretty easy to
309 // generalise it to the Fixity::None code.
310 lhs = self.parse_range_expr(prec, lhs, op, cur_op_span)?;
314 let fixity = op.fixity();
315 let prec_adjustment = match fixity {
318 // We currently have no non-associative operators that are not handled above by
319 // the special cases. The code is here only for future convenience.
322 let rhs = self.with_res(restrictions - Restrictions::STMT_EXPR, |this| {
323 this.parse_assoc_expr_with(prec + prec_adjustment, LhsExpr::NotYetParsed)
326 let span = self.mk_expr_sp(&lhs, lhs_span, rhs.span);
339 | AssocOp::ShiftRight
345 | AssocOp::GreaterEqual => {
346 let ast_op = op.to_ast_binop().unwrap();
347 let binary = self.mk_binary(source_map::respan(cur_op_span, ast_op), lhs, rhs);
348 self.mk_expr(span, binary)
350 AssocOp::Assign => self.mk_expr(span, ExprKind::Assign(lhs, rhs, cur_op_span)),
351 AssocOp::AssignOp(k) => {
353 token::Plus => BinOpKind::Add,
354 token::Minus => BinOpKind::Sub,
355 token::Star => BinOpKind::Mul,
356 token::Slash => BinOpKind::Div,
357 token::Percent => BinOpKind::Rem,
358 token::Caret => BinOpKind::BitXor,
359 token::And => BinOpKind::BitAnd,
360 token::Or => BinOpKind::BitOr,
361 token::Shl => BinOpKind::Shl,
362 token::Shr => BinOpKind::Shr,
364 let aopexpr = self.mk_assign_op(source_map::respan(cur_op_span, aop), lhs, rhs);
365 self.mk_expr(span, aopexpr)
367 AssocOp::As | AssocOp::Colon | AssocOp::DotDot | AssocOp::DotDotEq => {
368 self.span_bug(span, "AssocOp should have been handled by special case")
372 if let Fixity::None = fixity {
376 if last_type_ascription_set {
377 self.last_type_ascription = None;
382 fn should_continue_as_assoc_expr(&mut self, lhs: &Expr) -> bool {
383 match (self.expr_is_complete(lhs), AssocOp::from_token(&self.token)) {
384 // Semi-statement forms are odd:
385 // See https://github.com/rust-lang/rust/issues/29071
386 (true, None) => false,
387 (false, _) => true, // Continue parsing the expression.
388 // An exhaustive check is done in the following block, but these are checked first
389 // because they *are* ambiguous but also reasonable looking incorrect syntax, so we
390 // want to keep their span info to improve diagnostics in these cases in a later stage.
391 (true, Some(AssocOp::Multiply)) | // `{ 42 } *foo = bar;` or `{ 42 } * 3`
392 (true, Some(AssocOp::Subtract)) | // `{ 42 } -5`
393 (true, Some(AssocOp::Add)) // `{ 42 } + 42
394 // If the next token is a keyword, then the tokens above *are* unambiguously incorrect:
395 // `if x { a } else { b } && if y { c } else { d }`
396 if !self.look_ahead(1, |t| t.is_used_keyword()) => {
397 // These cases are ambiguous and can't be identified in the parser alone.
398 let sp = self.sess.source_map().start_point(self.token.span);
399 self.sess.ambiguous_block_expr_parse.borrow_mut().insert(sp, lhs.span);
402 (true, Some(AssocOp::LAnd)) |
403 (true, Some(AssocOp::LOr)) |
404 (true, Some(AssocOp::BitOr)) => {
405 // `{ 42 } &&x` (#61475) or `{ 42 } && if x { 1 } else { 0 }`. Separated from the
406 // above due to #74233.
407 // These cases are ambiguous and can't be identified in the parser alone.
409 // Bitwise AND is left out because guessing intent is hard. We can make
410 // suggestions based on the assumption that double-refs are rarely intentional,
411 // and closures are distinct enough that they don't get mixed up with their
413 let sp = self.sess.source_map().start_point(self.token.span);
414 self.sess.ambiguous_block_expr_parse.borrow_mut().insert(sp, lhs.span);
417 (true, Some(op)) if !op.can_continue_expr_unambiguously() => false,
419 self.error_found_expr_would_be_stmt(lhs);
425 /// We've found an expression that would be parsed as a statement,
426 /// but the next token implies this should be parsed as an expression.
427 /// For example: `if let Some(x) = x { x } else { 0 } / 2`.
428 fn error_found_expr_would_be_stmt(&self, lhs: &Expr) {
429 self.sess.emit_err(FoundExprWouldBeStmt {
430 span: self.token.span,
431 token: self.token.clone(),
432 suggestion: ExprParenthesesNeeded::surrounding(lhs.span),
436 /// Possibly translate the current token to an associative operator.
437 /// The method does not advance the current token.
439 /// Also performs recovery for `and` / `or` which are mistaken for `&&` and `||` respectively.
440 fn check_assoc_op(&self) -> Option<Spanned<AssocOp>> {
441 let (op, span) = match (AssocOp::from_token(&self.token), self.token.ident()) {
442 // When parsing const expressions, stop parsing when encountering `>`.
447 | AssocOp::GreaterEqual
448 | AssocOp::AssignOp(token::BinOpToken::Shr),
451 ) if self.restrictions.contains(Restrictions::CONST_EXPR) => {
454 (Some(op), _) => (op, self.token.span),
455 (None, Some((Ident { name: sym::and, span }, false))) if self.may_recover() => {
456 self.sess.emit_err(InvalidLogicalOperator {
457 span: self.token.span,
458 incorrect: "and".into(),
459 sub: InvalidLogicalOperatorSub::Conjunction(self.token.span),
461 (AssocOp::LAnd, span)
463 (None, Some((Ident { name: sym::or, span }, false))) if self.may_recover() => {
464 self.sess.emit_err(InvalidLogicalOperator {
465 span: self.token.span,
466 incorrect: "or".into(),
467 sub: InvalidLogicalOperatorSub::Disjunction(self.token.span),
473 Some(source_map::respan(span, op))
476 /// Checks if this expression is a successfully parsed statement.
477 fn expr_is_complete(&self, e: &Expr) -> bool {
478 self.restrictions.contains(Restrictions::STMT_EXPR)
479 && !classify::expr_requires_semi_to_be_stmt(e)
482 /// Parses `x..y`, `x..=y`, and `x..`/`x..=`.
483 /// The other two variants are handled in `parse_prefix_range_expr` below.
490 ) -> PResult<'a, P<Expr>> {
491 let rhs = if self.is_at_start_of_range_notation_rhs() {
492 Some(self.parse_assoc_expr_with(prec + 1, LhsExpr::NotYetParsed)?)
496 let rhs_span = rhs.as_ref().map_or(cur_op_span, |x| x.span);
497 let span = self.mk_expr_sp(&lhs, lhs.span, rhs_span);
499 if op == AssocOp::DotDot { RangeLimits::HalfOpen } else { RangeLimits::Closed };
500 let range = self.mk_range(Some(lhs), rhs, limits);
501 Ok(self.mk_expr(span, range))
504 fn is_at_start_of_range_notation_rhs(&self) -> bool {
505 if self.token.can_begin_expr() {
506 // Parse `for i in 1.. { }` as infinite loop, not as `for i in (1..{})`.
507 if self.token == token::OpenDelim(Delimiter::Brace) {
508 return !self.restrictions.contains(Restrictions::NO_STRUCT_LITERAL);
516 /// Parses prefix-forms of range notation: `..expr`, `..`, `..=expr`.
517 fn parse_prefix_range_expr(&mut self, attrs: Option<AttrWrapper>) -> PResult<'a, P<Expr>> {
518 // Check for deprecated `...` syntax.
519 if self.token == token::DotDotDot {
520 self.err_dotdotdot_syntax(self.token.span);
524 [token::DotDot, token::DotDotDot, token::DotDotEq].contains(&self.token.kind),
525 "parse_prefix_range_expr: token {:?} is not DotDot/DotDotEq",
529 let limits = match self.token.kind {
530 token::DotDot => RangeLimits::HalfOpen,
531 _ => RangeLimits::Closed,
533 let op = AssocOp::from_token(&self.token);
534 // FIXME: `parse_prefix_range_expr` is called when the current
535 // token is `DotDot`, `DotDotDot`, or `DotDotEq`. If we haven't already
536 // parsed attributes, then trying to parse them here will always fail.
537 // We should figure out how we want attributes on range expressions to work.
538 let attrs = self.parse_or_use_outer_attributes(attrs)?;
539 self.collect_tokens_for_expr(attrs, |this, attrs| {
540 let lo = this.token.span;
542 let (span, opt_end) = if this.is_at_start_of_range_notation_rhs() {
543 // RHS must be parsed with more associativity than the dots.
544 this.parse_assoc_expr_with(op.unwrap().precedence() + 1, LhsExpr::NotYetParsed)
545 .map(|x| (lo.to(x.span), Some(x)))?
549 let range = this.mk_range(None, opt_end, limits);
550 Ok(this.mk_expr_with_attrs(span, range, attrs))
554 /// Parses a prefix-unary-operator expr.
555 fn parse_prefix_expr(&mut self, attrs: Option<AttrWrapper>) -> PResult<'a, P<Expr>> {
556 let attrs = self.parse_or_use_outer_attributes(attrs)?;
557 let lo = self.token.span;
559 macro_rules! make_it {
560 ($this:ident, $attrs:expr, |this, _| $body:expr) => {
561 $this.collect_tokens_for_expr($attrs, |$this, attrs| {
562 let (hi, ex) = $body?;
563 Ok($this.mk_expr_with_attrs(lo.to(hi), ex, attrs))
570 // Note: when adding new unary operators, don't forget to adjust TokenKind::can_begin_expr()
571 match this.token.uninterpolate().kind {
572 token::Not => make_it!(this, attrs, |this, _| this.parse_unary_expr(lo, UnOp::Not)), // `!expr`
573 token::Tilde => make_it!(this, attrs, |this, _| this.recover_tilde_expr(lo)), // `~expr`
574 token::BinOp(token::Minus) => {
575 make_it!(this, attrs, |this, _| this.parse_unary_expr(lo, UnOp::Neg))
577 token::BinOp(token::Star) => {
578 make_it!(this, attrs, |this, _| this.parse_unary_expr(lo, UnOp::Deref))
580 token::BinOp(token::And) | token::AndAnd => {
581 make_it!(this, attrs, |this, _| this.parse_borrow_expr(lo))
583 token::BinOp(token::Plus) if this.look_ahead(1, |tok| tok.is_numeric_lit()) => {
585 LeadingPlusNotSupported { span: lo, remove_plus: None, add_parentheses: None };
587 // a block on the LHS might have been intended to be an expression instead
588 if let Some(sp) = this.sess.ambiguous_block_expr_parse.borrow().get(&lo) {
589 err.add_parentheses = Some(ExprParenthesesNeeded::surrounding(*sp));
591 err.remove_plus = Some(lo);
593 this.sess.emit_err(err);
596 this.parse_prefix_expr(None)
598 // Recover from `++x`:
599 token::BinOp(token::Plus)
600 if this.look_ahead(1, |t| *t == token::BinOp(token::Plus)) =>
602 let prev_is_semi = this.prev_token == token::Semi;
603 let pre_span = this.token.span.to(this.look_ahead(1, |t| t.span));
608 let operand_expr = this.parse_dot_or_call_expr(Default::default())?;
609 this.recover_from_prefix_increment(operand_expr, pre_span, prev_is_semi)
611 token::Ident(..) if this.token.is_keyword(kw::Box) => {
612 make_it!(this, attrs, |this, _| this.parse_box_expr(lo))
614 token::Ident(..) if this.may_recover() && this.is_mistaken_not_ident_negation() => {
615 make_it!(this, attrs, |this, _| this.recover_not_expr(lo))
617 _ => return this.parse_dot_or_call_expr(Some(attrs)),
621 fn parse_prefix_expr_common(&mut self, lo: Span) -> PResult<'a, (Span, P<Expr>)> {
623 let expr = self.parse_prefix_expr(None);
624 let (span, expr) = self.interpolated_or_expr_span(expr)?;
625 Ok((lo.to(span), expr))
628 fn parse_unary_expr(&mut self, lo: Span, op: UnOp) -> PResult<'a, (Span, ExprKind)> {
629 let (span, expr) = self.parse_prefix_expr_common(lo)?;
630 Ok((span, self.mk_unary(op, expr)))
633 // Recover on `!` suggesting for bitwise negation instead.
634 fn recover_tilde_expr(&mut self, lo: Span) -> PResult<'a, (Span, ExprKind)> {
635 self.sess.emit_err(TildeAsUnaryOperator(lo));
637 self.parse_unary_expr(lo, UnOp::Not)
640 /// Parse `box expr`.
641 fn parse_box_expr(&mut self, lo: Span) -> PResult<'a, (Span, ExprKind)> {
642 let (span, expr) = self.parse_prefix_expr_common(lo)?;
643 self.sess.gated_spans.gate(sym::box_syntax, span);
644 Ok((span, ExprKind::Box(expr)))
647 fn is_mistaken_not_ident_negation(&self) -> bool {
648 let token_cannot_continue_expr = |t: &Token| match t.uninterpolate().kind {
649 // These tokens can start an expression after `!`, but
650 // can't continue an expression after an ident
651 token::Ident(name, is_raw) => token::ident_can_begin_expr(name, t.span, is_raw),
652 token::Literal(..) | token::Pound => true,
653 _ => t.is_whole_expr(),
655 self.token.is_ident_named(sym::not) && self.look_ahead(1, token_cannot_continue_expr)
658 /// Recover on `not expr` in favor of `!expr`.
659 fn recover_not_expr(&mut self, lo: Span) -> PResult<'a, (Span, ExprKind)> {
661 let negated_token = self.look_ahead(1, |t| t.clone());
663 let sub_diag = if negated_token.is_numeric_lit() {
664 NotAsNegationOperatorSub::SuggestNotBitwise
665 } else if negated_token.is_bool_lit() {
666 NotAsNegationOperatorSub::SuggestNotLogical
668 NotAsNegationOperatorSub::SuggestNotDefault
671 self.sess.emit_err(NotAsNegationOperator {
672 negated: negated_token.span,
673 negated_desc: super::token_descr(&negated_token),
674 // Span the `not` plus trailing whitespace to avoid
675 // trailing whitespace after the `!` in our suggestion
677 self.sess.source_map().span_until_non_whitespace(lo.to(negated_token.span)),
682 self.parse_unary_expr(lo, UnOp::Not)
685 /// Returns the span of expr, if it was not interpolated or the span of the interpolated token.
686 fn interpolated_or_expr_span(
688 expr: PResult<'a, P<Expr>>,
689 ) -> PResult<'a, (Span, P<Expr>)> {
692 match self.prev_token.kind {
693 TokenKind::Interpolated(..) => self.prev_token.span,
701 fn parse_assoc_op_cast(
705 expr_kind: fn(P<Expr>, P<Ty>) -> ExprKind,
706 ) -> PResult<'a, P<Expr>> {
707 let mk_expr = |this: &mut Self, lhs: P<Expr>, rhs: P<Ty>| {
708 this.mk_expr(this.mk_expr_sp(&lhs, lhs_span, rhs.span), expr_kind(lhs, rhs))
711 // Save the state of the parser before parsing type normally, in case there is a
712 // LessThan comparison after this cast.
713 let parser_snapshot_before_type = self.clone();
714 let cast_expr = match self.parse_as_cast_ty() {
715 Ok(rhs) => mk_expr(self, lhs, rhs),
717 if !self.may_recover() {
718 return Err(type_err);
721 // Rewind to before attempting to parse the type with generics, to recover
722 // from situations like `x as usize < y` in which we first tried to parse
723 // `usize < y` as a type with generic arguments.
724 let parser_snapshot_after_type = mem::replace(self, parser_snapshot_before_type);
726 // Check for typo of `'a: loop { break 'a }` with a missing `'`.
727 match (&lhs.kind, &self.token.kind) {
730 ExprKind::Path(None, ast::Path { segments, .. }),
731 TokenKind::Ident(kw::For | kw::Loop | kw::While, false),
732 ) if segments.len() == 1 => {
733 let snapshot = self.create_snapshot_for_diagnostic();
735 ident: Ident::from_str_and_span(
736 &format!("'{}", segments[0].ident),
737 segments[0].ident.span,
740 match self.parse_labeled_expr(label, false) {
743 self.sess.emit_err(MalformedLoopLabel {
744 span: label.ident.span,
745 correct_label: label.ident,
751 self.restore_snapshot(snapshot);
758 match self.parse_path(PathStyle::Expr) {
760 let span_after_type = parser_snapshot_after_type.token.span;
764 self.mk_ty(path.span, TyKind::Path(None, path.clone())),
767 let args_span = self.look_ahead(1, |t| t.span).to(span_after_type);
768 let suggestion = ComparisonOrShiftInterpretedAsGenericSugg {
769 left: expr.span.shrink_to_lo(),
770 right: expr.span.shrink_to_hi(),
773 match self.token.kind {
774 token::Lt => self.sess.emit_err(ComparisonInterpretedAsGeneric {
775 comparison: self.token.span,
780 token::BinOp(token::Shl) => {
781 self.sess.emit_err(ShiftInterpretedAsGeneric {
782 shift: self.token.span,
789 // We can end up here even without `<` being the next token, for
790 // example because `parse_ty_no_plus` returns `Err` on keywords,
791 // but `parse_path` returns `Ok` on them due to error recovery.
792 // Return original error and parser state.
793 *self = parser_snapshot_after_type;
794 return Err(type_err);
798 // Successfully parsed the type path leaving a `<` yet to parse.
801 // Keep `x as usize` as an expression in AST and continue parsing.
805 // Couldn't parse as a path, return original error and parser state.
807 *self = parser_snapshot_after_type;
808 return Err(type_err);
814 self.parse_and_disallow_postfix_after_cast(cast_expr)
817 /// Parses a postfix operators such as `.`, `?`, or index (`[]`) after a cast,
818 /// then emits an error and returns the newly parsed tree.
819 /// The resulting parse tree for `&x as T[0]` has a precedence of `((&x) as T)[0]`.
820 fn parse_and_disallow_postfix_after_cast(
823 ) -> PResult<'a, P<Expr>> {
824 let span = cast_expr.span;
825 let (cast_kind, maybe_ascription_span) =
826 if let ExprKind::Type(ascripted_expr, _) = &cast_expr.kind {
827 ("type ascription", Some(ascripted_expr.span.shrink_to_hi().with_hi(span.hi())))
832 let with_postfix = self.parse_dot_or_call_expr_with_(cast_expr, span)?;
834 // Check if an illegal postfix operator has been added after the cast.
835 // If the resulting expression is not a cast, it is an illegal postfix operator.
836 if !matches!(with_postfix.kind, ExprKind::Cast(_, _) | ExprKind::Type(_, _)) {
838 "{cast_kind} cannot be followed by {}",
839 match with_postfix.kind {
840 ExprKind::Index(_, _) => "indexing",
841 ExprKind::Try(_) => "`?`",
842 ExprKind::Field(_, _) => "a field access",
843 ExprKind::MethodCall(_) => "a method call",
844 ExprKind::Call(_, _) => "a function call",
845 ExprKind::Await(_) => "`.await`",
846 ExprKind::Err => return Ok(with_postfix),
847 _ => unreachable!("parse_dot_or_call_expr_with_ shouldn't produce this"),
850 let mut err = self.struct_span_err(span, &msg);
852 let suggest_parens = |err: &mut Diagnostic| {
853 let suggestions = vec![
854 (span.shrink_to_lo(), "(".to_string()),
855 (span.shrink_to_hi(), ")".to_string()),
857 err.multipart_suggestion(
858 "try surrounding the expression in parentheses",
860 Applicability::MachineApplicable,
864 // If type ascription is "likely an error", the user will already be getting a useful
865 // help message, and doesn't need a second.
866 if self.last_type_ascription.map_or(false, |last_ascription| last_ascription.1) {
867 self.maybe_annotate_with_ascription(&mut err, false);
868 } else if let Some(ascription_span) = maybe_ascription_span {
869 let is_nightly = self.sess.unstable_features.is_nightly_build();
871 suggest_parens(&mut err);
876 "{}remove the type ascription",
877 if is_nightly { "alternatively, " } else { "" }
881 Applicability::MaybeIncorrect
883 Applicability::MachineApplicable
887 suggest_parens(&mut err);
894 fn parse_assoc_op_ascribe(&mut self, lhs: P<Expr>, lhs_span: Span) -> PResult<'a, P<Expr>> {
895 let maybe_path = self.could_ascription_be_path(&lhs.kind);
896 self.last_type_ascription = Some((self.prev_token.span, maybe_path));
897 let lhs = self.parse_assoc_op_cast(lhs, lhs_span, ExprKind::Type)?;
898 self.sess.gated_spans.gate(sym::type_ascription, lhs.span);
902 /// Parse `& mut? <expr>` or `& raw [ const | mut ] <expr>`.
903 fn parse_borrow_expr(&mut self, lo: Span) -> PResult<'a, (Span, ExprKind)> {
905 let has_lifetime = self.token.is_lifetime() && self.look_ahead(1, |t| t != &token::Colon);
906 let lifetime = has_lifetime.then(|| self.expect_lifetime()); // For recovery, see below.
907 let (borrow_kind, mutbl) = self.parse_borrow_modifiers(lo);
908 let expr = self.parse_prefix_expr(None);
909 let (hi, expr) = self.interpolated_or_expr_span(expr)?;
910 let span = lo.to(hi);
911 if let Some(lt) = lifetime {
912 self.error_remove_borrow_lifetime(span, lt.ident.span);
914 Ok((span, ExprKind::AddrOf(borrow_kind, mutbl, expr)))
917 fn error_remove_borrow_lifetime(&self, span: Span, lt_span: Span) {
918 self.sess.emit_err(LifetimeInBorrowExpression { span, lifetime_span: lt_span });
921 /// Parse `mut?` or `raw [ const | mut ]`.
922 fn parse_borrow_modifiers(&mut self, lo: Span) -> (ast::BorrowKind, ast::Mutability) {
923 if self.check_keyword(kw::Raw) && self.look_ahead(1, Token::is_mutability) {
924 // `raw [ const | mut ]`.
925 let found_raw = self.eat_keyword(kw::Raw);
927 let mutability = self.parse_const_or_mut().unwrap();
928 self.sess.gated_spans.gate(sym::raw_ref_op, lo.to(self.prev_token.span));
929 (ast::BorrowKind::Raw, mutability)
932 (ast::BorrowKind::Ref, self.parse_mutability())
936 /// Parses `a.b` or `a(13)` or `a[4]` or just `a`.
937 fn parse_dot_or_call_expr(&mut self, attrs: Option<AttrWrapper>) -> PResult<'a, P<Expr>> {
938 let attrs = self.parse_or_use_outer_attributes(attrs)?;
939 self.collect_tokens_for_expr(attrs, |this, attrs| {
940 let base = this.parse_bottom_expr();
941 let (span, base) = this.interpolated_or_expr_span(base)?;
942 this.parse_dot_or_call_expr_with(base, span, attrs)
946 pub(super) fn parse_dot_or_call_expr_with(
950 mut attrs: ast::AttrVec,
951 ) -> PResult<'a, P<Expr>> {
952 // Stitch the list of outer attributes onto the return value.
953 // A little bit ugly, but the best way given the current code
955 let res = self.parse_dot_or_call_expr_with_(e0, lo);
956 if attrs.is_empty() {
960 expr.map(|mut expr| {
961 attrs.extend(expr.attrs);
969 fn parse_dot_or_call_expr_with_(&mut self, mut e: P<Expr>, lo: Span) -> PResult<'a, P<Expr>> {
971 let has_question = if self.prev_token.kind == TokenKind::Ident(kw::Return, false) {
972 // we are using noexpect here because we don't expect a `?` directly after a `return`
973 // which could be suggested otherwise
974 self.eat_noexpect(&token::Question)
976 self.eat(&token::Question)
980 e = self.mk_expr(lo.to(self.prev_token.span), ExprKind::Try(e));
983 let has_dot = if self.prev_token.kind == TokenKind::Ident(kw::Return, false) {
984 // we are using noexpect here because we don't expect a `.` directly after a `return`
985 // which could be suggested otherwise
986 self.eat_noexpect(&token::Dot)
988 self.eat(&token::Dot)
992 e = self.parse_dot_suffix_expr(lo, e)?;
995 if self.expr_is_complete(&e) {
998 e = match self.token.kind {
999 token::OpenDelim(Delimiter::Parenthesis) => self.parse_fn_call_expr(lo, e),
1000 token::OpenDelim(Delimiter::Bracket) => self.parse_index_expr(lo, e)?,
1006 fn look_ahead_type_ascription_as_field(&mut self) -> bool {
1007 self.look_ahead(1, |t| t.is_ident())
1008 && self.look_ahead(2, |t| t == &token::Colon)
1009 && self.look_ahead(3, |t| t.can_begin_expr())
1012 fn parse_dot_suffix_expr(&mut self, lo: Span, base: P<Expr>) -> PResult<'a, P<Expr>> {
1013 match self.token.uninterpolate().kind {
1014 token::Ident(..) => self.parse_dot_suffix(base, lo),
1015 token::Literal(token::Lit { kind: token::Integer, symbol, suffix }) => {
1016 Ok(self.parse_tuple_field_access_expr(lo, base, symbol, suffix, None))
1018 token::Literal(token::Lit { kind: token::Float, symbol, suffix }) => {
1019 Ok(self.parse_tuple_field_access_expr_float(lo, base, symbol, suffix))
1022 self.error_unexpected_after_dot();
1028 fn error_unexpected_after_dot(&self) {
1029 // FIXME Could factor this out into non_fatal_unexpected or something.
1030 let actual = pprust::token_to_string(&self.token);
1031 self.struct_span_err(self.token.span, &format!("unexpected token: `{actual}`")).emit();
1034 // We need an identifier or integer, but the next token is a float.
1035 // Break the float into components to extract the identifier or integer.
1036 // FIXME: With current `TokenCursor` it's hard to break tokens into more than 2
1037 // parts unless those parts are processed immediately. `TokenCursor` should either
1038 // support pushing "future tokens" (would be also helpful to `break_and_eat`), or
1039 // we should break everything including floats into more basic proc-macro style
1040 // tokens in the lexer (probably preferable).
1041 fn parse_tuple_field_access_expr_float(
1046 suffix: Option<Symbol>,
1049 enum FloatComponent {
1053 use FloatComponent::*;
1055 let float_str = float.as_str();
1056 let mut components = Vec::new();
1057 let mut ident_like = String::new();
1058 for c in float_str.chars() {
1059 if c == '_' || c.is_ascii_alphanumeric() {
1061 } else if matches!(c, '.' | '+' | '-') {
1062 if !ident_like.is_empty() {
1063 components.push(IdentLike(mem::take(&mut ident_like)));
1065 components.push(Punct(c));
1067 panic!("unexpected character in a float token: {:?}", c)
1070 if !ident_like.is_empty() {
1071 components.push(IdentLike(ident_like));
1074 // With proc macros the span can refer to anything, the source may be too short,
1075 // or too long, or non-ASCII. It only makes sense to break our span into components
1076 // if its underlying text is identical to our float literal.
1077 let span = self.token.span;
1078 let can_take_span_apart =
1079 || self.span_to_snippet(span).as_deref() == Ok(float_str).as_deref();
1081 match &*components {
1084 self.parse_tuple_field_access_expr(lo, base, Symbol::intern(&i), suffix, None)
1087 [IdentLike(i), Punct('.')] => {
1088 let (ident_span, dot_span) = if can_take_span_apart() {
1089 let (span, ident_len) = (span.data(), BytePos::from_usize(i.len()));
1090 let ident_span = span.with_hi(span.lo + ident_len);
1091 let dot_span = span.with_lo(span.lo + ident_len);
1092 (ident_span, dot_span)
1096 assert!(suffix.is_none());
1097 let symbol = Symbol::intern(&i);
1098 self.token = Token::new(token::Ident(symbol, false), ident_span);
1099 let next_token = (Token::new(token::Dot, dot_span), self.token_spacing);
1100 self.parse_tuple_field_access_expr(lo, base, symbol, None, Some(next_token))
1103 [IdentLike(i1), Punct('.'), IdentLike(i2)] => {
1104 let (ident1_span, dot_span, ident2_span) = if can_take_span_apart() {
1105 let (span, ident1_len) = (span.data(), BytePos::from_usize(i1.len()));
1106 let ident1_span = span.with_hi(span.lo + ident1_len);
1108 .with_lo(span.lo + ident1_len)
1109 .with_hi(span.lo + ident1_len + BytePos(1));
1110 let ident2_span = self.token.span.with_lo(span.lo + ident1_len + BytePos(1));
1111 (ident1_span, dot_span, ident2_span)
1115 let symbol1 = Symbol::intern(&i1);
1116 self.token = Token::new(token::Ident(symbol1, false), ident1_span);
1117 // This needs to be `Spacing::Alone` to prevent regressions.
1118 // See issue #76399 and PR #76285 for more details
1119 let next_token1 = (Token::new(token::Dot, dot_span), Spacing::Alone);
1121 self.parse_tuple_field_access_expr(lo, base, symbol1, None, Some(next_token1));
1122 let symbol2 = Symbol::intern(&i2);
1123 let next_token2 = Token::new(token::Ident(symbol2, false), ident2_span);
1124 self.bump_with((next_token2, self.token_spacing)); // `.`
1125 self.parse_tuple_field_access_expr(lo, base1, symbol2, suffix, None)
1127 // 1e+ | 1e- (recovered)
1128 [IdentLike(_), Punct('+' | '-')] |
1130 [IdentLike(_), Punct('+' | '-'), IdentLike(_)] |
1132 [IdentLike(_), Punct('.'), IdentLike(_), Punct('+' | '-')] |
1134 [IdentLike(_), Punct('.'), IdentLike(_), Punct('+' | '-'), IdentLike(_)] => {
1135 // See the FIXME about `TokenCursor` above.
1136 self.error_unexpected_after_dot();
1139 _ => panic!("unexpected components in a float token: {:?}", components),
1143 fn parse_tuple_field_access_expr(
1148 suffix: Option<Symbol>,
1149 next_token: Option<(Token, Spacing)>,
1152 Some(next_token) => self.bump_with(next_token),
1153 None => self.bump(),
1155 let span = self.prev_token.span;
1156 let field = ExprKind::Field(base, Ident::new(field, span));
1157 if let Some(suffix) = suffix {
1158 self.expect_no_tuple_index_suffix(span, suffix);
1160 self.mk_expr(lo.to(span), field)
1163 /// Parse a function call expression, `expr(...)`.
1164 fn parse_fn_call_expr(&mut self, lo: Span, fun: P<Expr>) -> P<Expr> {
1165 let snapshot = if self.token.kind == token::OpenDelim(Delimiter::Parenthesis)
1166 && self.look_ahead_type_ascription_as_field()
1168 Some((self.create_snapshot_for_diagnostic(), fun.kind.clone()))
1172 let open_paren = self.token.span;
1175 .parse_paren_expr_seq()
1176 .map(|args| self.mk_expr(lo.to(self.prev_token.span), self.mk_call(fun, args)));
1178 self.maybe_recover_struct_lit_bad_delims(lo, open_paren, &mut seq, snapshot)
1182 self.recover_seq_parse_error(Delimiter::Parenthesis, lo, seq)
1185 /// If we encounter a parser state that looks like the user has written a `struct` literal with
1186 /// parentheses instead of braces, recover the parser state and provide suggestions.
1187 #[instrument(skip(self, seq, snapshot), level = "trace")]
1188 fn maybe_recover_struct_lit_bad_delims(
1192 seq: &mut PResult<'a, P<Expr>>,
1193 snapshot: Option<(SnapshotParser<'a>, ExprKind)>,
1194 ) -> Option<P<Expr>> {
1195 if !self.may_recover() {
1199 match (seq.as_mut(), snapshot) {
1200 (Err(err), Some((mut snapshot, ExprKind::Path(None, path)))) => {
1201 snapshot.bump(); // `(`
1202 match snapshot.parse_struct_fields(path.clone(), false, Delimiter::Parenthesis) {
1204 if snapshot.eat(&token::CloseDelim(Delimiter::Parenthesis)) =>
1206 // We are certain we have `Enum::Foo(a: 3, b: 4)`, suggest
1207 // `Enum::Foo { a: 3, b: 4 }` or `Enum::Foo(3, 4)`.
1208 self.restore_snapshot(snapshot);
1209 let close_paren = self.prev_token.span;
1210 let span = lo.to(self.prev_token.span);
1211 if !fields.is_empty() {
1212 let mut replacement_err = ParenthesesWithStructFields {
1215 braces_for_struct: BracesForStructLiteral {
1217 second: close_paren,
1219 no_fields_for_fn: NoFieldsForFnCall {
1222 .map(|field| field.span.until(field.expr.span))
1226 .into_diagnostic(&self.sess.span_diagnostic);
1227 replacement_err.emit();
1229 let old_err = mem::replace(err, replacement_err);
1234 return Some(self.mk_expr_err(span));
1247 /// Parse an indexing expression `expr[...]`.
1248 fn parse_index_expr(&mut self, lo: Span, base: P<Expr>) -> PResult<'a, P<Expr>> {
1249 let prev_span = self.prev_token.span;
1250 let open_delim_span = self.token.span;
1252 let index = self.parse_expr()?;
1253 self.suggest_missing_semicolon_before_array(prev_span, open_delim_span)?;
1254 self.expect(&token::CloseDelim(Delimiter::Bracket))?;
1255 Ok(self.mk_expr(lo.to(self.prev_token.span), self.mk_index(base, index)))
1258 /// Assuming we have just parsed `.`, continue parsing into an expression.
1259 fn parse_dot_suffix(&mut self, self_arg: P<Expr>, lo: Span) -> PResult<'a, P<Expr>> {
1260 if self.token.uninterpolated_span().rust_2018() && self.eat_keyword(kw::Await) {
1261 return Ok(self.mk_await_expr(self_arg, lo));
1264 let fn_span_lo = self.token.span;
1265 let mut seg = self.parse_path_segment(PathStyle::Expr, None)?;
1266 self.check_trailing_angle_brackets(&seg, &[&token::OpenDelim(Delimiter::Parenthesis)]);
1267 self.check_turbofish_missing_angle_brackets(&mut seg);
1269 if self.check(&token::OpenDelim(Delimiter::Parenthesis)) {
1270 // Method call `expr.f()`
1271 let args = self.parse_paren_expr_seq()?;
1272 let fn_span = fn_span_lo.to(self.prev_token.span);
1273 let span = lo.to(self.prev_token.span);
1276 ExprKind::MethodCall(Box::new(ast::MethodCall {
1284 // Field access `expr.f`
1285 if let Some(args) = seg.args {
1286 self.sess.emit_err(FieldExpressionWithGeneric(args.span()));
1289 let span = lo.to(self.prev_token.span);
1290 Ok(self.mk_expr(span, ExprKind::Field(self_arg, seg.ident)))
1294 /// At the bottom (top?) of the precedence hierarchy,
1295 /// Parses things like parenthesized exprs, macros, `return`, etc.
1297 /// N.B., this does not parse outer attributes, and is private because it only works
1298 /// correctly if called from `parse_dot_or_call_expr()`.
1299 fn parse_bottom_expr(&mut self) -> PResult<'a, P<Expr>> {
1300 maybe_recover_from_interpolated_ty_qpath!(self, true);
1301 maybe_whole_expr!(self);
1303 // Outer attributes are already parsed and will be
1304 // added to the return value after the fact.
1306 // Note: when adding new syntax here, don't forget to adjust `TokenKind::can_begin_expr()`.
1307 let lo = self.token.span;
1308 if let token::Literal(_) = self.token.kind {
1309 // This match arm is a special-case of the `_` match arm below and
1310 // could be removed without changing functionality, but it's faster
1311 // to have it here, especially for programs with large constants.
1312 self.parse_lit_expr()
1313 } else if self.check(&token::OpenDelim(Delimiter::Parenthesis)) {
1314 self.parse_tuple_parens_expr()
1315 } else if self.check(&token::OpenDelim(Delimiter::Brace)) {
1316 self.parse_block_expr(None, lo, BlockCheckMode::Default)
1317 } else if self.check(&token::BinOp(token::Or)) || self.check(&token::OrOr) {
1318 self.parse_closure_expr().map_err(|mut err| {
1319 // If the input is something like `if a { 1 } else { 2 } | if a { 3 } else { 4 }`
1320 // then suggest parens around the lhs.
1321 if let Some(sp) = self.sess.ambiguous_block_expr_parse.borrow().get(&lo) {
1322 err.subdiagnostic(ExprParenthesesNeeded::surrounding(*sp));
1326 } else if self.check(&token::OpenDelim(Delimiter::Bracket)) {
1327 self.parse_array_or_repeat_expr(Delimiter::Bracket)
1328 } else if self.check_path() {
1329 self.parse_path_start_expr()
1330 } else if self.check_keyword(kw::Move) || self.check_keyword(kw::Static) {
1331 self.parse_closure_expr()
1332 } else if self.eat_keyword(kw::If) {
1333 self.parse_if_expr()
1334 } else if self.check_keyword(kw::For) {
1335 if self.choose_generics_over_qpath(1) {
1336 self.parse_closure_expr()
1338 assert!(self.eat_keyword(kw::For));
1339 self.parse_for_expr(None, self.prev_token.span)
1341 } else if self.eat_keyword(kw::While) {
1342 self.parse_while_expr(None, self.prev_token.span)
1343 } else if let Some(label) = self.eat_label() {
1344 self.parse_labeled_expr(label, true)
1345 } else if self.eat_keyword(kw::Loop) {
1346 let sp = self.prev_token.span;
1347 self.parse_loop_expr(None, self.prev_token.span).map_err(|mut err| {
1348 err.span_label(sp, "while parsing this `loop` expression");
1351 } else if self.eat_keyword(kw::Continue) {
1352 let kind = ExprKind::Continue(self.eat_label());
1353 Ok(self.mk_expr(lo.to(self.prev_token.span), kind))
1354 } else if self.eat_keyword(kw::Match) {
1355 let match_sp = self.prev_token.span;
1356 self.parse_match_expr().map_err(|mut err| {
1357 err.span_label(match_sp, "while parsing this `match` expression");
1360 } else if self.eat_keyword(kw::Unsafe) {
1361 let sp = self.prev_token.span;
1362 self.parse_block_expr(None, lo, BlockCheckMode::Unsafe(ast::UserProvided)).map_err(
1364 err.span_label(sp, "while parsing this `unsafe` expression");
1368 } else if self.check_inline_const(0) {
1369 self.parse_const_block(lo.to(self.token.span), false)
1370 } else if self.may_recover() && self.is_do_catch_block() {
1371 self.recover_do_catch()
1372 } else if self.is_try_block() {
1373 self.expect_keyword(kw::Try)?;
1374 self.parse_try_block(lo)
1375 } else if self.eat_keyword(kw::Return) {
1376 self.parse_return_expr()
1377 } else if self.eat_keyword(kw::Break) {
1378 self.parse_break_expr()
1379 } else if self.eat_keyword(kw::Yield) {
1380 self.parse_yield_expr()
1381 } else if self.is_do_yeet() {
1382 self.parse_yeet_expr()
1383 } else if self.check_keyword(kw::Let) {
1384 self.parse_let_expr()
1385 } else if self.eat_keyword(kw::Underscore) {
1386 Ok(self.mk_expr(self.prev_token.span, ExprKind::Underscore))
1387 } else if !self.unclosed_delims.is_empty() && self.check(&token::Semi) {
1388 // Don't complain about bare semicolons after unclosed braces
1389 // recovery in order to keep the error count down. Fixing the
1390 // delimiters will possibly also fix the bare semicolon found in
1391 // expression context. For example, silence the following error:
1393 // error: expected expression, found `;`
1397 // | ^ expected expression
1399 Ok(self.mk_expr_err(self.token.span))
1400 } else if self.token.uninterpolated_span().rust_2018() {
1401 // `Span::rust_2018()` is somewhat expensive; don't get it repeatedly.
1402 if self.check_keyword(kw::Async) {
1403 if self.is_async_block() {
1404 // Check for `async {` and `async move {`.
1405 self.parse_async_block()
1407 self.parse_closure_expr()
1409 } else if self.eat_keyword(kw::Await) {
1410 self.recover_incorrect_await_syntax(lo, self.prev_token.span)
1412 self.parse_lit_expr()
1415 self.parse_lit_expr()
1419 fn parse_lit_expr(&mut self) -> PResult<'a, P<Expr>> {
1420 let lo = self.token.span;
1421 match self.parse_opt_token_lit() {
1422 Some((token_lit, _)) => {
1423 let expr = self.mk_expr(lo.to(self.prev_token.span), ExprKind::Lit(token_lit));
1424 self.maybe_recover_from_bad_qpath(expr)
1426 None => self.try_macro_suggestion(),
1430 fn parse_tuple_parens_expr(&mut self) -> PResult<'a, P<Expr>> {
1431 let lo = self.token.span;
1432 self.expect(&token::OpenDelim(Delimiter::Parenthesis))?;
1433 let (es, trailing_comma) = match self.parse_seq_to_end(
1434 &token::CloseDelim(Delimiter::Parenthesis),
1435 SeqSep::trailing_allowed(token::Comma),
1436 |p| p.parse_expr_catch_underscore(),
1440 return Ok(self.recover_seq_parse_error(Delimiter::Parenthesis, lo, Err(err)));
1443 let kind = if es.len() == 1 && !trailing_comma {
1444 // `(e)` is parenthesized `e`.
1445 ExprKind::Paren(es.into_iter().next().unwrap())
1447 // `(e,)` is a tuple with only one field, `e`.
1450 let expr = self.mk_expr(lo.to(self.prev_token.span), kind);
1451 self.maybe_recover_from_bad_qpath(expr)
1454 fn parse_array_or_repeat_expr(&mut self, close_delim: Delimiter) -> PResult<'a, P<Expr>> {
1455 let lo = self.token.span;
1456 self.bump(); // `[` or other open delim
1458 let close = &token::CloseDelim(close_delim);
1459 let kind = if self.eat(close) {
1461 ExprKind::Array(Vec::new())
1464 let first_expr = self.parse_expr()?;
1465 if self.eat(&token::Semi) {
1466 // Repeating array syntax: `[ 0; 512 ]`
1467 let count = self.parse_anon_const_expr()?;
1468 self.expect(close)?;
1469 ExprKind::Repeat(first_expr, count)
1470 } else if self.eat(&token::Comma) {
1471 // Vector with two or more elements.
1472 let sep = SeqSep::trailing_allowed(token::Comma);
1473 let (remaining_exprs, _) = self.parse_seq_to_end(close, sep, |p| p.parse_expr())?;
1474 let mut exprs = vec![first_expr];
1475 exprs.extend(remaining_exprs);
1476 ExprKind::Array(exprs)
1478 // Vector with one element
1479 self.expect(close)?;
1480 ExprKind::Array(vec![first_expr])
1483 let expr = self.mk_expr(lo.to(self.prev_token.span), kind);
1484 self.maybe_recover_from_bad_qpath(expr)
1487 fn parse_path_start_expr(&mut self) -> PResult<'a, P<Expr>> {
1488 let (qself, path) = if self.eat_lt() {
1489 let (qself, path) = self.parse_qpath(PathStyle::Expr)?;
1492 (None, self.parse_path(PathStyle::Expr)?)
1495 // `!`, as an operator, is prefix, so we know this isn't that.
1496 let (span, kind) = if self.eat(&token::Not) {
1497 // MACRO INVOCATION expression
1498 if qself.is_some() {
1499 self.sess.emit_err(MacroInvocationWithQualifiedPath(path.span));
1502 let mac = P(MacCall {
1504 args: self.parse_delim_args()?,
1505 prior_type_ascription: self.last_type_ascription,
1507 (lo.to(self.prev_token.span), ExprKind::MacCall(mac))
1508 } else if self.check(&token::OpenDelim(Delimiter::Brace)) &&
1509 let Some(expr) = self.maybe_parse_struct_expr(&qself, &path) {
1510 if qself.is_some() {
1511 self.sess.gated_spans.gate(sym::more_qualified_paths, path.span);
1515 (path.span, ExprKind::Path(qself, path))
1518 let expr = self.mk_expr(span, kind);
1519 self.maybe_recover_from_bad_qpath(expr)
1522 /// Parse `'label: $expr`. The label is already parsed.
1523 fn parse_labeled_expr(
1526 mut consume_colon: bool,
1527 ) -> PResult<'a, P<Expr>> {
1528 let lo = label_.ident.span;
1529 let label = Some(label_);
1530 let ate_colon = self.eat(&token::Colon);
1531 let expr = if self.eat_keyword(kw::While) {
1532 self.parse_while_expr(label, lo)
1533 } else if self.eat_keyword(kw::For) {
1534 self.parse_for_expr(label, lo)
1535 } else if self.eat_keyword(kw::Loop) {
1536 self.parse_loop_expr(label, lo)
1537 } else if self.check_noexpect(&token::OpenDelim(Delimiter::Brace))
1538 || self.token.is_whole_block()
1540 self.parse_block_expr(label, lo, BlockCheckMode::Default)
1541 } else if !ate_colon
1542 && self.may_recover()
1543 && (matches!(self.token.kind, token::CloseDelim(_) | token::Comma)
1544 || self.token.is_op())
1546 let lit = self.recover_unclosed_char(label_.ident, |self_| {
1547 self_.sess.create_err(UnexpectedTokenAfterLabel {
1548 span: self_.token.span,
1550 enclose_in_block: None,
1553 consume_colon = false;
1554 Ok(self.mk_expr(lo, ExprKind::Lit(lit.token_lit)))
1555 } else if !ate_colon
1556 && (self.check_noexpect(&TokenKind::Comma) || self.check_noexpect(&TokenKind::Gt))
1558 // We're probably inside of a `Path<'a>` that needs a turbofish
1559 self.sess.emit_err(UnexpectedTokenAfterLabel {
1560 span: self.token.span,
1562 enclose_in_block: None,
1564 consume_colon = false;
1565 Ok(self.mk_expr_err(lo))
1567 let mut err = UnexpectedTokenAfterLabel {
1568 span: self.token.span,
1570 enclose_in_block: None,
1573 // Continue as an expression in an effort to recover on `'label: non_block_expr`.
1574 let expr = self.parse_expr().map(|expr| {
1575 let span = expr.span;
1577 let found_labeled_breaks = {
1578 struct FindLabeledBreaksVisitor(bool);
1580 impl<'ast> Visitor<'ast> for FindLabeledBreaksVisitor {
1581 fn visit_expr_post(&mut self, ex: &'ast Expr) {
1582 if let ExprKind::Break(Some(_label), _) = ex.kind {
1588 let mut vis = FindLabeledBreaksVisitor(false);
1589 vis.visit_expr(&expr);
1593 // Suggestion involves adding a (as of time of writing this, unstable) labeled block.
1595 // If there are no breaks that may use this label, suggest removing the label and
1596 // recover to the unmodified expression.
1597 if !found_labeled_breaks {
1598 err.remove_label = Some(lo.until(span));
1603 err.enclose_in_block = Some(UnexpectedTokenAfterLabelSugg {
1604 left: span.shrink_to_lo(),
1605 right: span.shrink_to_hi(),
1608 // Replace `'label: non_block_expr` with `'label: {non_block_expr}` in order to suppress future errors about `break 'label`.
1609 let stmt = self.mk_stmt(span, StmtKind::Expr(expr));
1610 let blk = self.mk_block(vec![stmt], BlockCheckMode::Default, span);
1611 self.mk_expr(span, ExprKind::Block(blk, label))
1614 self.sess.emit_err(err);
1618 if !ate_colon && consume_colon {
1619 self.sess.emit_err(RequireColonAfterLabeledExpression {
1622 label_end: lo.shrink_to_hi(),
1629 /// Emit an error when a char is parsed as a lifetime because of a missing quote
1630 pub(super) fn recover_unclosed_char(
1633 err: impl FnOnce(&Self) -> DiagnosticBuilder<'a, ErrorGuaranteed>,
1635 if let Some(mut diag) =
1636 self.sess.span_diagnostic.steal_diagnostic(lifetime.span, StashKey::LifetimeIsChar)
1638 diag.span_suggestion_verbose(
1639 lifetime.span.shrink_to_hi(),
1640 "add `'` to close the char literal",
1642 Applicability::MaybeIncorrect,
1647 .span_suggestion_verbose(
1648 lifetime.span.shrink_to_hi(),
1649 "add `'` to close the char literal",
1651 Applicability::MaybeIncorrect,
1655 let name = lifetime.without_first_quote().name;
1657 token_lit: token::Lit::new(token::LitKind::Char, name, None),
1658 kind: ast::LitKind::Char(name.as_str().chars().next().unwrap_or('_')),
1659 span: lifetime.span,
1663 /// Recover on the syntax `do catch { ... }` suggesting `try { ... }` instead.
1664 fn recover_do_catch(&mut self) -> PResult<'a, P<Expr>> {
1665 let lo = self.token.span;
1667 self.bump(); // `do`
1668 self.bump(); // `catch`
1670 let span = lo.to(self.prev_token.span);
1671 self.sess.emit_err(DoCatchSyntaxRemoved { span });
1673 self.parse_try_block(lo)
1676 /// Parse an expression if the token can begin one.
1677 fn parse_expr_opt(&mut self) -> PResult<'a, Option<P<Expr>>> {
1678 Ok(if self.token.can_begin_expr() { Some(self.parse_expr()?) } else { None })
1681 /// Parse `"return" expr?`.
1682 fn parse_return_expr(&mut self) -> PResult<'a, P<Expr>> {
1683 let lo = self.prev_token.span;
1684 let kind = ExprKind::Ret(self.parse_expr_opt()?);
1685 let expr = self.mk_expr(lo.to(self.prev_token.span), kind);
1686 self.maybe_recover_from_bad_qpath(expr)
1689 /// Parse `"do" "yeet" expr?`.
1690 fn parse_yeet_expr(&mut self) -> PResult<'a, P<Expr>> {
1691 let lo = self.token.span;
1693 self.bump(); // `do`
1694 self.bump(); // `yeet`
1696 let kind = ExprKind::Yeet(self.parse_expr_opt()?);
1698 let span = lo.to(self.prev_token.span);
1699 self.sess.gated_spans.gate(sym::yeet_expr, span);
1700 let expr = self.mk_expr(span, kind);
1701 self.maybe_recover_from_bad_qpath(expr)
1704 /// Parse `"break" (('label (:? expr)?) | expr?)` with `"break"` token already eaten.
1705 /// If the label is followed immediately by a `:` token, the label and `:` are
1706 /// parsed as part of the expression (i.e. a labeled loop). The language team has
1707 /// decided in #87026 to require parentheses as a visual aid to avoid confusion if
1708 /// the break expression of an unlabeled break is a labeled loop (as in
1709 /// `break 'lbl: loop {}`); a labeled break with an unlabeled loop as its value
1710 /// expression only gets a warning for compatibility reasons; and a labeled break
1711 /// with a labeled loop does not even get a warning because there is no ambiguity.
1712 fn parse_break_expr(&mut self) -> PResult<'a, P<Expr>> {
1713 let lo = self.prev_token.span;
1714 let mut label = self.eat_label();
1715 let kind = if label.is_some() && self.token == token::Colon {
1716 // The value expression can be a labeled loop, see issue #86948, e.g.:
1717 // `loop { break 'label: loop { break 'label 42; }; }`
1718 let lexpr = self.parse_labeled_expr(label.take().unwrap(), true)?;
1719 self.sess.emit_err(LabeledLoopInBreak {
1721 sub: WrapExpressionInParentheses {
1722 left: lexpr.span.shrink_to_lo(),
1723 right: lexpr.span.shrink_to_hi(),
1727 } else if self.token != token::OpenDelim(Delimiter::Brace)
1728 || !self.restrictions.contains(Restrictions::NO_STRUCT_LITERAL)
1730 let expr = self.parse_expr_opt()?;
1731 if let Some(expr) = &expr {
1735 ExprKind::While(_, _, None)
1736 | ExprKind::ForLoop(_, _, _, None)
1737 | ExprKind::Loop(_, None)
1738 | ExprKind::Block(_, None)
1741 self.sess.buffer_lint_with_diagnostic(
1742 BREAK_WITH_LABEL_AND_LOOP,
1745 "this labeled break expression is easy to confuse with an unlabeled break with a labeled value expression",
1746 BuiltinLintDiagnostics::BreakWithLabelAndLoop(expr.span),
1754 let expr = self.mk_expr(lo.to(self.prev_token.span), ExprKind::Break(label, kind));
1755 self.maybe_recover_from_bad_qpath(expr)
1758 /// Parse `"yield" expr?`.
1759 fn parse_yield_expr(&mut self) -> PResult<'a, P<Expr>> {
1760 let lo = self.prev_token.span;
1761 let kind = ExprKind::Yield(self.parse_expr_opt()?);
1762 let span = lo.to(self.prev_token.span);
1763 self.sess.gated_spans.gate(sym::generators, span);
1764 let expr = self.mk_expr(span, kind);
1765 self.maybe_recover_from_bad_qpath(expr)
1768 /// Returns a string literal if the next token is a string literal.
1769 /// In case of error returns `Some(lit)` if the next token is a literal with a wrong kind,
1770 /// and returns `None` if the next token is not literal at all.
1771 pub fn parse_str_lit(&mut self) -> Result<ast::StrLit, Option<Lit>> {
1772 match self.parse_opt_ast_lit() {
1773 Some(lit) => match lit.kind {
1774 ast::LitKind::Str(symbol_unescaped, style) => Ok(ast::StrLit {
1776 symbol: lit.token_lit.symbol,
1777 suffix: lit.token_lit.suffix,
1781 _ => Err(Some(lit)),
1787 fn handle_missing_lit(&mut self) -> PResult<'a, Lit> {
1788 if let token::Interpolated(inner) = &self.token.kind {
1789 let expr = match inner.as_ref() {
1790 token::NtExpr(expr) => Some(expr),
1791 token::NtLiteral(expr) => Some(expr),
1794 if let Some(expr) = expr {
1795 if matches!(expr.kind, ExprKind::Err) {
1796 let mut err = InvalidInterpolatedExpression { span: self.token.span }
1797 .into_diagnostic(&self.sess.span_diagnostic);
1798 err.downgrade_to_delayed_bug();
1803 let token = self.token.clone();
1804 let err = |self_: &Self| {
1805 let msg = format!("unexpected token: {}", super::token_descr(&token));
1806 self_.struct_span_err(token.span, &msg)
1808 // On an error path, eagerly consider a lifetime to be an unclosed character lit
1809 if self.token.is_lifetime() {
1810 let lt = self.expect_lifetime();
1811 Ok(self.recover_unclosed_char(lt.ident, err))
1817 pub(super) fn parse_token_lit(&mut self) -> PResult<'a, (token::Lit, Span)> {
1818 self.parse_opt_token_lit()
1820 .or_else(|()| self.handle_missing_lit().map(|lit| (lit.token_lit, lit.span)))
1823 pub(super) fn parse_ast_lit(&mut self) -> PResult<'a, Lit> {
1824 self.parse_opt_ast_lit().ok_or(()).or_else(|()| self.handle_missing_lit())
1827 fn recover_after_dot(&mut self) -> Option<Token> {
1828 let mut recovered = None;
1829 if self.token == token::Dot {
1830 // Attempt to recover `.4` as `0.4`. We don't currently have any syntax where
1831 // dot would follow an optional literal, so we do this unconditionally.
1832 recovered = self.look_ahead(1, |next_token| {
1833 if let token::Literal(token::Lit { kind: token::Integer, symbol, suffix }) =
1836 if self.token.span.hi() == next_token.span.lo() {
1837 let s = String::from("0.") + symbol.as_str();
1838 let kind = TokenKind::lit(token::Float, Symbol::intern(&s), suffix);
1839 return Some(Token::new(kind, self.token.span.to(next_token.span)));
1844 if let Some(token) = &recovered {
1846 self.sess.emit_err(FloatLiteralRequiresIntegerPart {
1848 correct: pprust::token_to_string(token).into_owned(),
1856 /// Matches `lit = true | false | token_lit`.
1857 /// Returns `None` if the next token is not a literal.
1858 pub(super) fn parse_opt_token_lit(&mut self) -> Option<(token::Lit, Span)> {
1859 let recovered = self.recover_after_dot();
1860 let token = recovered.as_ref().unwrap_or(&self.token);
1861 let span = token.span;
1862 token::Lit::from_token(token).map(|token_lit| {
1868 /// Matches `lit = true | false | token_lit`.
1869 /// Returns `None` if the next token is not a literal.
1870 pub(super) fn parse_opt_ast_lit(&mut self) -> Option<Lit> {
1871 let recovered = self.recover_after_dot();
1872 let token = recovered.as_ref().unwrap_or(&self.token);
1873 match token::Lit::from_token(token) {
1874 Some(token_lit) => {
1875 match Lit::from_token_lit(token_lit, token.span) {
1881 let span = token.span;
1882 let token::Literal(lit) = token.kind else {
1886 report_lit_error(&self.sess, err, lit, span);
1887 // Pack possible quotes and prefixes from the original literal into
1888 // the error literal's symbol so they can be pretty-printed faithfully.
1889 let suffixless_lit = token::Lit::new(lit.kind, lit.symbol, None);
1890 let symbol = Symbol::intern(&suffixless_lit.to_string());
1891 let lit = token::Lit::new(token::Err, symbol, lit.suffix);
1892 Some(Lit::from_token_lit(lit, span).unwrap_or_else(|_| unreachable!()))
1900 pub(super) fn expect_no_tuple_index_suffix(&self, span: Span, suffix: Symbol) {
1901 if [sym::i32, sym::u32, sym::isize, sym::usize].contains(&suffix) {
1902 // #59553: warn instead of reject out of hand to allow the fix to percolate
1903 // through the ecosystem when people fix their macros
1904 self.sess.emit_warning(InvalidLiteralSuffixOnTupleIndex {
1907 exception: Some(()),
1910 self.sess.emit_err(InvalidLiteralSuffixOnTupleIndex { span, suffix, exception: None });
1914 /// Matches `'-' lit | lit` (cf. `ast_validation::AstValidator::check_expr_within_pat`).
1915 /// Keep this in sync with `Token::can_begin_literal_maybe_minus`.
1916 pub fn parse_literal_maybe_minus(&mut self) -> PResult<'a, P<Expr>> {
1917 maybe_whole_expr!(self);
1919 let lo = self.token.span;
1920 let minus_present = self.eat(&token::BinOp(token::Minus));
1921 let (token_lit, span) = self.parse_token_lit()?;
1922 let expr = self.mk_expr(span, ExprKind::Lit(token_lit));
1925 Ok(self.mk_expr(lo.to(self.prev_token.span), self.mk_unary(UnOp::Neg, expr)))
1931 fn is_array_like_block(&mut self) -> bool {
1932 self.look_ahead(1, |t| matches!(t.kind, TokenKind::Ident(..) | TokenKind::Literal(_)))
1933 && self.look_ahead(2, |t| t == &token::Comma)
1934 && self.look_ahead(3, |t| t.can_begin_expr())
1937 /// Emits a suggestion if it looks like the user meant an array but
1938 /// accidentally used braces, causing the code to be interpreted as a block
1940 fn maybe_suggest_brackets_instead_of_braces(&mut self, lo: Span) -> Option<P<Expr>> {
1941 let mut snapshot = self.create_snapshot_for_diagnostic();
1942 match snapshot.parse_array_or_repeat_expr(Delimiter::Brace) {
1944 self.sess.emit_err(ArrayBracketsInsteadOfSpaces {
1946 sub: ArrayBracketsInsteadOfSpacesSugg {
1948 right: snapshot.prev_token.span,
1952 self.restore_snapshot(snapshot);
1953 Some(self.mk_expr_err(arr.span))
1962 fn suggest_missing_semicolon_before_array(
1965 open_delim_span: Span,
1966 ) -> PResult<'a, ()> {
1967 if !self.may_recover() {
1971 if self.token.kind == token::Comma {
1972 if !self.sess.source_map().is_multiline(prev_span.until(self.token.span)) {
1975 let mut snapshot = self.create_snapshot_for_diagnostic();
1977 match snapshot.parse_seq_to_before_end(
1978 &token::CloseDelim(Delimiter::Bracket),
1979 SeqSep::trailing_allowed(token::Comma),
1983 // When the close delim is `)`, `token.kind` is expected to be `token::CloseDelim(Delimiter::Parenthesis)`,
1984 // but the actual `token.kind` is `token::CloseDelim(Delimiter::Bracket)`.
1985 // This is because the `token.kind` of the close delim is treated as the same as
1986 // that of the open delim in `TokenTreesReader::parse_token_tree`, even if the delimiters of them are different.
1987 // Therefore, `token.kind` should not be compared here.
1989 .span_to_snippet(snapshot.token.span)
1990 .map_or(false, |snippet| snippet == "]") =>
1992 return Err(MissingSemicolonBeforeArray {
1993 open_delim: open_delim_span,
1994 semicolon: prev_span.shrink_to_hi(),
1995 }.into_diagnostic(&self.sess.span_diagnostic));
1998 Err(err) => err.cancel(),
2004 /// Parses a block or unsafe block.
2005 pub(super) fn parse_block_expr(
2007 opt_label: Option<Label>,
2009 blk_mode: BlockCheckMode,
2010 ) -> PResult<'a, P<Expr>> {
2011 if self.may_recover() && self.is_array_like_block() {
2012 if let Some(arr) = self.maybe_suggest_brackets_instead_of_braces(lo) {
2017 if self.token.is_whole_block() {
2018 self.sess.emit_err(InvalidBlockMacroSegment {
2019 span: self.token.span,
2020 context: lo.to(self.token.span),
2024 let (attrs, blk) = self.parse_block_common(lo, blk_mode)?;
2025 Ok(self.mk_expr_with_attrs(blk.span, ExprKind::Block(blk, opt_label), attrs))
2028 /// Parse a block which takes no attributes and has no label
2029 fn parse_simple_block(&mut self) -> PResult<'a, P<Expr>> {
2030 let blk = self.parse_block()?;
2031 Ok(self.mk_expr(blk.span, ExprKind::Block(blk, None)))
2034 /// Parses a closure expression (e.g., `move |args| expr`).
2035 fn parse_closure_expr(&mut self) -> PResult<'a, P<Expr>> {
2036 let lo = self.token.span;
2038 let binder = if self.check_keyword(kw::For) {
2039 let lo = self.token.span;
2040 let lifetime_defs = self.parse_late_bound_lifetime_defs()?;
2041 let span = lo.to(self.prev_token.span);
2043 self.sess.gated_spans.gate(sym::closure_lifetime_binder, span);
2045 ClosureBinder::For { span, generic_params: P::from_vec(lifetime_defs) }
2047 ClosureBinder::NotPresent
2051 if self.eat_keyword(kw::Static) { Movability::Static } else { Movability::Movable };
2053 let asyncness = if self.token.uninterpolated_span().rust_2018() {
2054 self.parse_asyncness(Case::Sensitive)
2059 let capture_clause = self.parse_capture_clause()?;
2060 let fn_decl = self.parse_fn_block_decl()?;
2061 let decl_hi = self.prev_token.span;
2062 let mut body = match fn_decl.output {
2063 FnRetTy::Default(_) => {
2064 let restrictions = self.restrictions - Restrictions::STMT_EXPR;
2065 self.parse_expr_res(restrictions, None)?
2068 // If an explicit return type is given, require a block to appear (RFC 968).
2069 let body_lo = self.token.span;
2070 self.parse_block_expr(None, body_lo, BlockCheckMode::Default)?
2074 if let Async::Yes { span, .. } = asyncness {
2075 // Feature-gate `async ||` closures.
2076 self.sess.gated_spans.gate(sym::async_closure, span);
2079 if self.token.kind == TokenKind::Semi
2080 && matches!(self.token_cursor.frame.delim_sp, Some((Delimiter::Parenthesis, _)))
2081 && self.may_recover()
2083 // It is likely that the closure body is a block but where the
2084 // braces have been removed. We will recover and eat the next
2085 // statements later in the parsing process.
2086 body = self.mk_expr_err(body.span);
2089 let body_span = body.span;
2091 let closure = self.mk_expr(
2093 ExprKind::Closure(Box::new(ast::Closure {
2100 fn_decl_span: lo.to(decl_hi),
2104 // Disable recovery for closure body
2106 ClosureSpans { whole_closure: closure.span, closing_pipe: decl_hi, body: body_span };
2107 self.current_closure = Some(spans);
2112 /// Parses an optional `move` prefix to a closure-like construct.
2113 fn parse_capture_clause(&mut self) -> PResult<'a, CaptureBy> {
2114 if self.eat_keyword(kw::Move) {
2115 // Check for `move async` and recover
2116 if self.check_keyword(kw::Async) {
2117 let move_async_span = self.token.span.with_lo(self.prev_token.span.data().lo);
2118 Err(AsyncMoveOrderIncorrect { span: move_async_span }
2119 .into_diagnostic(&self.sess.span_diagnostic))
2121 Ok(CaptureBy::Value)
2128 /// Parses the `|arg, arg|` header of a closure.
2129 fn parse_fn_block_decl(&mut self) -> PResult<'a, P<FnDecl>> {
2130 let inputs = if self.eat(&token::OrOr) {
2133 self.expect(&token::BinOp(token::Or))?;
2135 .parse_seq_to_before_tokens(
2136 &[&token::BinOp(token::Or), &token::OrOr],
2137 SeqSep::trailing_allowed(token::Comma),
2138 TokenExpectType::NoExpect,
2139 |p| p.parse_fn_block_param(),
2146 self.parse_ret_ty(AllowPlus::Yes, RecoverQPath::Yes, RecoverReturnSign::Yes)?;
2148 Ok(P(FnDecl { inputs, output }))
2151 /// Parses a parameter in a closure header (e.g., `|arg, arg|`).
2152 fn parse_fn_block_param(&mut self) -> PResult<'a, Param> {
2153 let lo = self.token.span;
2154 let attrs = self.parse_outer_attributes()?;
2155 self.collect_tokens_trailing_token(attrs, ForceCollect::No, |this, attrs| {
2156 let pat = this.parse_pat_no_top_alt(PARAM_EXPECTED)?;
2157 let ty = if this.eat(&token::Colon) {
2160 this.mk_ty(this.prev_token.span, TyKind::Infer)
2168 span: lo.to(this.prev_token.span),
2170 is_placeholder: false,
2172 TrailingToken::MaybeComma,
2177 /// Parses an `if` expression (`if` token already eaten).
2178 fn parse_if_expr(&mut self) -> PResult<'a, P<Expr>> {
2179 let lo = self.prev_token.span;
2180 let cond = self.parse_cond_expr()?;
2181 self.parse_if_after_cond(lo, cond)
2184 fn parse_if_after_cond(&mut self, lo: Span, mut cond: P<Expr>) -> PResult<'a, P<Expr>> {
2185 let cond_span = cond.span;
2186 // Tries to interpret `cond` as either a missing expression if it's a block,
2187 // or as an unfinished expression if it's a binop and the RHS is a block.
2188 // We could probably add more recoveries here too...
2189 let mut recover_block_from_condition = |this: &mut Self| {
2190 let block = match &mut cond.kind {
2191 ExprKind::Binary(Spanned { span: binop_span, .. }, _, right)
2192 if let ExprKind::Block(_, None) = right.kind => {
2193 self.sess.emit_err(IfExpressionMissingThenBlock {
2195 sub: IfExpressionMissingThenBlockSub::UnfinishedCondition(
2196 cond_span.shrink_to_lo().to(*binop_span)
2199 std::mem::replace(right, this.mk_expr_err(binop_span.shrink_to_hi()))
2201 ExprKind::Block(_, None) => {
2202 self.sess.emit_err(IfExpressionMissingCondition {
2203 if_span: lo.shrink_to_hi(),
2204 block_span: self.sess.source_map().start_point(cond_span),
2206 std::mem::replace(&mut cond, this.mk_expr_err(cond_span.shrink_to_hi()))
2212 if let ExprKind::Block(block, _) = &block.kind {
2219 let thn = if self.token.is_keyword(kw::Else) {
2220 if let Some(block) = recover_block_from_condition(self) {
2223 self.sess.emit_err(IfExpressionMissingThenBlock {
2225 sub: IfExpressionMissingThenBlockSub::AddThenBlock(cond_span.shrink_to_hi()),
2227 self.mk_block_err(cond_span.shrink_to_hi())
2230 let attrs = self.parse_outer_attributes()?; // For recovery.
2231 let block = if self.check(&token::OpenDelim(Delimiter::Brace)) {
2234 if let Some(block) = recover_block_from_condition(self) {
2237 self.error_on_extra_if(&cond)?;
2238 // Parse block, which will always fail, but we can add a nice note to the error
2239 self.parse_block().map_err(|mut err| {
2242 "the `if` expression is missing a block after this condition",
2248 self.error_on_if_block_attrs(lo, false, block.span, attrs);
2251 let els = if self.eat_keyword(kw::Else) { Some(self.parse_else_expr()?) } else { None };
2252 Ok(self.mk_expr(lo.to(self.prev_token.span), ExprKind::If(cond, thn, els)))
2255 /// Parses the condition of a `if` or `while` expression.
2256 fn parse_cond_expr(&mut self) -> PResult<'a, P<Expr>> {
2258 self.parse_expr_res(Restrictions::NO_STRUCT_LITERAL | Restrictions::ALLOW_LET, None)?;
2260 if let ExprKind::Let(..) = cond.kind {
2261 // Remove the last feature gating of a `let` expression since it's stable.
2262 self.sess.gated_spans.ungate_last(sym::let_chains, cond.span);
2268 /// Parses a `let $pat = $expr` pseudo-expression.
2269 fn parse_let_expr(&mut self) -> PResult<'a, P<Expr>> {
2270 // This is a *approximate* heuristic that detects if `let` chains are
2271 // being parsed in the right position. It's approximate because it
2272 // doesn't deny all invalid `let` expressions, just completely wrong usages.
2273 let not_in_chain = !matches!(
2274 self.prev_token.kind,
2275 TokenKind::AndAnd | TokenKind::Ident(kw::If, _) | TokenKind::Ident(kw::While, _)
2277 if !self.restrictions.contains(Restrictions::ALLOW_LET) || not_in_chain {
2278 self.sess.emit_err(ExpectedExpressionFoundLet { span: self.token.span });
2281 self.bump(); // Eat `let` token
2282 let lo = self.prev_token.span;
2283 let pat = self.parse_pat_allow_top_alt(
2287 CommaRecoveryMode::LikelyTuple,
2289 if self.token == token::EqEq {
2290 self.sess.emit_err(ExpectedEqForLetExpr {
2291 span: self.token.span,
2292 sugg_span: self.token.span,
2296 self.expect(&token::Eq)?;
2298 let expr = self.with_res(self.restrictions | Restrictions::NO_STRUCT_LITERAL, |this| {
2299 this.parse_assoc_expr_with(1 + prec_let_scrutinee_needs_par(), None.into())
2301 let span = lo.to(expr.span);
2302 self.sess.gated_spans.gate(sym::let_chains, span);
2303 Ok(self.mk_expr(span, ExprKind::Let(pat, expr, span)))
2306 /// Parses an `else { ... }` expression (`else` token already eaten).
2307 fn parse_else_expr(&mut self) -> PResult<'a, P<Expr>> {
2308 let else_span = self.prev_token.span; // `else`
2309 let attrs = self.parse_outer_attributes()?; // For recovery.
2310 let expr = if self.eat_keyword(kw::If) {
2311 self.parse_if_expr()?
2312 } else if self.check(&TokenKind::OpenDelim(Delimiter::Brace)) {
2313 self.parse_simple_block()?
2315 let snapshot = self.create_snapshot_for_diagnostic();
2316 let first_tok = super::token_descr(&self.token);
2317 let first_tok_span = self.token.span;
2318 match self.parse_expr() {
2320 // If it's not a free-standing expression, and is followed by a block,
2321 // then it's very likely the condition to an `else if`.
2322 if self.check(&TokenKind::OpenDelim(Delimiter::Brace))
2323 && classify::expr_requires_semi_to_be_stmt(&cond) =>
2325 self.sess.emit_err(ExpectedElseBlock {
2329 condition_start: cond.span.shrink_to_lo(),
2331 self.parse_if_after_cond(cond.span.shrink_to_lo(), cond)?
2335 self.restore_snapshot(snapshot);
2336 self.parse_simple_block()?
2339 self.restore_snapshot(snapshot);
2340 self.parse_simple_block()?
2344 self.error_on_if_block_attrs(else_span, true, expr.span, attrs);
2348 fn error_on_if_block_attrs(
2355 if attrs.is_empty() {
2359 let attrs: &[ast::Attribute] = &attrs.take_for_recovery(self.sess);
2360 let (attributes, last) = match attrs {
2362 [x0 @ xn] | [x0, .., xn] => (x0.span.to(xn.span), xn.span),
2364 let ctx = if is_ctx_else { "else" } else { "if" };
2365 self.sess.emit_err(OuterAttributeNotAllowedOnIfElse {
2369 ctx: ctx.to_string(),
2374 fn error_on_extra_if(&mut self, cond: &P<Expr>) -> PResult<'a, ()> {
2375 if let ExprKind::Binary(Spanned { span: binop_span, node: binop}, _, right) = &cond.kind &&
2376 let BinOpKind::And = binop &&
2377 let ExprKind::If(cond, ..) = &right.kind {
2378 Err(self.sess.create_err(UnexpectedIfWithIf(binop_span.shrink_to_hi().to(cond.span.shrink_to_lo()))))
2384 /// Parses `for <src_pat> in <src_expr> <src_loop_block>` (`for` token already eaten).
2385 fn parse_for_expr(&mut self, opt_label: Option<Label>, lo: Span) -> PResult<'a, P<Expr>> {
2386 // Record whether we are about to parse `for (`.
2387 // This is used below for recovery in case of `for ( $stuff ) $block`
2388 // in which case we will suggest `for $stuff $block`.
2389 let begin_paren = match self.token.kind {
2390 token::OpenDelim(Delimiter::Parenthesis) => Some(self.token.span),
2394 let pat = self.parse_pat_allow_top_alt(
2398 CommaRecoveryMode::LikelyTuple,
2400 if !self.eat_keyword(kw::In) {
2401 self.error_missing_in_for_loop();
2403 self.check_for_for_in_in_typo(self.prev_token.span);
2404 let expr = self.parse_expr_res(Restrictions::NO_STRUCT_LITERAL, None)?;
2406 let pat = self.recover_parens_around_for_head(pat, begin_paren);
2408 let (attrs, loop_block) = self.parse_inner_attrs_and_block()?;
2410 let kind = ExprKind::ForLoop(pat, expr, loop_block, opt_label);
2411 Ok(self.mk_expr_with_attrs(lo.to(self.prev_token.span), kind, attrs))
2414 fn error_missing_in_for_loop(&mut self) {
2415 let (span, sub): (_, fn(_) -> _) = if self.token.is_ident_named(sym::of) {
2416 // Possibly using JS syntax (#75311).
2417 let span = self.token.span;
2419 (span, MissingInInForLoopSub::InNotOf)
2421 (self.prev_token.span.between(self.token.span), MissingInInForLoopSub::AddIn)
2424 self.sess.emit_err(MissingInInForLoop { span, sub: sub(span) });
2427 /// Parses a `while` or `while let` expression (`while` token already eaten).
2428 fn parse_while_expr(&mut self, opt_label: Option<Label>, lo: Span) -> PResult<'a, P<Expr>> {
2429 let cond = self.parse_cond_expr().map_err(|mut err| {
2430 err.span_label(lo, "while parsing the condition of this `while` expression");
2433 let (attrs, body) = self.parse_inner_attrs_and_block().map_err(|mut err| {
2434 err.span_label(lo, "while parsing the body of this `while` expression");
2435 err.span_label(cond.span, "this `while` condition successfully parsed");
2438 Ok(self.mk_expr_with_attrs(
2439 lo.to(self.prev_token.span),
2440 ExprKind::While(cond, body, opt_label),
2445 /// Parses `loop { ... }` (`loop` token already eaten).
2446 fn parse_loop_expr(&mut self, opt_label: Option<Label>, lo: Span) -> PResult<'a, P<Expr>> {
2447 let (attrs, body) = self.parse_inner_attrs_and_block()?;
2448 Ok(self.mk_expr_with_attrs(
2449 lo.to(self.prev_token.span),
2450 ExprKind::Loop(body, opt_label),
2455 pub(crate) fn eat_label(&mut self) -> Option<Label> {
2456 self.token.lifetime().map(|ident| {
2462 /// Parses a `match ... { ... }` expression (`match` token already eaten).
2463 fn parse_match_expr(&mut self) -> PResult<'a, P<Expr>> {
2464 let match_span = self.prev_token.span;
2465 let lo = self.prev_token.span;
2466 let scrutinee = self.parse_expr_res(Restrictions::NO_STRUCT_LITERAL, None)?;
2467 if let Err(mut e) = self.expect(&token::OpenDelim(Delimiter::Brace)) {
2468 if self.token == token::Semi {
2469 e.span_suggestion_short(
2471 "try removing this `match`",
2473 Applicability::MaybeIncorrect, // speculative
2476 if self.maybe_recover_unexpected_block_label() {
2483 let attrs = self.parse_inner_attributes()?;
2485 let mut arms: Vec<Arm> = Vec::new();
2486 while self.token != token::CloseDelim(Delimiter::Brace) {
2487 match self.parse_arm() {
2488 Ok(arm) => arms.push(arm),
2490 // Recover by skipping to the end of the block.
2492 self.recover_stmt();
2493 let span = lo.to(self.token.span);
2494 if self.token == token::CloseDelim(Delimiter::Brace) {
2497 return Ok(self.mk_expr_with_attrs(
2499 ExprKind::Match(scrutinee, arms),
2505 let hi = self.token.span;
2507 Ok(self.mk_expr_with_attrs(lo.to(hi), ExprKind::Match(scrutinee, arms), attrs))
2510 /// Attempt to recover from match arm body with statements and no surrounding braces.
2511 fn parse_arm_body_missing_braces(
2513 first_expr: &P<Expr>,
2515 ) -> Option<P<Expr>> {
2516 if self.token.kind != token::Semi {
2519 let start_snapshot = self.create_snapshot_for_diagnostic();
2520 let semi_sp = self.token.span;
2523 vec![self.mk_stmt(first_expr.span, ast::StmtKind::Expr(first_expr.clone()))];
2524 let err = |this: &Parser<'_>, stmts: Vec<ast::Stmt>| {
2525 let span = stmts[0].span.to(stmts[stmts.len() - 1].span);
2527 this.sess.emit_err(MatchArmBodyWithoutBraces {
2530 num_statements: stmts.len(),
2531 sub: if stmts.len() > 1 {
2532 MatchArmBodyWithoutBracesSugg::AddBraces {
2533 left: span.shrink_to_lo(),
2534 right: span.shrink_to_hi(),
2537 MatchArmBodyWithoutBracesSugg::UseComma { semicolon: semi_sp }
2540 this.mk_expr_err(span)
2542 // We might have either a `,` -> `;` typo, or a block without braces. We need
2543 // a more subtle parsing strategy.
2545 if self.token.kind == token::CloseDelim(Delimiter::Brace) {
2546 // We have reached the closing brace of the `match` expression.
2547 return Some(err(self, stmts));
2549 if self.token.kind == token::Comma {
2550 self.restore_snapshot(start_snapshot);
2553 let pre_pat_snapshot = self.create_snapshot_for_diagnostic();
2554 match self.parse_pat_no_top_alt(None) {
2556 if self.token.kind == token::FatArrow {
2558 self.restore_snapshot(pre_pat_snapshot);
2559 return Some(err(self, stmts));
2567 self.restore_snapshot(pre_pat_snapshot);
2568 match self.parse_stmt_without_recovery(true, ForceCollect::No) {
2569 // Consume statements for as long as possible.
2574 self.restore_snapshot(start_snapshot);
2577 // We couldn't parse either yet another statement missing it's
2578 // enclosing block nor the next arm's pattern or closing brace.
2581 self.restore_snapshot(start_snapshot);
2589 pub(super) fn parse_arm(&mut self) -> PResult<'a, Arm> {
2590 // Used to check the `let_chains` and `if_let_guard` features mostly by scanning
2592 fn check_let_expr(expr: &Expr) -> (bool, bool) {
2594 ExprKind::Binary(BinOp { node: BinOpKind::And, .. }, lhs, rhs) => {
2595 let lhs_rslt = check_let_expr(lhs);
2596 let rhs_rslt = check_let_expr(rhs);
2597 (lhs_rslt.0 || rhs_rslt.0, false)
2599 ExprKind::Let(..) => (true, true),
2603 let attrs = self.parse_outer_attributes()?;
2604 self.collect_tokens_trailing_token(attrs, ForceCollect::No, |this, attrs| {
2605 let lo = this.token.span;
2606 let pat = this.parse_pat_allow_top_alt(
2610 CommaRecoveryMode::EitherTupleOrPipe,
2612 let guard = if this.eat_keyword(kw::If) {
2613 let if_span = this.prev_token.span;
2614 let cond = this.parse_expr_res(Restrictions::ALLOW_LET, None)?;
2615 let (has_let_expr, does_not_have_bin_op) = check_let_expr(&cond);
2617 if does_not_have_bin_op {
2618 // Remove the last feature gating of a `let` expression since it's stable.
2619 this.sess.gated_spans.ungate_last(sym::let_chains, cond.span);
2621 let span = if_span.to(cond.span);
2622 this.sess.gated_spans.gate(sym::if_let_guard, span);
2628 let arrow_span = this.token.span;
2629 if let Err(mut err) = this.expect(&token::FatArrow) {
2630 // We might have a `=>` -> `=` or `->` typo (issue #89396).
2631 if TokenKind::FatArrow
2633 .map_or(false, |similar_tokens| similar_tokens.contains(&this.token.kind))
2635 err.span_suggestion(
2637 "try using a fat arrow here",
2639 Applicability::MaybeIncorrect,
2647 let arm_start_span = this.token.span;
2649 let expr = this.parse_expr_res(Restrictions::STMT_EXPR, None).map_err(|mut err| {
2650 err.span_label(arrow_span, "while parsing the `match` arm starting here");
2654 let require_comma = classify::expr_requires_semi_to_be_stmt(&expr)
2655 && this.token != token::CloseDelim(Delimiter::Brace);
2657 let hi = this.prev_token.span;
2660 let sm = this.sess.source_map();
2661 if let Some(body) = this.parse_arm_body_missing_braces(&expr, arrow_span) {
2662 let span = body.span;
2671 is_placeholder: false,
2673 TrailingToken::None,
2676 this.expect_one_of(&[token::Comma], &[token::CloseDelim(Delimiter::Brace)])
2677 .or_else(|mut err| {
2678 if this.token == token::FatArrow {
2679 if let Ok(expr_lines) = sm.span_to_lines(expr.span)
2680 && let Ok(arm_start_lines) = sm.span_to_lines(arm_start_span)
2681 && arm_start_lines.lines[0].end_col == expr_lines.lines[0].end_col
2682 && expr_lines.lines.len() == 2
2684 // We check whether there's any trailing code in the parse span,
2685 // if there isn't, we very likely have the following:
2688 // | -- - missing comma
2692 // | - ^^ self.token.span
2694 // | parsed until here as `"y" & X`
2695 err.span_suggestion_short(
2696 arm_start_span.shrink_to_hi(),
2697 "missing a comma here to end this `match` arm",
2699 Applicability::MachineApplicable,
2704 // FIXME(compiler-errors): We could also recover `; PAT =>` here
2706 // Try to parse a following `PAT =>`, if successful
2707 // then we should recover.
2708 let mut snapshot = this.create_snapshot_for_diagnostic();
2709 let pattern_follows = snapshot
2710 .parse_pat_allow_top_alt(
2714 CommaRecoveryMode::EitherTupleOrPipe,
2716 .map_err(|err| err.cancel())
2718 if pattern_follows && snapshot.check(&TokenKind::FatArrow) {
2720 this.sess.emit_err(MissingCommaAfterMatchArm {
2721 span: hi.shrink_to_hi(),
2726 err.span_label(arrow_span, "while parsing the `match` arm starting here");
2730 this.eat(&token::Comma);
2741 is_placeholder: false,
2743 TrailingToken::None,
2748 /// Parses a `try {...}` expression (`try` token already eaten).
2749 fn parse_try_block(&mut self, span_lo: Span) -> PResult<'a, P<Expr>> {
2750 let (attrs, body) = self.parse_inner_attrs_and_block()?;
2751 if self.eat_keyword(kw::Catch) {
2752 Err(CatchAfterTry { span: self.prev_token.span }
2753 .into_diagnostic(&self.sess.span_diagnostic))
2755 let span = span_lo.to(body.span);
2756 self.sess.gated_spans.gate(sym::try_blocks, span);
2757 Ok(self.mk_expr_with_attrs(span, ExprKind::TryBlock(body), attrs))
2761 fn is_do_catch_block(&self) -> bool {
2762 self.token.is_keyword(kw::Do)
2763 && self.is_keyword_ahead(1, &[kw::Catch])
2764 && self.look_ahead(2, |t| *t == token::OpenDelim(Delimiter::Brace))
2765 && !self.restrictions.contains(Restrictions::NO_STRUCT_LITERAL)
2768 fn is_do_yeet(&self) -> bool {
2769 self.token.is_keyword(kw::Do) && self.is_keyword_ahead(1, &[kw::Yeet])
2772 fn is_try_block(&self) -> bool {
2773 self.token.is_keyword(kw::Try)
2774 && self.look_ahead(1, |t| *t == token::OpenDelim(Delimiter::Brace))
2775 && self.token.uninterpolated_span().rust_2018()
2778 /// Parses an `async move? {...}` expression.
2779 fn parse_async_block(&mut self) -> PResult<'a, P<Expr>> {
2780 let lo = self.token.span;
2781 self.expect_keyword(kw::Async)?;
2782 let capture_clause = self.parse_capture_clause()?;
2783 let (attrs, body) = self.parse_inner_attrs_and_block()?;
2784 let kind = ExprKind::Async(capture_clause, DUMMY_NODE_ID, body);
2785 Ok(self.mk_expr_with_attrs(lo.to(self.prev_token.span), kind, attrs))
2788 fn is_async_block(&self) -> bool {
2789 self.token.is_keyword(kw::Async)
2792 self.is_keyword_ahead(1, &[kw::Move])
2793 && self.look_ahead(2, |t| *t == token::OpenDelim(Delimiter::Brace))
2796 self.look_ahead(1, |t| *t == token::OpenDelim(Delimiter::Brace))
2800 fn is_certainly_not_a_block(&self) -> bool {
2801 self.look_ahead(1, |t| t.is_ident())
2803 // `{ ident, ` cannot start a block.
2804 self.look_ahead(2, |t| t == &token::Comma)
2805 || self.look_ahead(2, |t| t == &token::Colon)
2807 // `{ ident: token, ` cannot start a block.
2808 self.look_ahead(4, |t| t == &token::Comma) ||
2809 // `{ ident: ` cannot start a block unless it's a type ascription `ident: Type`.
2810 self.look_ahead(3, |t| !t.can_begin_type())
2815 fn maybe_parse_struct_expr(
2817 qself: &Option<P<ast::QSelf>>,
2819 ) -> Option<PResult<'a, P<Expr>>> {
2820 let struct_allowed = !self.restrictions.contains(Restrictions::NO_STRUCT_LITERAL);
2821 if struct_allowed || self.is_certainly_not_a_block() {
2822 if let Err(err) = self.expect(&token::OpenDelim(Delimiter::Brace)) {
2823 return Some(Err(err));
2825 let expr = self.parse_struct_expr(qself.clone(), path.clone(), true);
2826 if let (Ok(expr), false) = (&expr, struct_allowed) {
2827 // This is a struct literal, but we don't can't accept them here.
2828 self.sess.emit_err(StructLiteralNotAllowedHere {
2830 sub: StructLiteralNotAllowedHereSugg {
2831 left: path.span.shrink_to_lo(),
2832 right: expr.span.shrink_to_hi(),
2841 pub(super) fn parse_struct_fields(
2845 close_delim: Delimiter,
2846 ) -> PResult<'a, (Vec<ExprField>, ast::StructRest, bool)> {
2847 let mut fields = Vec::new();
2848 let mut base = ast::StructRest::None;
2849 let mut recover_async = false;
2851 let mut async_block_err = |e: &mut Diagnostic, span: Span| {
2852 recover_async = true;
2853 e.span_label(span, "`async` blocks are only allowed in Rust 2018 or later");
2854 e.help_use_latest_edition();
2857 while self.token != token::CloseDelim(close_delim) {
2858 if self.eat(&token::DotDot) || self.recover_struct_field_dots(close_delim) {
2859 let exp_span = self.prev_token.span;
2860 // We permit `.. }` on the left-hand side of a destructuring assignment.
2861 if self.check(&token::CloseDelim(close_delim)) {
2862 base = ast::StructRest::Rest(self.prev_token.span.shrink_to_hi());
2865 match self.parse_expr() {
2866 Ok(e) => base = ast::StructRest::Base(e),
2867 Err(mut e) if recover => {
2869 self.recover_stmt();
2871 Err(e) => return Err(e),
2873 self.recover_struct_comma_after_dotdot(exp_span);
2877 let recovery_field = self.find_struct_error_after_field_looking_code();
2878 let parsed_field = match self.parse_expr_field() {
2881 if pth == kw::Async {
2882 async_block_err(&mut e, pth.span);
2884 e.span_label(pth.span, "while parsing this struct");
2888 // If the next token is a comma, then try to parse
2889 // what comes next as additional fields, rather than
2890 // bailing out until next `}`.
2891 if self.token != token::Comma {
2892 self.recover_stmt_(SemiColonMode::Comma, BlockMode::Ignore);
2893 if self.token != token::Comma {
2901 let is_shorthand = parsed_field.as_ref().map_or(false, |f| f.is_shorthand);
2902 // A shorthand field can be turned into a full field with `:`.
2903 // We should point this out.
2904 self.check_or_expected(!is_shorthand, TokenType::Token(token::Colon));
2906 match self.expect_one_of(&[token::Comma], &[token::CloseDelim(close_delim)]) {
2908 if let Some(f) = parsed_field.or(recovery_field) {
2909 // Only include the field if there's no parse error for the field name.
2914 if pth == kw::Async {
2915 async_block_err(&mut e, pth.span);
2917 e.span_label(pth.span, "while parsing this struct");
2918 if let Some(f) = recovery_field {
2921 self.prev_token.span.shrink_to_hi(),
2922 "try adding a comma",
2924 Applicability::MachineApplicable,
2926 } else if is_shorthand
2927 && (AssocOp::from_token(&self.token).is_some()
2928 || matches!(&self.token.kind, token::OpenDelim(_))
2929 || self.token.kind == token::Dot)
2931 // Looks like they tried to write a shorthand, complex expression.
2932 let ident = parsed_field.expect("is_shorthand implies Some").ident;
2934 ident.span.shrink_to_lo(),
2935 "try naming a field",
2936 &format!("{ident}: "),
2937 Applicability::HasPlaceholders,
2945 self.recover_stmt_(SemiColonMode::Comma, BlockMode::Ignore);
2946 self.eat(&token::Comma);
2950 Ok((fields, base, recover_async))
2953 /// Precondition: already parsed the '{'.
2954 pub(super) fn parse_struct_expr(
2956 qself: Option<P<ast::QSelf>>,
2959 ) -> PResult<'a, P<Expr>> {
2961 let (fields, base, recover_async) =
2962 self.parse_struct_fields(pth.clone(), recover, Delimiter::Brace)?;
2963 let span = lo.to(self.token.span);
2964 self.expect(&token::CloseDelim(Delimiter::Brace))?;
2965 let expr = if recover_async {
2968 ExprKind::Struct(P(ast::StructExpr { qself, path: pth, fields, rest: base }))
2970 Ok(self.mk_expr(span, expr))
2973 /// Use in case of error after field-looking code: `S { foo: () with a }`.
2974 fn find_struct_error_after_field_looking_code(&self) -> Option<ExprField> {
2975 match self.token.ident() {
2976 Some((ident, is_raw))
2977 if (is_raw || !ident.is_reserved())
2978 && self.look_ahead(1, |t| *t == token::Colon) =>
2980 Some(ast::ExprField {
2982 span: self.token.span,
2983 expr: self.mk_expr_err(self.token.span),
2984 is_shorthand: false,
2985 attrs: AttrVec::new(),
2987 is_placeholder: false,
2994 fn recover_struct_comma_after_dotdot(&mut self, span: Span) {
2995 if self.token != token::Comma {
2998 self.sess.emit_err(CommaAfterBaseStruct {
2999 span: span.to(self.prev_token.span),
3000 comma: self.token.span,
3002 self.recover_stmt();
3005 fn recover_struct_field_dots(&mut self, close_delim: Delimiter) -> bool {
3006 if !self.look_ahead(1, |t| *t == token::CloseDelim(close_delim))
3007 && self.eat(&token::DotDotDot)
3009 // recover from typo of `...`, suggest `..`
3010 let span = self.prev_token.span;
3011 self.sess.emit_err(MissingDotDot { token_span: span, sugg_span: span });
3017 /// Parses `ident (COLON expr)?`.
3018 fn parse_expr_field(&mut self) -> PResult<'a, ExprField> {
3019 let attrs = self.parse_outer_attributes()?;
3020 self.collect_tokens_trailing_token(attrs, ForceCollect::No, |this, attrs| {
3021 let lo = this.token.span;
3023 // Check if a colon exists one ahead. This means we're parsing a fieldname.
3024 let is_shorthand = !this.look_ahead(1, |t| t == &token::Colon || t == &token::Eq);
3025 let (ident, expr) = if is_shorthand {
3026 // Mimic `x: x` for the `x` field shorthand.
3027 let ident = this.parse_ident_common(false)?;
3028 let path = ast::Path::from_ident(ident);
3029 (ident, this.mk_expr(ident.span, ExprKind::Path(None, path)))
3031 let ident = this.parse_field_name()?;
3032 this.error_on_eq_field_init(ident);
3034 (ident, this.parse_expr()?)
3040 span: lo.to(expr.span),
3045 is_placeholder: false,
3047 TrailingToken::MaybeComma,
3052 /// Check for `=`. This means the source incorrectly attempts to
3053 /// initialize a field with an eq rather than a colon.
3054 fn error_on_eq_field_init(&self, field_name: Ident) {
3055 if self.token != token::Eq {
3059 self.sess.emit_err(EqFieldInit {
3060 span: self.token.span,
3061 eq: field_name.span.shrink_to_hi().to(self.token.span),
3065 fn err_dotdotdot_syntax(&self, span: Span) {
3066 self.sess.emit_err(DotDotDot { span });
3069 fn err_larrow_operator(&self, span: Span) {
3070 self.sess.emit_err(LeftArrowOperator { span });
3073 fn mk_assign_op(&self, binop: BinOp, lhs: P<Expr>, rhs: P<Expr>) -> ExprKind {
3074 ExprKind::AssignOp(binop, lhs, rhs)
3079 start: Option<P<Expr>>,
3080 end: Option<P<Expr>>,
3081 limits: RangeLimits,
3083 if end.is_none() && limits == RangeLimits::Closed {
3084 self.inclusive_range_with_incorrect_end(self.prev_token.span);
3087 ExprKind::Range(start, end, limits)
3091 fn mk_unary(&self, unop: UnOp, expr: P<Expr>) -> ExprKind {
3092 ExprKind::Unary(unop, expr)
3095 fn mk_binary(&self, binop: BinOp, lhs: P<Expr>, rhs: P<Expr>) -> ExprKind {
3096 ExprKind::Binary(binop, lhs, rhs)
3099 fn mk_index(&self, expr: P<Expr>, idx: P<Expr>) -> ExprKind {
3100 ExprKind::Index(expr, idx)
3103 fn mk_call(&self, f: P<Expr>, args: Vec<P<Expr>>) -> ExprKind {
3104 ExprKind::Call(f, args)
3107 fn mk_await_expr(&mut self, self_arg: P<Expr>, lo: Span) -> P<Expr> {
3108 let span = lo.to(self.prev_token.span);
3109 let await_expr = self.mk_expr(span, ExprKind::Await(self_arg));
3110 self.recover_from_await_method_call();
3114 pub(crate) fn mk_expr_with_attrs(&self, span: Span, kind: ExprKind, attrs: AttrVec) -> P<Expr> {
3115 P(Expr { kind, span, attrs, id: DUMMY_NODE_ID, tokens: None })
3118 pub(crate) fn mk_expr(&self, span: Span, kind: ExprKind) -> P<Expr> {
3119 P(Expr { kind, span, attrs: AttrVec::new(), id: DUMMY_NODE_ID, tokens: None })
3122 pub(super) fn mk_expr_err(&self, span: Span) -> P<Expr> {
3123 self.mk_expr(span, ExprKind::Err)
3126 /// Create expression span ensuring the span of the parent node
3127 /// is larger than the span of lhs and rhs, including the attributes.
3128 fn mk_expr_sp(&self, lhs: &P<Expr>, lhs_span: Span, rhs_span: Span) -> Span {
3131 .find(|a| a.style == AttrStyle::Outer)
3132 .map_or(lhs_span, |a| a.span)
3136 fn collect_tokens_for_expr(
3139 f: impl FnOnce(&mut Self, ast::AttrVec) -> PResult<'a, P<Expr>>,
3140 ) -> PResult<'a, P<Expr>> {
3141 self.collect_tokens_trailing_token(attrs, ForceCollect::No, |this, attrs| {
3142 let res = f(this, attrs)?;
3143 let trailing = if this.restrictions.contains(Restrictions::STMT_EXPR)
3144 && this.token.kind == token::Semi
3147 } else if this.token.kind == token::Gt {
3150 // FIXME - pass this through from the place where we know
3151 // we need a comma, rather than assuming that `#[attr] expr,`
3152 // always captures a trailing comma
3153 TrailingToken::MaybeComma