Skip to content

Commit 8fe989d

Browse files
committed
Auto merge of #81611 - cjgillot:meowner, r=estebank
Only store a LocalDefId in some HIR nodes Some HIR nodes are guaranteed to be HIR owners: Item, TraitItem, ImplItem, ForeignItem and MacroDef. As a consequence, we do not need to store the `HirId`'s `local_id`, and we can directly store a `LocalDefId`. This allows to avoid a bit of the dance with `tcx.hir().local_def_id` and `tcx.hir().local_def_id_to_hir_id` mappings.
2 parents a143517 + 91d8e59 commit 8fe989d

File tree

117 files changed

+1126
-1190
lines changed

Some content is hidden

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

117 files changed

+1126
-1190
lines changed

Diff for: compiler/rustc_ast_lowering/src/item.rs

+33-26
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,10 @@ impl ItemLowerer<'_, '_, '_> {
3535

3636
impl<'a> Visitor<'a> for ItemLowerer<'a, '_, '_> {
3737
fn visit_mod(&mut self, m: &'a Mod, _s: Span, _attrs: &[Attribute], n: NodeId) {
38-
let hir_id = self.lctx.lower_node_id(n);
38+
let def_id = self.lctx.lower_node_id(n).expect_owner();
3939

4040
self.lctx.modules.insert(
41-
hir_id,
41+
def_id,
4242
hir::ModuleItems {
4343
items: BTreeSet::new(),
4444
trait_items: BTreeSet::new(),
@@ -48,7 +48,7 @@ impl<'a> Visitor<'a> for ItemLowerer<'a, '_, '_> {
4848
);
4949

5050
let old = self.lctx.current_module;
51-
self.lctx.current_module = hir_id;
51+
self.lctx.current_module = def_id;
5252
visit::walk_mod(self, m);
5353
self.lctx.current_module = old;
5454
}
@@ -58,8 +58,8 @@ impl<'a> Visitor<'a> for ItemLowerer<'a, '_, '_> {
5858
self.lctx.with_hir_id_owner(item.id, |lctx| {
5959
lctx.without_in_scope_lifetime_defs(|lctx| {
6060
if let Some(hir_item) = lctx.lower_item(item) {
61-
item_hir_id = Some(hir_item.hir_id);
62-
lctx.insert_item(hir_item);
61+
let id = lctx.insert_item(hir_item);
62+
item_hir_id = Some(id);
6363
}
6464
})
6565
});
@@ -92,13 +92,13 @@ impl<'a> Visitor<'a> for ItemLowerer<'a, '_, '_> {
9292
self.lctx.with_hir_id_owner(item.id, |lctx| match ctxt {
9393
AssocCtxt::Trait => {
9494
let hir_item = lctx.lower_trait_item(item);
95-
let id = hir::TraitItemId { hir_id: hir_item.hir_id };
95+
let id = hir_item.trait_item_id();
9696
lctx.trait_items.insert(id, hir_item);
9797
lctx.modules.get_mut(&lctx.current_module).unwrap().trait_items.insert(id);
9898
}
9999
AssocCtxt::Impl => {
100100
let hir_item = lctx.lower_impl_item(item);
101-
let id = hir::ImplItemId { hir_id: hir_item.hir_id };
101+
let id = hir_item.impl_item_id();
102102
lctx.impl_items.insert(id, hir_item);
103103
lctx.modules.get_mut(&lctx.current_module).unwrap().impl_items.insert(id);
104104
}
@@ -111,7 +111,7 @@ impl<'a> Visitor<'a> for ItemLowerer<'a, '_, '_> {
111111
self.lctx.allocate_hir_id_counter(item.id);
112112
self.lctx.with_hir_id_owner(item.id, |lctx| {
113113
let hir_item = lctx.lower_foreign_item(item);
114-
let id = hir::ForeignItemId { hir_id: hir_item.hir_id };
114+
let id = hir_item.foreign_item_id();
115115
lctx.foreign_items.insert(id, hir_item);
116116
lctx.modules.get_mut(&lctx.current_module).unwrap().foreign_items.insert(id);
117117
});
@@ -128,7 +128,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
128128
// only used when lowering a child item of a trait or impl.
129129
fn with_parent_item_lifetime_defs<T>(
130130
&mut self,
131-
parent_hir_id: hir::HirId,
131+
parent_hir_id: hir::ItemId,
132132
f: impl FnOnce(&mut LoweringContext<'_, '_>) -> T,
133133
) -> T {
134134
let old_len = self.in_scope_lifetimes.len();
@@ -197,7 +197,9 @@ impl<'hir> LoweringContext<'_, 'hir> {
197197

198198
node_ids
199199
.into_iter()
200-
.map(|node_id| hir::ItemId { id: self.allocate_hir_id_counter(node_id) })
200+
.map(|node_id| hir::ItemId {
201+
def_id: self.allocate_hir_id_counter(node_id).expect_owner(),
202+
})
201203
.collect()
202204
}
203205

@@ -232,13 +234,13 @@ impl<'hir> LoweringContext<'_, 'hir> {
232234

233235
if let ItemKind::MacroDef(MacroDef { ref body, macro_rules }) = i.kind {
234236
if !macro_rules || self.sess.contains_name(&i.attrs, sym::macro_export) {
235-
let hir_id = self.lower_node_id(i.id);
237+
let def_id = self.lower_node_id(i.id).expect_owner();
236238
let body = P(self.lower_mac_args(body));
237239
self.exported_macros.push(hir::MacroDef {
238240
ident,
239241
vis,
240242
attrs,
241-
hir_id,
243+
def_id,
242244
span: i.span,
243245
ast: MacroDef { body, macro_rules },
244246
});
@@ -250,7 +252,14 @@ impl<'hir> LoweringContext<'_, 'hir> {
250252

251253
let kind = self.lower_item_kind(i.span, i.id, &mut ident, attrs, &mut vis, &i.kind);
252254

253-
Some(hir::Item { hir_id: self.lower_node_id(i.id), ident, attrs, kind, vis, span: i.span })
255+
Some(hir::Item {
256+
def_id: self.lower_node_id(i.id).expect_owner(),
257+
ident,
258+
attrs,
259+
kind,
260+
vis,
261+
span: i.span,
262+
})
254263
}
255264

256265
fn lower_item_kind(
@@ -387,8 +396,6 @@ impl<'hir> LoweringContext<'_, 'hir> {
387396
self_ty: ref ty,
388397
items: ref impl_items,
389398
}) => {
390-
let def_id = self.resolver.local_def_id(id);
391-
392399
// Lower the "impl header" first. This ordering is important
393400
// for in-band lifetimes! Consider `'a` here:
394401
//
@@ -402,10 +409,10 @@ impl<'hir> LoweringContext<'_, 'hir> {
402409
// method, it will not be considered an in-band
403410
// lifetime to be added, but rather a reference to a
404411
// parent lifetime.
405-
let lowered_trait_impl_id = self.lower_node_id(id);
412+
let lowered_trait_def_id = self.lower_node_id(id).expect_owner();
406413
let (generics, (trait_ref, lowered_ty)) = self.add_in_band_defs(
407414
ast_generics,
408-
def_id,
415+
lowered_trait_def_id,
409416
AnonymousLifetimeMode::CreateParameter,
410417
|this, _| {
411418
let trait_ref = trait_ref.as_ref().map(|trait_ref| {
@@ -417,7 +424,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
417424
this.trait_impls
418425
.entry(def_id)
419426
.or_default()
420-
.push(lowered_trait_impl_id);
427+
.push(lowered_trait_def_id);
421428
}
422429
}
423430

@@ -557,7 +564,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
557564
let vis = this.rebuild_vis(&vis);
558565

559566
this.insert_item(hir::Item {
560-
hir_id: new_id,
567+
def_id: new_id.expect_owner(),
561568
ident,
562569
attrs,
563570
kind,
@@ -629,7 +636,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
629636
this.lower_use_tree(use_tree, &prefix, id, &mut vis, &mut ident, attrs);
630637

631638
this.insert_item(hir::Item {
632-
hir_id: new_hir_id,
639+
def_id: new_hir_id.expect_owner(),
633640
ident,
634641
attrs,
635642
kind,
@@ -702,7 +709,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
702709
fn lower_foreign_item(&mut self, i: &ForeignItem) -> hir::ForeignItem<'hir> {
703710
let def_id = self.resolver.local_def_id(i.id);
704711
hir::ForeignItem {
705-
hir_id: self.lower_node_id(i.id),
712+
def_id,
706713
ident: i.ident,
707714
attrs: self.lower_attrs(&i.attrs),
708715
kind: match i.kind {
@@ -737,7 +744,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
737744

738745
fn lower_foreign_item_ref(&mut self, i: &ForeignItem) -> hir::ForeignItemRef<'hir> {
739746
hir::ForeignItemRef {
740-
id: hir::ForeignItemId { hir_id: self.lower_node_id(i.id) },
747+
id: hir::ForeignItemId { def_id: self.lower_node_id(i.id).expect_owner() },
741748
ident: i.ident,
742749
span: i.span,
743750
vis: self.lower_visibility(&i.vis, Some(i.id)),
@@ -837,7 +844,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
837844
};
838845

839846
hir::TraitItem {
840-
hir_id: self.lower_node_id(i.id),
847+
def_id: trait_item_def_id,
841848
ident: i.ident,
842849
attrs: self.lower_attrs(&i.attrs),
843850
generics,
@@ -857,7 +864,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
857864
}
858865
AssocItemKind::MacCall(..) => unimplemented!(),
859866
};
860-
let id = hir::TraitItemId { hir_id: self.lower_node_id(i.id) };
867+
let id = hir::TraitItemId { def_id: self.lower_node_id(i.id).expect_owner() };
861868
let defaultness = hir::Defaultness::Default { has_value: has_default };
862869
hir::TraitItemRef { id, ident: i.ident, span: i.span, defaultness, kind }
863870
}
@@ -922,7 +929,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
922929
let has_value = true;
923930
let (defaultness, _) = self.lower_defaultness(i.kind.defaultness(), has_value);
924931
hir::ImplItem {
925-
hir_id: self.lower_node_id(i.id),
932+
def_id: self.lower_node_id(i.id).expect_owner(),
926933
ident: i.ident,
927934
attrs: self.lower_attrs(&i.attrs),
928935
generics,
@@ -938,7 +945,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
938945
let has_value = true;
939946
let (defaultness, _) = self.lower_defaultness(i.kind.defaultness(), has_value);
940947
hir::ImplItemRef {
941-
id: hir::ImplItemId { hir_id: self.lower_node_id(i.id) },
948+
id: hir::ImplItemId { def_id: self.lower_node_id(i.id).expect_owner() },
942949
ident: i.ident,
943950
span: i.span,
944951
vis: self.lower_visibility(&i.vis, Some(i.id)),

Diff for: compiler/rustc_ast_lowering/src/lib.rs

+24-25
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ use rustc_data_structures::sync::Lrc;
4848
use rustc_errors::struct_span_err;
4949
use rustc_hir as hir;
5050
use rustc_hir::def::{DefKind, Namespace, PartialRes, PerNS, Res};
51-
use rustc_hir::def_id::{DefId, DefIdMap, LocalDefId, CRATE_DEF_INDEX};
51+
use rustc_hir::def_id::{DefId, DefIdMap, LocalDefId, CRATE_DEF_ID};
5252
use rustc_hir::definitions::{DefKey, DefPathData, Definitions};
5353
use rustc_hir::intravisit;
5454
use rustc_hir::{ConstArg, GenericArg, ParamName};
@@ -99,7 +99,7 @@ struct LoweringContext<'a, 'hir: 'a> {
9999
arena: &'hir Arena<'hir>,
100100

101101
/// The items being lowered are collected here.
102-
items: BTreeMap<hir::HirId, hir::Item<'hir>>,
102+
items: BTreeMap<hir::ItemId, hir::Item<'hir>>,
103103

104104
trait_items: BTreeMap<hir::TraitItemId, hir::TraitItem<'hir>>,
105105
impl_items: BTreeMap<hir::ImplItemId, hir::ImplItem<'hir>>,
@@ -108,9 +108,9 @@ struct LoweringContext<'a, 'hir: 'a> {
108108
exported_macros: Vec<hir::MacroDef<'hir>>,
109109
non_exported_macro_attrs: Vec<ast::Attribute>,
110110

111-
trait_impls: BTreeMap<DefId, Vec<hir::HirId>>,
111+
trait_impls: BTreeMap<DefId, Vec<LocalDefId>>,
112112

113-
modules: BTreeMap<hir::HirId, hir::ModuleItems>,
113+
modules: BTreeMap<LocalDefId, hir::ModuleItems>,
114114

115115
generator_kind: Option<hir::GeneratorKind>,
116116

@@ -158,7 +158,7 @@ struct LoweringContext<'a, 'hir: 'a> {
158158
/// vector.
159159
in_scope_lifetimes: Vec<ParamName>,
160160

161-
current_module: hir::HirId,
161+
current_module: LocalDefId,
162162

163163
type_def_lifetime_params: DefIdMap<usize>,
164164

@@ -314,8 +314,8 @@ pub fn lower_crate<'a, 'hir>(
314314
is_in_dyn_type: false,
315315
anonymous_lifetime_mode: AnonymousLifetimeMode::PassThrough,
316316
type_def_lifetime_params: Default::default(),
317-
current_module: hir::CRATE_HIR_ID,
318-
current_hir_id_owner: vec![(LocalDefId { local_def_index: CRATE_DEF_INDEX }, 0)],
317+
current_module: CRATE_DEF_ID,
318+
current_hir_id_owner: vec![(CRATE_DEF_ID, 0)],
319319
item_local_id_counters: Default::default(),
320320
node_id_to_hir_id: IndexVec::new(),
321321
generator_kind: None,
@@ -605,12 +605,11 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
605605
}
606606
}
607607

608-
fn insert_item(&mut self, item: hir::Item<'hir>) {
609-
let id = item.hir_id;
610-
// FIXME: Use `debug_asset-rt`.
611-
assert_eq!(id.local_id, hir::ItemLocalId::from_u32(0));
608+
fn insert_item(&mut self, item: hir::Item<'hir>) -> hir::ItemId {
609+
let id = hir::ItemId { def_id: item.def_id };
612610
self.items.insert(id, item);
613611
self.modules.get_mut(&self.current_module).unwrap().items.insert(id);
612+
id
614613
}
615614

616615
fn allocate_hir_id_counter(&mut self, owner: NodeId) -> hir::HirId {
@@ -1547,29 +1546,27 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
15471546
};
15481547

15491548
trace!("lower_opaque_impl_trait: {:#?}", opaque_ty_def_id);
1550-
let opaque_ty_id =
1551-
lctx.generate_opaque_type(opaque_ty_node_id, opaque_ty_item, span, opaque_ty_span);
1549+
lctx.generate_opaque_type(opaque_ty_def_id, opaque_ty_item, span, opaque_ty_span);
15521550

15531551
// `impl Trait` now just becomes `Foo<'a, 'b, ..>`.
1554-
hir::TyKind::OpaqueDef(hir::ItemId { id: opaque_ty_id }, lifetimes)
1552+
hir::TyKind::OpaqueDef(hir::ItemId { def_id: opaque_ty_def_id }, lifetimes)
15551553
})
15561554
}
15571555

15581556
/// Registers a new opaque type with the proper `NodeId`s and
15591557
/// returns the lowered node-ID for the opaque type.
15601558
fn generate_opaque_type(
15611559
&mut self,
1562-
opaque_ty_node_id: NodeId,
1560+
opaque_ty_id: LocalDefId,
15631561
opaque_ty_item: hir::OpaqueTy<'hir>,
15641562
span: Span,
15651563
opaque_ty_span: Span,
1566-
) -> hir::HirId {
1564+
) {
15671565
let opaque_ty_item_kind = hir::ItemKind::OpaqueTy(opaque_ty_item);
1568-
let opaque_ty_id = self.lower_node_id(opaque_ty_node_id);
15691566
// Generate an `type Foo = impl Trait;` declaration.
15701567
trace!("registering opaque type with id {:#?}", opaque_ty_id);
15711568
let opaque_ty_item = hir::Item {
1572-
hir_id: opaque_ty_id,
1569+
def_id: opaque_ty_id,
15731570
ident: Ident::invalid(),
15741571
attrs: Default::default(),
15751572
kind: opaque_ty_item_kind,
@@ -1581,7 +1578,6 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
15811578
// automatically for all AST items. But this opaque type item
15821579
// does not actually exist in the AST.
15831580
self.insert_item(opaque_ty_item);
1584-
opaque_ty_id
15851581
}
15861582

15871583
fn lifetimes_from_impl_trait_bounds(
@@ -2010,7 +2006,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
20102006
// grow.
20112007
let input_lifetimes_count = self.in_scope_lifetimes.len() + self.lifetimes_to_define.len();
20122008

2013-
let (opaque_ty_id, lifetime_params) = self.with_hir_id_owner(opaque_ty_node_id, |this| {
2009+
let lifetime_params = self.with_hir_id_owner(opaque_ty_node_id, |this| {
20142010
// We have to be careful to get elision right here. The
20152011
// idea is that we create a lifetime parameter for each
20162012
// lifetime in the return type. So, given a return type
@@ -2061,10 +2057,9 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
20612057
};
20622058

20632059
trace!("exist ty from async fn def id: {:#?}", opaque_ty_def_id);
2064-
let opaque_ty_id =
2065-
this.generate_opaque_type(opaque_ty_node_id, opaque_ty_item, span, opaque_ty_span);
2060+
this.generate_opaque_type(opaque_ty_def_id, opaque_ty_item, span, opaque_ty_span);
20662061

2067-
(opaque_ty_id, lifetime_params)
2062+
lifetime_params
20682063
});
20692064

20702065
// As documented above on the variable
@@ -2107,7 +2102,8 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
21072102
// Foo = impl Trait` is, internally, created as a child of the
21082103
// async fn, so the *type parameters* are inherited. It's
21092104
// only the lifetime parameters that we must supply.
2110-
let opaque_ty_ref = hir::TyKind::OpaqueDef(hir::ItemId { id: opaque_ty_id }, generic_args);
2105+
let opaque_ty_ref =
2106+
hir::TyKind::OpaqueDef(hir::ItemId { def_id: opaque_ty_def_id }, generic_args);
21112107
let opaque_ty = self.ty(opaque_ty_span, opaque_ty_ref);
21122108
hir::FnRetTy::Return(self.arena.alloc(opaque_ty))
21132109
}
@@ -2432,7 +2428,10 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
24322428
let mut ids: SmallVec<[hir::Stmt<'hir>; 1]> = item_ids
24332429
.into_iter()
24342430
.map(|item_id| {
2435-
let item_id = hir::ItemId { id: self.lower_node_id(item_id) };
2431+
let item_id = hir::ItemId {
2432+
// All the items that `lower_local` finds are `impl Trait` types.
2433+
def_id: self.lower_node_id(item_id).expect_owner(),
2434+
};
24362435
self.stmt(s.span, hir::StmtKind::Item(item_id))
24372436
})
24382437
.collect();

Diff for: compiler/rustc_codegen_cranelift/src/driver/aot.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -164,8 +164,8 @@ fn module_codegen(tcx: TyCtxt<'_>, cgu_name: rustc_span::Symbol) -> ModuleCodege
164164
MonoItem::Static(def_id) => {
165165
crate::constant::codegen_static(&mut cx.constants_cx, def_id)
166166
}
167-
MonoItem::GlobalAsm(hir_id) => {
168-
let item = cx.tcx.hir().expect_item(hir_id);
167+
MonoItem::GlobalAsm(item_id) => {
168+
let item = cx.tcx.hir().item(item_id);
169169
if let rustc_hir::ItemKind::GlobalAsm(rustc_hir::GlobalAsm { asm }) = item.kind {
170170
cx.global_asm.push_str(&*asm.as_str());
171171
cx.global_asm.push_str("\n\n");

Diff for: compiler/rustc_codegen_cranelift/src/driver/jit.rs

+3-4
Original file line numberDiff line numberDiff line change
@@ -93,10 +93,9 @@ pub(super) fn run_jit(tcx: TyCtxt<'_>, codegen_mode: CodegenMode) -> ! {
9393
MonoItem::Static(def_id) => {
9494
crate::constant::codegen_static(&mut cx.constants_cx, def_id);
9595
}
96-
MonoItem::GlobalAsm(hir_id) => {
97-
let item = cx.tcx.hir().expect_item(hir_id);
98-
tcx.sess
99-
.span_fatal(item.span, "Global asm is not supported in JIT mode");
96+
MonoItem::GlobalAsm(item_id) => {
97+
let item = cx.tcx.hir().item(item_id);
98+
tcx.sess.span_fatal(item.span, "Global asm is not supported in JIT mode");
10099
}
101100
}
102101
}

0 commit comments

Comments
 (0)