@@ -11,10 +11,13 @@ use std::fmt;
11
11
12
12
use rustc_ast:: visit:: walk_list;
13
13
use rustc_data_structures:: fx:: { FxHashSet , FxIndexMap , FxIndexSet } ;
14
+ use rustc_data_structures:: sorted_map:: SortedMap ;
14
15
use rustc_hir as hir;
15
16
use rustc_hir:: def:: { DefKind , Res } ;
16
17
use rustc_hir:: intravisit:: { self , Visitor } ;
17
- use rustc_hir:: { GenericArg , GenericParam , GenericParamKind , HirId , HirIdMap , LifetimeName , Node } ;
18
+ use rustc_hir:: {
19
+ GenericArg , GenericParam , GenericParamKind , HirId , ItemLocalMap , LifetimeName , Node ,
20
+ } ;
18
21
use rustc_macros:: extension;
19
22
use rustc_middle:: hir:: nested_filter;
20
23
use rustc_middle:: middle:: resolve_bound_vars:: * ;
@@ -73,15 +76,15 @@ impl ResolvedArg {
73
76
struct NamedVarMap {
74
77
// maps from every use of a named (not anonymous) bound var to a
75
78
// `ResolvedArg` describing how that variable is bound
76
- defs : HirIdMap < ResolvedArg > ,
79
+ defs : ItemLocalMap < ResolvedArg > ,
77
80
78
81
// Maps relevant hir items to the bound vars on them. These include:
79
82
// - function defs
80
83
// - function pointers
81
84
// - closures
82
85
// - trait refs
83
86
// - bound types (like `T` in `for<'a> T<'a>: Foo`)
84
- late_bound_vars : HirIdMap < Vec < ty:: BoundVariableKind > > ,
87
+ late_bound_vars : ItemLocalMap < Vec < ty:: BoundVariableKind > > ,
85
88
}
86
89
87
90
struct BoundVarContext < ' a , ' tcx > {
@@ -224,10 +227,10 @@ pub(crate) fn provide(providers: &mut Providers) {
224
227
* providers = Providers {
225
228
resolve_bound_vars,
226
229
227
- named_variable_map : |tcx, id| tcx. resolve_bound_vars ( id) . defs . get ( & id ) ,
230
+ named_variable_map : |tcx, id| & tcx. resolve_bound_vars ( id) . defs ,
228
231
is_late_bound_map,
229
232
object_lifetime_default,
230
- late_bound_vars_map : |tcx, id| tcx. resolve_bound_vars ( id) . late_bound_vars . get ( & id ) ,
233
+ late_bound_vars_map : |tcx, id| & tcx. resolve_bound_vars ( id) . late_bound_vars ,
231
234
232
235
..* providers
233
236
} ;
@@ -264,16 +267,12 @@ fn resolve_bound_vars(tcx: TyCtxt<'_>, local_def_id: hir::OwnerId) -> ResolveBou
264
267
hir:: OwnerNode :: Synthetic => unreachable ! ( ) ,
265
268
}
266
269
267
- let mut rl = ResolveBoundVars :: default ( ) ;
268
-
269
- for ( hir_id, v) in named_variable_map. defs {
270
- let map = rl. defs . entry ( hir_id. owner ) . or_default ( ) ;
271
- map. insert ( hir_id. local_id , v) ;
272
- }
273
- for ( hir_id, v) in named_variable_map. late_bound_vars {
274
- let map = rl. late_bound_vars . entry ( hir_id. owner ) . or_default ( ) ;
275
- map. insert ( hir_id. local_id , v) ;
276
- }
270
+ let defs = named_variable_map. defs . into_sorted_stable_ord ( ) ;
271
+ let late_bound_vars = named_variable_map. late_bound_vars . into_sorted_stable_ord ( ) ;
272
+ let rl = ResolveBoundVars {
273
+ defs : SortedMap :: from_presorted_elements ( defs) ,
274
+ late_bound_vars : SortedMap :: from_presorted_elements ( late_bound_vars) ,
275
+ } ;
277
276
278
277
debug ! ( ?rl. defs) ;
279
278
debug ! ( ?rl. late_bound_vars) ;
@@ -339,7 +338,7 @@ impl<'a, 'tcx> BoundVarContext<'a, 'tcx> {
339
338
Scope :: Binder { hir_id, .. } => {
340
339
// Nested poly trait refs have the binders concatenated
341
340
let mut full_binders =
342
- self . map . late_bound_vars . entry ( * hir_id) . or_default ( ) . clone ( ) ;
341
+ self . map . late_bound_vars . entry ( hir_id. local_id ) . or_default ( ) . clone ( ) ;
343
342
full_binders. extend ( supertrait_bound_vars) ;
344
343
break ( full_binders, BinderScopeType :: Concatenating ) ;
345
344
}
@@ -678,7 +677,7 @@ impl<'a, 'tcx> Visitor<'tcx> for BoundVarContext<'a, 'tcx> {
678
677
hir:: TyKind :: Ref ( lifetime_ref, ref mt) => {
679
678
self . visit_lifetime ( lifetime_ref) ;
680
679
let scope = Scope :: ObjectLifetimeDefault {
681
- lifetime : self . map . defs . get ( & lifetime_ref. hir_id ) . cloned ( ) ,
680
+ lifetime : self . map . defs . get ( & lifetime_ref. hir_id . local_id ) . cloned ( ) ,
682
681
s : self . scope ,
683
682
} ;
684
683
self . with ( scope, |this| this. visit_ty ( mt. ty ) ) ;
@@ -705,7 +704,7 @@ impl<'a, 'tcx> Visitor<'tcx> for BoundVarContext<'a, 'tcx> {
705
704
// and ban them. Type variables instantiated inside binders aren't
706
705
// well-supported at the moment, so this doesn't work.
707
706
// In the future, this should be fixed and this error should be removed.
708
- let def = self . map . defs . get ( & lifetime. hir_id ) . copied ( ) ;
707
+ let def = self . map . defs . get ( & lifetime. hir_id . local_id ) . copied ( ) ;
709
708
let Some ( ResolvedArg :: LateBound ( _, _, lifetime_def_id) ) = def else { continue } ;
710
709
let lifetime_hir_id = self . tcx . local_def_id_to_hir_id ( lifetime_def_id) ;
711
710
@@ -842,7 +841,7 @@ impl<'a, 'tcx> Visitor<'tcx> for BoundVarContext<'a, 'tcx> {
842
841
let bound_vars: Vec < _ > =
843
842
self . tcx . fn_sig ( sig_id) . skip_binder ( ) . bound_vars ( ) . iter ( ) . collect ( ) ;
844
843
let hir_id = self . tcx . local_def_id_to_hir_id ( def_id) ;
845
- self . map . late_bound_vars . insert ( hir_id, bound_vars) ;
844
+ self . map . late_bound_vars . insert ( hir_id. local_id , bound_vars) ;
846
845
}
847
846
self . visit_fn_like_elision ( fd. inputs , output, matches ! ( fk, intravisit:: FnKind :: Closure ) ) ;
848
847
intravisit:: walk_fn_kind ( self , fk) ;
@@ -1015,10 +1014,10 @@ impl<'a, 'tcx> BoundVarContext<'a, 'tcx> {
1015
1014
}
1016
1015
1017
1016
fn record_late_bound_vars ( & mut self , hir_id : HirId , binder : Vec < ty:: BoundVariableKind > ) {
1018
- if let Some ( old) = self . map . late_bound_vars . insert ( hir_id, binder) {
1017
+ if let Some ( old) = self . map . late_bound_vars . insert ( hir_id. local_id , binder) {
1019
1018
bug ! (
1020
1019
"overwrote bound vars for {hir_id:?}:\n old={old:?}\n new={:?}" ,
1021
- self . map. late_bound_vars[ & hir_id]
1020
+ self . map. late_bound_vars[ & hir_id. local_id ]
1022
1021
)
1023
1022
}
1024
1023
}
@@ -1393,9 +1392,9 @@ impl<'a, 'tcx> BoundVarContext<'a, 'tcx> {
1393
1392
kind. descr( param_def_id. to_def_id( ) )
1394
1393
) ,
1395
1394
} ;
1396
- self . map . defs . insert ( hir_id, ResolvedArg :: Error ( guar) ) ;
1395
+ self . map . defs . insert ( hir_id. local_id , ResolvedArg :: Error ( guar) ) ;
1397
1396
} else {
1398
- self . map . defs . insert ( hir_id, def) ;
1397
+ self . map . defs . insert ( hir_id. local_id , def) ;
1399
1398
}
1400
1399
return ;
1401
1400
}
@@ -1428,7 +1427,7 @@ impl<'a, 'tcx> BoundVarContext<'a, 'tcx> {
1428
1427
bug ! ( "unexpected def-kind: {}" , kind. descr( param_def_id. to_def_id( ) ) )
1429
1428
}
1430
1429
} ) ;
1431
- self . map . defs . insert ( hir_id, ResolvedArg :: Error ( guar) ) ;
1430
+ self . map . defs . insert ( hir_id. local_id , ResolvedArg :: Error ( guar) ) ;
1432
1431
return ;
1433
1432
}
1434
1433
Scope :: Root { .. } => break ,
@@ -1538,7 +1537,7 @@ impl<'a, 'tcx> BoundVarContext<'a, 'tcx> {
1538
1537
// This index can be used with `generic_args` since `parent_count == 0`.
1539
1538
let index = generics. param_def_id_to_index [ & param_def_id] as usize ;
1540
1539
generic_args. args . get ( index) . and_then ( |arg| match arg {
1541
- GenericArg :: Lifetime ( lt) => map. defs . get ( & lt. hir_id ) . copied ( ) ,
1540
+ GenericArg :: Lifetime ( lt) => map. defs . get ( & lt. hir_id . local_id ) . copied ( ) ,
1542
1541
_ => None ,
1543
1542
} )
1544
1543
}
@@ -1828,7 +1827,7 @@ impl<'a, 'tcx> BoundVarContext<'a, 'tcx> {
1828
1827
#[ instrument( level = "debug" , skip( self ) ) ]
1829
1828
fn insert_lifetime ( & mut self , lifetime_ref : & ' tcx hir:: Lifetime , def : ResolvedArg ) {
1830
1829
debug ! ( span = ?lifetime_ref. ident. span) ;
1831
- self . map . defs . insert ( lifetime_ref. hir_id , def) ;
1830
+ self . map . defs . insert ( lifetime_ref. hir_id . local_id , def) ;
1832
1831
}
1833
1832
1834
1833
/// Sometimes we resolve a lifetime, but later find that it is an
@@ -1839,8 +1838,7 @@ impl<'a, 'tcx> BoundVarContext<'a, 'tcx> {
1839
1838
lifetime_ref : & ' tcx hir:: Lifetime ,
1840
1839
bad_def : ResolvedArg ,
1841
1840
) {
1842
- // FIXME(#120456) - is `swap_remove` correct?
1843
- let old_value = self . map . defs . swap_remove ( & lifetime_ref. hir_id ) ;
1841
+ let old_value = self . map . defs . remove ( & lifetime_ref. hir_id . local_id ) ;
1844
1842
assert_eq ! ( old_value, Some ( bad_def) ) ;
1845
1843
}
1846
1844
}
0 commit comments