@@ -224,22 +224,14 @@ enum LifetimeUseSet {
224
224
225
225
#[ derive( Copy , Clone , Debug ) ]
226
226
enum LifetimeRibKind {
227
- /// This rib acts as a barrier to forbid reference to lifetimes of a parent item.
228
- Item ,
229
-
227
+ // -- Ribs introducing named lifetimes
228
+ //
230
229
/// This rib declares generic parameters.
230
+ /// Only for this kind the `LifetimeRib::bindings` field can be non-empty.
231
231
Generics { binder : NodeId , span : Span , kind : LifetimeBinderKind } ,
232
232
233
- /// FIXME(const_generics): This patches over an ICE caused by non-'static lifetimes in const
234
- /// generics. We are disallowing this until we can decide on how we want to handle non-'static
235
- /// lifetimes in const generics. See issue #74052 for discussion.
236
- ConstGeneric ,
237
-
238
- /// Non-static lifetimes are prohibited in anonymous constants under `min_const_generics`.
239
- /// This function will emit an error if `generic_const_exprs` is not enabled, the body identified by
240
- /// `body_id` is an anonymous constant and `lifetime_ref` is non-static.
241
- AnonConst ,
242
-
233
+ // -- Ribs introducing unnamed lifetimes
234
+ //
243
235
/// Create a new anonymous lifetime parameter and reference it.
244
236
///
245
237
/// If `report_in_path`, report an error when encountering lifetime elision in a path:
@@ -256,16 +248,31 @@ enum LifetimeRibKind {
256
248
/// ```
257
249
AnonymousCreateParameter { binder : NodeId , report_in_path : bool } ,
258
250
251
+ /// Replace all anonymous lifetimes by provided lifetime.
252
+ Elided ( LifetimeRes ) ,
253
+
254
+ // -- Barrier ribs that stop lifetime lookup, or continue it but produce an error later.
255
+ //
259
256
/// Give a hard error when either `&` or `'_` is written. Used to
260
257
/// rule out things like `where T: Foo<'_>`. Does not imply an
261
258
/// error on default object bounds (e.g., `Box<dyn Foo>`).
262
259
AnonymousReportError ,
263
260
264
- /// Replace all anonymous lifetimes by provided lifetime.
265
- Elided ( LifetimeRes ) ,
266
-
267
261
/// Signal we cannot find which should be the anonymous lifetime.
268
262
ElisionFailure ,
263
+
264
+ /// FIXME(const_generics): This patches over an ICE caused by non-'static lifetimes in const
265
+ /// generics. We are disallowing this until we can decide on how we want to handle non-'static
266
+ /// lifetimes in const generics. See issue #74052 for discussion.
267
+ ConstGeneric ,
268
+
269
+ /// Non-static lifetimes are prohibited in anonymous constants under `min_const_generics`.
270
+ /// This function will emit an error if `generic_const_exprs` is not enabled, the body
271
+ /// identified by `body_id` is an anonymous constant and `lifetime_ref` is non-static.
272
+ AnonConst ,
273
+
274
+ /// This rib acts as a barrier to forbid reference to lifetimes of a parent item.
275
+ Item ,
269
276
}
270
277
271
278
#[ derive( Copy , Clone , Debug ) ]
@@ -748,35 +755,31 @@ impl<'a: 'ast, 'ast> Visitor<'ast> for LateResolutionVisitor<'a, '_, 'ast> {
748
755
fn visit_foreign_item ( & mut self , foreign_item : & ' ast ForeignItem ) {
749
756
match foreign_item. kind {
750
757
ForeignItemKind :: TyAlias ( box TyAlias { ref generics, .. } ) => {
751
- self . with_lifetime_rib ( LifetimeRibKind :: Item , |this| {
752
- this. with_generic_param_rib (
753
- & generics. params ,
754
- ItemRibKind ( HasGenericParams :: Yes ( generics. span ) ) ,
755
- LifetimeRibKind :: Generics {
756
- binder : foreign_item. id ,
757
- kind : LifetimeBinderKind :: Item ,
758
- span : generics. span ,
759
- } ,
760
- |this| visit:: walk_foreign_item ( this, foreign_item) ,
761
- )
762
- } ) ;
758
+ self . with_generic_param_rib (
759
+ & generics. params ,
760
+ ItemRibKind ( HasGenericParams :: Yes ( generics. span ) ) ,
761
+ LifetimeRibKind :: Generics {
762
+ binder : foreign_item. id ,
763
+ kind : LifetimeBinderKind :: Item ,
764
+ span : generics. span ,
765
+ } ,
766
+ |this| visit:: walk_foreign_item ( this, foreign_item) ,
767
+ ) ;
763
768
}
764
769
ForeignItemKind :: Fn ( box Fn { ref generics, .. } ) => {
765
- self . with_lifetime_rib ( LifetimeRibKind :: Item , |this| {
766
- this. with_generic_param_rib (
767
- & generics. params ,
768
- ItemRibKind ( HasGenericParams :: Yes ( generics. span ) ) ,
769
- LifetimeRibKind :: Generics {
770
- binder : foreign_item. id ,
771
- kind : LifetimeBinderKind :: Function ,
772
- span : generics. span ,
773
- } ,
774
- |this| visit:: walk_foreign_item ( this, foreign_item) ,
775
- )
776
- } ) ;
770
+ self . with_generic_param_rib (
771
+ & generics. params ,
772
+ ItemRibKind ( HasGenericParams :: Yes ( generics. span ) ) ,
773
+ LifetimeRibKind :: Generics {
774
+ binder : foreign_item. id ,
775
+ kind : LifetimeBinderKind :: Function ,
776
+ span : generics. span ,
777
+ } ,
778
+ |this| visit:: walk_foreign_item ( this, foreign_item) ,
779
+ ) ;
777
780
}
778
781
ForeignItemKind :: Static ( ..) => {
779
- self . with_item_rib ( |this| {
782
+ self . with_static_rib ( |this| {
780
783
visit:: walk_foreign_item ( this, foreign_item) ;
781
784
} ) ;
782
785
}
@@ -1391,9 +1394,8 @@ impl<'a: 'ast, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> {
1391
1394
return self . resolve_anonymous_lifetime ( lifetime, false ) ;
1392
1395
}
1393
1396
1394
- let mut indices = ( 0 ..self . lifetime_ribs . len ( ) ) . rev ( ) ;
1395
- for i in & mut indices {
1396
- let rib = & self . lifetime_ribs [ i] ;
1397
+ let mut lifetime_rib_iter = self . lifetime_ribs . iter ( ) . rev ( ) ;
1398
+ while let Some ( rib) = lifetime_rib_iter. next ( ) {
1397
1399
let normalized_ident = ident. normalize_to_macros_2_0 ( ) ;
1398
1400
if let Some ( & ( _, res) ) = rib. bindings . get ( & normalized_ident) {
1399
1401
self . record_lifetime_res ( lifetime. id , res, LifetimeElisionCandidate :: Named ) ;
@@ -1423,9 +1425,10 @@ impl<'a: 'ast, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> {
1423
1425
} else {
1424
1426
LifetimeUseSet :: Many
1425
1427
} ) ,
1426
- LifetimeRibKind :: Generics { .. }
1427
- | LifetimeRibKind :: ConstGeneric
1428
- | LifetimeRibKind :: AnonConst => None ,
1428
+ LifetimeRibKind :: Generics { .. } => None ,
1429
+ LifetimeRibKind :: ConstGeneric | LifetimeRibKind :: AnonConst => {
1430
+ span_bug ! ( ident. span, "unexpected rib kind: {:?}" , rib. kind)
1431
+ }
1429
1432
} )
1430
1433
. unwrap_or ( LifetimeUseSet :: Many ) ;
1431
1434
debug ! ( ?use_ctxt, ?use_set) ;
@@ -1460,13 +1463,16 @@ impl<'a: 'ast, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> {
1460
1463
) ;
1461
1464
return ;
1462
1465
}
1463
- _ => { }
1466
+ LifetimeRibKind :: AnonymousCreateParameter { .. }
1467
+ | LifetimeRibKind :: Elided ( _)
1468
+ | LifetimeRibKind :: Generics { .. }
1469
+ | LifetimeRibKind :: ElisionFailure
1470
+ | LifetimeRibKind :: AnonymousReportError => { }
1464
1471
}
1465
1472
}
1466
1473
1467
1474
let mut outer_res = None ;
1468
- for i in indices {
1469
- let rib = & self . lifetime_ribs [ i] ;
1475
+ for rib in lifetime_rib_iter {
1470
1476
let normalized_ident = ident. normalize_to_macros_2_0 ( ) ;
1471
1477
if let Some ( ( & outer, _) ) = rib. bindings . get_key_value ( & normalized_ident) {
1472
1478
outer_res = Some ( outer) ;
@@ -1493,8 +1499,7 @@ impl<'a: 'ast, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> {
1493
1499
count : 1 ,
1494
1500
} ;
1495
1501
let elision_candidate = LifetimeElisionCandidate :: Missing ( missing_lifetime) ;
1496
- for i in ( 0 ..self . lifetime_ribs . len ( ) ) . rev ( ) {
1497
- let rib = & mut self . lifetime_ribs [ i] ;
1502
+ for rib in self . lifetime_ribs . iter ( ) . rev ( ) {
1498
1503
debug ! ( ?rib. kind) ;
1499
1504
match rib. kind {
1500
1505
LifetimeRibKind :: AnonymousCreateParameter { binder, .. } => {
@@ -1534,9 +1539,11 @@ impl<'a: 'ast, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> {
1534
1539
return ;
1535
1540
}
1536
1541
LifetimeRibKind :: Item => break ,
1537
- LifetimeRibKind :: Generics { .. }
1538
- | LifetimeRibKind :: ConstGeneric
1539
- | LifetimeRibKind :: AnonConst => { }
1542
+ LifetimeRibKind :: Generics { .. } | LifetimeRibKind :: ConstGeneric => { }
1543
+ LifetimeRibKind :: AnonConst => {
1544
+ // There is always an `Elided(LifetimeRes::Static)` inside an `AnonConst`.
1545
+ span_bug ! ( lifetime. ident. span, "unexpected rib kind: {:?}" , rib. kind)
1546
+ }
1540
1547
}
1541
1548
}
1542
1549
self . record_lifetime_res ( lifetime. id , LifetimeRes :: Error , elision_candidate) ;
@@ -1751,9 +1758,11 @@ impl<'a: 'ast, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> {
1751
1758
self . report_missing_lifetime_specifiers ( vec ! [ missing_lifetime] , None ) ;
1752
1759
break ;
1753
1760
}
1754
- LifetimeRibKind :: Generics { .. }
1755
- | LifetimeRibKind :: ConstGeneric
1756
- | LifetimeRibKind :: AnonConst => { }
1761
+ LifetimeRibKind :: Generics { .. } | LifetimeRibKind :: ConstGeneric => { }
1762
+ LifetimeRibKind :: AnonConst => {
1763
+ // There is always an `Elided(LifetimeRes::Static)` inside an `AnonConst`.
1764
+ span_bug ! ( elided_lifetime_span, "unexpected rib kind: {:?}" , rib. kind)
1765
+ }
1757
1766
}
1758
1767
}
1759
1768
@@ -2204,7 +2213,7 @@ impl<'a: 'ast, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> {
2204
2213
}
2205
2214
2206
2215
ItemKind :: Static ( ref ty, _, ref expr) | ItemKind :: Const ( _, ref ty, ref expr) => {
2207
- self . with_item_rib ( |this| {
2216
+ self . with_static_rib ( |this| {
2208
2217
this. with_lifetime_rib ( LifetimeRibKind :: Elided ( LifetimeRes :: Static ) , |this| {
2209
2218
this. visit_ty ( ty) ;
2210
2219
} ) ;
@@ -2399,11 +2408,9 @@ impl<'a: 'ast, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> {
2399
2408
self . label_ribs . pop ( ) ;
2400
2409
}
2401
2410
2402
- fn with_item_rib ( & mut self , f : impl FnOnce ( & mut Self ) ) {
2411
+ fn with_static_rib ( & mut self , f : impl FnOnce ( & mut Self ) ) {
2403
2412
let kind = ItemRibKind ( HasGenericParams :: No ) ;
2404
- self . with_lifetime_rib ( LifetimeRibKind :: Item , |this| {
2405
- this. with_rib ( ValueNS , kind, |this| this. with_rib ( TypeNS , kind, f) )
2406
- } )
2413
+ self . with_rib ( ValueNS , kind, |this| this. with_rib ( TypeNS , kind, f) )
2407
2414
}
2408
2415
2409
2416
// HACK(min_const_generics,const_evaluatable_unchecked): We
@@ -3938,7 +3945,7 @@ impl<'a: 'ast, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> {
3938
3945
fn_id : NodeId ,
3939
3946
async_node_id : Option < ( NodeId , Span ) > ,
3940
3947
) {
3941
- if let Some ( ( async_node_id, _ ) ) = async_node_id {
3948
+ if let Some ( ( async_node_id, span ) ) = async_node_id {
3942
3949
let mut extra_lifetime_params =
3943
3950
self . r . extra_lifetime_params_map . get ( & fn_id) . cloned ( ) . unwrap_or_default ( ) ;
3944
3951
for rib in self . lifetime_ribs . iter ( ) . rev ( ) {
@@ -3952,7 +3959,12 @@ impl<'a: 'ast, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> {
3952
3959
extra_lifetime_params. extend ( earlier_fresh) ;
3953
3960
}
3954
3961
}
3955
- _ => { }
3962
+ LifetimeRibKind :: Generics { .. } => { }
3963
+ _ => {
3964
+ // We are in a function definition. We should only find `Generics`
3965
+ // and `AnonymousCreateParameter` inside the innermost `Item`.
3966
+ span_bug ! ( span, "unexpected rib kind: {:?}" , rib. kind)
3967
+ }
3956
3968
}
3957
3969
}
3958
3970
self . r . extra_lifetime_params_map . insert ( async_node_id, extra_lifetime_params) ;
0 commit comments