@@ -1128,7 +1128,7 @@ impl<'a> Parser<'a> {
1128
1128
self . expect_keyword ( keywords:: Const ) ?;
1129
1129
let ident = self . parse_ident ( ) ?;
1130
1130
self . expect ( & token:: Colon ) ?;
1131
- let ty = self . parse_ty_sum ( ) ?;
1131
+ let ty = self . parse_ty ( ) ?;
1132
1132
let default = if self . check ( & token:: Eq ) {
1133
1133
self . bump ( ) ;
1134
1134
let expr = self . parse_expr ( ) ?;
@@ -1244,24 +1244,24 @@ impl<'a> Parser<'a> {
1244
1244
/// Parse a possibly mutable type
1245
1245
pub fn parse_mt ( & mut self ) -> PResult < ' a , MutTy > {
1246
1246
let mutbl = self . parse_mutability ( ) ?;
1247
- let t = self . parse_ty ( ) ?;
1247
+ let t = self . parse_ty_no_plus ( ) ?;
1248
1248
Ok ( MutTy { ty : t, mutbl : mutbl } )
1249
1249
}
1250
1250
1251
1251
/// Parse optional return type [ -> TY ] in function decl
1252
1252
pub fn parse_ret_ty ( & mut self ) -> PResult < ' a , FunctionRetTy > {
1253
1253
if self . eat ( & token:: RArrow ) {
1254
- Ok ( FunctionRetTy :: Ty ( self . parse_ty ( ) ?) )
1254
+ Ok ( FunctionRetTy :: Ty ( self . parse_ty_no_plus ( ) ?) )
1255
1255
} else {
1256
1256
let pos = self . span . lo ;
1257
1257
Ok ( FunctionRetTy :: Default ( mk_sp ( pos, pos) ) )
1258
1258
}
1259
1259
}
1260
1260
1261
- /// Parse a type in a context where `T1+T2` is allowed .
1262
- pub fn parse_ty_sum ( & mut self ) -> PResult < ' a , P < Ty > > {
1261
+ /// Parse a type.
1262
+ pub fn parse_ty ( & mut self ) -> PResult < ' a , P < Ty > > {
1263
1263
let lo = self . span . lo ;
1264
- let lhs = self . parse_ty ( ) ?;
1264
+ let lhs = self . parse_ty_no_plus ( ) ?;
1265
1265
1266
1266
if !self . eat ( & token:: BinOp ( token:: Plus ) ) {
1267
1267
return Ok ( lhs) ;
@@ -1331,8 +1331,12 @@ impl<'a> Parser<'a> {
1331
1331
Ok ( P ( Ty { id : ast:: DUMMY_NODE_ID , node : sum, span : sp} ) )
1332
1332
}
1333
1333
1334
- /// Parse a type.
1335
- pub fn parse_ty ( & mut self ) -> PResult < ' a , P < Ty > > {
1334
+ /// Parse a type in restricted contexts where `+` is not permitted.
1335
+ /// Example 1: `&'a TYPE`
1336
+ /// `+` is prohibited to maintain operator priority (P(+) < P(&)).
1337
+ /// Example 2: `value1 as TYPE + value2`
1338
+ /// `+` is prohibited to avoid interactions with expression grammar.
1339
+ pub fn parse_ty_no_plus ( & mut self ) -> PResult < ' a , P < Ty > > {
1336
1340
maybe_whole ! ( self , NtTy , |x| x) ;
1337
1341
1338
1342
let lo = self . span . lo ;
@@ -1346,7 +1350,7 @@ impl<'a> Parser<'a> {
1346
1350
let mut ts = vec ! [ ] ;
1347
1351
let mut last_comma = false ;
1348
1352
while self . token != token:: CloseDelim ( token:: Paren ) {
1349
- ts. push ( self . parse_ty_sum ( ) ?) ;
1353
+ ts. push ( self . parse_ty ( ) ?) ;
1350
1354
if self . check ( & token:: Comma ) {
1351
1355
last_comma = true ;
1352
1356
self . bump ( ) ;
@@ -1371,7 +1375,7 @@ impl<'a> Parser<'a> {
1371
1375
} else if self . check ( & token:: OpenDelim ( token:: Bracket ) ) {
1372
1376
// VECTOR
1373
1377
self . expect ( & token:: OpenDelim ( token:: Bracket ) ) ?;
1374
- let t = self . parse_ty_sum ( ) ?;
1378
+ let t = self . parse_ty ( ) ?;
1375
1379
1376
1380
// Parse the `; e` in `[ i32; e ]`
1377
1381
// where `e` is a const expression
@@ -1452,7 +1456,7 @@ impl<'a> Parser<'a> {
1452
1456
`*mut T` or `*const T` as appropriate)") ;
1453
1457
Mutability :: Immutable
1454
1458
} ;
1455
- let t = self . parse_ty ( ) ?;
1459
+ let t = self . parse_ty_no_plus ( ) ?;
1456
1460
Ok ( MutTy { ty : t, mutbl : mutbl } )
1457
1461
}
1458
1462
@@ -1499,7 +1503,7 @@ impl<'a> Parser<'a> {
1499
1503
} )
1500
1504
} ;
1501
1505
1502
- let t = self . parse_ty_sum ( ) ?;
1506
+ let t = self . parse_ty ( ) ?;
1503
1507
1504
1508
Ok ( Arg {
1505
1509
ty : t,
@@ -1517,7 +1521,7 @@ impl<'a> Parser<'a> {
1517
1521
pub fn parse_fn_block_arg ( & mut self ) -> PResult < ' a , Arg > {
1518
1522
let pat = self . parse_pat ( ) ?;
1519
1523
let t = if self . eat ( & token:: Colon ) {
1520
- self . parse_ty_sum ( ) ?
1524
+ self . parse_ty ( ) ?
1521
1525
} else {
1522
1526
P ( Ty {
1523
1527
id : ast:: DUMMY_NODE_ID ,
@@ -1658,7 +1662,7 @@ impl<'a> Parser<'a> {
1658
1662
pub fn parse_qualified_path ( & mut self , mode : PathStyle )
1659
1663
-> PResult < ' a , ( QSelf , ast:: Path ) > {
1660
1664
let span = self . prev_span ;
1661
- let self_type = self . parse_ty_sum ( ) ?;
1665
+ let self_type = self . parse_ty ( ) ?;
1662
1666
let mut path = if self . eat_keyword ( keywords:: As ) {
1663
1667
self . parse_path ( PathStyle :: Type ) ?
1664
1668
} else {
@@ -1768,10 +1772,10 @@ impl<'a> Parser<'a> {
1768
1772
let inputs = self . parse_seq_to_end (
1769
1773
& token:: CloseDelim ( token:: Paren ) ,
1770
1774
SeqSep :: trailing_allowed ( token:: Comma ) ,
1771
- |p| p. parse_ty_sum ( ) ) ?;
1775
+ |p| p. parse_ty ( ) ) ?;
1772
1776
1773
1777
let output_ty = if self . eat ( & token:: RArrow ) {
1774
- Some ( self . parse_ty ( ) ?)
1778
+ Some ( self . parse_ty_no_plus ( ) ?)
1775
1779
} else {
1776
1780
None
1777
1781
} ;
@@ -2981,12 +2985,12 @@ impl<'a> Parser<'a> {
2981
2985
}
2982
2986
// Special cases:
2983
2987
if op == AssocOp :: As {
2984
- let rhs = self . parse_ty ( ) ?;
2988
+ let rhs = self . parse_ty_no_plus ( ) ?;
2985
2989
let ( lo, hi) = ( lhs_span. lo , rhs. span . hi ) ;
2986
2990
lhs = self . mk_expr ( lo, hi, ExprKind :: Cast ( lhs, rhs) , ThinVec :: new ( ) ) ;
2987
2991
continue
2988
2992
} else if op == AssocOp :: Colon {
2989
- let rhs = self . parse_ty ( ) ?;
2993
+ let rhs = self . parse_ty_no_plus ( ) ?;
2990
2994
let ( lo, hi) = ( lhs_span. lo , rhs. span . hi ) ;
2991
2995
lhs = self . mk_expr ( lo, hi, ExprKind :: Type ( lhs, rhs) , ThinVec :: new ( ) ) ;
2992
2996
continue
@@ -3754,7 +3758,7 @@ impl<'a> Parser<'a> {
3754
3758
3755
3759
let mut ty = None ;
3756
3760
if self . eat ( & token:: Colon ) {
3757
- ty = Some ( self . parse_ty_sum ( ) ?) ;
3761
+ ty = Some ( self . parse_ty ( ) ?) ;
3758
3762
}
3759
3763
let init = self . parse_initializer ( ) ?;
3760
3764
Ok ( P ( ast:: Local {
@@ -3775,7 +3779,7 @@ impl<'a> Parser<'a> {
3775
3779
-> PResult < ' a , StructField > {
3776
3780
let name = self . parse_ident ( ) ?;
3777
3781
self . expect ( & token:: Colon ) ?;
3778
- let ty = self . parse_ty_sum ( ) ?;
3782
+ let ty = self . parse_ty ( ) ?;
3779
3783
Ok ( StructField {
3780
3784
span : mk_sp ( lo, self . prev_span . hi ) ,
3781
3785
ident : Some ( name) ,
@@ -4250,7 +4254,7 @@ impl<'a> Parser<'a> {
4250
4254
4251
4255
let default = if self . check ( & token:: Eq ) {
4252
4256
self . bump ( ) ;
4253
- Some ( self . parse_ty_sum ( ) ?)
4257
+ Some ( self . parse_ty ( ) ?)
4254
4258
} else {
4255
4259
None
4256
4260
} ;
@@ -4345,7 +4349,7 @@ impl<'a> Parser<'a> {
4345
4349
let mut err = self . diagnostic ( ) . struct_span_err ( self . span , & msg) ;
4346
4350
4347
4351
let span_hi = self . span . hi ;
4348
- let span_hi = match self . parse_ty ( ) {
4352
+ let span_hi = match self . parse_ty_no_plus ( ) {
4349
4353
Ok ( ..) => self . span . hi ,
4350
4354
Err ( ref mut err) => {
4351
4355
self . cancel ( err) ;
@@ -4368,7 +4372,7 @@ impl<'a> Parser<'a> {
4368
4372
if p. look_ahead ( 1 , |t| t == & token:: Eq ) {
4369
4373
Ok ( None )
4370
4374
} else {
4371
- Ok ( Some ( p. parse_ty_sum ( ) ?) )
4375
+ Ok ( Some ( p. parse_ty ( ) ?) )
4372
4376
}
4373
4377
}
4374
4378
) ?;
@@ -4386,7 +4390,7 @@ impl<'a> Parser<'a> {
4386
4390
let lo = p. span . lo ;
4387
4391
let ident = p. parse_ident ( ) ?;
4388
4392
p. expect ( & token:: Eq ) ?;
4389
- let ty = p. parse_ty ( ) ?;
4393
+ let ty = p. parse_ty_no_plus ( ) ?;
4390
4394
let hi = ty. span . hi ;
4391
4395
let span = mk_sp ( lo, hi) ;
4392
4396
return Ok ( TypeBinding { id : ast:: DUMMY_NODE_ID ,
@@ -4484,7 +4488,7 @@ impl<'a> Parser<'a> {
4484
4488
vec ! [ ]
4485
4489
} ;
4486
4490
4487
- let bounded_ty = self . parse_ty ( ) ?;
4491
+ let bounded_ty = self . parse_ty_no_plus ( ) ?;
4488
4492
4489
4493
if self . eat ( & token:: Colon ) {
4490
4494
let bounds = self . parse_ty_param_bounds ( ) ?;
@@ -4507,7 +4511,7 @@ impl<'a> Parser<'a> {
4507
4511
4508
4512
parsed_something = true ;
4509
4513
} else if self . eat ( & token:: Eq ) {
4510
- // let ty = try!(self.parse_ty ());
4514
+ // let ty = try!(self.parse_ty_no_plus ());
4511
4515
let hi = self . prev_span . hi ;
4512
4516
let span = mk_sp ( lo, hi) ;
4513
4517
// where_clause.predicates.push(
@@ -4679,7 +4683,7 @@ impl<'a> Parser<'a> {
4679
4683
// self: TYPE
4680
4684
let eself_ident = expect_ident ( self ) ;
4681
4685
if self . eat ( & token:: Colon ) {
4682
- let ty = self . parse_ty_sum ( ) ?;
4686
+ let ty = self . parse_ty ( ) ?;
4683
4687
( SelfKind :: Explicit ( ty, Mutability :: Immutable ) , eself_ident)
4684
4688
} else {
4685
4689
( SelfKind :: Value ( Mutability :: Immutable ) , eself_ident)
@@ -4691,7 +4695,7 @@ impl<'a> Parser<'a> {
4691
4695
self . bump ( ) ;
4692
4696
let eself_ident = expect_ident ( self ) ;
4693
4697
if self . eat ( & token:: Colon ) {
4694
- let ty = self . parse_ty_sum ( ) ?;
4698
+ let ty = self . parse_ty ( ) ?;
4695
4699
( SelfKind :: Explicit ( ty, Mutability :: Mutable ) , eself_ident)
4696
4700
} else {
4697
4701
( SelfKind :: Value ( Mutability :: Mutable ) , eself_ident)
@@ -4848,14 +4852,14 @@ impl<'a> Parser<'a> {
4848
4852
let ( name, node) = if self . eat_keyword ( keywords:: Type ) {
4849
4853
let name = self . parse_ident ( ) ?;
4850
4854
self . expect ( & token:: Eq ) ?;
4851
- let typ = self . parse_ty_sum ( ) ?;
4855
+ let typ = self . parse_ty ( ) ?;
4852
4856
self . expect ( & token:: Semi ) ?;
4853
4857
( name, ast:: ImplItemKind :: Type ( typ) )
4854
4858
} else if self . is_const_item ( ) {
4855
4859
self . expect_keyword ( keywords:: Const ) ?;
4856
4860
let name = self . parse_ident ( ) ?;
4857
4861
self . expect ( & token:: Colon ) ?;
4858
- let typ = self . parse_ty_sum ( ) ?;
4862
+ let typ = self . parse_ty ( ) ?;
4859
4863
self . expect ( & token:: Eq ) ?;
4860
4864
let expr = self . parse_expr ( ) ?;
4861
4865
self . expect ( & token:: Semi ) ?;
@@ -4979,7 +4983,7 @@ impl<'a> Parser<'a> {
4979
4983
} ;
4980
4984
4981
4985
// Parse the trait.
4982
- let mut ty = self . parse_ty_sum ( ) ?;
4986
+ let mut ty = self . parse_ty ( ) ?;
4983
4987
4984
4988
// Parse traits, if necessary.
4985
4989
let opt_trait = if could_be_trait && self . eat_keyword ( keywords:: For ) {
@@ -5020,7 +5024,7 @@ impl<'a> Parser<'a> {
5020
5024
ItemKind :: DefaultImpl ( unsafety, opt_trait. unwrap ( ) ) , None ) )
5021
5025
} else {
5022
5026
if opt_trait. is_some ( ) {
5023
- ty = self . parse_ty_sum ( ) ?;
5027
+ ty = self . parse_ty ( ) ?;
5024
5028
}
5025
5029
generics. where_clause = self . parse_where_clause ( ) ?;
5026
5030
@@ -5172,15 +5176,15 @@ impl<'a> Parser<'a> {
5172
5176
let mut vis = p. parse_visibility ( false ) ?;
5173
5177
let ty_is_interpolated =
5174
5178
p. token . is_interpolated ( ) || p. look_ahead ( 1 , |t| t. is_interpolated ( ) ) ;
5175
- let mut ty = p. parse_ty_sum ( ) ?;
5179
+ let mut ty = p. parse_ty ( ) ?;
5176
5180
5177
5181
// Handle `pub(path) type`, in which `vis` will be `pub` and `ty` will be `(path)`.
5178
5182
if vis == Visibility :: Public && !ty_is_interpolated &&
5179
5183
p. token != token:: Comma && p. token != token:: CloseDelim ( token:: Paren ) {
5180
5184
ty = if let TyKind :: Paren ( ref path_ty) = ty. node {
5181
5185
if let TyKind :: Path ( None , ref path) = path_ty. node {
5182
5186
vis = Visibility :: Restricted { path : P ( path. clone ( ) ) , id : path_ty. id } ;
5183
- Some ( p. parse_ty_sum ( ) ?)
5187
+ Some ( p. parse_ty ( ) ?)
5184
5188
} else {
5185
5189
None
5186
5190
}
@@ -5298,7 +5302,7 @@ impl<'a> Parser<'a> {
5298
5302
fn parse_item_const ( & mut self , m : Option < Mutability > ) -> PResult < ' a , ItemInfo > {
5299
5303
let id = self . parse_ident ( ) ?;
5300
5304
self . expect ( & token:: Colon ) ?;
5301
- let ty = self . parse_ty_sum ( ) ?;
5305
+ let ty = self . parse_ty ( ) ?;
5302
5306
self . expect ( & token:: Eq ) ?;
5303
5307
let e = self . parse_expr ( ) ?;
5304
5308
self . expect ( & token:: Semi ) ?;
@@ -5539,7 +5543,7 @@ impl<'a> Parser<'a> {
5539
5543
5540
5544
let ident = self . parse_ident ( ) ?;
5541
5545
self . expect ( & token:: Colon ) ?;
5542
- let ty = self . parse_ty_sum ( ) ?;
5546
+ let ty = self . parse_ty ( ) ?;
5543
5547
let hi = self . span . hi ;
5544
5548
self . expect ( & token:: Semi ) ?;
5545
5549
Ok ( ForeignItem {
@@ -5628,7 +5632,7 @@ impl<'a> Parser<'a> {
5628
5632
let mut tps = self . parse_generics ( ) ?;
5629
5633
tps. where_clause = self . parse_where_clause ( ) ?;
5630
5634
self . expect ( & token:: Eq ) ?;
5631
- let ty = self . parse_ty_sum ( ) ?;
5635
+ let ty = self . parse_ty ( ) ?;
5632
5636
self . expect ( & token:: Semi ) ?;
5633
5637
Ok ( ( ident, ItemKind :: Ty ( ty, tps) , None ) )
5634
5638
}
0 commit comments