@@ -1184,7 +1184,7 @@ impl<'a> Parser<'a> {
1184
1184
let lo = self . span . lo ;
1185
1185
1186
1186
let ( name, node) = if self . eat_keyword ( keywords:: Type ) {
1187
- let TyParam { ident, bounds, default, ..} = self . parse_ty_param ( ) ?;
1187
+ let TyParam { ident, bounds, default, ..} = self . parse_ty_param ( vec ! [ ] ) ?;
1188
1188
self . expect ( & token:: Semi ) ?;
1189
1189
( ident, TraitItemKind :: Type ( bounds, default) )
1190
1190
} else if self . is_const_item ( ) {
@@ -1923,10 +1923,22 @@ impl<'a> Parser<'a> {
1923
1923
1924
1924
/// Parses `lifetime_defs = [ lifetime_defs { ',' lifetime_defs } ]` where `lifetime_def =
1925
1925
/// lifetime [':' lifetimes]`
1926
- pub fn parse_lifetime_defs ( & mut self ) -> PResult < ' a , Vec < ast:: LifetimeDef > > {
1927
-
1926
+ ///
1927
+ /// If `followed_by_ty_params` is None, then we are in a context
1928
+ /// where only lifetime parameters are allowed, and thus we should
1929
+ /// error if we encounter attributes after the bound lifetimes.
1930
+ ///
1931
+ /// If `followed_by_ty_params` is Some(r), then there may be type
1932
+ /// parameter bindings after the lifetimes, so we should pass
1933
+ /// along the parsed attributes to be attached to the first such
1934
+ /// type parmeter.
1935
+ pub fn parse_lifetime_defs ( & mut self ,
1936
+ followed_by_ty_params : Option < & mut Vec < ast:: Attribute > > )
1937
+ -> PResult < ' a , Vec < ast:: LifetimeDef > >
1938
+ {
1928
1939
let mut res = Vec :: new ( ) ;
1929
1940
loop {
1941
+ let attrs = self . parse_outer_attributes ( ) ?;
1930
1942
match self . token {
1931
1943
token:: Lifetime ( _) => {
1932
1944
let lifetime = self . parse_lifetime ( ) ?;
@@ -1936,11 +1948,20 @@ impl<'a> Parser<'a> {
1936
1948
} else {
1937
1949
Vec :: new ( )
1938
1950
} ;
1939
- res. push ( ast:: LifetimeDef { lifetime : lifetime,
1951
+ res. push ( ast:: LifetimeDef { attrs : attrs. into ( ) ,
1952
+ lifetime : lifetime,
1940
1953
bounds : bounds } ) ;
1941
1954
}
1942
1955
1943
1956
_ => {
1957
+ if let Some ( recv) = followed_by_ty_params {
1958
+ assert ! ( recv. is_empty( ) ) ;
1959
+ * recv = attrs;
1960
+ } else {
1961
+ let msg = "encountered trailing attributes after lifetime parameters" ;
1962
+ return Err ( self . fatal ( msg) ) ;
1963
+ }
1964
+ debug ! ( "parse_lifetime_defs ret {:?}" , res) ;
1944
1965
return Ok ( res) ;
1945
1966
}
1946
1967
}
@@ -4238,7 +4259,7 @@ impl<'a> Parser<'a> {
4238
4259
}
4239
4260
4240
4261
/// Matches typaram = IDENT (`?` unbound)? optbounds ( EQ ty )?
4241
- fn parse_ty_param ( & mut self ) -> PResult < ' a , TyParam > {
4262
+ fn parse_ty_param ( & mut self , preceding_attrs : Vec < ast :: Attribute > ) -> PResult < ' a , TyParam > {
4242
4263
let span = self . span ;
4243
4264
let ident = self . parse_ident ( ) ?;
4244
4265
@@ -4252,6 +4273,7 @@ impl<'a> Parser<'a> {
4252
4273
} ;
4253
4274
4254
4275
Ok ( TyParam {
4276
+ attrs : preceding_attrs. into ( ) ,
4255
4277
ident : ident,
4256
4278
id : ast:: DUMMY_NODE_ID ,
4257
4279
bounds : bounds,
@@ -4272,11 +4294,18 @@ impl<'a> Parser<'a> {
4272
4294
let span_lo = self . span . lo ;
4273
4295
4274
4296
if self . eat ( & token:: Lt ) {
4275
- let lifetime_defs = self . parse_lifetime_defs ( ) ?;
4297
+ let mut attrs = vec ! [ ] ;
4298
+ let lifetime_defs = self . parse_lifetime_defs ( Some ( & mut attrs) ) ?;
4276
4299
let mut seen_default = false ;
4300
+ let mut post_lifetime_attrs = Some ( attrs) ;
4277
4301
let ty_params = self . parse_seq_to_gt ( Some ( token:: Comma ) , |p| {
4278
4302
p. forbid_lifetime ( ) ?;
4279
- let ty_param = p. parse_ty_param ( ) ?;
4303
+ let attrs = match post_lifetime_attrs. as_mut ( ) {
4304
+ None => p. parse_outer_attributes ( ) ?,
4305
+ Some ( attrs) => mem:: replace ( attrs, vec ! [ ] ) ,
4306
+ } ;
4307
+ post_lifetime_attrs = None ;
4308
+ let ty_param = p. parse_ty_param ( attrs) ?;
4280
4309
if ty_param. default . is_some ( ) {
4281
4310
seen_default = true ;
4282
4311
} else if seen_default {
@@ -4433,7 +4462,7 @@ impl<'a> Parser<'a> {
4433
4462
let bound_lifetimes = if self . eat_keyword ( keywords:: For ) {
4434
4463
// Higher ranked constraint.
4435
4464
self . expect ( & token:: Lt ) ?;
4436
- let lifetime_defs = self . parse_lifetime_defs ( ) ?;
4465
+ let lifetime_defs = self . parse_lifetime_defs ( None ) ?;
4437
4466
self . expect_gt ( ) ?;
4438
4467
lifetime_defs
4439
4468
} else {
@@ -5006,7 +5035,7 @@ impl<'a> Parser<'a> {
5006
5035
fn parse_late_bound_lifetime_defs ( & mut self ) -> PResult < ' a , Vec < ast:: LifetimeDef > > {
5007
5036
if self . eat_keyword ( keywords:: For ) {
5008
5037
self . expect ( & token:: Lt ) ?;
5009
- let lifetime_defs = self . parse_lifetime_defs ( ) ?;
5038
+ let lifetime_defs = self . parse_lifetime_defs ( None ) ?;
5010
5039
self . expect_gt ( ) ?;
5011
5040
Ok ( lifetime_defs)
5012
5041
} else {
0 commit comments