Skip to content

Commit

Permalink
Auto merge of #81611 - cjgillot:meowner, r=estebank
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
bors committed Feb 16, 2021
2 parents a143517 + 91d8e59 commit 8fe989d
Show file tree
Hide file tree
Showing 117 changed files with 1,126 additions and 1,190 deletions.
59 changes: 33 additions & 26 deletions compiler/rustc_ast_lowering/src/item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,10 @@ impl ItemLowerer<'_, '_, '_> {

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

self.lctx.modules.insert(
hir_id,
def_id,
hir::ModuleItems {
items: BTreeSet::new(),
trait_items: BTreeSet::new(),
Expand All @@ -48,7 +48,7 @@ impl<'a> Visitor<'a> for ItemLowerer<'a, '_, '_> {
);

let old = self.lctx.current_module;
self.lctx.current_module = hir_id;
self.lctx.current_module = def_id;
visit::walk_mod(self, m);
self.lctx.current_module = old;
}
Expand All @@ -58,8 +58,8 @@ impl<'a> Visitor<'a> for ItemLowerer<'a, '_, '_> {
self.lctx.with_hir_id_owner(item.id, |lctx| {
lctx.without_in_scope_lifetime_defs(|lctx| {
if let Some(hir_item) = lctx.lower_item(item) {
item_hir_id = Some(hir_item.hir_id);
lctx.insert_item(hir_item);
let id = lctx.insert_item(hir_item);
item_hir_id = Some(id);
}
})
});
Expand Down Expand Up @@ -92,13 +92,13 @@ impl<'a> Visitor<'a> for ItemLowerer<'a, '_, '_> {
self.lctx.with_hir_id_owner(item.id, |lctx| match ctxt {
AssocCtxt::Trait => {
let hir_item = lctx.lower_trait_item(item);
let id = hir::TraitItemId { hir_id: hir_item.hir_id };
let id = hir_item.trait_item_id();
lctx.trait_items.insert(id, hir_item);
lctx.modules.get_mut(&lctx.current_module).unwrap().trait_items.insert(id);
}
AssocCtxt::Impl => {
let hir_item = lctx.lower_impl_item(item);
let id = hir::ImplItemId { hir_id: hir_item.hir_id };
let id = hir_item.impl_item_id();
lctx.impl_items.insert(id, hir_item);
lctx.modules.get_mut(&lctx.current_module).unwrap().impl_items.insert(id);
}
Expand All @@ -111,7 +111,7 @@ impl<'a> Visitor<'a> for ItemLowerer<'a, '_, '_> {
self.lctx.allocate_hir_id_counter(item.id);
self.lctx.with_hir_id_owner(item.id, |lctx| {
let hir_item = lctx.lower_foreign_item(item);
let id = hir::ForeignItemId { hir_id: hir_item.hir_id };
let id = hir_item.foreign_item_id();
lctx.foreign_items.insert(id, hir_item);
lctx.modules.get_mut(&lctx.current_module).unwrap().foreign_items.insert(id);
});
Expand All @@ -128,7 +128,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
// only used when lowering a child item of a trait or impl.
fn with_parent_item_lifetime_defs<T>(
&mut self,
parent_hir_id: hir::HirId,
parent_hir_id: hir::ItemId,
f: impl FnOnce(&mut LoweringContext<'_, '_>) -> T,
) -> T {
let old_len = self.in_scope_lifetimes.len();
Expand Down Expand Up @@ -197,7 +197,9 @@ impl<'hir> LoweringContext<'_, 'hir> {

node_ids
.into_iter()
.map(|node_id| hir::ItemId { id: self.allocate_hir_id_counter(node_id) })
.map(|node_id| hir::ItemId {
def_id: self.allocate_hir_id_counter(node_id).expect_owner(),
})
.collect()
}

Expand Down Expand Up @@ -232,13 +234,13 @@ impl<'hir> LoweringContext<'_, 'hir> {

if let ItemKind::MacroDef(MacroDef { ref body, macro_rules }) = i.kind {
if !macro_rules || self.sess.contains_name(&i.attrs, sym::macro_export) {
let hir_id = self.lower_node_id(i.id);
let def_id = self.lower_node_id(i.id).expect_owner();
let body = P(self.lower_mac_args(body));
self.exported_macros.push(hir::MacroDef {
ident,
vis,
attrs,
hir_id,
def_id,
span: i.span,
ast: MacroDef { body, macro_rules },
});
Expand All @@ -250,7 +252,14 @@ impl<'hir> LoweringContext<'_, 'hir> {

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

Some(hir::Item { hir_id: self.lower_node_id(i.id), ident, attrs, kind, vis, span: i.span })
Some(hir::Item {
def_id: self.lower_node_id(i.id).expect_owner(),
ident,
attrs,
kind,
vis,
span: i.span,
})
}

fn lower_item_kind(
Expand Down Expand Up @@ -387,8 +396,6 @@ impl<'hir> LoweringContext<'_, 'hir> {
self_ty: ref ty,
items: ref impl_items,
}) => {
let def_id = self.resolver.local_def_id(id);

// Lower the "impl header" first. This ordering is important
// for in-band lifetimes! Consider `'a` here:
//
Expand All @@ -402,10 +409,10 @@ impl<'hir> LoweringContext<'_, 'hir> {
// method, it will not be considered an in-band
// lifetime to be added, but rather a reference to a
// parent lifetime.
let lowered_trait_impl_id = self.lower_node_id(id);
let lowered_trait_def_id = self.lower_node_id(id).expect_owner();
let (generics, (trait_ref, lowered_ty)) = self.add_in_band_defs(
ast_generics,
def_id,
lowered_trait_def_id,
AnonymousLifetimeMode::CreateParameter,
|this, _| {
let trait_ref = trait_ref.as_ref().map(|trait_ref| {
Expand All @@ -417,7 +424,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
this.trait_impls
.entry(def_id)
.or_default()
.push(lowered_trait_impl_id);
.push(lowered_trait_def_id);
}
}

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

this.insert_item(hir::Item {
hir_id: new_id,
def_id: new_id.expect_owner(),
ident,
attrs,
kind,
Expand Down Expand Up @@ -629,7 +636,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
this.lower_use_tree(use_tree, &prefix, id, &mut vis, &mut ident, attrs);

this.insert_item(hir::Item {
hir_id: new_hir_id,
def_id: new_hir_id.expect_owner(),
ident,
attrs,
kind,
Expand Down Expand Up @@ -702,7 +709,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
fn lower_foreign_item(&mut self, i: &ForeignItem) -> hir::ForeignItem<'hir> {
let def_id = self.resolver.local_def_id(i.id);
hir::ForeignItem {
hir_id: self.lower_node_id(i.id),
def_id,
ident: i.ident,
attrs: self.lower_attrs(&i.attrs),
kind: match i.kind {
Expand Down Expand Up @@ -737,7 +744,7 @@ impl<'hir> LoweringContext<'_, 'hir> {

fn lower_foreign_item_ref(&mut self, i: &ForeignItem) -> hir::ForeignItemRef<'hir> {
hir::ForeignItemRef {
id: hir::ForeignItemId { hir_id: self.lower_node_id(i.id) },
id: hir::ForeignItemId { def_id: self.lower_node_id(i.id).expect_owner() },
ident: i.ident,
span: i.span,
vis: self.lower_visibility(&i.vis, Some(i.id)),
Expand Down Expand Up @@ -837,7 +844,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
};

hir::TraitItem {
hir_id: self.lower_node_id(i.id),
def_id: trait_item_def_id,
ident: i.ident,
attrs: self.lower_attrs(&i.attrs),
generics,
Expand All @@ -857,7 +864,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
}
AssocItemKind::MacCall(..) => unimplemented!(),
};
let id = hir::TraitItemId { hir_id: self.lower_node_id(i.id) };
let id = hir::TraitItemId { def_id: self.lower_node_id(i.id).expect_owner() };
let defaultness = hir::Defaultness::Default { has_value: has_default };
hir::TraitItemRef { id, ident: i.ident, span: i.span, defaultness, kind }
}
Expand Down Expand Up @@ -922,7 +929,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
let has_value = true;
let (defaultness, _) = self.lower_defaultness(i.kind.defaultness(), has_value);
hir::ImplItem {
hir_id: self.lower_node_id(i.id),
def_id: self.lower_node_id(i.id).expect_owner(),
ident: i.ident,
attrs: self.lower_attrs(&i.attrs),
generics,
Expand All @@ -938,7 +945,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
let has_value = true;
let (defaultness, _) = self.lower_defaultness(i.kind.defaultness(), has_value);
hir::ImplItemRef {
id: hir::ImplItemId { hir_id: self.lower_node_id(i.id) },
id: hir::ImplItemId { def_id: self.lower_node_id(i.id).expect_owner() },
ident: i.ident,
span: i.span,
vis: self.lower_visibility(&i.vis, Some(i.id)),
Expand Down
49 changes: 24 additions & 25 deletions compiler/rustc_ast_lowering/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ use rustc_data_structures::sync::Lrc;
use rustc_errors::struct_span_err;
use rustc_hir as hir;
use rustc_hir::def::{DefKind, Namespace, PartialRes, PerNS, Res};
use rustc_hir::def_id::{DefId, DefIdMap, LocalDefId, CRATE_DEF_INDEX};
use rustc_hir::def_id::{DefId, DefIdMap, LocalDefId, CRATE_DEF_ID};
use rustc_hir::definitions::{DefKey, DefPathData, Definitions};
use rustc_hir::intravisit;
use rustc_hir::{ConstArg, GenericArg, ParamName};
Expand Down Expand Up @@ -99,7 +99,7 @@ struct LoweringContext<'a, 'hir: 'a> {
arena: &'hir Arena<'hir>,

/// The items being lowered are collected here.
items: BTreeMap<hir::HirId, hir::Item<'hir>>,
items: BTreeMap<hir::ItemId, hir::Item<'hir>>,

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

trait_impls: BTreeMap<DefId, Vec<hir::HirId>>,
trait_impls: BTreeMap<DefId, Vec<LocalDefId>>,

modules: BTreeMap<hir::HirId, hir::ModuleItems>,
modules: BTreeMap<LocalDefId, hir::ModuleItems>,

generator_kind: Option<hir::GeneratorKind>,

Expand Down Expand Up @@ -158,7 +158,7 @@ struct LoweringContext<'a, 'hir: 'a> {
/// vector.
in_scope_lifetimes: Vec<ParamName>,

current_module: hir::HirId,
current_module: LocalDefId,

type_def_lifetime_params: DefIdMap<usize>,

Expand Down Expand Up @@ -314,8 +314,8 @@ pub fn lower_crate<'a, 'hir>(
is_in_dyn_type: false,
anonymous_lifetime_mode: AnonymousLifetimeMode::PassThrough,
type_def_lifetime_params: Default::default(),
current_module: hir::CRATE_HIR_ID,
current_hir_id_owner: vec![(LocalDefId { local_def_index: CRATE_DEF_INDEX }, 0)],
current_module: CRATE_DEF_ID,
current_hir_id_owner: vec![(CRATE_DEF_ID, 0)],
item_local_id_counters: Default::default(),
node_id_to_hir_id: IndexVec::new(),
generator_kind: None,
Expand Down Expand Up @@ -605,12 +605,11 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
}
}

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

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

trace!("lower_opaque_impl_trait: {:#?}", opaque_ty_def_id);
let opaque_ty_id =
lctx.generate_opaque_type(opaque_ty_node_id, opaque_ty_item, span, opaque_ty_span);
lctx.generate_opaque_type(opaque_ty_def_id, opaque_ty_item, span, opaque_ty_span);

// `impl Trait` now just becomes `Foo<'a, 'b, ..>`.
hir::TyKind::OpaqueDef(hir::ItemId { id: opaque_ty_id }, lifetimes)
hir::TyKind::OpaqueDef(hir::ItemId { def_id: opaque_ty_def_id }, lifetimes)
})
}

/// Registers a new opaque type with the proper `NodeId`s and
/// returns the lowered node-ID for the opaque type.
fn generate_opaque_type(
&mut self,
opaque_ty_node_id: NodeId,
opaque_ty_id: LocalDefId,
opaque_ty_item: hir::OpaqueTy<'hir>,
span: Span,
opaque_ty_span: Span,
) -> hir::HirId {
) {
let opaque_ty_item_kind = hir::ItemKind::OpaqueTy(opaque_ty_item);
let opaque_ty_id = self.lower_node_id(opaque_ty_node_id);
// Generate an `type Foo = impl Trait;` declaration.
trace!("registering opaque type with id {:#?}", opaque_ty_id);
let opaque_ty_item = hir::Item {
hir_id: opaque_ty_id,
def_id: opaque_ty_id,
ident: Ident::invalid(),
attrs: Default::default(),
kind: opaque_ty_item_kind,
Expand All @@ -1581,7 +1578,6 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
// automatically for all AST items. But this opaque type item
// does not actually exist in the AST.
self.insert_item(opaque_ty_item);
opaque_ty_id
}

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

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

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

(opaque_ty_id, lifetime_params)
lifetime_params
});

// As documented above on the variable
Expand Down Expand Up @@ -2107,7 +2102,8 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
// Foo = impl Trait` is, internally, created as a child of the
// async fn, so the *type parameters* are inherited. It's
// only the lifetime parameters that we must supply.
let opaque_ty_ref = hir::TyKind::OpaqueDef(hir::ItemId { id: opaque_ty_id }, generic_args);
let opaque_ty_ref =
hir::TyKind::OpaqueDef(hir::ItemId { def_id: opaque_ty_def_id }, generic_args);
let opaque_ty = self.ty(opaque_ty_span, opaque_ty_ref);
hir::FnRetTy::Return(self.arena.alloc(opaque_ty))
}
Expand Down Expand Up @@ -2432,7 +2428,10 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
let mut ids: SmallVec<[hir::Stmt<'hir>; 1]> = item_ids
.into_iter()
.map(|item_id| {
let item_id = hir::ItemId { id: self.lower_node_id(item_id) };
let item_id = hir::ItemId {
// All the items that `lower_local` finds are `impl Trait` types.
def_id: self.lower_node_id(item_id).expect_owner(),
};
self.stmt(s.span, hir::StmtKind::Item(item_id))
})
.collect();
Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_codegen_cranelift/src/driver/aot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -164,8 +164,8 @@ fn module_codegen(tcx: TyCtxt<'_>, cgu_name: rustc_span::Symbol) -> ModuleCodege
MonoItem::Static(def_id) => {
crate::constant::codegen_static(&mut cx.constants_cx, def_id)
}
MonoItem::GlobalAsm(hir_id) => {
let item = cx.tcx.hir().expect_item(hir_id);
MonoItem::GlobalAsm(item_id) => {
let item = cx.tcx.hir().item(item_id);
if let rustc_hir::ItemKind::GlobalAsm(rustc_hir::GlobalAsm { asm }) = item.kind {
cx.global_asm.push_str(&*asm.as_str());
cx.global_asm.push_str("\n\n");
Expand Down
7 changes: 3 additions & 4 deletions compiler/rustc_codegen_cranelift/src/driver/jit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,10 +93,9 @@ pub(super) fn run_jit(tcx: TyCtxt<'_>, codegen_mode: CodegenMode) -> ! {
MonoItem::Static(def_id) => {
crate::constant::codegen_static(&mut cx.constants_cx, def_id);
}
MonoItem::GlobalAsm(hir_id) => {
let item = cx.tcx.hir().expect_item(hir_id);
tcx.sess
.span_fatal(item.span, "Global asm is not supported in JIT mode");
MonoItem::GlobalAsm(item_id) => {
let item = cx.tcx.hir().item(item_id);
tcx.sess.span_fatal(item.span, "Global asm is not supported in JIT mode");
}
}
}
Expand Down
Loading

0 comments on commit 8fe989d

Please sign in to comment.