@@ -2,7 +2,7 @@ use super::ty::{AllowPlus, RecoverQPath, RecoverReturnSign};
2
2
use super :: { Parser , Restrictions , TokenType } ;
3
3
use crate :: maybe_whole;
4
4
use rustc_ast:: ptr:: P ;
5
- use rustc_ast:: token:: { self , Delimiter , Token } ;
5
+ use rustc_ast:: token:: { self , Delimiter , Token , TokenKind } ;
6
6
use rustc_ast:: {
7
7
self as ast, AngleBracketedArg , AngleBracketedArgs , AnonConst , AssocConstraint ,
8
8
AssocConstraintKind , BlockCheckMode , GenericArg , GenericArgs , Generics , ParenthesizedArgs ,
@@ -96,7 +96,7 @@ impl<'a> Parser<'a> {
96
96
/// ^ help: use double colon
97
97
/// ```
98
98
fn recover_colon_before_qpath_proj ( & mut self ) -> bool {
99
- if self . token . kind != token :: Colon
99
+ if ! self . check_noexpect ( & TokenKind :: Colon )
100
100
|| self . look_ahead ( 1 , |t| !t. is_ident ( ) || t. is_reserved_ident ( ) )
101
101
{
102
102
return false ;
@@ -478,7 +478,7 @@ impl<'a> Parser<'a> {
478
478
while let Some ( arg) = self . parse_angle_arg ( ty_generics) ? {
479
479
args. push ( arg) ;
480
480
if !self . eat ( & token:: Comma ) {
481
- if self . token . kind == token :: Semi
481
+ if self . check_noexpect ( & TokenKind :: Semi )
482
482
&& self . look_ahead ( 1 , |t| t. is_ident ( ) || t. is_lifetime ( ) )
483
483
{
484
484
// Add `>` to the list of expected tokens.
@@ -517,7 +517,11 @@ impl<'a> Parser<'a> {
517
517
let arg = self . parse_generic_arg ( ty_generics) ?;
518
518
match arg {
519
519
Some ( arg) => {
520
- if self . check ( & token:: Colon ) | self . check ( & token:: Eq ) {
520
+ // we are using noexpect here because we first want to find out if either `=` or `:`
521
+ // is present and then use that info to push the other token onto the tokens list
522
+ let separated =
523
+ self . check_noexpect ( & token:: Colon ) || self . check_noexpect ( & token:: Eq ) ;
524
+ if separated && ( self . check ( & token:: Colon ) | self . check ( & token:: Eq ) ) {
521
525
let arg_span = arg. span ( ) ;
522
526
let ( binder, ident, gen_args) = match self . get_ident_from_generic_arg ( & arg) {
523
527
Ok ( ident_gen_args) => ident_gen_args,
@@ -553,6 +557,14 @@ impl<'a> Parser<'a> {
553
557
AssocConstraint { id : ast:: DUMMY_NODE_ID , ident, gen_args, kind, span } ;
554
558
Ok ( Some ( AngleBracketedArg :: Constraint ( constraint) ) )
555
559
} else {
560
+ // we only want to suggest `:` and `=` in contexts where the previous token
561
+ // is an ident and the current token or the next token is an ident
562
+ if self . prev_token . is_ident ( )
563
+ && ( self . token . is_ident ( ) || self . look_ahead ( 1 , |token| token. is_ident ( ) ) )
564
+ {
565
+ self . check ( & token:: Colon ) ;
566
+ self . check ( & token:: Eq ) ;
567
+ }
556
568
Ok ( Some ( AngleBracketedArg :: Arg ( arg) ) )
557
569
}
558
570
}
0 commit comments