@@ -28,7 +28,7 @@ use std::borrow::Cow;
28
28
use std:: cell:: Cell ;
29
29
use std:: mem:: take;
30
30
31
- use tracing:: debug ;
31
+ use tracing:: * ;
32
32
33
33
// This counts the no of times a lifetime is used
34
34
#[ derive( Clone , Copy , Debug ) ]
@@ -192,7 +192,7 @@ crate struct LifetimeContext<'a, 'tcx> {
192
192
#[ derive( Debug ) ]
193
193
enum Scope < ' a > {
194
194
/// 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
196
196
/// it should be shifted by the number of `Binder`s in between the
197
197
/// declaration `Binder` and the location it's referenced from.
198
198
Binder {
@@ -207,6 +207,12 @@ enum Scope<'a> {
207
207
/// impls, but not other kinds of items.
208
208
track_lifetime_uses : bool ,
209
209
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
+
210
216
/// Whether or not this binder would serve as the parent
211
217
/// binder for opaque types introduced within. For example:
212
218
///
@@ -467,6 +473,7 @@ impl<'a, 'tcx> Visitor<'tcx> for LifetimeContext<'a, 'tcx> {
467
473
lifetimes,
468
474
next_early_index : index + non_lifetime_count,
469
475
opaque_type_parent : true ,
476
+ from_anon_const : false ,
470
477
track_lifetime_uses,
471
478
s : ROOT_SCOPE ,
472
479
} ;
@@ -480,9 +487,36 @@ impl<'a, 'tcx> Visitor<'tcx> for LifetimeContext<'a, 'tcx> {
480
487
}
481
488
482
489
fn visit_anon_const ( & mut self , ct : & ' tcx hir:: AnonConst < ' tcx > ) {
483
- self . visit_generics ( & ct. generics ) ;
484
490
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
+ } ) ;
486
520
}
487
521
488
522
fn visit_foreign_item ( & mut self , item : & ' tcx hir:: ForeignItem < ' tcx > ) {
@@ -535,6 +569,7 @@ impl<'a, 'tcx> Visitor<'tcx> for LifetimeContext<'a, 'tcx> {
535
569
s : self . scope ,
536
570
next_early_index,
537
571
track_lifetime_uses : true ,
572
+ from_anon_const : false ,
538
573
opaque_type_parent : false ,
539
574
} ;
540
575
self . with ( scope, |old_scope, this| {
@@ -708,6 +743,7 @@ impl<'a, 'tcx> Visitor<'tcx> for LifetimeContext<'a, 'tcx> {
708
743
next_early_index,
709
744
s : this. scope ,
710
745
track_lifetime_uses : true ,
746
+ from_anon_const : false ,
711
747
opaque_type_parent : false ,
712
748
} ;
713
749
this. with ( scope, |_old_scope, this| {
@@ -723,6 +759,7 @@ impl<'a, 'tcx> Visitor<'tcx> for LifetimeContext<'a, 'tcx> {
723
759
next_early_index,
724
760
s : self . scope ,
725
761
track_lifetime_uses : true ,
762
+ from_anon_const : false ,
726
763
opaque_type_parent : false ,
727
764
} ;
728
765
self . with ( scope, |_old_scope, this| {
@@ -775,6 +812,7 @@ impl<'a, 'tcx> Visitor<'tcx> for LifetimeContext<'a, 'tcx> {
775
812
next_early_index : index + non_lifetime_count,
776
813
s : self . scope ,
777
814
track_lifetime_uses : true ,
815
+ from_anon_const : false ,
778
816
opaque_type_parent : true ,
779
817
} ;
780
818
self . with ( scope, |old_scope, this| {
@@ -837,6 +875,7 @@ impl<'a, 'tcx> Visitor<'tcx> for LifetimeContext<'a, 'tcx> {
837
875
next_early_index : index + non_lifetime_count,
838
876
s : self . scope ,
839
877
track_lifetime_uses : true ,
878
+ from_anon_const : false ,
840
879
opaque_type_parent : true ,
841
880
} ;
842
881
self . with ( scope, |old_scope, this| {
@@ -934,6 +973,7 @@ impl<'a, 'tcx> Visitor<'tcx> for LifetimeContext<'a, 'tcx> {
934
973
s : self . scope ,
935
974
next_early_index,
936
975
track_lifetime_uses : true ,
976
+ from_anon_const : false ,
937
977
opaque_type_parent : false ,
938
978
} ;
939
979
let result = self . with ( scope, |old_scope, this| {
@@ -977,6 +1017,7 @@ impl<'a, 'tcx> Visitor<'tcx> for LifetimeContext<'a, 'tcx> {
977
1017
s : self . scope ,
978
1018
next_early_index : self . next_early_index ( ) ,
979
1019
track_lifetime_uses : true ,
1020
+ from_anon_const : false ,
980
1021
opaque_type_parent : false ,
981
1022
} ;
982
1023
self . with ( scope, |_, this| {
@@ -1027,6 +1068,7 @@ impl<'a, 'tcx> Visitor<'tcx> for LifetimeContext<'a, 'tcx> {
1027
1068
s : self . scope ,
1028
1069
next_early_index,
1029
1070
track_lifetime_uses : true ,
1071
+ from_anon_const : false ,
1030
1072
opaque_type_parent : false ,
1031
1073
} ;
1032
1074
self . with ( scope, |old_scope, this| {
@@ -1370,6 +1412,7 @@ impl<'a, 'tcx> LifetimeContext<'a, 'tcx> {
1370
1412
f ( self )
1371
1413
}
1372
1414
1415
+ #[ instrument( skip( self , f) ) ]
1373
1416
fn with < F > ( & mut self , wrap_scope : Scope < ' _ > , f : F )
1374
1417
where
1375
1418
F : for < ' b > FnOnce ( ScopeRef < ' _ > , & mut LifetimeContext < ' b , ' tcx > ) ,
@@ -1390,10 +1433,8 @@ impl<'a, 'tcx> LifetimeContext<'a, 'tcx> {
1390
1433
lifetime_uses,
1391
1434
missing_named_lifetime_spots,
1392
1435
} ;
1393
- debug ! ( "entering scope {:?}" , this. scope) ;
1394
1436
f ( self . scope , & mut this) ;
1395
1437
this. check_uses_for_lifetimes_defined_by_scope ( ) ;
1396
- debug ! ( "exiting scope {:?}" , this. scope) ;
1397
1438
self . labels_in_fn = this. labels_in_fn ;
1398
1439
self . xcrate_object_lifetime_defaults = this. xcrate_object_lifetime_defaults ;
1399
1440
self . missing_named_lifetime_spots = this. missing_named_lifetime_spots ;
@@ -1537,6 +1578,10 @@ impl<'a, 'tcx> LifetimeContext<'a, 'tcx> {
1537
1578
1538
1579
fn check_uses_for_lifetimes_defined_by_scope ( & mut self ) {
1539
1580
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
+ }
1540
1585
Scope :: Binder { lifetimes, .. } => lifetimes,
1541
1586
_ => {
1542
1587
debug ! ( "check_uses_for_lifetimes_defined_by_scope: not in a binder scope" ) ;
@@ -1743,6 +1788,7 @@ impl<'a, 'tcx> LifetimeContext<'a, 'tcx> {
1743
1788
next_early_index,
1744
1789
s : self . scope ,
1745
1790
opaque_type_parent : true ,
1791
+ from_anon_const : false ,
1746
1792
track_lifetime_uses : false ,
1747
1793
} ;
1748
1794
self . with ( scope, move |old_scope, this| {
@@ -2342,6 +2388,13 @@ impl<'a, 'tcx> LifetimeContext<'a, 'tcx> {
2342
2388
self . outer_index . shift_out ( 1 ) ;
2343
2389
}
2344
2390
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
+
2345
2398
fn visit_param_bound ( & mut self , bound : & hir:: GenericBound < ' _ > ) {
2346
2399
if let hir:: GenericBound :: LangItemTrait { .. } = bound {
2347
2400
self . outer_index . shift_in ( 1 ) ;
0 commit comments