Skip to content

Commit 6ec58a4

Browse files
committed
Simplify bound var resolution.
1 parent c7cb45a commit 6ec58a4

File tree

4 files changed

+39
-44
lines changed

4 files changed

+39
-44
lines changed

compiler/rustc_hir_analysis/src/collect/resolve_bound_vars.rs

+27-29
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,13 @@ use std::fmt;
1111

1212
use rustc_ast::visit::walk_list;
1313
use rustc_data_structures::fx::{FxHashSet, FxIndexMap, FxIndexSet};
14+
use rustc_data_structures::sorted_map::SortedMap;
1415
use rustc_hir as hir;
1516
use rustc_hir::def::{DefKind, Res};
1617
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+
};
1821
use rustc_macros::extension;
1922
use rustc_middle::hir::nested_filter;
2023
use rustc_middle::middle::resolve_bound_vars::*;
@@ -74,15 +77,15 @@ impl ResolvedArg {
7477
struct 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

8891
struct 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
}
@@ -677,7 +676,7 @@ impl<'a, 'tcx> Visitor<'tcx> for BoundVarContext<'a, 'tcx> {
677676
hir::TyKind::Ref(lifetime_ref, ref mt) => {
678677
self.visit_lifetime(lifetime_ref);
679678
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(),
681680
s: self.scope,
682681
};
683682
self.with(scope, |this| this.visit_ty(mt.ty));
@@ -704,7 +703,7 @@ impl<'a, 'tcx> Visitor<'tcx> for BoundVarContext<'a, 'tcx> {
704703
// and ban them. Type variables instantiated inside binders aren't
705704
// well-supported at the moment, so this doesn't work.
706705
// 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();
708707
let Some(ResolvedArg::LateBound(_, _, lifetime_def_id)) = def else { continue };
709708
let lifetime_hir_id = self.tcx.local_def_id_to_hir_id(lifetime_def_id);
710709

@@ -841,7 +840,7 @@ impl<'a, 'tcx> Visitor<'tcx> for BoundVarContext<'a, 'tcx> {
841840
let bound_vars: Vec<_> =
842841
self.tcx.fn_sig(sig_id).skip_binder().bound_vars().iter().collect();
843842
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);
845844
}
846845
self.visit_fn_like_elision(fd.inputs, output, matches!(fk, intravisit::FnKind::Closure));
847846
intravisit::walk_fn_kind(self, fk);
@@ -1019,10 +1018,10 @@ impl<'a, 'tcx> BoundVarContext<'a, 'tcx> {
10191018
}
10201019

10211020
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) {
10231022
bug!(
10241023
"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]
10261025
)
10271026
}
10281027
}
@@ -1381,9 +1380,9 @@ impl<'a, 'tcx> BoundVarContext<'a, 'tcx> {
13811380
kind.descr(param_def_id.to_def_id())
13821381
),
13831382
};
1384-
self.map.defs.insert(hir_id, ResolvedArg::Error(guar));
1383+
self.map.defs.insert(hir_id.local_id, ResolvedArg::Error(guar));
13851384
} else {
1386-
self.map.defs.insert(hir_id, def);
1385+
self.map.defs.insert(hir_id.local_id, def);
13871386
}
13881387
return;
13891388
}
@@ -1416,7 +1415,7 @@ impl<'a, 'tcx> BoundVarContext<'a, 'tcx> {
14161415
bug!("unexpected def-kind: {}", kind.descr(param_def_id.to_def_id()))
14171416
}
14181417
});
1419-
self.map.defs.insert(hir_id, ResolvedArg::Error(guar));
1418+
self.map.defs.insert(hir_id.local_id, ResolvedArg::Error(guar));
14201419
return;
14211420
}
14221421
Scope::Root { .. } => break,
@@ -1526,7 +1525,7 @@ impl<'a, 'tcx> BoundVarContext<'a, 'tcx> {
15261525
// This index can be used with `generic_args` since `parent_count == 0`.
15271526
let index = generics.param_def_id_to_index[&param_def_id] as usize;
15281527
generic_args.args.get(index).and_then(|arg| match arg {
1529-
GenericArg::Lifetime(lt) => map.defs.get(&lt.hir_id).copied(),
1528+
GenericArg::Lifetime(lt) => map.defs.get(&lt.hir_id.local_id).copied(),
15301529
_ => None,
15311530
})
15321531
}
@@ -1816,7 +1815,7 @@ impl<'a, 'tcx> BoundVarContext<'a, 'tcx> {
18161815
#[instrument(level = "debug", skip(self))]
18171816
fn insert_lifetime(&mut self, lifetime_ref: &'tcx hir::Lifetime, def: ResolvedArg) {
18181817
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);
18201819
}
18211820

18221821
/// Sometimes we resolve a lifetime, but later find that it is an
@@ -1827,8 +1826,7 @@ impl<'a, 'tcx> BoundVarContext<'a, 'tcx> {
18271826
lifetime_ref: &'tcx hir::Lifetime,
18281827
bad_def: ResolvedArg,
18291828
) {
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);
18321830
assert_eq!(old_value, Some(bad_def));
18331831
}
18341832

@@ -1998,7 +1996,7 @@ impl<'a, 'tcx> BoundVarContext<'a, 'tcx> {
19981996
// See where these vars are used in `HirTyLowerer::lower_ty_maybe_return_type_notation`.
19991997
// And this is exercised in:
20001998
// `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();
20022000
let existing_bound_vars_saved = existing_bound_vars.clone();
20032001
existing_bound_vars.extend(bound_vars);
20042002
self.record_late_bound_vars(item_segment.hir_id, existing_bound_vars_saved);

compiler/rustc_middle/src/middle/resolve_bound_vars.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
//! Name resolution for lifetimes and late-bound type and const variables: type declarations.
22
3-
use rustc_data_structures::fx::FxIndexMap;
3+
use rustc_data_structures::sorted_map::SortedMap;
44
use rustc_errors::ErrorGuaranteed;
5+
use rustc_hir::ItemLocalId;
56
use rustc_hir::def_id::{DefId, LocalDefId};
6-
use rustc_hir::{ItemLocalId, OwnerId};
77
use rustc_macros::{Decodable, Encodable, HashStable, TyDecodable, TyEncodable};
88

99
use crate::ty;
@@ -47,11 +47,11 @@ pub enum ObjectLifetimeDefault {
4747

4848
/// Maps the id of each lifetime reference to the lifetime decl
4949
/// that it corresponds to.
50-
#[derive(Default, HashStable, Debug)]
50+
#[derive(HashStable, Debug)]
5151
pub struct ResolveBoundVars {
5252
/// Maps from every use of a named (not anonymous) lifetime to a
5353
/// `Region` describing how that region is bound
54-
pub defs: FxIndexMap<OwnerId, FxIndexMap<ItemLocalId, ResolvedArg>>,
54+
pub defs: SortedMap<ItemLocalId, ResolvedArg>,
5555

56-
pub late_bound_vars: FxIndexMap<OwnerId, FxIndexMap<ItemLocalId, Vec<ty::BoundVariableKind>>>,
56+
pub late_bound_vars: SortedMap<ItemLocalId, Vec<ty::BoundVariableKind>>,
5757
}

compiler/rustc_middle/src/query/mod.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ use rustc_ast::expand::StrippedCfgItem;
1616
use rustc_ast::expand::allocator::AllocatorKind;
1717
use rustc_data_structures::fingerprint::Fingerprint;
1818
use rustc_data_structures::fx::{FxIndexMap, FxIndexSet};
19+
use rustc_data_structures::sorted_map::SortedMap;
1920
use rustc_data_structures::steal::Steal;
2021
use rustc_data_structures::svh::Svh;
2122
use rustc_data_structures::sync::Lrc;
@@ -1742,8 +1743,7 @@ rustc_queries! {
17421743
arena_cache
17431744
desc { |tcx| "resolving lifetimes for `{}`", tcx.def_path_str(owner_id) }
17441745
}
1745-
query named_variable_map(owner_id: hir::OwnerId) ->
1746-
Option<&'tcx FxIndexMap<ItemLocalId, ResolvedArg>> {
1746+
query named_variable_map(owner_id: hir::OwnerId) -> &'tcx SortedMap<ItemLocalId, ResolvedArg> {
17471747
desc { |tcx| "looking up a named region inside `{}`", tcx.def_path_str(owner_id) }
17481748
}
17491749
query is_late_bound_map(owner_id: hir::OwnerId) -> Option<&'tcx FxIndexSet<ItemLocalId>> {
@@ -1759,7 +1759,7 @@ rustc_queries! {
17591759
separate_provide_extern
17601760
}
17611761
query late_bound_vars_map(owner_id: hir::OwnerId)
1762-
-> Option<&'tcx FxIndexMap<ItemLocalId, Vec<ty::BoundVariableKind>>> {
1762+
-> &'tcx SortedMap<ItemLocalId, Vec<ty::BoundVariableKind>> {
17631763
desc { |tcx| "looking up late bound vars inside `{}`", tcx.def_path_str(owner_id) }
17641764
}
17651765

compiler/rustc_middle/src/ty/context.rs

+4-7
Original file line numberDiff line numberDiff line change
@@ -2996,7 +2996,7 @@ impl<'tcx> TyCtxt<'tcx> {
29962996

29972997
pub fn named_bound_var(self, id: HirId) -> Option<resolve_bound_vars::ResolvedArg> {
29982998
debug!(?id, "named_region");
2999-
self.named_variable_map(id.owner).and_then(|map| map.get(&id.local_id).cloned())
2999+
self.named_variable_map(id.owner).get(&id.local_id).cloned()
30003000
}
30013001

30023002
pub fn is_late_bound(self, id: HirId) -> bool {
@@ -3005,12 +3005,9 @@ impl<'tcx> TyCtxt<'tcx> {
30053005

30063006
pub fn late_bound_vars(self, id: HirId) -> &'tcx List<ty::BoundVariableKind> {
30073007
self.mk_bound_variable_kinds(
3008-
&self
3009-
.late_bound_vars_map(id.owner)
3010-
.and_then(|map| map.get(&id.local_id).cloned())
3011-
.unwrap_or_else(|| {
3012-
bug!("No bound vars found for {}", self.hir().node_to_string(id))
3013-
}),
3008+
&self.late_bound_vars_map(id.owner).get(&id.local_id).cloned().unwrap_or_else(|| {
3009+
bug!("No bound vars found for {}", self.hir().node_to_string(id))
3010+
}),
30143011
)
30153012
}
30163013

0 commit comments

Comments
 (0)