@@ -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::*;
@@ -74,15 +77,15 @@ impl ResolvedArg {
74
77
struct NamedVarMap {
75
78
// maps from every use of a named (not anonymous) bound var to a
76
79
// `ResolvedArg` describing how that variable is bound
77
- defs: HirIdMap <ResolvedArg>,
80
+ defs: ItemLocalMap <ResolvedArg>,
78
81
79
82
// Maps relevant hir items to the bound vars on them. These include:
80
83
// - function defs
81
84
// - function pointers
82
85
// - closures
83
86
// - trait refs
84
87
// - 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>>,
86
89
}
87
90
88
91
struct BoundVarContext<'a, 'tcx> {
@@ -225,10 +228,10 @@ pub(crate) fn provide(providers: &mut Providers) {
225
228
*providers = Providers {
226
229
resolve_bound_vars,
227
230
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,
229
232
is_late_bound_map,
230
233
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,
232
235
233
236
..*providers
234
237
};
@@ -265,16 +268,12 @@ fn resolve_bound_vars(tcx: TyCtxt<'_>, local_def_id: hir::OwnerId) -> ResolveBou
265
268
hir::OwnerNode::Synthetic => unreachable!(),
266
269
}
267
270
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
+ };
278
277
279
278
debug!(?rl.defs);
280
279
debug!(?rl.late_bound_vars);
@@ -340,7 +339,7 @@ impl<'a, 'tcx> BoundVarContext<'a, 'tcx> {
340
339
Scope::Binder { hir_id, .. } => {
341
340
// Nested poly trait refs have the binders concatenated
342
341
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();
344
343
full_binders.extend(supertrait_bound_vars);
345
344
break (full_binders, BinderScopeType::Concatenating);
346
345
}
@@ -677,7 +676,7 @@ impl<'a, 'tcx> Visitor<'tcx> for BoundVarContext<'a, 'tcx> {
677
676
hir::TyKind::Ref(lifetime_ref, ref mt) => {
678
677
self.visit_lifetime(lifetime_ref);
679
678
let scope = Scope::ObjectLifetimeDefault {
680
- lifetime: self.map.defs.get(&lifetime_ref.hir_id).cloned(),
679
+ lifetime: self.map.defs.get(&lifetime_ref.hir_id.local_id ).cloned(),
681
680
s: self.scope,
682
681
};
683
682
self.with(scope, |this| this.visit_ty(mt.ty));
@@ -704,7 +703,7 @@ impl<'a, 'tcx> Visitor<'tcx> for BoundVarContext<'a, 'tcx> {
704
703
// and ban them. Type variables instantiated inside binders aren't
705
704
// well-supported at the moment, so this doesn't work.
706
705
// In the future, this should be fixed and this error should be removed.
707
- let def = self.map.defs.get(&lifetime.hir_id).copied();
706
+ let def = self.map.defs.get(&lifetime.hir_id.local_id ).copied();
708
707
let Some(ResolvedArg::LateBound(_, _, lifetime_def_id)) = def else { continue };
709
708
let lifetime_hir_id = self.tcx.local_def_id_to_hir_id(lifetime_def_id);
710
709
@@ -841,7 +840,7 @@ impl<'a, 'tcx> Visitor<'tcx> for BoundVarContext<'a, 'tcx> {
841
840
let bound_vars: Vec<_> =
842
841
self.tcx.fn_sig(sig_id).skip_binder().bound_vars().iter().collect();
843
842
let hir_id = self.tcx.local_def_id_to_hir_id(def_id);
844
- self.map.late_bound_vars.insert(hir_id, bound_vars);
843
+ self.map.late_bound_vars.insert(hir_id.local_id , bound_vars);
845
844
}
846
845
self.visit_fn_like_elision(fd.inputs, output, matches!(fk, intravisit::FnKind::Closure));
847
846
intravisit::walk_fn_kind(self, fk);
@@ -1019,10 +1018,10 @@ impl<'a, 'tcx> BoundVarContext<'a, 'tcx> {
1019
1018
}
1020
1019
1021
1020
fn record_late_bound_vars(&mut self, hir_id: HirId, binder: Vec<ty::BoundVariableKind>) {
1022
- if let Some(old) = self.map.late_bound_vars.insert(hir_id, binder) {
1021
+ if let Some(old) = self.map.late_bound_vars.insert(hir_id.local_id , binder) {
1023
1022
bug!(
1024
1023
"overwrote bound vars for {hir_id:?}:\nold={old:?}\nnew={:?}",
1025
- self.map.late_bound_vars[&hir_id]
1024
+ self.map.late_bound_vars[&hir_id.local_id ]
1026
1025
)
1027
1026
}
1028
1027
}
@@ -1381,9 +1380,9 @@ impl<'a, 'tcx> BoundVarContext<'a, 'tcx> {
1381
1380
kind.descr(param_def_id.to_def_id())
1382
1381
),
1383
1382
};
1384
- self.map.defs.insert(hir_id, ResolvedArg::Error(guar));
1383
+ self.map.defs.insert(hir_id.local_id , ResolvedArg::Error(guar));
1385
1384
} else {
1386
- self.map.defs.insert(hir_id, def);
1385
+ self.map.defs.insert(hir_id.local_id , def);
1387
1386
}
1388
1387
return;
1389
1388
}
@@ -1416,7 +1415,7 @@ impl<'a, 'tcx> BoundVarContext<'a, 'tcx> {
1416
1415
bug!("unexpected def-kind: {}", kind.descr(param_def_id.to_def_id()))
1417
1416
}
1418
1417
});
1419
- self.map.defs.insert(hir_id, ResolvedArg::Error(guar));
1418
+ self.map.defs.insert(hir_id.local_id , ResolvedArg::Error(guar));
1420
1419
return;
1421
1420
}
1422
1421
Scope::Root { .. } => break,
@@ -1526,7 +1525,7 @@ impl<'a, 'tcx> BoundVarContext<'a, 'tcx> {
1526
1525
// This index can be used with `generic_args` since `parent_count == 0`.
1527
1526
let index = generics.param_def_id_to_index[¶m_def_id] as usize;
1528
1527
generic_args.args.get(index).and_then(|arg| match arg {
1529
- GenericArg::Lifetime(lt) => map.defs.get(<.hir_id).copied(),
1528
+ GenericArg::Lifetime(lt) => map.defs.get(<.hir_id.local_id ).copied(),
1530
1529
_ => None,
1531
1530
})
1532
1531
}
@@ -1816,7 +1815,7 @@ impl<'a, 'tcx> BoundVarContext<'a, 'tcx> {
1816
1815
#[instrument(level = "debug", skip(self))]
1817
1816
fn insert_lifetime(&mut self, lifetime_ref: &'tcx hir::Lifetime, def: ResolvedArg) {
1818
1817
debug!(span = ?lifetime_ref.ident.span);
1819
- self.map.defs.insert(lifetime_ref.hir_id, def);
1818
+ self.map.defs.insert(lifetime_ref.hir_id.local_id , def);
1820
1819
}
1821
1820
1822
1821
/// Sometimes we resolve a lifetime, but later find that it is an
@@ -1827,8 +1826,7 @@ impl<'a, 'tcx> BoundVarContext<'a, 'tcx> {
1827
1826
lifetime_ref: &'tcx hir::Lifetime,
1828
1827
bad_def: ResolvedArg,
1829
1828
) {
1830
- // FIXME(#120456) - is `swap_remove` correct?
1831
- let old_value = self.map.defs.swap_remove(&lifetime_ref.hir_id);
1829
+ let old_value = self.map.defs.remove(&lifetime_ref.hir_id.local_id);
1832
1830
assert_eq!(old_value, Some(bad_def));
1833
1831
}
1834
1832
@@ -1998,7 +1996,7 @@ impl<'a, 'tcx> BoundVarContext<'a, 'tcx> {
1998
1996
// See where these vars are used in `HirTyLowerer::lower_ty_maybe_return_type_notation`.
1999
1997
// And this is exercised in:
2000
1998
// `tests/ui/associated-type-bounds/return-type-notation/higher-ranked-bound-works.rs`.
2001
- let existing_bound_vars = self.map.late_bound_vars.get_mut(&hir_id).unwrap();
1999
+ let existing_bound_vars = self.map.late_bound_vars.get_mut(&hir_id.local_id ).unwrap();
2002
2000
let existing_bound_vars_saved = existing_bound_vars.clone();
2003
2001
existing_bound_vars.extend(bound_vars);
2004
2002
self.record_late_bound_vars(item_segment.hir_id, existing_bound_vars_saved);
0 commit comments