@@ -516,13 +516,21 @@ impl<'a> Parser<'a> {
516516 }
517517 }
518518
519+ pub fn parse_ident_or_self_type ( & mut self ) -> ast:: Ident {
520+ if self . is_self_type_ident ( ) {
521+ self . expect_self_type_ident ( )
522+ } else {
523+ self . parse_ident ( )
524+ }
525+ }
526+
519527 pub fn parse_path_list_item ( & mut self ) -> ast:: PathListItem {
520528 let lo = self . span . lo ;
521529 let node = if self . eat_keyword_noexpect ( keywords:: Mod ) {
522530 let span = self . last_span ;
523531 self . span_warn ( span, "deprecated syntax; use the `self` keyword now" ) ;
524532 ast:: PathListMod { id : ast:: DUMMY_NODE_ID }
525- } else if self . eat_keyword ( keywords:: Self ) {
533+ } else if self . eat_keyword ( keywords:: SelfValue ) {
526534 ast:: PathListMod { id : ast:: DUMMY_NODE_ID }
527535 } else {
528536 let ident = self . parse_ident ( ) ;
@@ -1797,7 +1805,7 @@ impl<'a> Parser<'a> {
17971805 let mut segments = Vec :: new ( ) ;
17981806 loop {
17991807 // First, parse an identifier.
1800- let identifier = self . parse_ident ( ) ;
1808+ let identifier = self . parse_ident_or_self_type ( ) ;
18011809
18021810 // Parse types, optionally.
18031811 let parameters = if self . eat_lt ( ) {
@@ -1850,7 +1858,7 @@ impl<'a> Parser<'a> {
18501858 let mut segments = Vec :: new ( ) ;
18511859 loop {
18521860 // First, parse an identifier.
1853- let identifier = self . parse_ident ( ) ;
1861+ let identifier = self . parse_ident_or_self_type ( ) ;
18541862
18551863 // If we do not see a `::`, stop.
18561864 if !self . eat ( & token:: ModSep ) {
@@ -1895,7 +1903,7 @@ impl<'a> Parser<'a> {
18951903 let mut segments = Vec :: new ( ) ;
18961904 loop {
18971905 // First, parse an identifier.
1898- let identifier = self . parse_ident ( ) ;
1906+ let identifier = self . parse_ident_or_self_type ( ) ;
18991907
19001908 // Assemble and push the result.
19011909 segments. push ( ast:: PathSegment {
@@ -2166,10 +2174,8 @@ impl<'a> Parser<'a> {
21662174 token:: BinOp ( token:: Or ) | token:: OrOr => {
21672175 return self . parse_lambda_expr ( CaptureByRef ) ;
21682176 } ,
2169- // FIXME #13626: Should be able to stick in
2170- // token::SELF_KEYWORD_NAME
21712177 token:: Ident ( id @ ast:: Ident {
2172- name : ast :: Name ( token:: SELF_KEYWORD_NAME_NUM ) ,
2178+ name : token:: SELF_KEYWORD_NAME ,
21732179 ctxt : _
21742180 } , token:: Plain ) => {
21752181 self . bump ( ) ;
@@ -3411,7 +3417,7 @@ impl<'a> Parser<'a> {
34113417 && self . token != token:: ModSep )
34123418 || self . token . is_keyword ( keywords:: True )
34133419 || self . token . is_keyword ( keywords:: False ) {
3414- // Parse an expression pattern or exp .. exp.
3420+ // Parse an expression pattern or exp ... exp.
34153421 //
34163422 // These expressions are limited to literals (possibly
34173423 // preceded by unary-minus) or identifiers.
@@ -3532,15 +3538,17 @@ impl<'a> Parser<'a> {
35323538 enum_path. segments . len ( ) == 1 &&
35333539 enum_path. segments [ 0 ] . parameters . is_empty ( )
35343540 {
3535- // it could still be either an enum
3536- // or an identifier pattern, resolve
3537- // will sort it out:
3538- pat = PatIdent ( BindByValue ( MutImmutable ) ,
3539- codemap:: Spanned {
3540- span : enum_path. span ,
3541- node : enum_path. segments [ 0 ]
3542- . identifier } ,
3543- None ) ;
3541+ // NB: If enum_path is a single identifier,
3542+ // this should not be reachable due to special
3543+ // handling further above.
3544+ //
3545+ // However, previously a PatIdent got emitted
3546+ // here, so we preserve the branch just in case.
3547+ //
3548+ // A rewrite of the logic in this function
3549+ // would probably make this obvious.
3550+ self . span_bug ( enum_path. span ,
3551+ "ident only path should have been covered already" ) ;
35443552 } else {
35453553 pat = PatEnum ( enum_path, Some ( args) ) ;
35463554 }
@@ -4380,6 +4388,27 @@ impl<'a> Parser<'a> {
43804388 }
43814389 }
43824390
4391+ fn is_self_type_ident ( & mut self ) -> bool {
4392+ match self . token {
4393+ token:: Ident ( id, token:: Plain ) => id. name == special_idents:: type_self. name ,
4394+ _ => false
4395+ }
4396+ }
4397+
4398+ fn expect_self_type_ident ( & mut self ) -> ast:: Ident {
4399+ match self . token {
4400+ token:: Ident ( id, token:: Plain ) if id. name == special_idents:: type_self. name => {
4401+ self . bump ( ) ;
4402+ id
4403+ } ,
4404+ _ => {
4405+ let token_str = self . this_token_to_string ( ) ;
4406+ self . fatal ( & format ! ( "expected `Self`, found `{}`" ,
4407+ token_str) [ ] )
4408+ }
4409+ }
4410+ }
4411+
43834412 /// Parse the argument list and result type of a function
43844413 /// that may have a self type.
43854414 fn parse_fn_decl_with_self < F > ( & mut self , parse_arg_fn : F ) -> ( ExplicitSelf , P < FnDecl > ) where
@@ -4396,22 +4425,22 @@ impl<'a> Parser<'a> {
43964425 //
43974426 // We already know that the current token is `&`.
43984427
4399- if this. look_ahead ( 1 , |t| t. is_keyword ( keywords:: Self ) ) {
4428+ if this. look_ahead ( 1 , |t| t. is_keyword ( keywords:: SelfValue ) ) {
44004429 this. bump ( ) ;
44014430 SelfRegion ( None , MutImmutable , this. expect_self_ident ( ) )
44024431 } else if this. look_ahead ( 1 , |t| t. is_mutability ( ) ) &&
4403- this. look_ahead ( 2 , |t| t. is_keyword ( keywords:: Self ) ) {
4432+ this. look_ahead ( 2 , |t| t. is_keyword ( keywords:: SelfValue ) ) {
44044433 this. bump ( ) ;
44054434 let mutability = this. parse_mutability ( ) ;
44064435 SelfRegion ( None , mutability, this. expect_self_ident ( ) )
44074436 } else if this. look_ahead ( 1 , |t| t. is_lifetime ( ) ) &&
4408- this. look_ahead ( 2 , |t| t. is_keyword ( keywords:: Self ) ) {
4437+ this. look_ahead ( 2 , |t| t. is_keyword ( keywords:: SelfValue ) ) {
44094438 this. bump ( ) ;
44104439 let lifetime = this. parse_lifetime ( ) ;
44114440 SelfRegion ( Some ( lifetime) , MutImmutable , this. expect_self_ident ( ) )
44124441 } else if this. look_ahead ( 1 , |t| t. is_lifetime ( ) ) &&
44134442 this. look_ahead ( 2 , |t| t. is_mutability ( ) ) &&
4414- this. look_ahead ( 3 , |t| t. is_keyword ( keywords:: Self ) ) {
4443+ this. look_ahead ( 3 , |t| t. is_keyword ( keywords:: SelfValue ) ) {
44154444 this. bump ( ) ;
44164445 let lifetime = this. parse_lifetime ( ) ;
44174446 let mutability = this. parse_mutability ( ) ;
@@ -4466,7 +4495,7 @@ impl<'a> Parser<'a> {
44664495 SelfValue ( self_ident)
44674496 }
44684497 } else if self . token . is_mutability ( ) &&
4469- self . look_ahead ( 1 , |t| t. is_keyword ( keywords:: Self ) ) {
4498+ self . look_ahead ( 1 , |t| t. is_keyword ( keywords:: SelfValue ) ) {
44704499 mutbl_self = self . parse_mutability ( ) ;
44714500 let self_ident = self . expect_self_ident ( ) ;
44724501
0 commit comments