|
1 | 1 | use super::ty::AllowPlus;
|
2 | 2 | use super::{BlockMode, Parser, PathStyle, SemiColonMode, SeqSep, TokenExpectType, TokenType};
|
3 | 3 |
|
4 |
| -use rustc_ast::ast::{self, BinOpKind, BindingMode, BlockCheckMode, Expr, ExprKind, Item, Param}; |
5 |
| -use rustc_ast::ast::{AttrVec, ItemKind, Mutability, Pat, PatKind, PathSegment, QSelf, Ty, TyKind}; |
| 4 | +use rustc_ast::ast::{ |
| 5 | + self, AngleBracketedArgs, AttrVec, BinOpKind, BindingMode, BlockCheckMode, Expr, ExprKind, |
| 6 | + Item, ItemKind, Mutability, Param, Pat, PatKind, PathSegment, QSelf, Ty, TyKind, |
| 7 | +}; |
6 | 8 | use rustc_ast::ptr::P;
|
7 | 9 | use rustc_ast::token::{self, Lit, LitKind, TokenKind};
|
8 | 10 | use rustc_ast::util::parser::AssocOp;
|
@@ -488,6 +490,57 @@ impl<'a> Parser<'a> {
|
488 | 490 | false
|
489 | 491 | }
|
490 | 492 |
|
| 493 | + /// Check if a method call with an intended turbofish has been written without surrounding |
| 494 | + /// angle brackets. |
| 495 | + pub(super) fn check_turbofish_missing_angle_brackets(&mut self, segment: &mut PathSegment) { |
| 496 | + if token::ModSep == self.token.kind && segment.args.is_none() { |
| 497 | + let snapshot = self.clone(); |
| 498 | + self.bump(); |
| 499 | + let lo = self.token.span; |
| 500 | + match self.parse_angle_args() { |
| 501 | + Ok(args) => { |
| 502 | + let span = lo.to(self.prev_token.span); |
| 503 | + // Detect trailing `>` like in `x.collect::Vec<_>>()`. |
| 504 | + let mut trailing_span = self.prev_token.span.shrink_to_hi(); |
| 505 | + while self.token.kind == token::BinOp(token::Shr) |
| 506 | + || self.token.kind == token::Gt |
| 507 | + { |
| 508 | + trailing_span = trailing_span.to(self.token.span); |
| 509 | + self.bump(); |
| 510 | + } |
| 511 | + if self.token.kind == token::OpenDelim(token::Paren) { |
| 512 | + // Recover from bad turbofish: `foo.collect::Vec<_>()`. |
| 513 | + let args = AngleBracketedArgs { args, span }.into(); |
| 514 | + segment.args = args; |
| 515 | + |
| 516 | + self.struct_span_err( |
| 517 | + span, |
| 518 | + "generic parameters without surrounding angle brackets", |
| 519 | + ) |
| 520 | + .multipart_suggestion( |
| 521 | + "surround the type parameters with angle brackets", |
| 522 | + vec![ |
| 523 | + (span.shrink_to_lo(), "<".to_string()), |
| 524 | + (trailing_span, ">".to_string()), |
| 525 | + ], |
| 526 | + Applicability::MachineApplicable, |
| 527 | + ) |
| 528 | + .emit(); |
| 529 | + } else { |
| 530 | + // This doesn't look like an invalid turbofish, can't recover parse state. |
| 531 | + *self = snapshot; |
| 532 | + } |
| 533 | + } |
| 534 | + Err(mut err) => { |
| 535 | + // We could't parse generic parameters, unlikely to be a turbofish. Rely on |
| 536 | + // generic parse error instead. |
| 537 | + err.cancel(); |
| 538 | + *self = snapshot; |
| 539 | + } |
| 540 | + } |
| 541 | + } |
| 542 | + } |
| 543 | + |
491 | 544 | /// Check to see if a pair of chained operators looks like an attempt at chained comparison,
|
492 | 545 | /// e.g. `1 < x <= 3`. If so, suggest either splitting the comparison into two, or
|
493 | 546 | /// parenthesising the leftmost comparison.
|
|
0 commit comments