Skip to content

Commit 15d6325

Browse files
committed
Remove HirId -> LocalDefId map from HIR.
1 parent 3175d03 commit 15d6325

File tree

46 files changed

+321
-382
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+321
-382
lines changed

compiler/rustc_ast_lowering/src/item.rs

-1
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,6 @@ impl<'a, 'hir> ItemLowerer<'a, 'hir> {
6767
current_hir_id_owner: hir::CRATE_OWNER_ID,
6868
item_local_id_counter: hir::ItemLocalId::new(0),
6969
node_id_to_local_id: Default::default(),
70-
local_id_to_def_id: SortedMap::new(),
7170
trait_map: Default::default(),
7271

7372
// Lowering state.

compiler/rustc_ast_lowering/src/lib.rs

+1-12
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,6 @@ struct LoweringContext<'a, 'hir> {
119119

120120
current_hir_id_owner: hir::OwnerId,
121121
item_local_id_counter: hir::ItemLocalId,
122-
local_id_to_def_id: SortedMap<ItemLocalId, LocalDefId>,
123122
trait_map: FxHashMap<ItemLocalId, Box<[TraitCandidate]>>,
124123

125124
impl_trait_defs: Vec<hir::GenericParam<'hir>>,
@@ -567,7 +566,6 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
567566
let current_attrs = std::mem::take(&mut self.attrs);
568567
let current_bodies = std::mem::take(&mut self.bodies);
569568
let current_node_ids = std::mem::take(&mut self.node_id_to_local_id);
570-
let current_id_to_def_id = std::mem::take(&mut self.local_id_to_def_id);
571569
let current_trait_map = std::mem::take(&mut self.trait_map);
572570
let current_owner =
573571
std::mem::replace(&mut self.current_hir_id_owner, hir::OwnerId { def_id });
@@ -594,7 +592,6 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
594592
self.attrs = current_attrs;
595593
self.bodies = current_bodies;
596594
self.node_id_to_local_id = current_node_ids;
597-
self.local_id_to_def_id = current_id_to_def_id;
598595
self.trait_map = current_trait_map;
599596
self.current_hir_id_owner = current_owner;
600597
self.item_local_id_counter = current_local_counter;
@@ -629,7 +626,6 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
629626
fn make_owner_info(&mut self, node: hir::OwnerNode<'hir>) -> &'hir hir::OwnerInfo<'hir> {
630627
let attrs = std::mem::take(&mut self.attrs);
631628
let mut bodies = std::mem::take(&mut self.bodies);
632-
let local_id_to_def_id = std::mem::take(&mut self.local_id_to_def_id);
633629
let trait_map = std::mem::take(&mut self.trait_map);
634630

635631
#[cfg(debug_assertions)]
@@ -645,13 +641,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
645641
let (hash_including_bodies, hash_without_bodies) = self.hash_owner(node, &bodies);
646642
let (nodes, parenting) =
647643
index::index_hir(self.tcx.sess, &*self.tcx.definitions_untracked(), node, &bodies);
648-
let nodes = hir::OwnerNodes {
649-
hash_including_bodies,
650-
hash_without_bodies,
651-
nodes,
652-
bodies,
653-
local_id_to_def_id,
654-
};
644+
let nodes = hir::OwnerNodes { hash_including_bodies, hash_without_bodies, nodes, bodies };
655645
let attrs = {
656646
let hash = self.tcx.with_stable_hashing_context(|mut hcx| {
657647
let mut stable_hasher = StableHasher::new();
@@ -710,7 +700,6 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
710700
assert_ne!(local_id, hir::ItemLocalId::new(0));
711701
if let Some(def_id) = self.opt_local_def_id(ast_node_id) {
712702
self.children.push((def_id, hir::MaybeOwner::NonOwner(hir_id)));
713-
self.local_id_to_def_id.insert(local_id, def_id);
714703
}
715704

716705
if let Some(traits) = self.resolver.trait_map.remove(&ast_node_id) {

compiler/rustc_hir/src/hir.rs

-3
Original file line numberDiff line numberDiff line change
@@ -831,8 +831,6 @@ pub struct OwnerNodes<'tcx> {
831831
pub nodes: IndexVec<ItemLocalId, Option<ParentedNode<'tcx>>>,
832832
/// Content of local bodies.
833833
pub bodies: SortedMap<ItemLocalId, &'tcx Body<'tcx>>,
834-
/// Non-owning definitions contained in this owner.
835-
pub local_id_to_def_id: SortedMap<ItemLocalId, LocalDefId>,
836834
}
837835

838836
impl<'tcx> OwnerNodes<'tcx> {
@@ -862,7 +860,6 @@ impl fmt::Debug for OwnerNodes<'_> {
862860
.collect::<Vec<_>>(),
863861
)
864862
.field("bodies", &self.bodies)
865-
.field("local_id_to_def_id", &self.local_id_to_def_id)
866863
.field("hash_without_bodies", &self.hash_without_bodies)
867864
.field("hash_including_bodies", &self.hash_including_bodies)
868865
.finish()

compiler/rustc_hir/src/stable_hash_impls.rs

+2-7
Original file line numberDiff line numberDiff line change
@@ -100,13 +100,8 @@ impl<'tcx, HirCtx: crate::HashStableContext> HashStable<HirCtx> for OwnerNodes<'
100100
// `local_id_to_def_id` is also ignored because is dependent on the body, then just hashing
101101
// the body satisfies the condition of two nodes being different have different
102102
// `hash_stable` results.
103-
let OwnerNodes {
104-
hash_including_bodies,
105-
hash_without_bodies: _,
106-
nodes: _,
107-
bodies: _,
108-
local_id_to_def_id: _,
109-
} = *self;
103+
let OwnerNodes { hash_including_bodies, hash_without_bodies: _, nodes: _, bodies: _ } =
104+
*self;
110105
hash_including_bodies.hash_stable(hcx, hasher);
111106
}
112107
}

compiler/rustc_hir_analysis/src/check/check.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -665,7 +665,7 @@ fn check_item_type(tcx: TyCtxt<'_>, id: hir::ItemId) {
665665
DefKind::GlobalAsm => {
666666
let it = tcx.hir().item(id);
667667
let hir::ItemKind::GlobalAsm(asm) = it.kind else { span_bug!(it.span, "DefKind::GlobalAsm but got {:#?}", it) };
668-
InlineAsmCtxt::new_global_asm(tcx).check_asm(asm, id.hir_id());
668+
InlineAsmCtxt::new_global_asm(tcx).check_asm(asm, id.owner_id.def_id);
669669
}
670670
_ => {}
671671
}

compiler/rustc_hir_analysis/src/check/intrinsicck.rs

+3-4
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use rustc_data_structures::fx::FxHashSet;
33
use rustc_hir as hir;
44
use rustc_middle::ty::{self, Article, FloatTy, IntTy, Ty, TyCtxt, TypeVisitable, UintTy};
55
use rustc_session::lint;
6+
use rustc_span::def_id::LocalDefId;
67
use rustc_span::{Symbol, DUMMY_SP};
78
use rustc_target::asm::{InlineAsmReg, InlineAsmRegClass, InlineAsmRegOrRegClass, InlineAsmType};
89

@@ -253,10 +254,8 @@ impl<'a, 'tcx> InlineAsmCtxt<'a, 'tcx> {
253254
Some(asm_ty)
254255
}
255256

256-
pub fn check_asm(&self, asm: &hir::InlineAsm<'tcx>, enclosing_id: hir::HirId) {
257-
let hir = self.tcx.hir();
258-
let enclosing_def_id = hir.local_def_id(enclosing_id).to_def_id();
259-
let target_features = self.tcx.asm_target_features(enclosing_def_id);
257+
pub fn check_asm(&self, asm: &hir::InlineAsm<'tcx>, enclosing_id: LocalDefId) {
258+
let target_features = self.tcx.asm_target_features(enclosing_id.to_def_id());
260259
let Some(asm_arch) = self.tcx.sess.asm_arch else {
261260
self.tcx.sess.delay_span_bug(DUMMY_SP, "target architecture does not support asm");
262261
return;

compiler/rustc_hir_analysis/src/check/wfcheck.rs

+12-5
Original file line numberDiff line numberDiff line change
@@ -391,7 +391,7 @@ fn check_gat_where_clauses(tcx: TyCtxt<'_>, associated_items: &[hir::TraitItemRe
391391
gather_gat_bounds(
392392
tcx,
393393
param_env,
394-
item_def_id.def_id,
394+
item_def_id,
395395
sig.inputs_and_output,
396396
// We also assume that all of the function signature's parameter types
397397
// are well formed.
@@ -413,7 +413,7 @@ fn check_gat_where_clauses(tcx: TyCtxt<'_>, associated_items: &[hir::TraitItemRe
413413
gather_gat_bounds(
414414
tcx,
415415
param_env,
416-
item_def_id.def_id,
416+
item_def_id,
417417
tcx.explicit_item_bounds(item_def_id).to_vec(),
418418
&FxIndexSet::default(),
419419
gat_def_id.def_id,
@@ -563,7 +563,7 @@ fn augment_param_env<'tcx>(
563563
fn gather_gat_bounds<'tcx, T: TypeFoldable<'tcx>>(
564564
tcx: TyCtxt<'tcx>,
565565
param_env: ty::ParamEnv<'tcx>,
566-
item_def_id: LocalDefId,
566+
item_def_id: hir::OwnerId,
567567
to_check: T,
568568
wf_tys: &FxIndexSet<Ty<'tcx>>,
569569
gat_def_id: LocalDefId,
@@ -596,7 +596,7 @@ fn gather_gat_bounds<'tcx, T: TypeFoldable<'tcx>>(
596596
// reflected in a where clause on the GAT itself.
597597
for (ty, ty_idx) in &types {
598598
// In our example, requires that `Self: 'a`
599-
if ty_known_to_outlive(tcx, item_def_id, param_env, &wf_tys, *ty, *region_a) {
599+
if ty_known_to_outlive(tcx, item_def_id.def_id, param_env, &wf_tys, *ty, *region_a) {
600600
debug!(?ty_idx, ?region_a_idx);
601601
debug!("required clause: {ty} must outlive {region_a}");
602602
// Translate into the generic parameters of the GAT. In
@@ -634,7 +634,14 @@ fn gather_gat_bounds<'tcx, T: TypeFoldable<'tcx>>(
634634
if ty::ReStatic == **region_b || region_a == region_b {
635635
continue;
636636
}
637-
if region_known_to_outlive(tcx, item_def_id, param_env, &wf_tys, *region_a, *region_b) {
637+
if region_known_to_outlive(
638+
tcx,
639+
item_def_id.def_id,
640+
param_env,
641+
&wf_tys,
642+
*region_a,
643+
*region_b,
644+
) {
638645
debug!(?region_a_idx, ?region_b_idx);
639646
debug!("required clause: {region_a} must outlive {region_b}");
640647
// Translate into the generic parameters of the GAT.

compiler/rustc_hir_analysis/src/collect/lifetimes.rs

+25-18
Original file line numberDiff line numberDiff line change
@@ -1264,14 +1264,21 @@ impl<'a, 'tcx> LifetimeContext<'a, 'tcx> {
12641264
} else if let Some(body_id) = outermost_body {
12651265
let fn_id = self.tcx.hir().body_owner(body_id);
12661266
match self.tcx.hir().get(fn_id) {
1267-
Node::Item(hir::Item { kind: hir::ItemKind::Fn(..), .. })
1267+
Node::Item(hir::Item { owner_id, kind: hir::ItemKind::Fn(..), .. })
12681268
| Node::TraitItem(hir::TraitItem {
1269-
kind: hir::TraitItemKind::Fn(..), ..
1269+
owner_id,
1270+
kind: hir::TraitItemKind::Fn(..),
1271+
..
12701272
})
1271-
| Node::ImplItem(hir::ImplItem { kind: hir::ImplItemKind::Fn(..), .. })
1272-
| Node::Expr(hir::Expr { kind: hir::ExprKind::Closure(..), .. }) => {
1273-
let scope = self.tcx.hir().local_def_id(fn_id);
1274-
def = Region::Free(scope.to_def_id(), def.id().unwrap());
1273+
| Node::ImplItem(hir::ImplItem {
1274+
owner_id,
1275+
kind: hir::ImplItemKind::Fn(..),
1276+
..
1277+
}) => {
1278+
def = Region::Free(owner_id.to_def_id(), def.id().unwrap());
1279+
}
1280+
Node::Expr(hir::Expr { kind: hir::ExprKind::Closure(closure), .. }) => {
1281+
def = Region::Free(closure.def_id.to_def_id(), def.id().unwrap());
12751282
}
12761283
_ => {}
12771284
}
@@ -1658,10 +1665,12 @@ impl<'a, 'tcx> LifetimeContext<'a, 'tcx> {
16581665
/// "Constrained" basically means that it appears in any type but
16591666
/// not amongst the inputs to a projection. In other words, `<&'a
16601667
/// T as Trait<''b>>::Foo` does not constrain `'a` or `'b`.
1661-
fn is_late_bound_map(tcx: TyCtxt<'_>, def_id: LocalDefId) -> Option<&FxIndexSet<LocalDefId>> {
1662-
let hir_id = tcx.hir().local_def_id_to_hir_id(def_id);
1663-
let decl = tcx.hir().fn_decl_by_hir_id(hir_id)?;
1664-
let generics = tcx.hir().get_generics(def_id)?;
1668+
fn is_late_bound_map(
1669+
tcx: TyCtxt<'_>,
1670+
owner_id: hir::OwnerId,
1671+
) -> Option<&FxIndexSet<hir::ItemLocalId>> {
1672+
let decl = tcx.hir().fn_decl_by_hir_id(owner_id.into())?;
1673+
let generics = tcx.hir().get_generics(owner_id.def_id)?;
16651674

16661675
let mut late_bound = FxIndexSet::default();
16671676

@@ -1695,24 +1704,22 @@ fn is_late_bound_map(tcx: TyCtxt<'_>, def_id: LocalDefId) -> Option<&FxIndexSet<
16951704
hir::GenericParamKind::Type { .. } | hir::GenericParamKind::Const { .. } => continue,
16961705
}
16971706

1698-
let param_def_id = tcx.hir().local_def_id(param.hir_id);
1699-
17001707
// appears in the where clauses? early-bound.
1701-
if appears_in_where_clause.regions.contains(&param_def_id) {
1708+
if appears_in_where_clause.regions.contains(&param.def_id) {
17021709
continue;
17031710
}
17041711

17051712
// does not appear in the inputs, but appears in the return type? early-bound.
1706-
if !constrained_by_input.regions.contains(&param_def_id)
1707-
&& appears_in_output.regions.contains(&param_def_id)
1713+
if !constrained_by_input.regions.contains(&param.def_id)
1714+
&& appears_in_output.regions.contains(&param.def_id)
17081715
{
17091716
continue;
17101717
}
17111718

1712-
debug!("lifetime {:?} with id {:?} is late-bound", param.name.ident(), param.hir_id);
1719+
debug!("lifetime {:?} with id {:?} is late-bound", param.name.ident(), param.def_id);
17131720

1714-
let inserted = late_bound.insert(param_def_id);
1715-
assert!(inserted, "visited lifetime {:?} twice", param.hir_id);
1721+
let inserted = late_bound.insert(param.hir_id.local_id);
1722+
assert!(inserted, "visited lifetime {:?} twice", param.def_id);
17161723
}
17171724

17181725
debug!(?late_bound);

compiler/rustc_hir_analysis/src/collect/predicates_of.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,7 @@ fn gather_explicit_predicates_of(tcx: TyCtxt<'_>, def_id: DefId) -> ty::GenericP
280280
}
281281

282282
let hir::GenericParamKind::Lifetime { .. } = duplicate.kind else { continue };
283-
let dup_def = tcx.hir().local_def_id(duplicate.hir_id).to_def_id();
283+
let dup_def = duplicate.def_id.to_def_id();
284284

285285
let Some(dup_index) = generics.param_def_id_to_index(tcx, dup_def) else { bug!() };
286286

compiler/rustc_hir_analysis/src/collect/type_of.rs

+7-8
Original file line numberDiff line numberDiff line change
@@ -54,15 +54,14 @@ pub(super) fn opt_const_param_of(tcx: TyCtxt<'_>, def_id: LocalDefId) -> Option<
5454
// ty which is a fully resolved projection.
5555
// For the code example above, this would mean converting Self::Assoc<3>
5656
// into a ty::Alias(ty::Projection, <Self as Foo>::Assoc<3>)
57-
let item_hir_id = tcx
57+
let item_def_id = tcx
5858
.hir()
59-
.parent_iter(hir_id)
60-
.filter(|(_, node)| matches!(node, Node::Item(_)))
61-
.map(|(id, _)| id)
62-
.next()
63-
.unwrap();
64-
let item_did = tcx.hir().local_def_id(item_hir_id).to_def_id();
65-
let item_ctxt = &ItemCtxt::new(tcx, item_did) as &dyn crate::astconv::AstConv<'_>;
59+
.parent_owner_iter(hir_id)
60+
.find(|(_, node)| matches!(node, OwnerNode::Item(_)))
61+
.unwrap()
62+
.0
63+
.to_def_id();
64+
let item_ctxt = &ItemCtxt::new(tcx, item_def_id) as &dyn crate::astconv::AstConv<'_>;
6665
let ty = item_ctxt.ast_ty_to_ty(hir_ty);
6766

6867
// Iterate through the generics of the projection to find the one that corresponds to

compiler/rustc_hir_typeck/src/_match.rs

+1
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
186186
prior_arm: Option<(Option<hir::HirId>, Ty<'tcx>, Span)>,
187187
) {
188188
let hir = self.tcx.hir();
189+
189190
// First, check that we're actually in the tail of a function.
190191
let Some(body_id) = hir.maybe_body_owned_by(self.body_id) else { return; };
191192
let body = hir.body(body_id);

compiler/rustc_hir_typeck/src/check.rs

+8-3
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,12 @@ pub(super) fn check_fn<'a, 'tcx>(
130130
let gen_ty = if let (Some(_), Some(gen_kind)) = (can_be_generator, body.generator_kind) {
131131
let interior = fcx
132132
.next_ty_var(TypeVariableOrigin { kind: TypeVariableOriginKind::MiscVariable, span });
133-
fcx.deferred_generator_interiors.borrow_mut().push((fn_id, body.id(), interior, gen_kind));
133+
fcx.deferred_generator_interiors.borrow_mut().push((
134+
fn_def_id,
135+
body.id(),
136+
interior,
137+
gen_kind,
138+
));
134139

135140
let (resume_ty, yield_ty) = fcx.resume_yield_tys.unwrap();
136141
Some(GeneratorTypes {
@@ -167,12 +172,12 @@ pub(super) fn check_fn<'a, 'tcx>(
167172

168173
// Check that a function marked as `#[panic_handler]` has signature `fn(&PanicInfo) -> !`
169174
if let Some(panic_impl_did) = tcx.lang_items().panic_impl()
170-
&& panic_impl_did == hir.local_def_id(fn_id).to_def_id()
175+
&& panic_impl_did == fn_def_id.to_def_id()
171176
{
172177
check_panic_info_fn(tcx, panic_impl_did.expect_local(), fn_sig, decl, declared_ret_ty);
173178
}
174179

175-
if let Some(lang_start_defid) = tcx.lang_items().start_fn() && lang_start_defid == hir.local_def_id(fn_id).to_def_id() {
180+
if let Some(lang_start_defid) = tcx.lang_items().start_fn() && lang_start_defid == fn_def_id.to_def_id() {
176181
check_lang_start_fn(tcx, fn_sig, decl, fn_def_id);
177182
}
178183

compiler/rustc_hir_typeck/src/fn_ctxt/_impl.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -549,8 +549,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
549549
let generators = std::mem::take(&mut *self.deferred_generator_interiors.borrow_mut());
550550
debug!(?generators);
551551

552-
for &(expr_hir_id, body_id, interior, _) in generators.iter() {
553-
let expr_def_id = self.tcx.hir().local_def_id(expr_hir_id);
552+
for &(expr_def_id, body_id, interior, _) in generators.iter() {
554553
debug!(?expr_def_id);
555554

556555
// Create the `GeneratorWitness` type that we will unify with `interior`.

compiler/rustc_hir_typeck/src/fn_ctxt/checks.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
7979
}
8080
};
8181
InlineAsmCtxt::new_in_fn(self.tcx, self.param_env, get_operand_ty)
82-
.check_asm(asm, self.tcx.hir().local_def_id_to_hir_id(enclosing_id));
82+
.check_asm(asm, enclosing_id);
8383
}
8484
}
8585

compiler/rustc_hir_typeck/src/inherited.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ pub struct Inherited<'tcx> {
5656
pub(super) deferred_asm_checks: RefCell<Vec<(&'tcx hir::InlineAsm<'tcx>, hir::HirId)>>,
5757

5858
pub(super) deferred_generator_interiors:
59-
RefCell<Vec<(hir::HirId, hir::BodyId, Ty<'tcx>, hir::GeneratorKind)>>,
59+
RefCell<Vec<(LocalDefId, hir::BodyId, Ty<'tcx>, hir::GeneratorKind)>>,
6060

6161
pub(super) body_id: Option<hir::BodyId>,
6262

compiler/rustc_hir_typeck/src/writeback.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
4040
&self,
4141
body: &'tcx hir::Body<'tcx>,
4242
) -> &'tcx ty::TypeckResults<'tcx> {
43-
let item_id = self.tcx.hir().body_owner(body.id());
44-
let item_def_id = self.tcx.hir().local_def_id(item_id);
43+
let item_def_id = self.tcx.hir().body_owner_def_id(body.id());
4544

4645
// This attribute causes us to dump some writeback information
4746
// in the form of errors, which is used for unit tests.
@@ -55,7 +54,8 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
5554
// Type only exists for constants and statics, not functions.
5655
match self.tcx.hir().body_owner_kind(item_def_id) {
5756
hir::BodyOwnerKind::Const | hir::BodyOwnerKind::Static(_) => {
58-
wbcx.visit_node_id(body.value.span, item_id);
57+
let item_hir_id = self.tcx.hir().local_def_id_to_hir_id(item_def_id);
58+
wbcx.visit_node_id(body.value.span, item_hir_id);
5959
}
6060
hir::BodyOwnerKind::Closure | hir::BodyOwnerKind::Fn => (),
6161
}

0 commit comments

Comments
 (0)