@@ -16,11 +16,11 @@ use rustc_hir::def_id::{DefIdMap, LocalDefId};
16
16
use rustc_hir:: hir_id:: ItemLocalId ;
17
17
use rustc_hir:: intravisit:: { self , Visitor } ;
18
18
use rustc_hir:: { GenericArg , GenericParam , LifetimeName , Node } ;
19
- use rustc_hir:: { GenericParamKind , HirIdMap , HirIdSet } ;
19
+ use rustc_hir:: { GenericParamKind , HirIdMap } ;
20
20
use rustc_middle:: hir:: map:: Map ;
21
21
use rustc_middle:: hir:: nested_filter;
22
22
use rustc_middle:: middle:: resolve_lifetime:: * ;
23
- use rustc_middle:: ty:: { self , DefIdTree , GenericParamDefKind , TyCtxt } ;
23
+ use rustc_middle:: ty:: { self , GenericParamDefKind , TyCtxt } ;
24
24
use rustc_middle:: { bug, span_bug} ;
25
25
use rustc_span:: def_id:: DefId ;
26
26
use rustc_span:: symbol:: { kw, sym, Ident } ;
@@ -134,11 +134,6 @@ struct NamedRegionMap {
134
134
// `Region` describing how that region is bound
135
135
defs : HirIdMap < Region > ,
136
136
137
- // the set of lifetime def ids that are late-bound; a region can
138
- // be late-bound if (a) it does NOT appear in a where-clause and
139
- // (b) it DOES appear in the arguments.
140
- late_bound : HirIdSet ,
141
-
142
137
// Maps relevant hir items to the bound vars on them. These include:
143
138
// - function defs
144
139
// - function pointers
@@ -402,15 +397,15 @@ fn resolve_lifetimes_trait_definition(
402
397
tcx : TyCtxt < ' _ > ,
403
398
local_def_id : LocalDefId ,
404
399
) -> ResolveLifetimes {
405
- convert_named_region_map ( tcx , do_resolve ( tcx, local_def_id, true , false ) )
400
+ convert_named_region_map ( do_resolve ( tcx, local_def_id, true , false ) )
406
401
}
407
402
408
403
/// Computes the `ResolveLifetimes` map that contains data for an entire `Item`.
409
404
/// You should not read the result of this query directly, but rather use
410
405
/// `named_region_map`, `is_late_bound_map`, etc.
411
406
#[ tracing:: instrument( level = "debug" , skip( tcx) ) ]
412
407
fn resolve_lifetimes ( tcx : TyCtxt < ' _ > , local_def_id : LocalDefId ) -> ResolveLifetimes {
413
- convert_named_region_map ( tcx , do_resolve ( tcx, local_def_id, false , false ) )
408
+ convert_named_region_map ( do_resolve ( tcx, local_def_id, false , false ) )
414
409
}
415
410
416
411
fn do_resolve (
@@ -422,7 +417,6 @@ fn do_resolve(
422
417
let item = tcx. hir ( ) . expect_item ( local_def_id) ;
423
418
let mut named_region_map = NamedRegionMap {
424
419
defs : Default :: default ( ) ,
425
- late_bound : Default :: default ( ) ,
426
420
late_bound_vars : Default :: default ( ) ,
427
421
scope_for_path : with_scope_for_path. then ( || Default :: default ( ) ) ,
428
422
} ;
@@ -439,18 +433,13 @@ fn do_resolve(
439
433
named_region_map
440
434
}
441
435
442
- fn convert_named_region_map ( tcx : TyCtxt < ' _ > , named_region_map : NamedRegionMap ) -> ResolveLifetimes {
436
+ fn convert_named_region_map ( named_region_map : NamedRegionMap ) -> ResolveLifetimes {
443
437
let mut rl = ResolveLifetimes :: default ( ) ;
444
438
445
439
for ( hir_id, v) in named_region_map. defs {
446
440
let map = rl. defs . entry ( hir_id. owner ) . or_default ( ) ;
447
441
map. insert ( hir_id. local_id , v) ;
448
442
}
449
- for hir_id in named_region_map. late_bound {
450
- let map = rl. late_bound . entry ( hir_id. owner ) . or_default ( ) ;
451
- let def_id = tcx. hir ( ) . local_def_id ( hir_id) ;
452
- map. insert ( def_id) ;
453
- }
454
443
for ( hir_id, v) in named_region_map. late_bound_vars {
455
444
let map = rl. late_bound_vars . entry ( hir_id. owner ) . or_default ( ) ;
456
445
map. insert ( hir_id. local_id , v) ;
@@ -506,28 +495,6 @@ fn item_for(tcx: TyCtxt<'_>, local_def_id: LocalDefId) -> LocalDefId {
506
495
item
507
496
}
508
497
509
- fn is_late_bound_map < ' tcx > (
510
- tcx : TyCtxt < ' tcx > ,
511
- def_id : LocalDefId ,
512
- ) -> Option < ( LocalDefId , & ' tcx FxHashSet < LocalDefId > ) > {
513
- match tcx. def_kind ( def_id) {
514
- DefKind :: AnonConst | DefKind :: InlineConst => {
515
- let mut def_id = tcx. local_parent ( def_id) ;
516
- // We search for the next outer anon const or fn here
517
- // while skipping closures.
518
- //
519
- // Note that for `AnonConst` we still just recurse until we
520
- // find a function body, but who cares :shrug:
521
- while tcx. is_closure ( def_id. to_def_id ( ) ) {
522
- def_id = tcx. local_parent ( def_id) ;
523
- }
524
-
525
- tcx. is_late_bound_map ( def_id)
526
- }
527
- _ => resolve_lifetimes_for ( tcx, def_id) . late_bound . get ( & def_id) . map ( |lt| ( def_id, lt) ) ,
528
- }
529
- }
530
-
531
498
/// In traits, there is an implicit `Self` type parameter which comes before the generics.
532
499
/// We have to account for this when computing the index of the other generic parameters.
533
500
/// This function returns whether there is such an implicit parameter defined on the given item.
@@ -687,9 +654,9 @@ impl<'a, 'tcx> Visitor<'tcx> for LifetimeContext<'a, 'tcx> {
687
654
_ => { }
688
655
}
689
656
match item. kind {
690
- hir:: ItemKind :: Fn ( ref sig , ref generics, _) => {
657
+ hir:: ItemKind :: Fn ( _ , ref generics, _) => {
691
658
self . missing_named_lifetime_spots . push ( generics. into ( ) ) ;
692
- self . visit_early_late ( None , item. hir_id ( ) , & sig . decl , generics, |this| {
659
+ self . visit_early_late ( None , item. hir_id ( ) , generics, |this| {
693
660
intravisit:: walk_item ( this, item) ;
694
661
} ) ;
695
662
self . missing_named_lifetime_spots . pop ( ) ;
@@ -731,13 +698,6 @@ impl<'a, 'tcx> Visitor<'tcx> for LifetimeContext<'a, 'tcx> {
731
698
self . map . defs . insert ( hir:: HirId { owner, local_id } , * region) ;
732
699
} ) ;
733
700
}
734
- for ( & owner, late_bound) in resolved_lifetimes. late_bound . iter ( ) {
735
- late_bound. iter ( ) . for_each ( |& id| {
736
- let hir_id = self . tcx . local_def_id_to_hir_id ( id) ;
737
- debug_assert_eq ! ( owner, hir_id. owner) ;
738
- self . map . late_bound . insert ( hir_id) ;
739
- } ) ;
740
- }
741
701
for ( & owner, late_bound_vars) in
742
702
resolved_lifetimes. late_bound_vars . iter ( )
743
703
{
@@ -807,8 +767,8 @@ impl<'a, 'tcx> Visitor<'tcx> for LifetimeContext<'a, 'tcx> {
807
767
808
768
fn visit_foreign_item ( & mut self , item : & ' tcx hir:: ForeignItem < ' tcx > ) {
809
769
match item. kind {
810
- hir:: ForeignItemKind :: Fn ( ref decl , _, ref generics) => {
811
- self . visit_early_late ( None , item. hir_id ( ) , decl , generics, |this| {
770
+ hir:: ForeignItemKind :: Fn ( _ , _, ref generics) => {
771
+ self . visit_early_late ( None , item. hir_id ( ) , generics, |this| {
812
772
intravisit:: walk_foreign_item ( this, item) ;
813
773
} )
814
774
}
@@ -1085,13 +1045,12 @@ impl<'a, 'tcx> Visitor<'tcx> for LifetimeContext<'a, 'tcx> {
1085
1045
fn visit_trait_item ( & mut self , trait_item : & ' tcx hir:: TraitItem < ' tcx > ) {
1086
1046
use self :: hir:: TraitItemKind :: * ;
1087
1047
match trait_item. kind {
1088
- Fn ( ref sig , _) => {
1048
+ Fn ( _ , _) => {
1089
1049
self . missing_named_lifetime_spots . push ( ( & trait_item. generics ) . into ( ) ) ;
1090
1050
let tcx = self . tcx ;
1091
1051
self . visit_early_late (
1092
1052
Some ( tcx. hir ( ) . get_parent_item ( trait_item. hir_id ( ) ) ) ,
1093
1053
trait_item. hir_id ( ) ,
1094
- & sig. decl ,
1095
1054
& trait_item. generics ,
1096
1055
|this| intravisit:: walk_trait_item ( this, trait_item) ,
1097
1056
) ;
@@ -1153,13 +1112,12 @@ impl<'a, 'tcx> Visitor<'tcx> for LifetimeContext<'a, 'tcx> {
1153
1112
fn visit_impl_item ( & mut self , impl_item : & ' tcx hir:: ImplItem < ' tcx > ) {
1154
1113
use self :: hir:: ImplItemKind :: * ;
1155
1114
match impl_item. kind {
1156
- Fn ( ref sig , _ ) => {
1115
+ Fn ( .. ) => {
1157
1116
self . missing_named_lifetime_spots . push ( ( & impl_item. generics ) . into ( ) ) ;
1158
1117
let tcx = self . tcx ;
1159
1118
self . visit_early_late (
1160
1119
Some ( tcx. hir ( ) . get_parent_item ( impl_item. hir_id ( ) ) ) ,
1161
1120
impl_item. hir_id ( ) ,
1162
- & sig. decl ,
1163
1121
& impl_item. generics ,
1164
1122
|this| intravisit:: walk_impl_item ( this, impl_item) ,
1165
1123
) ;
@@ -1656,14 +1614,11 @@ impl<'a, 'tcx> LifetimeContext<'a, 'tcx> {
1656
1614
& mut self ,
1657
1615
parent_id : Option < LocalDefId > ,
1658
1616
hir_id : hir:: HirId ,
1659
- decl : & ' tcx hir:: FnDecl < ' tcx > ,
1660
1617
generics : & ' tcx hir:: Generics < ' tcx > ,
1661
1618
walk : F ,
1662
1619
) where
1663
1620
F : for <' b , ' c > FnOnce ( & ' b mut LifetimeContext < ' c , ' tcx > ) ,
1664
1621
{
1665
- insert_late_bound_lifetimes ( self . tcx , self . map , decl, generics) ;
1666
-
1667
1622
// Find the start of nested early scopes, e.g., in methods.
1668
1623
let mut next_early_index = 0 ;
1669
1624
if let Some ( parent_id) = parent_id {
@@ -1687,7 +1642,7 @@ impl<'a, 'tcx> LifetimeContext<'a, 'tcx> {
1687
1642
. iter ( )
1688
1643
. filter_map ( |param| match param. kind {
1689
1644
GenericParamKind :: Lifetime { .. } => {
1690
- if self . map . late_bound . contains ( & param. hir_id ) {
1645
+ if self . tcx . is_late_bound ( param. hir_id ) {
1691
1646
let late_bound_idx = named_late_bound_vars;
1692
1647
named_late_bound_vars += 1 ;
1693
1648
Some ( Region :: late ( late_bound_idx, self . tcx . hir ( ) , param) )
@@ -1708,7 +1663,7 @@ impl<'a, 'tcx> LifetimeContext<'a, 'tcx> {
1708
1663
. iter ( )
1709
1664
. filter ( |param| {
1710
1665
matches ! ( param. kind, GenericParamKind :: Lifetime { .. } )
1711
- && self . map . late_bound . contains ( & param. hir_id )
1666
+ && self . tcx . is_late_bound ( param. hir_id )
1712
1667
} )
1713
1668
. enumerate ( )
1714
1669
. map ( |( late_bound_idx, param) | {
@@ -2613,7 +2568,7 @@ impl<'a, 'tcx> LifetimeContext<'a, 'tcx> {
2613
2568
}
2614
2569
2615
2570
/// Detects late-bound lifetimes and inserts them into
2616
- /// `map. late_bound`.
2571
+ /// `late_bound`.
2617
2572
///
2618
2573
/// A region declared on a fn is **late-bound** if:
2619
2574
/// - it is constrained by an argument type;
@@ -2622,13 +2577,13 @@ impl<'a, 'tcx> LifetimeContext<'a, 'tcx> {
2622
2577
/// "Constrained" basically means that it appears in any type but
2623
2578
/// not amongst the inputs to a projection. In other words, `<&'a
2624
2579
/// T as Trait<''b>>::Foo` does not constrain `'a` or `'b`.
2625
- # [ tracing :: instrument ( level = "debug" , skip ( tcx, map ) ) ]
2626
- fn insert_late_bound_lifetimes (
2627
- tcx : TyCtxt < ' _ > ,
2628
- map : & mut NamedRegionMap ,
2629
- decl : & hir :: FnDecl < ' _ > ,
2630
- generics : & hir :: Generics < ' _ > ,
2631
- ) {
2580
+ fn is_late_bound_map ( tcx : TyCtxt < ' _ > , def_id : LocalDefId ) -> Option < & FxHashSet < LocalDefId > > {
2581
+ let hir_id = tcx . hir ( ) . local_def_id_to_hir_id ( def_id ) ;
2582
+ let decl = tcx . hir ( ) . fn_decl_by_hir_id ( hir_id ) ? ;
2583
+ let generics = tcx . hir ( ) . get_generics ( def_id ) ? ;
2584
+
2585
+ let mut late_bound = FxHashSet :: default ( ) ;
2586
+
2632
2587
let mut constrained_by_input = ConstrainedCollector :: default ( ) ;
2633
2588
for arg_ty in decl. inputs {
2634
2589
constrained_by_input. visit_ty ( arg_ty) ;
@@ -2675,11 +2630,12 @@ fn insert_late_bound_lifetimes(
2675
2630
2676
2631
debug ! ( "lifetime {:?} with id {:?} is late-bound" , param. name. ident( ) , param. hir_id) ;
2677
2632
2678
- let inserted = map . late_bound . insert ( param . hir_id ) ;
2633
+ let inserted = late_bound. insert ( param_def_id ) ;
2679
2634
assert ! ( inserted, "visited lifetime {:?} twice" , param. hir_id) ;
2680
2635
}
2681
2636
2682
- return ;
2637
+ debug ! ( ?late_bound) ;
2638
+ return Some ( tcx. arena . alloc ( late_bound) ) ;
2683
2639
2684
2640
#[ derive( Default ) ]
2685
2641
struct ConstrainedCollector {
0 commit comments