@@ -516,13 +516,21 @@ impl<'a> Parser<'a> {
516
516
}
517
517
}
518
518
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
+
519
527
pub fn parse_path_list_item ( & mut self ) -> ast:: PathListItem {
520
528
let lo = self . span . lo ;
521
529
let node = if self . eat_keyword_noexpect ( keywords:: Mod ) {
522
530
let span = self . last_span ;
523
531
self . span_warn ( span, "deprecated syntax; use the `self` keyword now" ) ;
524
532
ast:: PathListMod { id : ast:: DUMMY_NODE_ID }
525
- } else if self . eat_keyword ( keywords:: Self ) {
533
+ } else if self . eat_keyword ( keywords:: SelfValue ) {
526
534
ast:: PathListMod { id : ast:: DUMMY_NODE_ID }
527
535
} else {
528
536
let ident = self . parse_ident ( ) ;
@@ -1797,7 +1805,7 @@ impl<'a> Parser<'a> {
1797
1805
let mut segments = Vec :: new ( ) ;
1798
1806
loop {
1799
1807
// First, parse an identifier.
1800
- let identifier = self . parse_ident ( ) ;
1808
+ let identifier = self . parse_ident_or_self_type ( ) ;
1801
1809
1802
1810
// Parse types, optionally.
1803
1811
let parameters = if self . eat_lt ( ) {
@@ -1850,7 +1858,7 @@ impl<'a> Parser<'a> {
1850
1858
let mut segments = Vec :: new ( ) ;
1851
1859
loop {
1852
1860
// First, parse an identifier.
1853
- let identifier = self . parse_ident ( ) ;
1861
+ let identifier = self . parse_ident_or_self_type ( ) ;
1854
1862
1855
1863
// If we do not see a `::`, stop.
1856
1864
if !self . eat ( & token:: ModSep ) {
@@ -1895,7 +1903,7 @@ impl<'a> Parser<'a> {
1895
1903
let mut segments = Vec :: new ( ) ;
1896
1904
loop {
1897
1905
// First, parse an identifier.
1898
- let identifier = self . parse_ident ( ) ;
1906
+ let identifier = self . parse_ident_or_self_type ( ) ;
1899
1907
1900
1908
// Assemble and push the result.
1901
1909
segments. push ( ast:: PathSegment {
@@ -2166,10 +2174,8 @@ impl<'a> Parser<'a> {
2166
2174
token:: BinOp ( token:: Or ) | token:: OrOr => {
2167
2175
return self . parse_lambda_expr ( CaptureByRef ) ;
2168
2176
} ,
2169
- // FIXME #13626: Should be able to stick in
2170
- // token::SELF_KEYWORD_NAME
2171
2177
token:: Ident ( id @ ast:: Ident {
2172
- name : ast :: Name ( token:: SELF_KEYWORD_NAME_NUM ) ,
2178
+ name : token:: SELF_KEYWORD_NAME ,
2173
2179
ctxt : _
2174
2180
} , token:: Plain ) => {
2175
2181
self . bump ( ) ;
@@ -3411,7 +3417,7 @@ impl<'a> Parser<'a> {
3411
3417
&& self . token != token:: ModSep )
3412
3418
|| self . token . is_keyword ( keywords:: True )
3413
3419
|| self . token . is_keyword ( keywords:: False ) {
3414
- // Parse an expression pattern or exp .. exp.
3420
+ // Parse an expression pattern or exp ... exp.
3415
3421
//
3416
3422
// These expressions are limited to literals (possibly
3417
3423
// preceded by unary-minus) or identifiers.
@@ -3532,15 +3538,17 @@ impl<'a> Parser<'a> {
3532
3538
enum_path. segments . len ( ) == 1 &&
3533
3539
enum_path. segments [ 0 ] . parameters . is_empty ( )
3534
3540
{
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" ) ;
3544
3552
} else {
3545
3553
pat = PatEnum ( enum_path, Some ( args) ) ;
3546
3554
}
@@ -4380,6 +4388,27 @@ impl<'a> Parser<'a> {
4380
4388
}
4381
4389
}
4382
4390
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
+
4383
4412
/// Parse the argument list and result type of a function
4384
4413
/// that may have a self type.
4385
4414
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> {
4396
4425
//
4397
4426
// We already know that the current token is `&`.
4398
4427
4399
- if this. look_ahead ( 1 , |t| t. is_keyword ( keywords:: Self ) ) {
4428
+ if this. look_ahead ( 1 , |t| t. is_keyword ( keywords:: SelfValue ) ) {
4400
4429
this. bump ( ) ;
4401
4430
SelfRegion ( None , MutImmutable , this. expect_self_ident ( ) )
4402
4431
} 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 ) ) {
4404
4433
this. bump ( ) ;
4405
4434
let mutability = this. parse_mutability ( ) ;
4406
4435
SelfRegion ( None , mutability, this. expect_self_ident ( ) )
4407
4436
} 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 ) ) {
4409
4438
this. bump ( ) ;
4410
4439
let lifetime = this. parse_lifetime ( ) ;
4411
4440
SelfRegion ( Some ( lifetime) , MutImmutable , this. expect_self_ident ( ) )
4412
4441
} else if this. look_ahead ( 1 , |t| t. is_lifetime ( ) ) &&
4413
4442
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 ) ) {
4415
4444
this. bump ( ) ;
4416
4445
let lifetime = this. parse_lifetime ( ) ;
4417
4446
let mutability = this. parse_mutability ( ) ;
@@ -4466,7 +4495,7 @@ impl<'a> Parser<'a> {
4466
4495
SelfValue ( self_ident)
4467
4496
}
4468
4497
} 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 ) ) {
4470
4499
mutbl_self = self . parse_mutability ( ) ;
4471
4500
let self_ident = self . expect_self_ident ( ) ;
4472
4501
0 commit comments