Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 78f2ee6

Browse files
committedJan 30, 2025
Auto merge of #136270 - nnethercote:rm-NamedVarMap, r=<try>
Remove `NamedVarMap`. `NamedVarMap` is extremely similar to `ResolveBoundVars`. The former contains two `UnordMap<ItemLocalId, T>` fields (obscured behind `ItemLocalMap` typedefs). The latter contains two `SortedMap<ItemLocalId, T>` fields. We construct a `NamedVarMap` and then convert it into a `ResolveBoundVars` by sorting the `UnordMap`s, which is unnecessary busywork. This commit removes `NamedVarMap` and constructs a `ResolveBoundVars` directly. `SortedMap` and `NamedVarMap` have slightly different perf characteristics during construction (e.g. speed of insertion) but this code isn't hot enough for that to matter. A few details to note. - A `FIXME` comment is removed. - The detailed comments on the fields of `NamedVarMap` are copied to `ResolveBoundVars` (which has a single, incorrect comment). - `BoundVarContext::map` is renamed. - `ResolveBoundVars` gets a derived `Default` impl. r? `@ghost`
2 parents 4a5f1cc + 022c0ce commit 78f2ee6

File tree

2 files changed

+37
-72
lines changed

2 files changed

+37
-72
lines changed
 

‎compiler/rustc_hir_analysis/src/collect/resolve_bound_vars.rs

+26-68
Original file line numberDiff line numberDiff line change
@@ -12,21 +12,19 @@ use std::ops::ControlFlow;
1212

1313
use rustc_ast::visit::walk_list;
1414
use rustc_data_structures::fx::{FxHashSet, FxIndexMap, FxIndexSet};
15-
use rustc_data_structures::sorted_map::SortedMap;
1615
use rustc_errors::ErrorGuaranteed;
1716
use rustc_hir::def::{DefKind, Res};
1817
use rustc_hir::intravisit::{self, InferKind, Visitor, VisitorExt};
1918
use rustc_hir::{
20-
self as hir, AmbigArg, GenericArg, GenericParam, GenericParamKind, HirId, ItemLocalMap,
21-
LifetimeName, Node,
19+
self as hir, AmbigArg, GenericArg, GenericParam, GenericParamKind, HirId, LifetimeName, Node,
2220
};
2321
use rustc_macros::extension;
2422
use rustc_middle::hir::nested_filter;
2523
use rustc_middle::middle::resolve_bound_vars::*;
2624
use rustc_middle::query::Providers;
2725
use rustc_middle::ty::{self, TyCtxt, TypeSuperVisitable, TypeVisitor};
2826
use rustc_middle::{bug, span_bug};
29-
use rustc_span::def_id::{DefId, LocalDefId, LocalDefIdMap};
27+
use rustc_span::def_id::{DefId, LocalDefId};
3028
use rustc_span::{Ident, Span, sym};
3129
use tracing::{debug, debug_span, instrument};
3230

@@ -62,33 +60,9 @@ impl ResolvedArg {
6260
}
6361
}
6462

65-
/// Maps the id of each bound variable reference to the variable decl
66-
/// that it corresponds to.
67-
///
68-
/// FIXME. This struct gets converted to a `ResolveBoundVars` for
69-
/// actual use. It has the same data, but indexed by `LocalDefId`. This
70-
/// is silly.
71-
#[derive(Debug, Default)]
72-
struct NamedVarMap {
73-
// maps from every use of a named (not anonymous) bound var to a
74-
// `ResolvedArg` describing how that variable is bound
75-
defs: ItemLocalMap<ResolvedArg>,
76-
77-
// Maps relevant hir items to the bound vars on them. These include:
78-
// - function defs
79-
// - function pointers
80-
// - closures
81-
// - trait refs
82-
// - bound types (like `T` in `for<'a> T<'a>: Foo`)
83-
late_bound_vars: ItemLocalMap<Vec<ty::BoundVariableKind>>,
84-
85-
// List captured variables for each opaque type.
86-
opaque_captured_lifetimes: LocalDefIdMap<Vec<(ResolvedArg, LocalDefId)>>,
87-
}
88-
8963
struct BoundVarContext<'a, 'tcx> {
9064
tcx: TyCtxt<'tcx>,
91-
map: &'a mut NamedVarMap,
65+
rbv: &'a mut ResolveBoundVars,
9266
scope: ScopeRef<'a>,
9367
}
9468

@@ -267,19 +241,12 @@ pub(crate) fn provide(providers: &mut Providers) {
267241

268242
/// Computes the `ResolveBoundVars` map that contains data for an entire `Item`.
269243
/// You should not read the result of this query directly, but rather use
270-
/// `named_variable_map`, `is_late_bound_map`, etc.
244+
/// `named_variable_map`, `late_bound_vars_map`, etc.
271245
#[instrument(level = "debug", skip(tcx))]
272246
fn resolve_bound_vars(tcx: TyCtxt<'_>, local_def_id: hir::OwnerId) -> ResolveBoundVars {
273-
let mut named_variable_map = NamedVarMap {
274-
defs: Default::default(),
275-
late_bound_vars: Default::default(),
276-
opaque_captured_lifetimes: Default::default(),
277-
};
278-
let mut visitor = BoundVarContext {
279-
tcx,
280-
map: &mut named_variable_map,
281-
scope: &Scope::Root { opt_parent_item: None },
282-
};
247+
let mut rbv = ResolveBoundVars::default();
248+
let mut visitor =
249+
BoundVarContext { tcx, rbv: &mut rbv, scope: &Scope::Root { opt_parent_item: None } };
283250
match tcx.hir_owner_node(local_def_id) {
284251
hir::OwnerNode::Item(item) => visitor.visit_item(item),
285252
hir::OwnerNode::ForeignItem(item) => visitor.visit_foreign_item(item),
@@ -299,19 +266,10 @@ fn resolve_bound_vars(tcx: TyCtxt<'_>, local_def_id: hir::OwnerId) -> ResolveBou
299266
hir::OwnerNode::Synthetic => unreachable!(),
300267
}
301268

302-
let defs = named_variable_map.defs.into_sorted_stable_ord();
303-
let late_bound_vars = named_variable_map.late_bound_vars.into_sorted_stable_ord();
304-
let opaque_captured_lifetimes = named_variable_map.opaque_captured_lifetimes;
305-
let rl = ResolveBoundVars {
306-
defs: SortedMap::from_presorted_elements(defs),
307-
late_bound_vars: SortedMap::from_presorted_elements(late_bound_vars),
308-
opaque_captured_lifetimes,
309-
};
310-
311-
debug!(?rl.defs);
312-
debug!(?rl.late_bound_vars);
313-
debug!(?rl.opaque_captured_lifetimes);
314-
rl
269+
debug!(?rbv.defs);
270+
debug!(?rbv.late_bound_vars);
271+
debug!(?rbv.opaque_captured_lifetimes);
272+
rbv
315273
}
316274

317275
fn late_arg_as_bound_arg<'tcx>(
@@ -404,7 +362,7 @@ impl<'a, 'tcx> BoundVarContext<'a, 'tcx> {
404362
Scope::Binder { hir_id, .. } => {
405363
// Nested poly trait refs have the binders concatenated
406364
let mut full_binders =
407-
self.map.late_bound_vars.entry(hir_id.local_id).or_default().clone();
365+
self.rbv.late_bound_vars.get_mut_or_insert_default(hir_id.local_id).clone();
408366
full_binders.extend(supertrait_bound_vars);
409367
break (full_binders, BinderScopeType::Concatenating);
410368
}
@@ -646,7 +604,7 @@ impl<'a, 'tcx> Visitor<'tcx> for BoundVarContext<'a, 'tcx> {
646604

647605
let captures = captures.into_inner().into_iter().collect();
648606
debug!(?captures);
649-
self.map.opaque_captured_lifetimes.insert(opaque.def_id, captures);
607+
self.rbv.opaque_captured_lifetimes.insert(opaque.def_id, captures);
650608
}
651609

652610
#[instrument(level = "debug", skip(self))]
@@ -848,7 +806,7 @@ impl<'a, 'tcx> Visitor<'tcx> for BoundVarContext<'a, 'tcx> {
848806
hir::TyKind::Ref(lifetime_ref, ref mt) => {
849807
self.visit_lifetime(lifetime_ref);
850808
let scope = Scope::ObjectLifetimeDefault {
851-
lifetime: self.map.defs.get(&lifetime_ref.hir_id.local_id).cloned(),
809+
lifetime: self.rbv.defs.get(&lifetime_ref.hir_id.local_id).cloned(),
852810
s: self.scope,
853811
};
854812
self.with(scope, |this| this.visit_ty_unambig(mt.ty));
@@ -966,7 +924,7 @@ impl<'a, 'tcx> Visitor<'tcx> for BoundVarContext<'a, 'tcx> {
966924
let bound_vars: Vec<_> =
967925
self.tcx.fn_sig(sig_id).skip_binder().bound_vars().iter().collect();
968926
let hir_id = self.tcx.local_def_id_to_hir_id(def_id);
969-
self.map.late_bound_vars.insert(hir_id.local_id, bound_vars);
927+
self.rbv.late_bound_vars.insert(hir_id.local_id, bound_vars);
970928
}
971929
self.visit_fn_like_elision(fd.inputs, output, matches!(fk, intravisit::FnKind::Closure));
972930
intravisit::walk_fn_kind(self, fk);
@@ -1140,8 +1098,8 @@ impl<'a, 'tcx> BoundVarContext<'a, 'tcx> {
11401098
where
11411099
F: for<'b> FnOnce(&mut BoundVarContext<'b, 'tcx>),
11421100
{
1143-
let BoundVarContext { tcx, map, .. } = self;
1144-
let mut this = BoundVarContext { tcx: *tcx, map, scope: &wrap_scope };
1101+
let BoundVarContext { tcx, rbv, .. } = self;
1102+
let mut this = BoundVarContext { tcx: *tcx, rbv, scope: &wrap_scope };
11451103
let span = debug_span!("scope", scope = ?this.scope.debug_truncated());
11461104
{
11471105
let _enter = span.enter();
@@ -1150,10 +1108,10 @@ impl<'a, 'tcx> BoundVarContext<'a, 'tcx> {
11501108
}
11511109

11521110
fn record_late_bound_vars(&mut self, hir_id: HirId, binder: Vec<ty::BoundVariableKind>) {
1153-
if let Some(old) = self.map.late_bound_vars.insert(hir_id.local_id, binder) {
1111+
if let Some(old) = self.rbv.late_bound_vars.insert(hir_id.local_id, binder) {
11541112
bug!(
11551113
"overwrote bound vars for {hir_id:?}:\nold={old:?}\nnew={:?}",
1156-
self.map.late_bound_vars[&hir_id.local_id]
1114+
self.rbv.late_bound_vars[&hir_id.local_id]
11571115
)
11581116
}
11591117
}
@@ -1597,9 +1555,9 @@ impl<'a, 'tcx> BoundVarContext<'a, 'tcx> {
15971555
kind.descr(param_def_id.to_def_id())
15981556
),
15991557
};
1600-
self.map.defs.insert(hir_id.local_id, ResolvedArg::Error(guar));
1558+
self.rbv.defs.insert(hir_id.local_id, ResolvedArg::Error(guar));
16011559
} else {
1602-
self.map.defs.insert(hir_id.local_id, def);
1560+
self.rbv.defs.insert(hir_id.local_id, def);
16031561
}
16041562
return;
16051563
}
@@ -1632,7 +1590,7 @@ impl<'a, 'tcx> BoundVarContext<'a, 'tcx> {
16321590
bug!("unexpected def-kind: {}", kind.descr(param_def_id.to_def_id()))
16331591
}
16341592
});
1635-
self.map.defs.insert(hir_id.local_id, ResolvedArg::Error(guar));
1593+
self.rbv.defs.insert(hir_id.local_id, ResolvedArg::Error(guar));
16361594
return;
16371595
}
16381596
Scope::Root { .. } => break,
@@ -1725,7 +1683,7 @@ impl<'a, 'tcx> BoundVarContext<'a, 'tcx> {
17251683
}
17261684
};
17271685

1728-
let map = &self.map;
1686+
let rbv = &self.rbv;
17291687
let generics = self.tcx.generics_of(def_id);
17301688

17311689
// `type_def_id` points to an item, so there is nothing to inherit generics from.
@@ -1744,7 +1702,7 @@ impl<'a, 'tcx> BoundVarContext<'a, 'tcx> {
17441702
// This index can be used with `generic_args` since `parent_count == 0`.
17451703
let index = generics.param_def_id_to_index[&param_def_id] as usize;
17461704
generic_args.args.get(index).and_then(|arg| match arg {
1747-
GenericArg::Lifetime(lt) => map.defs.get(&lt.hir_id.local_id).copied(),
1705+
GenericArg::Lifetime(lt) => rbv.defs.get(&lt.hir_id.local_id).copied(),
17481706
_ => None,
17491707
})
17501708
}
@@ -2042,7 +2000,7 @@ impl<'a, 'tcx> BoundVarContext<'a, 'tcx> {
20422000
#[instrument(level = "debug", skip(self))]
20432001
fn insert_lifetime(&mut self, lifetime_ref: &'tcx hir::Lifetime, def: ResolvedArg) {
20442002
debug!(span = ?lifetime_ref.ident.span);
2045-
self.map.defs.insert(lifetime_ref.hir_id.local_id, def);
2003+
self.rbv.defs.insert(lifetime_ref.hir_id.local_id, def);
20462004
}
20472005

20482006
// When we have a return type notation type in a where clause, like
@@ -2197,7 +2155,7 @@ impl<'a, 'tcx> BoundVarContext<'a, 'tcx> {
21972155
// See where these vars are used in `HirTyLowerer::lower_ty_maybe_return_type_notation`.
21982156
// And this is exercised in:
21992157
// `tests/ui/associated-type-bounds/return-type-notation/higher-ranked-bound-works.rs`.
2200-
let existing_bound_vars = self.map.late_bound_vars.get_mut(&hir_id.local_id).unwrap();
2158+
let existing_bound_vars = self.rbv.late_bound_vars.get_mut(&hir_id.local_id).unwrap();
22012159
let existing_bound_vars_saved = existing_bound_vars.clone();
22022160
existing_bound_vars.extend(bound_vars);
22032161
self.record_late_bound_vars(item_segment.hir_id, existing_bound_vars_saved);

‎compiler/rustc_middle/src/middle/resolve_bound_vars.rs

+11-4
Original file line numberDiff line numberDiff line change
@@ -45,15 +45,22 @@ pub enum ObjectLifetimeDefault {
4545
Param(DefId),
4646
}
4747

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

56+
// Maps relevant hir items to the bound vars on them. These include:
57+
// - function defs
58+
// - function pointers
59+
// - closures
60+
// - trait refs
61+
// - bound types (like `T` in `for<'a> T<'a>: Foo`)
5662
pub late_bound_vars: SortedMap<ItemLocalId, Vec<ty::BoundVariableKind>>,
5763

64+
// List captured variables for each opaque type.
5865
pub opaque_captured_lifetimes: LocalDefIdMap<Vec<(ResolvedArg, LocalDefId)>>,
5966
}

0 commit comments

Comments
 (0)
Please sign in to comment.