@@ -11,10 +11,13 @@ use std::fmt;
1111
1212use rustc_ast:: visit:: walk_list;
1313use rustc_data_structures:: fx:: { FxHashSet , FxIndexMap , FxIndexSet } ;
14+ use rustc_data_structures:: sorted_map:: SortedMap ;
1415use rustc_hir as hir;
1516use rustc_hir:: def:: { DefKind , Res } ;
1617use 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+ } ;
1821use rustc_macros:: extension;
1922use rustc_middle:: hir:: nested_filter;
2023use rustc_middle:: middle:: resolve_bound_vars:: * ;
@@ -74,15 +77,15 @@ impl ResolvedArg {
7477struct NamedVarMap {
7578 // maps from every use of a named (not anonymous) bound var to a
7679 // `ResolvedArg` describing how that variable is bound
77- defs : HirIdMap < ResolvedArg > ,
80+ defs : ItemLocalMap < ResolvedArg > ,
7881
7982 // Maps relevant hir items to the bound vars on them. These include:
8083 // - function defs
8184 // - function pointers
8285 // - closures
8386 // - trait refs
8487 // - bound types (like `T` in `for<'a> T<'a>: Foo`)
85- late_bound_vars : HirIdMap < Vec < ty:: BoundVariableKind > > ,
88+ late_bound_vars : ItemLocalMap < Vec < ty:: BoundVariableKind > > ,
8689}
8790
8891struct BoundVarContext < ' a , ' tcx > {
@@ -225,10 +228,10 @@ pub(crate) fn provide(providers: &mut Providers) {
225228 * providers = Providers {
226229 resolve_bound_vars,
227230
228- named_variable_map : |tcx, id| tcx. resolve_bound_vars ( id) . defs . get ( & id ) ,
231+ named_variable_map : |tcx, id| & tcx. resolve_bound_vars ( id) . defs ,
229232 is_late_bound_map,
230233 object_lifetime_default,
231- late_bound_vars_map : |tcx, id| tcx. resolve_bound_vars ( id) . late_bound_vars . get ( & id ) ,
234+ late_bound_vars_map : |tcx, id| & tcx. resolve_bound_vars ( id) . late_bound_vars ,
232235
233236 ..* providers
234237 } ;
@@ -265,16 +268,12 @@ fn resolve_bound_vars(tcx: TyCtxt<'_>, local_def_id: hir::OwnerId) -> ResolveBou
265268 hir:: OwnerNode :: Synthetic => unreachable ! ( ) ,
266269 }
267270
268- let mut rl = ResolveBoundVars :: default ( ) ;
269-
270- for ( hir_id, v) in named_variable_map. defs {
271- let map = rl. defs . entry ( hir_id. owner ) . or_default ( ) ;
272- map. insert ( hir_id. local_id , v) ;
273- }
274- for ( hir_id, v) in named_variable_map. late_bound_vars {
275- let map = rl. late_bound_vars . entry ( hir_id. owner ) . or_default ( ) ;
276- map. insert ( hir_id. local_id , v) ;
277- }
271+ let defs = named_variable_map. defs . into_sorted_stable_ord ( ) ;
272+ let late_bound_vars = named_variable_map. late_bound_vars . into_sorted_stable_ord ( ) ;
273+ let rl = ResolveBoundVars {
274+ defs : SortedMap :: from_presorted_elements ( defs) ,
275+ late_bound_vars : SortedMap :: from_presorted_elements ( late_bound_vars) ,
276+ } ;
278277
279278 debug ! ( ?rl. defs) ;
280279 debug ! ( ?rl. late_bound_vars) ;
@@ -340,7 +339,7 @@ impl<'a, 'tcx> BoundVarContext<'a, 'tcx> {
340339 Scope :: Binder { hir_id, .. } => {
341340 // Nested poly trait refs have the binders concatenated
342341 let mut full_binders =
343- self . map . late_bound_vars . entry ( * hir_id) . or_default ( ) . clone ( ) ;
342+ self . map . late_bound_vars . entry ( hir_id. local_id ) . or_default ( ) . clone ( ) ;
344343 full_binders. extend ( supertrait_bound_vars) ;
345344 break ( full_binders, BinderScopeType :: Concatenating ) ;
346345 }
@@ -679,7 +678,7 @@ impl<'a, 'tcx> Visitor<'tcx> for BoundVarContext<'a, 'tcx> {
679678 hir:: TyKind :: Ref ( lifetime_ref, ref mt) => {
680679 self . visit_lifetime ( lifetime_ref) ;
681680 let scope = Scope :: ObjectLifetimeDefault {
682- lifetime : self . map . defs . get ( & lifetime_ref. hir_id ) . cloned ( ) ,
681+ lifetime : self . map . defs . get ( & lifetime_ref. hir_id . local_id ) . cloned ( ) ,
683682 s : self . scope ,
684683 } ;
685684 self . with ( scope, |this| this. visit_ty ( mt. ty ) ) ;
@@ -706,7 +705,7 @@ impl<'a, 'tcx> Visitor<'tcx> for BoundVarContext<'a, 'tcx> {
706705 // and ban them. Type variables instantiated inside binders aren't
707706 // well-supported at the moment, so this doesn't work.
708707 // In the future, this should be fixed and this error should be removed.
709- let def = self . map . defs . get ( & lifetime. hir_id ) . copied ( ) ;
708+ let def = self . map . defs . get ( & lifetime. hir_id . local_id ) . copied ( ) ;
710709 let Some ( ResolvedArg :: LateBound ( _, _, lifetime_def_id) ) = def else { continue } ;
711710 let lifetime_hir_id = self . tcx . local_def_id_to_hir_id ( lifetime_def_id) ;
712711
@@ -843,7 +842,7 @@ impl<'a, 'tcx> Visitor<'tcx> for BoundVarContext<'a, 'tcx> {
843842 let bound_vars: Vec < _ > =
844843 self . tcx . fn_sig ( sig_id) . skip_binder ( ) . bound_vars ( ) . iter ( ) . collect ( ) ;
845844 let hir_id = self . tcx . local_def_id_to_hir_id ( def_id) ;
846- self . map . late_bound_vars . insert ( hir_id, bound_vars) ;
845+ self . map . late_bound_vars . insert ( hir_id. local_id , bound_vars) ;
847846 }
848847 self . visit_fn_like_elision ( fd. inputs , output, matches ! ( fk, intravisit:: FnKind :: Closure ) ) ;
849848 intravisit:: walk_fn_kind ( self , fk) ;
@@ -1016,10 +1015,10 @@ impl<'a, 'tcx> BoundVarContext<'a, 'tcx> {
10161015 }
10171016
10181017 fn record_late_bound_vars ( & mut self , hir_id : HirId , binder : Vec < ty:: BoundVariableKind > ) {
1019- if let Some ( old) = self . map . late_bound_vars . insert ( hir_id, binder) {
1018+ if let Some ( old) = self . map . late_bound_vars . insert ( hir_id. local_id , binder) {
10201019 bug ! (
10211020 "overwrote bound vars for {hir_id:?}:\n old={old:?}\n new={:?}" ,
1022- self . map. late_bound_vars[ & hir_id]
1021+ self . map. late_bound_vars[ & hir_id. local_id ]
10231022 )
10241023 }
10251024 }
@@ -1394,9 +1393,9 @@ impl<'a, 'tcx> BoundVarContext<'a, 'tcx> {
13941393 kind. descr( param_def_id. to_def_id( ) )
13951394 ) ,
13961395 } ;
1397- self . map . defs . insert ( hir_id, ResolvedArg :: Error ( guar) ) ;
1396+ self . map . defs . insert ( hir_id. local_id , ResolvedArg :: Error ( guar) ) ;
13981397 } else {
1399- self . map . defs . insert ( hir_id, def) ;
1398+ self . map . defs . insert ( hir_id. local_id , def) ;
14001399 }
14011400 return ;
14021401 }
@@ -1429,7 +1428,7 @@ impl<'a, 'tcx> BoundVarContext<'a, 'tcx> {
14291428 bug ! ( "unexpected def-kind: {}" , kind. descr( param_def_id. to_def_id( ) ) )
14301429 }
14311430 } ) ;
1432- self . map . defs . insert ( hir_id, ResolvedArg :: Error ( guar) ) ;
1431+ self . map . defs . insert ( hir_id. local_id , ResolvedArg :: Error ( guar) ) ;
14331432 return ;
14341433 }
14351434 Scope :: Root { .. } => break ,
@@ -1539,7 +1538,7 @@ impl<'a, 'tcx> BoundVarContext<'a, 'tcx> {
15391538 // This index can be used with `generic_args` since `parent_count == 0`.
15401539 let index = generics. param_def_id_to_index [ & param_def_id] as usize ;
15411540 generic_args. args . get ( index) . and_then ( |arg| match arg {
1542- GenericArg :: Lifetime ( lt) => map. defs . get ( & lt. hir_id ) . copied ( ) ,
1541+ GenericArg :: Lifetime ( lt) => map. defs . get ( & lt. hir_id . local_id ) . copied ( ) ,
15431542 _ => None ,
15441543 } )
15451544 }
@@ -1829,7 +1828,7 @@ impl<'a, 'tcx> BoundVarContext<'a, 'tcx> {
18291828 #[ instrument( level = "debug" , skip( self ) ) ]
18301829 fn insert_lifetime ( & mut self , lifetime_ref : & ' tcx hir:: Lifetime , def : ResolvedArg ) {
18311830 debug ! ( span = ?lifetime_ref. ident. span) ;
1832- self . map . defs . insert ( lifetime_ref. hir_id , def) ;
1831+ self . map . defs . insert ( lifetime_ref. hir_id . local_id , def) ;
18331832 }
18341833
18351834 /// Sometimes we resolve a lifetime, but later find that it is an
@@ -1840,8 +1839,7 @@ impl<'a, 'tcx> BoundVarContext<'a, 'tcx> {
18401839 lifetime_ref : & ' tcx hir:: Lifetime ,
18411840 bad_def : ResolvedArg ,
18421841 ) {
1843- // FIXME(#120456) - is `swap_remove` correct?
1844- let old_value = self . map . defs . swap_remove ( & lifetime_ref. hir_id ) ;
1842+ let old_value = self . map . defs . remove ( & lifetime_ref. hir_id . local_id ) ;
18451843 assert_eq ! ( old_value, Some ( bad_def) ) ;
18461844 }
18471845}
0 commit comments