@@ -761,29 +761,37 @@ impl<'a> Parser<'a> {
761
761
} )
762
762
}
763
763
764
+ fn expected_ident_found ( & self ) -> DiagnosticBuilder < ' a > {
765
+ let mut err = self . struct_span_err ( self . span ,
766
+ & format ! ( "expected identifier, found {}" ,
767
+ self . this_token_descr( ) ) ) ;
768
+ if let Some ( token_descr) = self . token_descr ( ) {
769
+ err. span_label ( self . span , format ! ( "expected identifier, found {}" , token_descr) ) ;
770
+ } else {
771
+ err. span_label ( self . span , "expected identifier" ) ;
772
+ }
773
+ err
774
+ }
775
+
764
776
pub fn parse_ident ( & mut self ) -> PResult < ' a , ast:: Ident > {
765
- self . parse_ident_common ( true , false )
777
+ self . parse_ident_common ( true )
766
778
}
767
779
768
780
pub fn parse_ident_attr( & mut self ) -> PResult < ' a , ast:: Ident > {
769
- self . parse_ident_common ( true , true )
781
+ match self . token {
782
+ token:: Ident ( i) if i. name == keywords:: SelfType . name ( ) {
783
+ self . bump ( ) ;
784
+ Ok ( i)
785
+ }
786
+ _ => self . parse_ident ( ) ,
787
+ }
770
788
}
771
789
772
- fn parse_ident_common ( & mut self , recover : bool , accept_self : bool ) -> PResult < ' a , ast:: Ident > {
790
+ fn parse_ident_common ( & mut self , recover: bool ) -> PResult <' a, ast:: Ident > {
773
791
match self . token {
774
792
token:: Ident ( i) => {
775
- if self . token . is_reserved_ident ( )
776
- && !( accept_self && i. name == keywords:: SelfType . name ( ) )
777
- {
778
- let mut err = self . struct_span_err ( self . span ,
779
- & format ! ( "expected identifier, found {}" ,
780
- self . this_token_descr( ) ) ) ;
781
- if let Some ( token_descr) = self . token_descr ( ) {
782
- err. span_label ( self . span , format ! ( "expected identifier, found {}" ,
783
- token_descr) ) ;
784
- } else {
785
- err. span_label ( self . span , "expected identifier" ) ;
786
- }
793
+ if self . token . is_reserved_ident ( ) {
794
+ let mut err = self . expected_ident_found ( ) ;
787
795
if recover {
788
796
err. emit ( ) ;
789
797
} else {
@@ -797,14 +805,7 @@ impl<'a> Parser<'a> {
797
805
Err ( if self . prev_token_kind == PrevTokenKind :: DocComment {
798
806
self . span_fatal_err ( self . prev_span , Error :: UselessDocComment )
799
807
} else {
800
- let mut err = self . fatal ( & format ! ( "expected identifier, found `{}`" ,
801
- self . this_token_to_string( ) ) ) ;
802
- if let Some ( token_descr) = self . token_descr ( ) {
803
- err. span_label ( self . span , format ! ( "expected identifier, found {}" ,
804
- token_descr) ) ;
805
- } else {
806
- err. span_label ( self . span , "expected identifier" ) ;
807
- }
808
+ let mut err = self . expected_ident_found ( ) ;
808
809
if self . token == token:: Underscore {
809
810
err. note ( "`_` is a wildcard pattern, not an identifier" ) ;
810
811
}
@@ -2117,7 +2118,7 @@ impl<'a> Parser<'a> {
2117
2118
self . bump ( ) ;
2118
2119
Ok ( Ident :: with_empty_ctxt ( name) )
2119
2120
} else {
2120
- self . parse_ident_common ( false , false )
2121
+ self . parse_ident_common ( false )
2121
2122
}
2122
2123
}
2123
2124
@@ -2134,7 +2135,7 @@ impl<'a> Parser<'a> {
2134
2135
hi = self . prev_span ;
2135
2136
( fieldname, self . parse_expr ( ) ?, false )
2136
2137
} else {
2137
- let fieldname = self . parse_ident_common ( false , false ) ?;
2138
+ let fieldname = self . parse_ident_common ( false ) ?;
2138
2139
hi = self . prev_span ;
2139
2140
2140
2141
// Mimic `x: x` for the `x` field shorthand.
0 commit comments