]> git.lizzy.rs Git - rust.git/blob - src/librustc_parse/parser/expr.rs
0792f1b3b7f646cd8925b7ca3f7a8ced94aef328
[rust.git] / src / librustc_parse / parser / expr.rs
1 use super::{Parser, Restrictions, PrevTokenKind, TokenType, PathStyle, BlockMode};
2 use super::{SemiColonMode, SeqSep, TokenExpectType};
3 use super::pat::{GateOr, PARAM_EXPECTED};
4 use super::diagnostics::Error;
5 use crate::maybe_recover_from_interpolated_ty_qpath;
6
7 use rustc_data_structures::thin_vec::ThinVec;
8 use rustc_errors::{PResult, Applicability};
9 use syntax::ast::{self, DUMMY_NODE_ID, Attribute, AttrStyle, Ident, CaptureBy, BlockCheckMode};
10 use syntax::ast::{Expr, ExprKind, RangeLimits, Label, Movability, IsAsync, Arm, Ty, TyKind};
11 use syntax::ast::{FunctionRetTy, Param, FnDecl, BinOpKind, BinOp, UnOp, Mac, AnonConst, Field, Lit};
12 use syntax::token::{self, Token, TokenKind};
13 use syntax::print::pprust;
14 use syntax::ptr::P;
15 use syntax::util::classify;
16 use syntax::util::literal::LitError;
17 use syntax::util::parser::{AssocOp, Fixity, prec_let_scrutinee_needs_par};
18 use syntax_pos::source_map::{self, Span};
19 use syntax_pos::symbol::{kw, sym, Symbol};
20 use std::mem;
21
22 /// Possibly accepts an `token::Interpolated` expression (a pre-parsed expression
23 /// dropped into the token stream, which happens while parsing the result of
24 /// macro expansion). Placement of these is not as complex as I feared it would
25 /// be. The important thing is to make sure that lookahead doesn't balk at
26 /// `token::Interpolated` tokens.
27 macro_rules! maybe_whole_expr {
28     ($p:expr) => {
29         if let token::Interpolated(nt) = &$p.token.kind {
30             match &**nt {
31                 token::NtExpr(e) | token::NtLiteral(e) => {
32                     let e = e.clone();
33                     $p.bump();
34                     return Ok(e);
35                 }
36                 token::NtPath(path) => {
37                     let path = path.clone();
38                     $p.bump();
39                     return Ok($p.mk_expr(
40                         $p.token.span, ExprKind::Path(None, path), ThinVec::new()
41                     ));
42                 }
43                 token::NtBlock(block) => {
44                     let block = block.clone();
45                     $p.bump();
46                     return Ok($p.mk_expr(
47                         $p.token.span, ExprKind::Block(block, None), ThinVec::new()
48                     ));
49                 }
50                 // N.B., `NtIdent(ident)` is normalized to `Ident` in `fn bump`.
51                 _ => {},
52             };
53         }
54     }
55 }
56
57 #[derive(Debug)]
58 pub(super) enum LhsExpr {
59     NotYetParsed,
60     AttributesParsed(ThinVec<Attribute>),
61     AlreadyParsed(P<Expr>),
62 }
63
64 impl From<Option<ThinVec<Attribute>>> for LhsExpr {
65     /// Converts `Some(attrs)` into `LhsExpr::AttributesParsed(attrs)`
66     /// and `None` into `LhsExpr::NotYetParsed`.
67     ///
68     /// This conversion does not allocate.
69     fn from(o: Option<ThinVec<Attribute>>) -> Self {
70         if let Some(attrs) = o {
71             LhsExpr::AttributesParsed(attrs)
72         } else {
73             LhsExpr::NotYetParsed
74         }
75     }
76 }
77
78 impl From<P<Expr>> for LhsExpr {
79     /// Converts the `expr: P<Expr>` into `LhsExpr::AlreadyParsed(expr)`.
80     ///
81     /// This conversion does not allocate.
82     fn from(expr: P<Expr>) -> Self {
83         LhsExpr::AlreadyParsed(expr)
84     }
85 }
86
87 impl<'a> Parser<'a> {
88     /// Parses an expression.
89     #[inline]
90     pub fn parse_expr(&mut self) -> PResult<'a, P<Expr>> {
91         self.parse_expr_res(Restrictions::empty(), None)
92     }
93
94     fn parse_paren_expr_seq(&mut self) -> PResult<'a, Vec<P<Expr>>> {
95         self.parse_paren_comma_seq(|p| {
96             match p.parse_expr() {
97                 Ok(expr) => Ok(expr),
98                 Err(mut err) => match p.token.kind {
99                     token::Ident(name, false)
100                     if name == kw::Underscore && p.look_ahead(1, |t| {
101                         t == &token::Comma
102                     }) => {
103                         // Special-case handling of `foo(_, _, _)`
104                         err.emit();
105                         let sp = p.token.span;
106                         p.bump();
107                         Ok(p.mk_expr(sp, ExprKind::Err, ThinVec::new()))
108                     }
109                     _ => Err(err),
110                 },
111             }
112         }).map(|(r, _)| r)
113     }
114
115     /// Parses an expression, subject to the given restrictions.
116     #[inline]
117     pub(super) fn parse_expr_res(
118         &mut self,
119         r: Restrictions,
120         already_parsed_attrs: Option<ThinVec<Attribute>>
121     ) -> PResult<'a, P<Expr>> {
122         self.with_res(r, |this| this.parse_assoc_expr(already_parsed_attrs))
123     }
124
125     /// Parses an associative expression.
126     ///
127     /// This parses an expression accounting for associativity and precedence of the operators in
128     /// the expression.
129     #[inline]
130     fn parse_assoc_expr(
131         &mut self,
132         already_parsed_attrs: Option<ThinVec<Attribute>>,
133     ) -> PResult<'a, P<Expr>> {
134         self.parse_assoc_expr_with(0, already_parsed_attrs.into())
135     }
136
137     /// Parses an associative expression with operators of at least `min_prec` precedence.
138     pub(super) fn parse_assoc_expr_with(
139         &mut self,
140         min_prec: usize,
141         lhs: LhsExpr,
142     ) -> PResult<'a, P<Expr>> {
143         let mut lhs = if let LhsExpr::AlreadyParsed(expr) = lhs {
144             expr
145         } else {
146             let attrs = match lhs {
147                 LhsExpr::AttributesParsed(attrs) => Some(attrs),
148                 _ => None,
149             };
150             if [token::DotDot, token::DotDotDot, token::DotDotEq].contains(&self.token.kind) {
151                 return self.parse_prefix_range_expr(attrs);
152             } else {
153                 self.parse_prefix_expr(attrs)?
154             }
155         };
156         let last_type_ascription_set = self.last_type_ascription.is_some();
157
158         if !self.should_continue_as_assoc_expr(&lhs) {
159             self.last_type_ascription = None;
160             return Ok(lhs);
161         }
162
163         self.expected_tokens.push(TokenType::Operator);
164         while let Some(op) = self.check_assoc_op() {
165             // Adjust the span for interpolated LHS to point to the `$lhs` token and not to what
166             // it refers to. Interpolated identifiers are unwrapped early and never show up here
167             // as `PrevTokenKind::Interpolated` so if LHS is a single identifier we always process
168             // it as "interpolated", it doesn't change the answer for non-interpolated idents.
169             let lhs_span = match (self.prev_token_kind, &lhs.kind) {
170                 (PrevTokenKind::Interpolated, _) => self.prev_span,
171                 (PrevTokenKind::Ident, &ExprKind::Path(None, ref path))
172                     if path.segments.len() == 1 => self.prev_span,
173                 _ => lhs.span,
174             };
175
176             let cur_op_span = self.token.span;
177             let restrictions = if op.is_assign_like() {
178                 self.restrictions & Restrictions::NO_STRUCT_LITERAL
179             } else {
180                 self.restrictions
181             };
182             let prec = op.precedence();
183             if prec < min_prec {
184                 break;
185             }
186             // Check for deprecated `...` syntax
187             if self.token == token::DotDotDot && op == AssocOp::DotDotEq {
188                 self.err_dotdotdot_syntax(self.token.span);
189             }
190
191             if self.token == token::LArrow {
192                 self.err_larrow_operator(self.token.span);
193             }
194
195             self.bump();
196             if op.is_comparison() {
197                 if let Some(expr) = self.check_no_chained_comparison(&lhs, &op)? {
198                     return Ok(expr);
199                 }
200             }
201             // Special cases:
202             if op == AssocOp::As {
203                 lhs = self.parse_assoc_op_cast(lhs, lhs_span, ExprKind::Cast)?;
204                 continue
205             } else if op == AssocOp::Colon {
206                 let maybe_path = self.could_ascription_be_path(&lhs.kind);
207                 self.last_type_ascription = Some((self.prev_span, maybe_path));
208
209                 lhs = self.parse_assoc_op_cast(lhs, lhs_span, ExprKind::Type)?;
210                 self.sess.gated_spans.gate(sym::type_ascription, lhs.span);
211                 continue
212             } else if op == AssocOp::DotDot || op == AssocOp::DotDotEq {
213                 // If we didn’t have to handle `x..`/`x..=`, it would be pretty easy to
214                 // generalise it to the Fixity::None code.
215                 //
216                 // We have 2 alternatives here: `x..y`/`x..=y` and `x..`/`x..=` The other
217                 // two variants are handled with `parse_prefix_range_expr` call above.
218                 let rhs = if self.is_at_start_of_range_notation_rhs() {
219                     Some(self.parse_assoc_expr_with(prec + 1, LhsExpr::NotYetParsed)?)
220                 } else {
221                     None
222                 };
223                 let (lhs_span, rhs_span) = (lhs.span, if let Some(ref x) = rhs {
224                     x.span
225                 } else {
226                     cur_op_span
227                 });
228                 let limits = if op == AssocOp::DotDot {
229                     RangeLimits::HalfOpen
230                 } else {
231                     RangeLimits::Closed
232                 };
233
234                 let r = self.mk_range(Some(lhs), rhs, limits)?;
235                 lhs = self.mk_expr(lhs_span.to(rhs_span), r, ThinVec::new());
236                 break
237             }
238
239             let fixity = op.fixity();
240             let prec_adjustment = match fixity {
241                 Fixity::Right => 0,
242                 Fixity::Left => 1,
243                 // We currently have no non-associative operators that are not handled above by
244                 // the special cases. The code is here only for future convenience.
245                 Fixity::None => 1,
246             };
247             let rhs = self.with_res(
248                 restrictions - Restrictions::STMT_EXPR,
249                 |this| this.parse_assoc_expr_with(prec + prec_adjustment, LhsExpr::NotYetParsed)
250             )?;
251
252             // Make sure that the span of the parent node is larger than the span of lhs and rhs,
253             // including the attributes.
254             let lhs_span = lhs
255                 .attrs
256                 .iter()
257                 .filter(|a| a.style == AttrStyle::Outer)
258                 .next()
259                 .map_or(lhs_span, |a| a.span);
260             let span = lhs_span.to(rhs.span);
261             lhs = match op {
262                 AssocOp::Add | AssocOp::Subtract | AssocOp::Multiply | AssocOp::Divide |
263                 AssocOp::Modulus | AssocOp::LAnd | AssocOp::LOr | AssocOp::BitXor |
264                 AssocOp::BitAnd | AssocOp::BitOr | AssocOp::ShiftLeft | AssocOp::ShiftRight |
265                 AssocOp::Equal | AssocOp::Less | AssocOp::LessEqual | AssocOp::NotEqual |
266                 AssocOp::Greater | AssocOp::GreaterEqual => {
267                     let ast_op = op.to_ast_binop().unwrap();
268                     let binary = self.mk_binary(source_map::respan(cur_op_span, ast_op), lhs, rhs);
269                     self.mk_expr(span, binary, ThinVec::new())
270                 }
271                 AssocOp::Assign => self.mk_expr(span, ExprKind::Assign(lhs, rhs), ThinVec::new()),
272                 AssocOp::AssignOp(k) => {
273                     let aop = match k {
274                         token::Plus =>    BinOpKind::Add,
275                         token::Minus =>   BinOpKind::Sub,
276                         token::Star =>    BinOpKind::Mul,
277                         token::Slash =>   BinOpKind::Div,
278                         token::Percent => BinOpKind::Rem,
279                         token::Caret =>   BinOpKind::BitXor,
280                         token::And =>     BinOpKind::BitAnd,
281                         token::Or =>      BinOpKind::BitOr,
282                         token::Shl =>     BinOpKind::Shl,
283                         token::Shr =>     BinOpKind::Shr,
284                     };
285                     let aopexpr = self.mk_assign_op(source_map::respan(cur_op_span, aop), lhs, rhs);
286                     self.mk_expr(span, aopexpr, ThinVec::new())
287                 }
288                 AssocOp::As | AssocOp::Colon | AssocOp::DotDot | AssocOp::DotDotEq => {
289                     self.bug("AssocOp should have been handled by special case")
290                 }
291             };
292
293             if let Fixity::None = fixity { break }
294         }
295         if last_type_ascription_set {
296             self.last_type_ascription = None;
297         }
298         Ok(lhs)
299     }
300
301     fn should_continue_as_assoc_expr(&mut self, lhs: &Expr) -> bool {
302         match (self.expr_is_complete(lhs), self.check_assoc_op()) {
303             // Semi-statement forms are odd:
304             // See https://github.com/rust-lang/rust/issues/29071
305             (true, None) => false,
306             (false, _) => true, // Continue parsing the expression.
307             // An exhaustive check is done in the following block, but these are checked first
308             // because they *are* ambiguous but also reasonable looking incorrect syntax, so we
309             // want to keep their span info to improve diagnostics in these cases in a later stage.
310             (true, Some(AssocOp::Multiply)) | // `{ 42 } *foo = bar;` or `{ 42 } * 3`
311             (true, Some(AssocOp::Subtract)) | // `{ 42 } -5`
312             (true, Some(AssocOp::LAnd)) | // `{ 42 } &&x` (#61475)
313             (true, Some(AssocOp::Add)) // `{ 42 } + 42
314             // If the next token is a keyword, then the tokens above *are* unambiguously incorrect:
315             // `if x { a } else { b } && if y { c } else { d }`
316             if !self.look_ahead(1, |t| t.is_reserved_ident()) => {
317                 // These cases are ambiguous and can't be identified in the parser alone.
318                 let sp = self.sess.source_map().start_point(self.token.span);
319                 self.sess.ambiguous_block_expr_parse.borrow_mut().insert(sp, lhs.span);
320                 false
321             }
322             (true, Some(ref op)) if !op.can_continue_expr_unambiguously() => false,
323             (true, Some(_)) => {
324                 self.error_found_expr_would_be_stmt(lhs);
325                 true
326             }
327         }
328     }
329
330     /// We've found an expression that would be parsed as a statement,
331     /// but the next token implies this should be parsed as an expression.
332     /// For example: `if let Some(x) = x { x } else { 0 } / 2`.
333     fn error_found_expr_would_be_stmt(&self, lhs: &Expr) {
334         let mut err = self.struct_span_err(self.token.span, &format!(
335             "expected expression, found `{}`",
336             pprust::token_to_string(&self.token),
337         ));
338         err.span_label(self.token.span, "expected expression");
339         self.sess.expr_parentheses_needed(&mut err, lhs.span, Some(pprust::expr_to_string(&lhs)));
340         err.emit();
341     }
342
343     /// Possibly translate the current token to an associative operator.
344     /// The method does not advance the current token.
345     ///
346     /// Also performs recovery for `and` / `or` which are mistaken for `&&` and `||` respectively.
347     fn check_assoc_op(&self) -> Option<AssocOp> {
348         match (AssocOp::from_token(&self.token), &self.token.kind) {
349             (op @ Some(_), _) => op,
350             (None, token::Ident(sym::and, false)) => {
351                 self.error_bad_logical_op("and", "&&", "conjunction");
352                 Some(AssocOp::LAnd)
353             }
354             (None, token::Ident(sym::or, false)) => {
355                 self.error_bad_logical_op("or", "||", "disjunction");
356                 Some(AssocOp::LOr)
357             }
358             _ => None,
359         }
360     }
361
362     /// Error on `and` and `or` suggesting `&&` and `||` respectively.
363     fn error_bad_logical_op(&self, bad: &str, good: &str, english: &str) {
364         self.struct_span_err(self.token.span, &format!("`{}` is not a logical operator", bad))
365             .span_suggestion(
366                 self.token.span,
367                 &format!("instead of `{}`, use `{}` to perform logical {}", bad, good, english),
368                 good.to_string(),
369                 Applicability::MachineApplicable,
370             )
371             .note("unlike in e.g., python and PHP, `&&` and `||` are used for logical operators")
372             .emit();
373     }
374
375     /// Checks if this expression is a successfully parsed statement.
376     fn expr_is_complete(&self, e: &Expr) -> bool {
377         self.restrictions.contains(Restrictions::STMT_EXPR) &&
378             !classify::expr_requires_semi_to_be_stmt(e)
379     }
380
381     fn is_at_start_of_range_notation_rhs(&self) -> bool {
382         if self.token.can_begin_expr() {
383             // Parse `for i in 1.. { }` as infinite loop, not as `for i in (1..{})`.
384             if self.token == token::OpenDelim(token::Brace) {
385                 return !self.restrictions.contains(Restrictions::NO_STRUCT_LITERAL);
386             }
387             true
388         } else {
389             false
390         }
391     }
392
393     /// Parses prefix-forms of range notation: `..expr`, `..`, `..=expr`.
394     fn parse_prefix_range_expr(
395         &mut self,
396         already_parsed_attrs: Option<ThinVec<Attribute>>
397     ) -> PResult<'a, P<Expr>> {
398         // Check for deprecated `...` syntax.
399         if self.token == token::DotDotDot {
400             self.err_dotdotdot_syntax(self.token.span);
401         }
402
403         debug_assert!([token::DotDot, token::DotDotDot, token::DotDotEq].contains(&self.token.kind),
404                       "parse_prefix_range_expr: token {:?} is not DotDot/DotDotEq",
405                       self.token);
406         let tok = self.token.clone();
407         let attrs = self.parse_or_use_outer_attributes(already_parsed_attrs)?;
408         let lo = self.token.span;
409         let mut hi = self.token.span;
410         self.bump();
411         let opt_end = if self.is_at_start_of_range_notation_rhs() {
412             // RHS must be parsed with more associativity than the dots.
413             let next_prec = AssocOp::from_token(&tok).unwrap().precedence() + 1;
414             Some(self.parse_assoc_expr_with(next_prec, LhsExpr::NotYetParsed)
415                 .map(|x| {
416                     hi = x.span;
417                     x
418                 })?)
419         } else {
420             None
421         };
422         let limits = if tok == token::DotDot {
423             RangeLimits::HalfOpen
424         } else {
425             RangeLimits::Closed
426         };
427
428         let r = self.mk_range(None, opt_end, limits)?;
429         Ok(self.mk_expr(lo.to(hi), r, attrs))
430     }
431
432     /// Parses a prefix-unary-operator expr.
433     fn parse_prefix_expr(
434         &mut self,
435         already_parsed_attrs: Option<ThinVec<Attribute>>
436     ) -> PResult<'a, P<Expr>> {
437         let attrs = self.parse_or_use_outer_attributes(already_parsed_attrs)?;
438         let lo = self.token.span;
439         // Note: when adding new unary operators, don't forget to adjust TokenKind::can_begin_expr()
440         let (hi, ex) = match self.token.kind {
441             token::Not => {
442                 self.bump();
443                 let e = self.parse_prefix_expr(None);
444                 let (span, e) = self.interpolated_or_expr_span(e)?;
445                 (lo.to(span), self.mk_unary(UnOp::Not, e))
446             }
447             // Suggest `!` for bitwise negation when encountering a `~`
448             token::Tilde => {
449                 self.bump();
450                 let e = self.parse_prefix_expr(None);
451                 let (span, e) = self.interpolated_or_expr_span(e)?;
452                 let span_of_tilde = lo;
453                 self.struct_span_err(span_of_tilde, "`~` cannot be used as a unary operator")
454                     .span_suggestion_short(
455                         span_of_tilde,
456                         "use `!` to perform bitwise not",
457                         "!".to_owned(),
458                         Applicability::MachineApplicable
459                     )
460                     .emit();
461                 (lo.to(span), self.mk_unary(UnOp::Not, e))
462             }
463             token::BinOp(token::Minus) => {
464                 self.bump();
465                 let e = self.parse_prefix_expr(None);
466                 let (span, e) = self.interpolated_or_expr_span(e)?;
467                 (lo.to(span), self.mk_unary(UnOp::Neg, e))
468             }
469             token::BinOp(token::Star) => {
470                 self.bump();
471                 let e = self.parse_prefix_expr(None);
472                 let (span, e) = self.interpolated_or_expr_span(e)?;
473                 (lo.to(span), self.mk_unary(UnOp::Deref, e))
474             }
475             token::BinOp(token::And) | token::AndAnd => {
476                 self.parse_address_of(lo)?
477             }
478             token::Ident(..) if self.token.is_keyword(kw::Box) => {
479                 self.bump();
480                 let e = self.parse_prefix_expr(None);
481                 let (span, e) = self.interpolated_or_expr_span(e)?;
482                 let span = lo.to(span);
483                 self.sess.gated_spans.gate(sym::box_syntax, span);
484                 (span, ExprKind::Box(e))
485             }
486             token::Ident(..) if self.token.is_ident_named(sym::not) => {
487                 // `not` is just an ordinary identifier in Rust-the-language,
488                 // but as `rustc`-the-compiler, we can issue clever diagnostics
489                 // for confused users who really want to say `!`
490                 let token_cannot_continue_expr = |t: &Token| match t.kind {
491                     // These tokens can start an expression after `!`, but
492                     // can't continue an expression after an ident
493                     token::Ident(name, is_raw) => token::ident_can_begin_expr(name, t.span, is_raw),
494                     token::Literal(..) | token::Pound => true,
495                     _ => t.is_whole_expr(),
496                 };
497                 let cannot_continue_expr = self.look_ahead(1, token_cannot_continue_expr);
498                 if cannot_continue_expr {
499                     self.bump();
500                     // Emit the error ...
501                     self.struct_span_err(
502                         self.token.span,
503                         &format!("unexpected {} after identifier",self.this_token_descr())
504                     )
505                     .span_suggestion_short(
506                         // Span the `not` plus trailing whitespace to avoid
507                         // trailing whitespace after the `!` in our suggestion
508                         self.sess.source_map()
509                             .span_until_non_whitespace(lo.to(self.token.span)),
510                         "use `!` to perform logical negation",
511                         "!".to_owned(),
512                         Applicability::MachineApplicable
513                     )
514                     .emit();
515                     // â€”and recover! (just as if we were in the block
516                     // for the `token::Not` arm)
517                     let e = self.parse_prefix_expr(None);
518                     let (span, e) = self.interpolated_or_expr_span(e)?;
519                     (lo.to(span), self.mk_unary(UnOp::Not, e))
520                 } else {
521                     return self.parse_dot_or_call_expr(Some(attrs));
522                 }
523             }
524             _ => { return self.parse_dot_or_call_expr(Some(attrs)); }
525         };
526         return Ok(self.mk_expr(lo.to(hi), ex, attrs));
527     }
528
529     /// Returns the span of expr, if it was not interpolated or the span of the interpolated token.
530     fn interpolated_or_expr_span(
531         &self,
532         expr: PResult<'a, P<Expr>>,
533     ) -> PResult<'a, (Span, P<Expr>)> {
534         expr.map(|e| {
535             if self.prev_token_kind == PrevTokenKind::Interpolated {
536                 (self.prev_span, e)
537             } else {
538                 (e.span, e)
539             }
540         })
541     }
542
543     fn parse_assoc_op_cast(&mut self, lhs: P<Expr>, lhs_span: Span,
544                            expr_kind: fn(P<Expr>, P<Ty>) -> ExprKind)
545                            -> PResult<'a, P<Expr>> {
546         let mk_expr = |this: &mut Self, rhs: P<Ty>| {
547             this.mk_expr(lhs_span.to(rhs.span), expr_kind(lhs, rhs), ThinVec::new())
548         };
549
550         // Save the state of the parser before parsing type normally, in case there is a
551         // LessThan comparison after this cast.
552         let parser_snapshot_before_type = self.clone();
553         match self.parse_ty_no_plus() {
554             Ok(rhs) => {
555                 Ok(mk_expr(self, rhs))
556             }
557             Err(mut type_err) => {
558                 // Rewind to before attempting to parse the type with generics, to recover
559                 // from situations like `x as usize < y` in which we first tried to parse
560                 // `usize < y` as a type with generic arguments.
561                 let parser_snapshot_after_type = self.clone();
562                 mem::replace(self, parser_snapshot_before_type);
563
564                 match self.parse_path(PathStyle::Expr) {
565                     Ok(path) => {
566                         let (op_noun, op_verb) = match self.token.kind {
567                             token::Lt => ("comparison", "comparing"),
568                             token::BinOp(token::Shl) => ("shift", "shifting"),
569                             _ => {
570                                 // We can end up here even without `<` being the next token, for
571                                 // example because `parse_ty_no_plus` returns `Err` on keywords,
572                                 // but `parse_path` returns `Ok` on them due to error recovery.
573                                 // Return original error and parser state.
574                                 mem::replace(self, parser_snapshot_after_type);
575                                 return Err(type_err);
576                             }
577                         };
578
579                         // Successfully parsed the type path leaving a `<` yet to parse.
580                         type_err.cancel();
581
582                         // Report non-fatal diagnostics, keep `x as usize` as an expression
583                         // in AST and continue parsing.
584                         let msg = format!(
585                             "`<` is interpreted as a start of generic arguments for `{}`, not a {}",
586                             pprust::path_to_string(&path),
587                             op_noun,
588                         );
589                         let span_after_type = parser_snapshot_after_type.token.span;
590                         let expr = mk_expr(self, P(Ty {
591                             span: path.span,
592                             kind: TyKind::Path(None, path),
593                             id: DUMMY_NODE_ID,
594                         }));
595
596                         let expr_str = self.span_to_snippet(expr.span)
597                             .unwrap_or_else(|_| pprust::expr_to_string(&expr));
598
599                         self.struct_span_err(self.token.span, &msg)
600                             .span_label(
601                                 self.look_ahead(1, |t| t.span).to(span_after_type),
602                                 "interpreted as generic arguments"
603                             )
604                             .span_label(self.token.span, format!("not interpreted as {}", op_noun))
605                             .span_suggestion(
606                                 expr.span,
607                                 &format!("try {} the cast value", op_verb),
608                                 format!("({})", expr_str),
609                                 Applicability::MachineApplicable,
610                             )
611                             .emit();
612
613                         Ok(expr)
614                     }
615                     Err(mut path_err) => {
616                         // Couldn't parse as a path, return original error and parser state.
617                         path_err.cancel();
618                         mem::replace(self, parser_snapshot_after_type);
619                         Err(type_err)
620                     }
621                 }
622             }
623         }
624     }
625
626     /// Parse `& mut? <expr>` or `& raw [ const | mut ] <expr>`.
627     fn parse_address_of(&mut self, lo: Span) -> PResult<'a, (Span, ExprKind)> {
628         self.expect_and()?;
629         let (k, m) = if self.check_keyword(kw::Raw)
630             && self.look_ahead(1, Token::is_mutability)
631         {
632             let found_raw = self.eat_keyword(kw::Raw);
633             assert!(found_raw);
634             let mutability = self.parse_const_or_mut().unwrap();
635             self.sess.gated_spans.gate(sym::raw_ref_op, lo.to(self.prev_span));
636             (ast::BorrowKind::Raw, mutability)
637         } else {
638             (ast::BorrowKind::Ref, self.parse_mutability())
639         };
640         let e = self.parse_prefix_expr(None);
641         let (span, e) = self.interpolated_or_expr_span(e)?;
642         Ok((lo.to(span), ExprKind::AddrOf(k, m, e)))
643     }
644
645     /// Parses `a.b` or `a(13)` or `a[4]` or just `a`.
646     fn parse_dot_or_call_expr(
647         &mut self,
648         already_parsed_attrs: Option<ThinVec<Attribute>>,
649     ) -> PResult<'a, P<Expr>> {
650         let attrs = self.parse_or_use_outer_attributes(already_parsed_attrs)?;
651
652         let b = self.parse_bottom_expr();
653         let (span, b) = self.interpolated_or_expr_span(b)?;
654         self.parse_dot_or_call_expr_with(b, span, attrs)
655     }
656
657     pub(super) fn parse_dot_or_call_expr_with(
658         &mut self,
659         e0: P<Expr>,
660         lo: Span,
661         mut attrs: ThinVec<Attribute>,
662     ) -> PResult<'a, P<Expr>> {
663         // Stitch the list of outer attributes onto the return value.
664         // A little bit ugly, but the best way given the current code
665         // structure
666         self.parse_dot_or_call_expr_with_(e0, lo).map(|expr|
667             expr.map(|mut expr| {
668                 attrs.extend::<Vec<_>>(expr.attrs.into());
669                 expr.attrs = attrs;
670                 match expr.kind {
671                     ExprKind::If(..) if !expr.attrs.is_empty() => {
672                         // Just point to the first attribute in there...
673                         let span = expr.attrs[0].span;
674                         self.span_err(span, "attributes are not yet allowed on `if` expressions");
675                     }
676                     _ => {}
677                 }
678                 expr
679             })
680         )
681     }
682
683     fn parse_dot_or_call_expr_with_(&mut self, e0: P<Expr>, lo: Span) -> PResult<'a, P<Expr>> {
684         let mut e = e0;
685         let mut hi;
686         loop {
687             // expr?
688             while self.eat(&token::Question) {
689                 let hi = self.prev_span;
690                 e = self.mk_expr(lo.to(hi), ExprKind::Try(e), ThinVec::new());
691             }
692
693             // expr.f
694             if self.eat(&token::Dot) {
695                 match self.token.kind {
696                     token::Ident(..) => {
697                         e = self.parse_dot_suffix(e, lo)?;
698                     }
699                     token::Literal(token::Lit { kind: token::Integer, symbol, suffix }) => {
700                         let span = self.token.span;
701                         self.bump();
702                         let field = ExprKind::Field(e, Ident::new(symbol, span));
703                         e = self.mk_expr(lo.to(span), field, ThinVec::new());
704
705                         self.expect_no_suffix(span, "a tuple index", suffix);
706                     }
707                     token::Literal(token::Lit { kind: token::Float, symbol, .. }) => {
708                       self.bump();
709                       let fstr = symbol.as_str();
710                       let msg = format!("unexpected token: `{}`", symbol);
711                       let mut err = self.diagnostic().struct_span_err(self.prev_span, &msg);
712                       err.span_label(self.prev_span, "unexpected token");
713                       if fstr.chars().all(|x| "0123456789.".contains(x)) {
714                           let float = match fstr.parse::<f64>().ok() {
715                               Some(f) => f,
716                               None => continue,
717                           };
718                           let sugg = pprust::to_string(|s| {
719                               s.popen();
720                               s.print_expr(&e);
721                               s.s.word( ".");
722                               s.print_usize(float.trunc() as usize);
723                               s.pclose();
724                               s.s.word(".");
725                               s.s.word(fstr.splitn(2, ".").last().unwrap().to_string())
726                           });
727                           err.span_suggestion(
728                               lo.to(self.prev_span),
729                               "try parenthesizing the first index",
730                               sugg,
731                               Applicability::MachineApplicable
732                           );
733                       }
734                       return Err(err);
735
736                     }
737                     _ => {
738                         // FIXME Could factor this out into non_fatal_unexpected or something.
739                         let actual = self.this_token_to_string();
740                         self.span_err(self.token.span, &format!("unexpected token: `{}`", actual));
741                     }
742                 }
743                 continue;
744             }
745             if self.expr_is_complete(&e) { break; }
746             match self.token.kind {
747                 // expr(...)
748                 token::OpenDelim(token::Paren) => {
749                     let seq = self.parse_paren_expr_seq().map(|es| {
750                         let nd = self.mk_call(e, es);
751                         let hi = self.prev_span;
752                         self.mk_expr(lo.to(hi), nd, ThinVec::new())
753                     });
754                     e = self.recover_seq_parse_error(token::Paren, lo, seq);
755                 }
756
757                 // expr[...]
758                 // Could be either an index expression or a slicing expression.
759                 token::OpenDelim(token::Bracket) => {
760                     self.bump();
761                     let ix = self.parse_expr()?;
762                     hi = self.token.span;
763                     self.expect(&token::CloseDelim(token::Bracket))?;
764                     let index = self.mk_index(e, ix);
765                     e = self.mk_expr(lo.to(hi), index, ThinVec::new())
766                 }
767                 _ => return Ok(e)
768             }
769         }
770         return Ok(e);
771     }
772
773     /// Assuming we have just parsed `.`, continue parsing into an expression.
774     fn parse_dot_suffix(&mut self, self_arg: P<Expr>, lo: Span) -> PResult<'a, P<Expr>> {
775         if self.token.span.rust_2018() && self.eat_keyword(kw::Await) {
776             return self.mk_await_expr(self_arg, lo);
777         }
778
779         let segment = self.parse_path_segment(PathStyle::Expr)?;
780         self.check_trailing_angle_brackets(&segment, token::OpenDelim(token::Paren));
781
782         Ok(match self.token.kind {
783             token::OpenDelim(token::Paren) => {
784                 // Method call `expr.f()`
785                 let mut args = self.parse_paren_expr_seq()?;
786                 args.insert(0, self_arg);
787
788                 let span = lo.to(self.prev_span);
789                 self.mk_expr(span, ExprKind::MethodCall(segment, args), ThinVec::new())
790             }
791             _ => {
792                 // Field access `expr.f`
793                 if let Some(args) = segment.args {
794                     self.span_err(args.span(),
795                                   "field expressions may not have generic arguments");
796                 }
797
798                 let span = lo.to(self.prev_span);
799                 self.mk_expr(span, ExprKind::Field(self_arg, segment.ident), ThinVec::new())
800             }
801         })
802     }
803
804     /// At the bottom (top?) of the precedence hierarchy,
805     /// Parses things like parenthesized exprs, macros, `return`, etc.
806     ///
807     /// N.B., this does not parse outer attributes, and is private because it only works
808     /// correctly if called from `parse_dot_or_call_expr()`.
809     fn parse_bottom_expr(&mut self) -> PResult<'a, P<Expr>> {
810         maybe_recover_from_interpolated_ty_qpath!(self, true);
811         maybe_whole_expr!(self);
812
813         // Outer attributes are already parsed and will be
814         // added to the return value after the fact.
815         //
816         // Therefore, prevent sub-parser from parsing
817         // attributes by giving them a empty "already-parsed" list.
818         let mut attrs = ThinVec::new();
819
820         let lo = self.token.span;
821         let mut hi = self.token.span;
822
823         let ex: ExprKind;
824
825         macro_rules! parse_lit {
826             () => {
827                 match self.parse_opt_lit() {
828                     Some(literal) => {
829                         hi = self.prev_span;
830                         ex = ExprKind::Lit(literal);
831                     }
832                     None => {
833                         return Err(self.expected_expression_found());
834                     }
835                 }
836             }
837         }
838
839         // Note: when adding new syntax here, don't forget to adjust `TokenKind::can_begin_expr()`.
840         match self.token.kind {
841             // This match arm is a special-case of the `_` match arm below and
842             // could be removed without changing functionality, but it's faster
843             // to have it here, especially for programs with large constants.
844             token::Literal(_) => {
845                 parse_lit!()
846             }
847             token::OpenDelim(token::Paren) => {
848                 self.bump();
849
850                 attrs.extend(self.parse_inner_attributes()?);
851
852                 // `(e)` is parenthesized `e`.
853                 // `(e,)` is a tuple with only one field, `e`.
854                 let mut es = vec![];
855                 let mut trailing_comma = false;
856                 let mut recovered = false;
857                 while self.token != token::CloseDelim(token::Paren) {
858                     es.push(match self.parse_expr() {
859                         Ok(es) => es,
860                         Err(mut err) => {
861                             // Recover from parse error in tuple list.
862                             match self.token.kind {
863                                 token::Ident(name, false)
864                                 if name == kw::Underscore && self.look_ahead(1, |t| {
865                                     t == &token::Comma
866                                 }) => {
867                                     // Special-case handling of `Foo<(_, _, _)>`
868                                     err.emit();
869                                     let sp = self.token.span;
870                                     self.bump();
871                                     self.mk_expr(sp, ExprKind::Err, ThinVec::new())
872                                 }
873                                 _ => return Ok(
874                                     self.recover_seq_parse_error(token::Paren, lo, Err(err)),
875                                 ),
876                             }
877                         }
878                     });
879                     recovered = self.expect_one_of(
880                         &[],
881                         &[token::Comma, token::CloseDelim(token::Paren)],
882                     )?;
883                     if self.eat(&token::Comma) {
884                         trailing_comma = true;
885                     } else {
886                         trailing_comma = false;
887                         break;
888                     }
889                 }
890                 if !recovered {
891                     self.bump();
892                 }
893
894                 hi = self.prev_span;
895                 ex = if es.len() == 1 && !trailing_comma {
896                     ExprKind::Paren(es.into_iter().nth(0).unwrap())
897                 } else {
898                     ExprKind::Tup(es)
899                 };
900             }
901             token::OpenDelim(token::Brace) => {
902                 return self.parse_block_expr(None, lo, BlockCheckMode::Default, attrs);
903             }
904             token::BinOp(token::Or) | token::OrOr => {
905                 return self.parse_closure_expr(attrs);
906             }
907             token::OpenDelim(token::Bracket) => {
908                 self.bump();
909
910                 attrs.extend(self.parse_inner_attributes()?);
911
912                 if self.eat(&token::CloseDelim(token::Bracket)) {
913                     // Empty vector
914                     ex = ExprKind::Array(Vec::new());
915                 } else {
916                     // Non-empty vector
917                     let first_expr = self.parse_expr()?;
918                     if self.eat(&token::Semi) {
919                         // Repeating array syntax: `[ 0; 512 ]`
920                         let count = AnonConst {
921                             id: DUMMY_NODE_ID,
922                             value: self.parse_expr()?,
923                         };
924                         self.expect(&token::CloseDelim(token::Bracket))?;
925                         ex = ExprKind::Repeat(first_expr, count);
926                     } else if self.eat(&token::Comma) {
927                         // Vector with two or more elements
928                         let remaining_exprs = self.parse_seq_to_end(
929                             &token::CloseDelim(token::Bracket),
930                             SeqSep::trailing_allowed(token::Comma),
931                             |p| Ok(p.parse_expr()?)
932                         )?;
933                         let mut exprs = vec![first_expr];
934                         exprs.extend(remaining_exprs);
935                         ex = ExprKind::Array(exprs);
936                     } else {
937                         // Vector with one element
938                         self.expect(&token::CloseDelim(token::Bracket))?;
939                         ex = ExprKind::Array(vec![first_expr]);
940                     }
941                 }
942                 hi = self.prev_span;
943             }
944             _ => {
945                 if self.eat_lt() {
946                     let (qself, path) = self.parse_qpath(PathStyle::Expr)?;
947                     hi = path.span;
948                     return Ok(self.mk_expr(lo.to(hi), ExprKind::Path(Some(qself), path), attrs));
949                 }
950                 if self.token.is_path_start() {
951                     let path = self.parse_path(PathStyle::Expr)?;
952
953                     // `!`, as an operator, is prefix, so we know this isn't that.
954                     if self.eat(&token::Not) {
955                         // MACRO INVOCATION expression
956                         let args = self.parse_mac_args()?;
957                         hi = self.prev_span;
958                         ex = ExprKind::Mac(Mac {
959                             path,
960                             args,
961                             prior_type_ascription: self.last_type_ascription,
962                         });
963                     } else if self.check(&token::OpenDelim(token::Brace)) {
964                         if let Some(expr) = self.maybe_parse_struct_expr(lo, &path, &attrs) {
965                             return expr;
966                         } else {
967                             hi = path.span;
968                             ex = ExprKind::Path(None, path);
969                         }
970                     } else {
971                         hi = path.span;
972                         ex = ExprKind::Path(None, path);
973                     }
974
975                     let expr = self.mk_expr(lo.to(hi), ex, attrs);
976                     return self.maybe_recover_from_bad_qpath(expr, true);
977                 }
978                 if self.check_keyword(kw::Move) || self.check_keyword(kw::Static) {
979                     return self.parse_closure_expr(attrs);
980                 }
981                 if self.eat_keyword(kw::If) {
982                     return self.parse_if_expr(attrs);
983                 }
984                 if self.eat_keyword(kw::For) {
985                     let lo = self.prev_span;
986                     return self.parse_for_expr(None, lo, attrs);
987                 }
988                 if self.eat_keyword(kw::While) {
989                     let lo = self.prev_span;
990                     return self.parse_while_expr(None, lo, attrs);
991                 }
992                 if let Some(label) = self.eat_label() {
993                     let lo = label.ident.span;
994                     self.expect(&token::Colon)?;
995                     if self.eat_keyword(kw::While) {
996                         return self.parse_while_expr(Some(label), lo, attrs)
997                     }
998                     if self.eat_keyword(kw::For) {
999                         return self.parse_for_expr(Some(label), lo, attrs)
1000                     }
1001                     if self.eat_keyword(kw::Loop) {
1002                         return self.parse_loop_expr(Some(label), lo, attrs)
1003                     }
1004                     if self.token == token::OpenDelim(token::Brace) {
1005                         return self.parse_block_expr(Some(label),
1006                                                      lo,
1007                                                      BlockCheckMode::Default,
1008                                                      attrs);
1009                     }
1010                     let msg = "expected `while`, `for`, `loop` or `{` after a label";
1011                     let mut err = self.fatal(msg);
1012                     err.span_label(self.token.span, msg);
1013                     return Err(err);
1014                 }
1015                 if self.eat_keyword(kw::Loop) {
1016                     let lo = self.prev_span;
1017                     return self.parse_loop_expr(None, lo, attrs);
1018                 }
1019                 if self.eat_keyword(kw::Continue) {
1020                     let label = self.eat_label();
1021                     let ex = ExprKind::Continue(label);
1022                     let hi = self.prev_span;
1023                     return Ok(self.mk_expr(lo.to(hi), ex, attrs));
1024                 }
1025                 if self.eat_keyword(kw::Match) {
1026                     let match_sp = self.prev_span;
1027                     return self.parse_match_expr(attrs).map_err(|mut err| {
1028                         err.span_label(match_sp, "while parsing this match expression");
1029                         err
1030                     });
1031                 }
1032                 if self.eat_keyword(kw::Unsafe) {
1033                     return self.parse_block_expr(
1034                         None,
1035                         lo,
1036                         BlockCheckMode::Unsafe(ast::UserProvided),
1037                         attrs);
1038                 }
1039                 if self.is_do_catch_block() {
1040                     let mut db = self.fatal("found removed `do catch` syntax");
1041                     db.help("following RFC #2388, the new non-placeholder syntax is `try`");
1042                     return Err(db);
1043                 }
1044                 if self.is_try_block() {
1045                     let lo = self.token.span;
1046                     assert!(self.eat_keyword(kw::Try));
1047                     return self.parse_try_block(lo, attrs);
1048                 }
1049
1050                 // `Span::rust_2018()` is somewhat expensive; don't get it repeatedly.
1051                 let is_span_rust_2018 = self.token.span.rust_2018();
1052                 if is_span_rust_2018 && self.check_keyword(kw::Async) {
1053                     return if self.is_async_block() { // Check for `async {` and `async move {`.
1054                         self.parse_async_block(attrs)
1055                     } else {
1056                         self.parse_closure_expr(attrs)
1057                     };
1058                 }
1059                 if self.eat_keyword(kw::Return) {
1060                     if self.token.can_begin_expr() {
1061                         let e = self.parse_expr()?;
1062                         hi = e.span;
1063                         ex = ExprKind::Ret(Some(e));
1064                     } else {
1065                         ex = ExprKind::Ret(None);
1066                     }
1067                 } else if self.eat_keyword(kw::Break) {
1068                     let label = self.eat_label();
1069                     let e = if self.token.can_begin_expr()
1070                                && !(self.token == token::OpenDelim(token::Brace)
1071                                     && self.restrictions.contains(
1072                                            Restrictions::NO_STRUCT_LITERAL)) {
1073                         Some(self.parse_expr()?)
1074                     } else {
1075                         None
1076                     };
1077                     ex = ExprKind::Break(label, e);
1078                     hi = self.prev_span;
1079                 } else if self.eat_keyword(kw::Yield) {
1080                     if self.token.can_begin_expr() {
1081                         let e = self.parse_expr()?;
1082                         hi = e.span;
1083                         ex = ExprKind::Yield(Some(e));
1084                     } else {
1085                         ex = ExprKind::Yield(None);
1086                     }
1087
1088                     let span = lo.to(hi);
1089                     self.sess.gated_spans.gate(sym::generators, span);
1090                 } else if self.eat_keyword(kw::Let) {
1091                     return self.parse_let_expr(attrs);
1092                 } else if is_span_rust_2018 && self.eat_keyword(kw::Await) {
1093                     let (await_hi, e_kind) = self.parse_incorrect_await_syntax(lo, self.prev_span)?;
1094                     hi = await_hi;
1095                     ex = e_kind;
1096                 } else {
1097                     if !self.unclosed_delims.is_empty() && self.check(&token::Semi) {
1098                         // Don't complain about bare semicolons after unclosed braces
1099                         // recovery in order to keep the error count down. Fixing the
1100                         // delimiters will possibly also fix the bare semicolon found in
1101                         // expression context. For example, silence the following error:
1102                         //
1103                         //     error: expected expression, found `;`
1104                         //      --> file.rs:2:13
1105                         //       |
1106                         //     2 |     foo(bar(;
1107                         //       |             ^ expected expression
1108                         self.bump();
1109                         return Ok(self.mk_expr(self.token.span, ExprKind::Err, ThinVec::new()));
1110                     }
1111                     parse_lit!()
1112                 }
1113             }
1114         }
1115
1116         let expr = self.mk_expr(lo.to(hi), ex, attrs);
1117         self.maybe_recover_from_bad_qpath(expr, true)
1118     }
1119
1120     /// Returns a string literal if the next token is a string literal.
1121     /// In case of error returns `Some(lit)` if the next token is a literal with a wrong kind,
1122     /// and returns `None` if the next token is not literal at all.
1123     pub fn parse_str_lit(&mut self) -> Result<ast::StrLit, Option<Lit>> {
1124         match self.parse_opt_lit() {
1125             Some(lit) => match lit.kind {
1126                 ast::LitKind::Str(symbol_unescaped, style) => Ok(ast::StrLit {
1127                     style,
1128                     symbol: lit.token.symbol,
1129                     suffix: lit.token.suffix,
1130                     span: lit.span,
1131                     symbol_unescaped,
1132                 }),
1133                 _ => Err(Some(lit)),
1134             }
1135             None => Err(None),
1136         }
1137     }
1138
1139     pub(super) fn parse_lit(&mut self) -> PResult<'a, Lit> {
1140         self.parse_opt_lit().ok_or_else(|| {
1141             let msg = format!("unexpected token: {}", self.this_token_descr());
1142             self.span_fatal(self.token.span, &msg)
1143         })
1144     }
1145
1146     /// Matches `lit = true | false | token_lit`.
1147     /// Returns `None` if the next token is not a literal.
1148     pub(super) fn parse_opt_lit(&mut self) -> Option<Lit> {
1149         let mut recovered = None;
1150         if self.token == token::Dot {
1151             // Attempt to recover `.4` as `0.4`. We don't currently have any syntax where
1152             // dot would follow an optional literal, so we do this unconditionally.
1153             recovered = self.look_ahead(1, |next_token| {
1154                 if let token::Literal(token::Lit { kind: token::Integer, symbol, suffix })
1155                         = next_token.kind {
1156                     if self.token.span.hi() == next_token.span.lo() {
1157                         let s = String::from("0.") + &symbol.as_str();
1158                         let kind = TokenKind::lit(token::Float, Symbol::intern(&s), suffix);
1159                         return Some(Token::new(kind, self.token.span.to(next_token.span)));
1160                     }
1161                 }
1162                 None
1163             });
1164             if let Some(token) = &recovered {
1165                 self.bump();
1166                 self.struct_span_err(token.span, "float literals must have an integer part")
1167                     .span_suggestion(
1168                         token.span,
1169                         "must have an integer part",
1170                         pprust::token_to_string(token),
1171                         Applicability::MachineApplicable,
1172                     )
1173                     .emit();
1174             }
1175         }
1176
1177         let token = recovered.as_ref().unwrap_or(&self.token);
1178         match Lit::from_token(token) {
1179             Ok(lit) => {
1180                 self.bump();
1181                 Some(lit)
1182             }
1183             Err(LitError::NotLiteral) => {
1184                 None
1185             }
1186             Err(err) => {
1187                 let span = token.span;
1188                 let lit = match token.kind {
1189                     token::Literal(lit) => lit,
1190                     _ => unreachable!(),
1191                 };
1192                 self.bump();
1193                 self.report_lit_error(err, lit, span);
1194                 // Pack possible quotes and prefixes from the original literal into
1195                 // the error literal's symbol so they can be pretty-printed faithfully.
1196                 let suffixless_lit = token::Lit::new(lit.kind, lit.symbol, None);
1197                 let symbol = Symbol::intern(&suffixless_lit.to_string());
1198                 let lit = token::Lit::new(token::Err, symbol, lit.suffix);
1199                 Some(Lit::from_lit_token(lit, span).unwrap_or_else(|_| unreachable!()))
1200             }
1201         }
1202     }
1203
1204     fn report_lit_error(&self, err: LitError, lit: token::Lit, span: Span) {
1205         // Checks if `s` looks like i32 or u1234 etc.
1206         fn looks_like_width_suffix(first_chars: &[char], s: &str) -> bool {
1207             s.len() > 1
1208             && s.starts_with(first_chars)
1209             && s[1..].chars().all(|c| c.is_ascii_digit())
1210         }
1211
1212         let token::Lit { kind, suffix, .. } = lit;
1213         match err {
1214             // `NotLiteral` is not an error by itself, so we don't report
1215             // it and give the parser opportunity to try something else.
1216             LitError::NotLiteral => {}
1217             // `LexerError` *is* an error, but it was already reported
1218             // by lexer, so here we don't report it the second time.
1219             LitError::LexerError => {}
1220             LitError::InvalidSuffix => {
1221                 self.expect_no_suffix(
1222                     span,
1223                     &format!("{} {} literal", kind.article(), kind.descr()),
1224                     suffix,
1225                 );
1226             }
1227             LitError::InvalidIntSuffix => {
1228                 let suf = suffix.expect("suffix error with no suffix").as_str();
1229                 if looks_like_width_suffix(&['i', 'u'], &suf) {
1230                     // If it looks like a width, try to be helpful.
1231                     let msg = format!("invalid width `{}` for integer literal", &suf[1..]);
1232                     self.struct_span_err(span, &msg)
1233                         .help("valid widths are 8, 16, 32, 64 and 128")
1234                         .emit();
1235                 } else {
1236                     let msg = format!("invalid suffix `{}` for integer literal", suf);
1237                     self.struct_span_err(span, &msg)
1238                         .span_label(span, format!("invalid suffix `{}`", suf))
1239                         .help("the suffix must be one of the integral types (`u32`, `isize`, etc)")
1240                         .emit();
1241                 }
1242             }
1243             LitError::InvalidFloatSuffix => {
1244                 let suf = suffix.expect("suffix error with no suffix").as_str();
1245                 if looks_like_width_suffix(&['f'], &suf) {
1246                     // If it looks like a width, try to be helpful.
1247                     let msg = format!("invalid width `{}` for float literal", &suf[1..]);
1248                     self.struct_span_err(span, &msg)
1249                         .help("valid widths are 32 and 64")
1250                         .emit();
1251                 } else {
1252                     let msg = format!("invalid suffix `{}` for float literal", suf);
1253                     self.struct_span_err(span, &msg)
1254                         .span_label(span, format!("invalid suffix `{}`", suf))
1255                         .help("valid suffixes are `f32` and `f64`")
1256                         .emit();
1257                 }
1258             }
1259             LitError::NonDecimalFloat(base) => {
1260                 let descr = match base {
1261                     16 => "hexadecimal",
1262                     8 => "octal",
1263                     2 => "binary",
1264                     _ => unreachable!(),
1265                 };
1266                 self.struct_span_err(span, &format!("{} float literal is not supported", descr))
1267                     .span_label(span, "not supported")
1268                     .emit();
1269             }
1270             LitError::IntTooLarge => {
1271                 self.struct_span_err(span, "integer literal is too large")
1272                     .emit();
1273             }
1274         }
1275     }
1276
1277     pub(super) fn expect_no_suffix(&self, sp: Span, kind: &str, suffix: Option<Symbol>) {
1278         if let Some(suf) = suffix {
1279             let mut err = if kind == "a tuple index"
1280                 && [sym::i32, sym::u32, sym::isize, sym::usize].contains(&suf)
1281             {
1282                 // #59553: warn instead of reject out of hand to allow the fix to percolate
1283                 // through the ecosystem when people fix their macros
1284                 let mut err = self.sess.span_diagnostic.struct_span_warn(
1285                     sp,
1286                     &format!("suffixes on {} are invalid", kind),
1287                 );
1288                 err.note(&format!(
1289                     "`{}` is *temporarily* accepted on tuple index fields as it was \
1290                         incorrectly accepted on stable for a few releases",
1291                     suf,
1292                 ));
1293                 err.help(
1294                     "on proc macros, you'll want to use `syn::Index::from` or \
1295                         `proc_macro::Literal::*_unsuffixed` for code that will desugar \
1296                         to tuple field access",
1297                 );
1298                 err.note(
1299                     "for more context, see https://github.com/rust-lang/rust/issues/60210",
1300                 );
1301                 err
1302             } else {
1303                 self.struct_span_err(sp, &format!("suffixes on {} are invalid", kind))
1304             };
1305             err.span_label(sp, format!("invalid suffix `{}`", suf));
1306             err.emit();
1307         }
1308     }
1309
1310     /// Matches `'-' lit | lit` (cf. `ast_validation::AstValidator::check_expr_within_pat`).
1311     pub fn parse_literal_maybe_minus(&mut self) -> PResult<'a, P<Expr>> {
1312         maybe_whole_expr!(self);
1313
1314         let minus_lo = self.token.span;
1315         let minus_present = self.eat(&token::BinOp(token::Minus));
1316         let lo = self.token.span;
1317         let literal = self.parse_lit()?;
1318         let hi = self.prev_span;
1319         let expr = self.mk_expr(lo.to(hi), ExprKind::Lit(literal), ThinVec::new());
1320
1321         if minus_present {
1322             let minus_hi = self.prev_span;
1323             let unary = self.mk_unary(UnOp::Neg, expr);
1324             Ok(self.mk_expr(minus_lo.to(minus_hi), unary, ThinVec::new()))
1325         } else {
1326             Ok(expr)
1327         }
1328     }
1329
1330     /// Parses a block or unsafe block.
1331     pub(super) fn parse_block_expr(
1332         &mut self,
1333         opt_label: Option<Label>,
1334         lo: Span,
1335         blk_mode: BlockCheckMode,
1336         outer_attrs: ThinVec<Attribute>,
1337     ) -> PResult<'a, P<Expr>> {
1338         if let Some(label) = opt_label {
1339             self.sess.gated_spans.gate(sym::label_break_value, label.ident.span);
1340         }
1341
1342         self.expect(&token::OpenDelim(token::Brace))?;
1343
1344         let mut attrs = outer_attrs;
1345         attrs.extend(self.parse_inner_attributes()?);
1346
1347         let blk = self.parse_block_tail(lo, blk_mode)?;
1348         Ok(self.mk_expr(blk.span, ExprKind::Block(blk, opt_label), attrs))
1349     }
1350
1351     /// Parses a closure expression (e.g., `move |args| expr`).
1352     fn parse_closure_expr(&mut self, attrs: ThinVec<Attribute>) -> PResult<'a, P<Expr>> {
1353         let lo = self.token.span;
1354
1355         let movability = if self.eat_keyword(kw::Static) {
1356             Movability::Static
1357         } else {
1358             Movability::Movable
1359         };
1360
1361         let asyncness = if self.token.span.rust_2018() {
1362             self.parse_asyncness()
1363         } else {
1364             IsAsync::NotAsync
1365         };
1366         if asyncness.is_async() {
1367             // Feature-gate `async ||` closures.
1368             self.sess.gated_spans.gate(sym::async_closure, self.prev_span);
1369         }
1370
1371         let capture_clause = self.parse_capture_clause();
1372         let decl = self.parse_fn_block_decl()?;
1373         let decl_hi = self.prev_span;
1374         let body = match decl.output {
1375             FunctionRetTy::Default(_) => {
1376                 let restrictions = self.restrictions - Restrictions::STMT_EXPR;
1377                 self.parse_expr_res(restrictions, None)?
1378             },
1379             _ => {
1380                 // If an explicit return type is given, require a block to appear (RFC 968).
1381                 let body_lo = self.token.span;
1382                 self.parse_block_expr(None, body_lo, BlockCheckMode::Default, ThinVec::new())?
1383             }
1384         };
1385
1386         Ok(self.mk_expr(
1387             lo.to(body.span),
1388             ExprKind::Closure(capture_clause, asyncness, movability, decl, body, lo.to(decl_hi)),
1389             attrs))
1390     }
1391
1392     /// Parses an optional `move` prefix to a closure lke construct.
1393     fn parse_capture_clause(&mut self) -> CaptureBy {
1394         if self.eat_keyword(kw::Move) {
1395             CaptureBy::Value
1396         } else {
1397             CaptureBy::Ref
1398         }
1399     }
1400
1401     /// Parses the `|arg, arg|` header of a closure.
1402     fn parse_fn_block_decl(&mut self) -> PResult<'a, P<FnDecl>> {
1403         let inputs_captures = {
1404             if self.eat(&token::OrOr) {
1405                 Vec::new()
1406             } else {
1407                 self.expect(&token::BinOp(token::Or))?;
1408                 let args = self.parse_seq_to_before_tokens(
1409                     &[&token::BinOp(token::Or), &token::OrOr],
1410                     SeqSep::trailing_allowed(token::Comma),
1411                     TokenExpectType::NoExpect,
1412                     |p| p.parse_fn_block_param()
1413                 )?.0;
1414                 self.expect_or()?;
1415                 args
1416             }
1417         };
1418         let output = self.parse_ret_ty(true, true)?;
1419
1420         Ok(P(FnDecl {
1421             inputs: inputs_captures,
1422             output,
1423         }))
1424     }
1425
1426     /// Parses a parameter in a closure header (e.g., `|arg, arg|`).
1427     fn parse_fn_block_param(&mut self) -> PResult<'a, Param> {
1428         let lo = self.token.span;
1429         let attrs = self.parse_outer_attributes()?;
1430         let pat = self.parse_pat(PARAM_EXPECTED)?;
1431         let t = if self.eat(&token::Colon) {
1432             self.parse_ty()?
1433         } else {
1434             P(Ty {
1435                 id: DUMMY_NODE_ID,
1436                 kind: TyKind::Infer,
1437                 span: self.prev_span,
1438             })
1439         };
1440         let span = lo.to(self.token.span);
1441         Ok(Param {
1442             attrs: attrs.into(),
1443             ty: t,
1444             pat,
1445             span,
1446             id: DUMMY_NODE_ID,
1447             is_placeholder: false,
1448         })
1449     }
1450
1451     /// Parses an `if` expression (`if` token already eaten).
1452     fn parse_if_expr(&mut self, attrs: ThinVec<Attribute>) -> PResult<'a, P<Expr>> {
1453         let lo = self.prev_span;
1454         let cond = self.parse_cond_expr()?;
1455
1456         // Verify that the parsed `if` condition makes sense as a condition. If it is a block, then
1457         // verify that the last statement is either an implicit return (no `;`) or an explicit
1458         // return. This won't catch blocks with an explicit `return`, but that would be caught by
1459         // the dead code lint.
1460         if self.eat_keyword(kw::Else) || !cond.returns() {
1461             let sp = self.sess.source_map().next_point(lo);
1462             let mut err = self.diagnostic()
1463                 .struct_span_err(sp, "missing condition for `if` expression");
1464             err.span_label(sp, "expected if condition here");
1465             return Err(err)
1466         }
1467         let not_block = self.token != token::OpenDelim(token::Brace);
1468         let thn = self.parse_block().map_err(|mut err| {
1469             if not_block {
1470                 err.span_label(lo, "this `if` statement has a condition, but no block");
1471             }
1472             err
1473         })?;
1474         let mut els: Option<P<Expr>> = None;
1475         let mut hi = thn.span;
1476         if self.eat_keyword(kw::Else) {
1477             let elexpr = self.parse_else_expr()?;
1478             hi = elexpr.span;
1479             els = Some(elexpr);
1480         }
1481         Ok(self.mk_expr(lo.to(hi), ExprKind::If(cond, thn, els), attrs))
1482     }
1483
1484     /// Parses the condition of a `if` or `while` expression.
1485     fn parse_cond_expr(&mut self) -> PResult<'a, P<Expr>> {
1486         let cond = self.parse_expr_res(Restrictions::NO_STRUCT_LITERAL, None)?;
1487
1488         if let ExprKind::Let(..) = cond.kind {
1489             // Remove the last feature gating of a `let` expression since it's stable.
1490             self.sess.gated_spans.ungate_last(sym::let_chains, cond.span);
1491         }
1492
1493         Ok(cond)
1494     }
1495
1496     /// Parses a `let $pat = $expr` pseudo-expression.
1497     /// The `let` token has already been eaten.
1498     fn parse_let_expr(&mut self, attrs: ThinVec<Attribute>) -> PResult<'a, P<Expr>> {
1499         let lo = self.prev_span;
1500         let pat = self.parse_top_pat(GateOr::No)?;
1501         self.expect(&token::Eq)?;
1502         let expr = self.with_res(
1503             Restrictions::NO_STRUCT_LITERAL,
1504             |this| this.parse_assoc_expr_with(1 + prec_let_scrutinee_needs_par(), None.into())
1505         )?;
1506         let span = lo.to(expr.span);
1507         self.sess.gated_spans.gate(sym::let_chains, span);
1508         Ok(self.mk_expr(span, ExprKind::Let(pat, expr), attrs))
1509     }
1510
1511     /// Parses an `else { ... }` expression (`else` token already eaten).
1512     fn parse_else_expr(&mut self) -> PResult<'a, P<Expr>> {
1513         if self.eat_keyword(kw::If) {
1514             return self.parse_if_expr(ThinVec::new());
1515         } else {
1516             let blk = self.parse_block()?;
1517             return Ok(self.mk_expr(blk.span, ExprKind::Block(blk, None), ThinVec::new()));
1518         }
1519     }
1520
1521     /// Parses a `for ... in` expression (`for` token already eaten).
1522     fn parse_for_expr(
1523         &mut self,
1524         opt_label: Option<Label>,
1525         span_lo: Span,
1526         mut attrs: ThinVec<Attribute>
1527     ) -> PResult<'a, P<Expr>> {
1528         // Parse: `for <src_pat> in <src_expr> <src_loop_block>`
1529
1530         // Record whether we are about to parse `for (`.
1531         // This is used below for recovery in case of `for ( $stuff ) $block`
1532         // in which case we will suggest `for $stuff $block`.
1533         let begin_paren = match self.token.kind {
1534             token::OpenDelim(token::Paren) => Some(self.token.span),
1535             _ => None,
1536         };
1537
1538         let pat = self.parse_top_pat(GateOr::Yes)?;
1539         if !self.eat_keyword(kw::In) {
1540             let in_span = self.prev_span.between(self.token.span);
1541             self.struct_span_err(in_span, "missing `in` in `for` loop")
1542                 .span_suggestion_short(
1543                     in_span,
1544                     "try adding `in` here", " in ".into(),
1545                     // has been misleading, at least in the past (closed Issue #48492)
1546                     Applicability::MaybeIncorrect
1547                 )
1548                 .emit();
1549         }
1550         let in_span = self.prev_span;
1551         self.check_for_for_in_in_typo(in_span);
1552         let expr = self.parse_expr_res(Restrictions::NO_STRUCT_LITERAL, None)?;
1553
1554         let pat = self.recover_parens_around_for_head(pat, &expr, begin_paren);
1555
1556         let (iattrs, loop_block) = self.parse_inner_attrs_and_block()?;
1557         attrs.extend(iattrs);
1558
1559         let hi = self.prev_span;
1560         Ok(self.mk_expr(span_lo.to(hi), ExprKind::ForLoop(pat, expr, loop_block, opt_label), attrs))
1561     }
1562
1563     /// Parses a `while` or `while let` expression (`while` token already eaten).
1564     fn parse_while_expr(
1565         &mut self,
1566         opt_label: Option<Label>,
1567         span_lo: Span,
1568         mut attrs: ThinVec<Attribute>
1569     ) -> PResult<'a, P<Expr>> {
1570         let cond = self.parse_cond_expr()?;
1571         let (iattrs, body) = self.parse_inner_attrs_and_block()?;
1572         attrs.extend(iattrs);
1573         let span = span_lo.to(body.span);
1574         Ok(self.mk_expr(span, ExprKind::While(cond, body, opt_label), attrs))
1575     }
1576
1577     /// Parses `loop { ... }` (`loop` token already eaten).
1578     fn parse_loop_expr(
1579         &mut self,
1580         opt_label: Option<Label>,
1581         span_lo: Span,
1582         mut attrs: ThinVec<Attribute>
1583     ) -> PResult<'a, P<Expr>> {
1584         let (iattrs, body) = self.parse_inner_attrs_and_block()?;
1585         attrs.extend(iattrs);
1586         let span = span_lo.to(body.span);
1587         Ok(self.mk_expr(span, ExprKind::Loop(body, opt_label), attrs))
1588     }
1589
1590     fn eat_label(&mut self) -> Option<Label> {
1591         if let Some(ident) = self.token.lifetime() {
1592             let span = self.token.span;
1593             self.bump();
1594             Some(Label { ident: Ident::new(ident.name, span) })
1595         } else {
1596             None
1597         }
1598     }
1599
1600     /// Parses a `match ... { ... }` expression (`match` token already eaten).
1601     fn parse_match_expr(&mut self, mut attrs: ThinVec<Attribute>) -> PResult<'a, P<Expr>> {
1602         let match_span = self.prev_span;
1603         let lo = self.prev_span;
1604         let discriminant = self.parse_expr_res(Restrictions::NO_STRUCT_LITERAL, None)?;
1605         if let Err(mut e) = self.expect(&token::OpenDelim(token::Brace)) {
1606             if self.token == token::Semi {
1607                 e.span_suggestion_short(
1608                     match_span,
1609                     "try removing this `match`",
1610                     String::new(),
1611                     Applicability::MaybeIncorrect // speculative
1612                 );
1613             }
1614             return Err(e)
1615         }
1616         attrs.extend(self.parse_inner_attributes()?);
1617
1618         let mut arms: Vec<Arm> = Vec::new();
1619         while self.token != token::CloseDelim(token::Brace) {
1620             match self.parse_arm() {
1621                 Ok(arm) => arms.push(arm),
1622                 Err(mut e) => {
1623                     // Recover by skipping to the end of the block.
1624                     e.emit();
1625                     self.recover_stmt();
1626                     let span = lo.to(self.token.span);
1627                     if self.token == token::CloseDelim(token::Brace) {
1628                         self.bump();
1629                     }
1630                     return Ok(self.mk_expr(span, ExprKind::Match(discriminant, arms), attrs));
1631                 }
1632             }
1633         }
1634         let hi = self.token.span;
1635         self.bump();
1636         return Ok(self.mk_expr(lo.to(hi), ExprKind::Match(discriminant, arms), attrs));
1637     }
1638
1639     pub(super) fn parse_arm(&mut self) -> PResult<'a, Arm> {
1640         let attrs = self.parse_outer_attributes()?;
1641         let lo = self.token.span;
1642         let pat = self.parse_top_pat(GateOr::No)?;
1643         let guard = if self.eat_keyword(kw::If) {
1644             Some(self.parse_expr()?)
1645         } else {
1646             None
1647         };
1648         let arrow_span = self.token.span;
1649         self.expect(&token::FatArrow)?;
1650         let arm_start_span = self.token.span;
1651
1652         let expr = self.parse_expr_res(Restrictions::STMT_EXPR, None)
1653             .map_err(|mut err| {
1654                 err.span_label(arrow_span, "while parsing the `match` arm starting here");
1655                 err
1656             })?;
1657
1658         let require_comma = classify::expr_requires_semi_to_be_stmt(&expr)
1659             && self.token != token::CloseDelim(token::Brace);
1660
1661         let hi = self.token.span;
1662
1663         if require_comma {
1664             let cm = self.sess.source_map();
1665             self.expect_one_of(&[token::Comma], &[token::CloseDelim(token::Brace)])
1666                 .map_err(|mut err| {
1667                     match (cm.span_to_lines(expr.span), cm.span_to_lines(arm_start_span)) {
1668                         (Ok(ref expr_lines), Ok(ref arm_start_lines))
1669                         if arm_start_lines.lines[0].end_col == expr_lines.lines[0].end_col
1670                             && expr_lines.lines.len() == 2
1671                             && self.token == token::FatArrow => {
1672                             // We check whether there's any trailing code in the parse span,
1673                             // if there isn't, we very likely have the following:
1674                             //
1675                             // X |     &Y => "y"
1676                             //   |        --    - missing comma
1677                             //   |        |
1678                             //   |        arrow_span
1679                             // X |     &X => "x"
1680                             //   |      - ^^ self.token.span
1681                             //   |      |
1682                             //   |      parsed until here as `"y" & X`
1683                             err.span_suggestion_short(
1684                                 cm.next_point(arm_start_span),
1685                                 "missing a comma here to end this `match` arm",
1686                                 ",".to_owned(),
1687                                 Applicability::MachineApplicable
1688                             );
1689                         }
1690                         _ => {
1691                             err.span_label(arrow_span,
1692                                            "while parsing the `match` arm starting here");
1693                         }
1694                     }
1695                     err
1696                 })?;
1697         } else {
1698             self.eat(&token::Comma);
1699         }
1700
1701         Ok(ast::Arm {
1702             attrs,
1703             pat,
1704             guard,
1705             body: expr,
1706             span: lo.to(hi),
1707             id: DUMMY_NODE_ID,
1708             is_placeholder: false,
1709         })
1710     }
1711
1712     /// Parses a `try {...}` expression (`try` token already eaten).
1713     fn parse_try_block(
1714         &mut self,
1715         span_lo: Span,
1716         mut attrs: ThinVec<Attribute>
1717     ) -> PResult<'a, P<Expr>> {
1718         let (iattrs, body) = self.parse_inner_attrs_and_block()?;
1719         attrs.extend(iattrs);
1720         if self.eat_keyword(kw::Catch) {
1721             let mut error = self.struct_span_err(self.prev_span,
1722                                                  "keyword `catch` cannot follow a `try` block");
1723             error.help("try using `match` on the result of the `try` block instead");
1724             error.emit();
1725             Err(error)
1726         } else {
1727             let span = span_lo.to(body.span);
1728             self.sess.gated_spans.gate(sym::try_blocks, span);
1729             Ok(self.mk_expr(span, ExprKind::TryBlock(body), attrs))
1730         }
1731     }
1732
1733     fn is_do_catch_block(&self) -> bool {
1734         self.token.is_keyword(kw::Do) &&
1735         self.is_keyword_ahead(1, &[kw::Catch]) &&
1736         self.look_ahead(2, |t| *t == token::OpenDelim(token::Brace)) &&
1737         !self.restrictions.contains(Restrictions::NO_STRUCT_LITERAL)
1738     }
1739
1740     fn is_try_block(&self) -> bool {
1741         self.token.is_keyword(kw::Try) &&
1742         self.look_ahead(1, |t| *t == token::OpenDelim(token::Brace)) &&
1743         self.token.span.rust_2018() &&
1744         // Prevent `while try {} {}`, `if try {} {} else {}`, etc.
1745         !self.restrictions.contains(Restrictions::NO_STRUCT_LITERAL)
1746     }
1747
1748     /// Parses an `async move? {...}` expression.
1749     fn parse_async_block(&mut self, mut attrs: ThinVec<Attribute>) -> PResult<'a, P<Expr>> {
1750         let span_lo = self.token.span;
1751         self.expect_keyword(kw::Async)?;
1752         let capture_clause = self.parse_capture_clause();
1753         let (iattrs, body) = self.parse_inner_attrs_and_block()?;
1754         attrs.extend(iattrs);
1755         Ok(self.mk_expr(
1756             span_lo.to(body.span),
1757             ExprKind::Async(capture_clause, DUMMY_NODE_ID, body), attrs))
1758     }
1759
1760     fn is_async_block(&self) -> bool {
1761         self.token.is_keyword(kw::Async) &&
1762         (
1763             ( // `async move {`
1764                 self.is_keyword_ahead(1, &[kw::Move]) &&
1765                 self.look_ahead(2, |t| *t == token::OpenDelim(token::Brace))
1766             ) || ( // `async {`
1767                 self.look_ahead(1, |t| *t == token::OpenDelim(token::Brace))
1768             )
1769         )
1770     }
1771
1772     fn maybe_parse_struct_expr(
1773         &mut self,
1774         lo: Span,
1775         path: &ast::Path,
1776         attrs: &ThinVec<Attribute>,
1777     ) -> Option<PResult<'a, P<Expr>>> {
1778         let struct_allowed = !self.restrictions.contains(Restrictions::NO_STRUCT_LITERAL);
1779         let certainly_not_a_block = || self.look_ahead(1, |t| t.is_ident()) && (
1780             // `{ ident, ` cannot start a block.
1781             self.look_ahead(2, |t| t == &token::Comma) ||
1782             self.look_ahead(2, |t| t == &token::Colon) && (
1783                 // `{ ident: token, ` cannot start a block.
1784                 self.look_ahead(4, |t| t == &token::Comma) ||
1785                 // `{ ident: ` cannot start a block unless it's a type ascription `ident: Type`.
1786                 self.look_ahead(3, |t| !t.can_begin_type())
1787             )
1788         );
1789
1790         if struct_allowed || certainly_not_a_block() {
1791             // This is a struct literal, but we don't can't accept them here.
1792             let expr = self.parse_struct_expr(lo, path.clone(), attrs.clone());
1793             if let (Ok(expr), false) = (&expr, struct_allowed) {
1794                 self.struct_span_err(
1795                     expr.span,
1796                     "struct literals are not allowed here",
1797                 )
1798                 .multipart_suggestion(
1799                     "surround the struct literal with parentheses",
1800                     vec![
1801                         (lo.shrink_to_lo(), "(".to_string()),
1802                         (expr.span.shrink_to_hi(), ")".to_string()),
1803                     ],
1804                     Applicability::MachineApplicable,
1805                 )
1806                 .emit();
1807             }
1808             return Some(expr);
1809         }
1810         None
1811     }
1812
1813     pub(super) fn parse_struct_expr(
1814         &mut self,
1815         lo: Span,
1816         pth: ast::Path,
1817         mut attrs: ThinVec<Attribute>
1818     ) -> PResult<'a, P<Expr>> {
1819         let struct_sp = lo.to(self.prev_span);
1820         self.bump();
1821         let mut fields = Vec::new();
1822         let mut base = None;
1823
1824         attrs.extend(self.parse_inner_attributes()?);
1825
1826         while self.token != token::CloseDelim(token::Brace) {
1827             if self.eat(&token::DotDot) {
1828                 let exp_span = self.prev_span;
1829                 match self.parse_expr() {
1830                     Ok(e) => {
1831                         base = Some(e);
1832                     }
1833                     Err(mut e) => {
1834                         e.emit();
1835                         self.recover_stmt();
1836                     }
1837                 }
1838                 if self.token == token::Comma {
1839                     self.struct_span_err(
1840                         exp_span.to(self.prev_span),
1841                         "cannot use a comma after the base struct",
1842                     )
1843                     .span_suggestion_short(
1844                         self.token.span,
1845                         "remove this comma",
1846                         String::new(),
1847                         Applicability::MachineApplicable
1848                     )
1849                     .note("the base struct must always be the last field")
1850                     .emit();
1851                     self.recover_stmt();
1852                 }
1853                 break;
1854             }
1855
1856             let mut recovery_field = None;
1857             if let token::Ident(name, _) = self.token.kind {
1858                 if !self.token.is_reserved_ident() && self.look_ahead(1, |t| *t == token::Colon) {
1859                     // Use in case of error after field-looking code: `S { foo: () with a }`.
1860                     recovery_field = Some(ast::Field {
1861                         ident: Ident::new(name, self.token.span),
1862                         span: self.token.span,
1863                         expr: self.mk_expr(self.token.span, ExprKind::Err, ThinVec::new()),
1864                         is_shorthand: false,
1865                         attrs: ThinVec::new(),
1866                         id: DUMMY_NODE_ID,
1867                         is_placeholder: false,
1868                     });
1869                 }
1870             }
1871             let mut parsed_field = None;
1872             match self.parse_field() {
1873                 Ok(f) => parsed_field = Some(f),
1874                 Err(mut e) => {
1875                     e.span_label(struct_sp, "while parsing this struct");
1876                     e.emit();
1877
1878                     // If the next token is a comma, then try to parse
1879                     // what comes next as additional fields, rather than
1880                     // bailing out until next `}`.
1881                     if self.token != token::Comma {
1882                         self.recover_stmt_(SemiColonMode::Comma, BlockMode::Ignore);
1883                         if self.token != token::Comma {
1884                             break;
1885                         }
1886                     }
1887                 }
1888             }
1889
1890             match self.expect_one_of(&[token::Comma],
1891                                      &[token::CloseDelim(token::Brace)]) {
1892                 Ok(_) => if let Some(f) = parsed_field.or(recovery_field) {
1893                     // Only include the field if there's no parse error for the field name.
1894                     fields.push(f);
1895                 }
1896                 Err(mut e) => {
1897                     if let Some(f) = recovery_field {
1898                         fields.push(f);
1899                     }
1900                     e.span_label(struct_sp, "while parsing this struct");
1901                     e.emit();
1902                     self.recover_stmt_(SemiColonMode::Comma, BlockMode::Ignore);
1903                     self.eat(&token::Comma);
1904                 }
1905             }
1906         }
1907
1908         let span = lo.to(self.token.span);
1909         self.expect(&token::CloseDelim(token::Brace))?;
1910         return Ok(self.mk_expr(span, ExprKind::Struct(pth, fields, base), attrs));
1911     }
1912
1913     /// Parses `ident (COLON expr)?`.
1914     fn parse_field(&mut self) -> PResult<'a, Field> {
1915         let attrs = self.parse_outer_attributes()?;
1916         let lo = self.token.span;
1917
1918         // Check if a colon exists one ahead. This means we're parsing a fieldname.
1919         let (fieldname, expr, is_shorthand) = if self.look_ahead(1, |t| {
1920             t == &token::Colon || t == &token::Eq
1921         }) {
1922             let fieldname = self.parse_field_name()?;
1923
1924             // Check for an equals token. This means the source incorrectly attempts to
1925             // initialize a field with an eq rather than a colon.
1926             if self.token == token::Eq {
1927                 self.diagnostic()
1928                     .struct_span_err(self.token.span, "expected `:`, found `=`")
1929                     .span_suggestion(
1930                         fieldname.span.shrink_to_hi().to(self.token.span),
1931                         "replace equals symbol with a colon",
1932                         ":".to_string(),
1933                         Applicability::MachineApplicable,
1934                     )
1935                     .emit();
1936             }
1937             self.bump(); // `:`
1938             (fieldname, self.parse_expr()?, false)
1939         } else {
1940             let fieldname = self.parse_ident_common(false)?;
1941
1942             // Mimic `x: x` for the `x` field shorthand.
1943             let path = ast::Path::from_ident(fieldname);
1944             let expr = self.mk_expr(fieldname.span, ExprKind::Path(None, path), ThinVec::new());
1945             (fieldname, expr, true)
1946         };
1947         Ok(ast::Field {
1948             ident: fieldname,
1949             span: lo.to(expr.span),
1950             expr,
1951             is_shorthand,
1952             attrs: attrs.into(),
1953             id: DUMMY_NODE_ID,
1954             is_placeholder: false,
1955         })
1956     }
1957
1958     fn err_dotdotdot_syntax(&self, span: Span) {
1959         self.struct_span_err(span, "unexpected token: `...`")
1960             .span_suggestion(
1961                 span,
1962                 "use `..` for an exclusive range", "..".to_owned(),
1963                 Applicability::MaybeIncorrect
1964             )
1965             .span_suggestion(
1966                 span,
1967                 "or `..=` for an inclusive range", "..=".to_owned(),
1968                 Applicability::MaybeIncorrect
1969             )
1970             .emit();
1971     }
1972
1973     fn err_larrow_operator(&self, span: Span) {
1974         self.struct_span_err(
1975             span,
1976             "unexpected token: `<-`"
1977         ).span_suggestion(
1978             span,
1979             "if you meant to write a comparison against a negative value, add a \
1980              space in between `<` and `-`",
1981             "< -".to_string(),
1982             Applicability::MaybeIncorrect
1983         ).emit();
1984     }
1985
1986     fn mk_assign_op(&self, binop: BinOp, lhs: P<Expr>, rhs: P<Expr>) -> ExprKind {
1987         ExprKind::AssignOp(binop, lhs, rhs)
1988     }
1989
1990     fn mk_range(
1991         &self,
1992         start: Option<P<Expr>>,
1993         end: Option<P<Expr>>,
1994         limits: RangeLimits
1995     ) -> PResult<'a, ExprKind> {
1996         if end.is_none() && limits == RangeLimits::Closed {
1997             Err(self.span_fatal_err(self.token.span, Error::InclusiveRangeWithNoEnd))
1998         } else {
1999             Ok(ExprKind::Range(start, end, limits))
2000         }
2001     }
2002
2003     fn mk_unary(&self, unop: UnOp, expr: P<Expr>) -> ExprKind {
2004         ExprKind::Unary(unop, expr)
2005     }
2006
2007     fn mk_binary(&self, binop: BinOp, lhs: P<Expr>, rhs: P<Expr>) -> ExprKind {
2008         ExprKind::Binary(binop, lhs, rhs)
2009     }
2010
2011     fn mk_index(&self, expr: P<Expr>, idx: P<Expr>) -> ExprKind {
2012         ExprKind::Index(expr, idx)
2013     }
2014
2015     fn mk_call(&self, f: P<Expr>, args: Vec<P<Expr>>) -> ExprKind {
2016         ExprKind::Call(f, args)
2017     }
2018
2019     fn mk_await_expr(&mut self, self_arg: P<Expr>, lo: Span) -> PResult<'a, P<Expr>> {
2020         let span = lo.to(self.prev_span);
2021         let await_expr = self.mk_expr(span, ExprKind::Await(self_arg), ThinVec::new());
2022         self.recover_from_await_method_call();
2023         Ok(await_expr)
2024     }
2025
2026     crate fn mk_expr(&self, span: Span, kind: ExprKind, attrs: ThinVec<Attribute>) -> P<Expr> {
2027         P(Expr { kind, span, attrs, id: DUMMY_NODE_ID })
2028     }
2029
2030     pub(super) fn mk_expr_err(&self, span: Span) -> P<Expr> {
2031         self.mk_expr(span, ExprKind::Err, ThinVec::new())
2032     }
2033 }