@@ -28,7 +28,7 @@ use std::borrow::Cow;
2828use std:: cell:: Cell ;
2929use std:: mem:: take;
3030
31- use tracing:: debug ;
31+ use tracing:: * ;
3232
3333// This counts the no of times a lifetime is used
3434#[ derive( Clone , Copy , Debug ) ]
@@ -192,7 +192,7 @@ crate struct LifetimeContext<'a, 'tcx> {
192192#[ derive( Debug ) ]
193193enum Scope < ' a > {
194194 /// Declares lifetimes, and each can be early-bound or late-bound.
195- /// The `DebruijnIndex` of late-bound lifetimes starts at `1 ` and
195+ /// The `DebruijnIndex` of late-bound lifetimes starts at `0 ` and
196196 /// it should be shifted by the number of `Binder`s in between the
197197 /// declaration `Binder` and the location it's referenced from.
198198 Binder {
@@ -207,6 +207,12 @@ enum Scope<'a> {
207207 /// impls, but not other kinds of items.
208208 track_lifetime_uses : bool ,
209209
210+ /// Whether these lifetimes are synthetic and only added
211+ /// for anon consts.
212+ ///
213+ /// We do not emit lints in `check_uses_for_lifetimes_defined_by_scope`.
214+ from_anon_const : bool ,
215+
210216 /// Whether or not this binder would serve as the parent
211217 /// binder for opaque types introduced within. For example:
212218 ///
@@ -467,6 +473,7 @@ impl<'a, 'tcx> Visitor<'tcx> for LifetimeContext<'a, 'tcx> {
467473 lifetimes,
468474 next_early_index : index + non_lifetime_count,
469475 opaque_type_parent : true ,
476+ from_anon_const : false ,
470477 track_lifetime_uses,
471478 s : ROOT_SCOPE ,
472479 } ;
@@ -480,9 +487,36 @@ impl<'a, 'tcx> Visitor<'tcx> for LifetimeContext<'a, 'tcx> {
480487 }
481488
482489 fn visit_anon_const ( & mut self , ct : & ' tcx hir:: AnonConst < ' tcx > ) {
483- self . visit_generics ( & ct. generics ) ;
484490 self . visit_generic_args ( crate :: DUMMY_SP , & ct. generic_args ) ;
485- intravisit:: walk_anon_const ( self , ct) ;
491+
492+ let generics = & ct. generics ;
493+ let mut index = self . next_early_index ( ) ;
494+ debug ! ( "visit_anon_const: index = {}" , index) ;
495+ let lifetimes = generics
496+ . params
497+ . iter ( )
498+ . map ( |param| match param. kind {
499+ GenericParamKind :: Lifetime { .. } => {
500+ Region :: early ( & self . tcx . hir ( ) , & mut index, param)
501+ }
502+ GenericParamKind :: Type { .. } | GenericParamKind :: Const { .. } => {
503+ bug ! ( "unexpected param: {:?}" , param)
504+ }
505+ } )
506+ . collect ( ) ;
507+ let scope = Scope :: Binder {
508+ lifetimes,
509+ next_early_index : index,
510+ s : self . scope ,
511+ track_lifetime_uses : true ,
512+ from_anon_const : true ,
513+ opaque_type_parent : true ,
514+ } ;
515+ self . with ( scope, |_, this| {
516+ this. visit_id ( ct. hir_id ) ;
517+ this. visit_generics ( generics) ;
518+ this. visit_nested_body ( ct. body ) ;
519+ } ) ;
486520 }
487521
488522 fn visit_foreign_item ( & mut self , item : & ' tcx hir:: ForeignItem < ' tcx > ) {
@@ -535,6 +569,7 @@ impl<'a, 'tcx> Visitor<'tcx> for LifetimeContext<'a, 'tcx> {
535569 s : self . scope ,
536570 next_early_index,
537571 track_lifetime_uses : true ,
572+ from_anon_const : false ,
538573 opaque_type_parent : false ,
539574 } ;
540575 self . with ( scope, |old_scope, this| {
@@ -708,6 +743,7 @@ impl<'a, 'tcx> Visitor<'tcx> for LifetimeContext<'a, 'tcx> {
708743 next_early_index,
709744 s : this. scope ,
710745 track_lifetime_uses : true ,
746+ from_anon_const : false ,
711747 opaque_type_parent : false ,
712748 } ;
713749 this. with ( scope, |_old_scope, this| {
@@ -723,6 +759,7 @@ impl<'a, 'tcx> Visitor<'tcx> for LifetimeContext<'a, 'tcx> {
723759 next_early_index,
724760 s : self . scope ,
725761 track_lifetime_uses : true ,
762+ from_anon_const : false ,
726763 opaque_type_parent : false ,
727764 } ;
728765 self . with ( scope, |_old_scope, this| {
@@ -775,6 +812,7 @@ impl<'a, 'tcx> Visitor<'tcx> for LifetimeContext<'a, 'tcx> {
775812 next_early_index : index + non_lifetime_count,
776813 s : self . scope ,
777814 track_lifetime_uses : true ,
815+ from_anon_const : false ,
778816 opaque_type_parent : true ,
779817 } ;
780818 self . with ( scope, |old_scope, this| {
@@ -837,6 +875,7 @@ impl<'a, 'tcx> Visitor<'tcx> for LifetimeContext<'a, 'tcx> {
837875 next_early_index : index + non_lifetime_count,
838876 s : self . scope ,
839877 track_lifetime_uses : true ,
878+ from_anon_const : false ,
840879 opaque_type_parent : true ,
841880 } ;
842881 self . with ( scope, |old_scope, this| {
@@ -934,6 +973,7 @@ impl<'a, 'tcx> Visitor<'tcx> for LifetimeContext<'a, 'tcx> {
934973 s : self . scope ,
935974 next_early_index,
936975 track_lifetime_uses : true ,
976+ from_anon_const : false ,
937977 opaque_type_parent : false ,
938978 } ;
939979 let result = self . with ( scope, |old_scope, this| {
@@ -977,6 +1017,7 @@ impl<'a, 'tcx> Visitor<'tcx> for LifetimeContext<'a, 'tcx> {
9771017 s : self . scope ,
9781018 next_early_index : self . next_early_index ( ) ,
9791019 track_lifetime_uses : true ,
1020+ from_anon_const : false ,
9801021 opaque_type_parent : false ,
9811022 } ;
9821023 self . with ( scope, |_, this| {
@@ -1027,6 +1068,7 @@ impl<'a, 'tcx> Visitor<'tcx> for LifetimeContext<'a, 'tcx> {
10271068 s : self . scope ,
10281069 next_early_index,
10291070 track_lifetime_uses : true ,
1071+ from_anon_const : false ,
10301072 opaque_type_parent : false ,
10311073 } ;
10321074 self . with ( scope, |old_scope, this| {
@@ -1370,6 +1412,7 @@ impl<'a, 'tcx> LifetimeContext<'a, 'tcx> {
13701412 f ( self )
13711413 }
13721414
1415+ #[ instrument( skip( self , f) ) ]
13731416 fn with < F > ( & mut self , wrap_scope : Scope < ' _ > , f : F )
13741417 where
13751418 F : for < ' b > FnOnce ( ScopeRef < ' _ > , & mut LifetimeContext < ' b , ' tcx > ) ,
@@ -1390,10 +1433,8 @@ impl<'a, 'tcx> LifetimeContext<'a, 'tcx> {
13901433 lifetime_uses,
13911434 missing_named_lifetime_spots,
13921435 } ;
1393- debug ! ( "entering scope {:?}" , this. scope) ;
13941436 f ( self . scope , & mut this) ;
13951437 this. check_uses_for_lifetimes_defined_by_scope ( ) ;
1396- debug ! ( "exiting scope {:?}" , this. scope) ;
13971438 self . labels_in_fn = this. labels_in_fn ;
13981439 self . xcrate_object_lifetime_defaults = this. xcrate_object_lifetime_defaults ;
13991440 self . missing_named_lifetime_spots = this. missing_named_lifetime_spots ;
@@ -1537,6 +1578,10 @@ impl<'a, 'tcx> LifetimeContext<'a, 'tcx> {
15371578
15381579 fn check_uses_for_lifetimes_defined_by_scope ( & mut self ) {
15391580 let defined_by = match self . scope {
1581+ Scope :: Binder { from_anon_const : true , .. } => {
1582+ debug ! ( "check_uses_for_lifetimes_defined_by_scope: synthetic anon const binder" ) ;
1583+ return ;
1584+ }
15401585 Scope :: Binder { lifetimes, .. } => lifetimes,
15411586 _ => {
15421587 debug ! ( "check_uses_for_lifetimes_defined_by_scope: not in a binder scope" ) ;
@@ -1743,6 +1788,7 @@ impl<'a, 'tcx> LifetimeContext<'a, 'tcx> {
17431788 next_early_index,
17441789 s : self . scope ,
17451790 opaque_type_parent : true ,
1791+ from_anon_const : false ,
17461792 track_lifetime_uses : false ,
17471793 } ;
17481794 self . with ( scope, move |old_scope, this| {
@@ -2342,6 +2388,13 @@ impl<'a, 'tcx> LifetimeContext<'a, 'tcx> {
23422388 self . outer_index . shift_out ( 1 ) ;
23432389 }
23442390
2391+ fn visit_anon_const ( & mut self , _ct : & hir:: AnonConst < ' _ > ) {
2392+ // Do not look inside of anonymous constants, they should
2393+ // not participate in lifetime elision.
2394+
2395+ // FIXME(const_generics): is this true?
2396+ }
2397+
23452398 fn visit_param_bound ( & mut self , bound : & hir:: GenericBound < ' _ > ) {
23462399 if let hir:: GenericBound :: LangItemTrait { .. } = bound {
23472400 self . outer_index . shift_in ( 1 ) ;
0 commit comments