Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

librustc_hir: return LocalDefId instead of DefId in local_def_id #70909

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions src/librustc_ast_lowering/item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
hir::ItemKind::Const(ty, body_id)
}
ItemKind::Fn(_, FnSig { ref decl, header }, ref generics, ref body) => {
let fn_def_id = self.resolver.definitions().local_def_id(id).expect_local();
let fn_def_id = self.resolver.definitions().local_def_id(id);
self.with_new_scopes(|this| {
this.current_item = Some(ident.span);

Expand Down Expand Up @@ -346,7 +346,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
self_ty: ref ty,
items: ref impl_items,
} => {
let def_id = self.resolver.definitions().local_def_id(id).expect_local();
let def_id = self.resolver.definitions().local_def_id(id);

// Lower the "impl header" first. This ordering is important
// for in-band lifetimes! Consider `'a` here:
Expand Down Expand Up @@ -645,7 +645,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
}

fn lower_foreign_item(&mut self, i: &ForeignItem) -> hir::ForeignItem<'hir> {
let def_id = self.resolver.definitions().local_def_id(i.id).expect_local();
let def_id = self.resolver.definitions().local_def_id(i.id);
hir::ForeignItem {
hir_id: self.lower_node_id(i.id),
ident: i.ident,
Expand Down Expand Up @@ -746,7 +746,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
}

fn lower_trait_item(&mut self, i: &AssocItem) -> hir::TraitItem<'hir> {
let trait_item_def_id = self.resolver.definitions().local_def_id(i.id).expect_local();
let trait_item_def_id = self.resolver.definitions().local_def_id(i.id);

let (generics, kind) = match i.kind {
AssocItemKind::Const(_, ref ty, ref default) => {
Expand Down Expand Up @@ -811,7 +811,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
}

fn lower_impl_item(&mut self, i: &AssocItem) -> hir::ImplItem<'hir> {
let impl_item_def_id = self.resolver.definitions().local_def_id(i.id).expect_local();
let impl_item_def_id = self.resolver.definitions().local_def_id(i.id);

let (generics, kind) = match &i.kind {
AssocItemKind::Const(_, ty, expr) => {
Expand Down
14 changes: 5 additions & 9 deletions src/librustc_ast_lowering/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -464,8 +464,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
| ItemKind::Enum(_, ref generics)
| ItemKind::TyAlias(_, ref generics, ..)
| ItemKind::Trait(_, _, ref generics, ..) => {
let def_id =
self.lctx.resolver.definitions().local_def_id(item.id).expect_local();
let def_id = self.lctx.resolver.definitions().local_def_id(item.id);
let count = generics
.params
.iter()
Expand Down Expand Up @@ -600,7 +599,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
.item_local_id_counters
.insert(owner, HIR_ID_COUNTER_LOCKED)
.unwrap_or_else(|| panic!("no `item_local_id_counters` entry for {:?}", owner));
let def_id = self.resolver.definitions().local_def_id(owner).expect_local();
let def_id = self.resolver.definitions().local_def_id(owner);
self.current_hir_id_owner.push((def_id, counter));
let ret = f(self);
let (new_def_id, new_counter) = self.current_hir_id_owner.pop().unwrap();
Expand Down Expand Up @@ -1280,8 +1279,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
}
ImplTraitContext::Universal(in_band_ty_params) => {
// Add a definition for the in-band `Param`.
let def_id =
self.resolver.definitions().local_def_id(def_node_id).expect_local();
let def_id = self.resolver.definitions().local_def_id(def_node_id);

let hir_bounds = self.lower_param_bounds(
bounds,
Expand Down Expand Up @@ -1369,8 +1367,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
// frequently opened issues show.
let opaque_ty_span = self.mark_span_with_reason(DesugaringKind::OpaqueTy, span, None);

let opaque_ty_def_id =
self.resolver.definitions().local_def_id(opaque_ty_node_id).expect_local();
let opaque_ty_def_id = self.resolver.definitions().local_def_id(opaque_ty_node_id);

self.allocate_hir_id_counter(opaque_ty_node_id);

Expand Down Expand Up @@ -1799,8 +1796,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {

let opaque_ty_span = self.mark_span_with_reason(DesugaringKind::Async, span, None);

let opaque_ty_def_id =
self.resolver.definitions().local_def_id(opaque_ty_node_id).expect_local();
let opaque_ty_def_id = self.resolver.definitions().local_def_id(opaque_ty_node_id);

self.allocate_hir_id_counter(opaque_ty_node_id);

Expand Down
5 changes: 2 additions & 3 deletions src/librustc_hir/definitions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -326,10 +326,9 @@ impl Definitions {
self.node_id_to_def_id.get(&node).copied()
}

// FIXME(eddyb) this function can and should return `LocalDefId`.
#[inline]
pub fn local_def_id(&self, node: ast::NodeId) -> DefId {
self.opt_local_def_id(node).unwrap().to_def_id()
pub fn local_def_id(&self, node: ast::NodeId) -> LocalDefId {
self.opt_local_def_id(node).unwrap()
}

#[inline]
Expand Down
6 changes: 3 additions & 3 deletions src/librustc_middle/ty/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1162,17 +1162,17 @@ impl<'tcx> TyCtxt<'tcx> {
maybe_unused_trait_imports: resolutions
.maybe_unused_trait_imports
.into_iter()
.map(|id| definitions.local_def_id(id))
.map(|id| definitions.local_def_id(id).to_def_id())
.collect(),
maybe_unused_extern_crates: resolutions
.maybe_unused_extern_crates
.into_iter()
.map(|(id, sp)| (definitions.local_def_id(id), sp))
.map(|(id, sp)| (definitions.local_def_id(id).to_def_id(), sp))
.collect(),
glob_map: resolutions
.glob_map
.into_iter()
.map(|(id, names)| (definitions.local_def_id(id), names))
.map(|(id, names)| (definitions.local_def_id(id).to_def_id(), names))
.collect(),
Comment on lines 1162 to 1176
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These maps likely should be switched to LocalDefId.

extern_prelude: resolutions.extern_prelude,
untracked_crate: krate,
Expand Down
67 changes: 41 additions & 26 deletions src/librustc_resolve/build_reduced_graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ impl<'a> Resolver<'a> {

crate fn get_module(&mut self, def_id: DefId) -> Module<'a> {
if def_id.krate == LOCAL_CRATE {
return self.module_map[&def_id];
return self.module_map[&def_id.as_local().unwrap()];
eddyb marked this conversation as resolved.
Show resolved Hide resolved
}

if let Some(&module) = self.extern_module_map.get(&def_id) {
Expand Down Expand Up @@ -675,12 +675,18 @@ impl<'a, 'b> BuildReducedGraphVisitor<'a, 'b> {

ItemKind::Mod(..) => {
let def_id = self.r.definitions.local_def_id(item.id);
let module_kind = ModuleKind::Def(DefKind::Mod, def_id, ident.name);
let module_kind = ModuleKind::Def(DefKind::Mod, def_id.to_def_id(), ident.name);
let module = self.r.arenas.alloc_module(ModuleData {
no_implicit_prelude: parent.no_implicit_prelude || {
attr::contains_name(&item.attrs, sym::no_implicit_prelude)
},
..ModuleData::new(Some(parent), module_kind, def_id, expansion, item.span)
..ModuleData::new(
Some(parent),
module_kind,
def_id.to_def_id(),
expansion,
item.span,
)
});
self.r.define(parent, ident, TypeNS, (module, vis, sp, expansion));
self.r.module_map.insert(def_id, module);
Expand All @@ -691,15 +697,18 @@ impl<'a, 'b> BuildReducedGraphVisitor<'a, 'b> {

// These items live in the value namespace.
ItemKind::Static(..) => {
let res = Res::Def(DefKind::Static, self.r.definitions.local_def_id(item.id));
let res =
Res::Def(DefKind::Static, self.r.definitions.local_def_id(item.id).to_def_id());
self.r.define(parent, ident, ValueNS, (res, vis, sp, expansion));
}
ItemKind::Const(..) => {
let res = Res::Def(DefKind::Const, self.r.definitions.local_def_id(item.id));
let res =
Res::Def(DefKind::Const, self.r.definitions.local_def_id(item.id).to_def_id());
self.r.define(parent, ident, ValueNS, (res, vis, sp, expansion));
}
ItemKind::Fn(..) => {
let res = Res::Def(DefKind::Fn, self.r.definitions.local_def_id(item.id));
let res =
Res::Def(DefKind::Fn, self.r.definitions.local_def_id(item.id).to_def_id());
self.r.define(parent, ident, ValueNS, (res, vis, sp, expansion));

// Functions introducing procedural macros reserve a slot
Expand All @@ -713,12 +722,12 @@ impl<'a, 'b> BuildReducedGraphVisitor<'a, 'b> {
None => DefKind::TyAlias,
Some(_) => DefKind::OpaqueTy,
};
let res = Res::Def(def_kind, self.r.definitions.local_def_id(item.id));
let res = Res::Def(def_kind, self.r.definitions.local_def_id(item.id).to_def_id());
self.r.define(parent, ident, TypeNS, (res, vis, sp, expansion));
}

ItemKind::Enum(_, _) => {
let def_id = self.r.definitions.local_def_id(item.id);
let def_id = self.r.definitions.local_def_id(item.id).to_def_id();
self.r.variant_vis.insert(def_id, vis);
let module_kind = ModuleKind::Def(DefKind::Enum, def_id, ident.name);
let module = self.r.new_module(
Expand All @@ -733,14 +742,17 @@ impl<'a, 'b> BuildReducedGraphVisitor<'a, 'b> {
}

ItemKind::TraitAlias(..) => {
let res = Res::Def(DefKind::TraitAlias, self.r.definitions.local_def_id(item.id));
let res = Res::Def(
DefKind::TraitAlias,
self.r.definitions.local_def_id(item.id).to_def_id(),
);
self.r.define(parent, ident, TypeNS, (res, vis, sp, expansion));
}

// These items live in both the type and value namespaces.
ItemKind::Struct(ref vdata, _) => {
// Define a name in the type namespace.
let def_id = self.r.definitions.local_def_id(item.id);
let def_id = self.r.definitions.local_def_id(item.id).to_def_id();
let res = Res::Def(DefKind::Struct, def_id);
self.r.define(parent, ident, TypeNS, (res, vis, sp, expansion));

Expand Down Expand Up @@ -773,15 +785,15 @@ impl<'a, 'b> BuildReducedGraphVisitor<'a, 'b> {
}
let ctor_res = Res::Def(
DefKind::Ctor(CtorOf::Struct, CtorKind::from_ast(vdata)),
self.r.definitions.local_def_id(ctor_node_id),
self.r.definitions.local_def_id(ctor_node_id).to_def_id(),
);
self.r.define(parent, ident, ValueNS, (ctor_res, ctor_vis, sp, expansion));
self.r.struct_constructors.insert(def_id, (ctor_res, ctor_vis));
}
}

ItemKind::Union(ref vdata, _) => {
let def_id = self.r.definitions.local_def_id(item.id);
let def_id = self.r.definitions.local_def_id(item.id).to_def_id();
let res = Res::Def(DefKind::Union, def_id);
self.r.define(parent, ident, TypeNS, (res, vis, sp, expansion));

Expand All @@ -790,7 +802,7 @@ impl<'a, 'b> BuildReducedGraphVisitor<'a, 'b> {
}

ItemKind::Trait(..) => {
let def_id = self.r.definitions.local_def_id(item.id);
let def_id = self.r.definitions.local_def_id(item.id).to_def_id();

// Add all the items within to a new module.
let module_kind = ModuleKind::Def(DefKind::Trait, def_id, ident.name);
Expand All @@ -815,15 +827,18 @@ impl<'a, 'b> BuildReducedGraphVisitor<'a, 'b> {
/// Constructs the reduced graph for one foreign item.
fn build_reduced_graph_for_foreign_item(&mut self, item: &ForeignItem) {
let (res, ns) = match item.kind {
ForeignItemKind::Fn(..) => {
(Res::Def(DefKind::Fn, self.r.definitions.local_def_id(item.id)), ValueNS)
}
ForeignItemKind::Static(..) => {
(Res::Def(DefKind::Static, self.r.definitions.local_def_id(item.id)), ValueNS)
}
ForeignItemKind::TyAlias(..) => {
(Res::Def(DefKind::ForeignTy, self.r.definitions.local_def_id(item.id)), TypeNS)
}
ForeignItemKind::Fn(..) => (
Res::Def(DefKind::Fn, self.r.definitions.local_def_id(item.id).to_def_id()),
ValueNS,
),
ForeignItemKind::Static(..) => (
Res::Def(DefKind::Static, self.r.definitions.local_def_id(item.id).to_def_id()),
ValueNS,
),
ForeignItemKind::TyAlias(..) => (
Res::Def(DefKind::ForeignTy, self.r.definitions.local_def_id(item.id).to_def_id()),
TypeNS,
),
ForeignItemKind::MacCall(_) => unreachable!(),
};
let parent = self.parent_scope.module;
Expand Down Expand Up @@ -1121,7 +1136,7 @@ impl<'a, 'b> BuildReducedGraphVisitor<'a, 'b> {
_ => unreachable!(),
};

let def_id = self.r.definitions.local_def_id(item.id);
let def_id = self.r.definitions.local_def_id(item.id).to_def_id();
let res = Res::Def(DefKind::Macro(ext.macro_kind()), def_id);
self.r.macro_map.insert(def_id, ext);
self.r.local_macro_def_scopes.insert(item.id, parent_scope.module);
Expand Down Expand Up @@ -1251,7 +1266,7 @@ impl<'a, 'b> Visitor<'b> for BuildReducedGraphVisitor<'a, 'b> {
}

// Add the item to the trait info.
let item_def_id = self.r.definitions.local_def_id(item.id);
let item_def_id = self.r.definitions.local_def_id(item.id).to_def_id();
let (res, ns) = match item.kind {
AssocItemKind::Const(..) => (Res::Def(DefKind::AssocConst, item_def_id), ValueNS),
AssocItemKind::Fn(_, ref sig, _, _) => {
Expand Down Expand Up @@ -1353,7 +1368,7 @@ impl<'a, 'b> Visitor<'b> for BuildReducedGraphVisitor<'a, 'b> {
let ident = variant.ident;

// Define a name in the type namespace.
let def_id = self.r.definitions.local_def_id(variant.id);
let def_id = self.r.definitions.local_def_id(variant.id).to_def_id();
let res = Res::Def(DefKind::Variant, def_id);
self.r.define(parent, ident, TypeNS, (res, vis, variant.span, expn_id));

Expand All @@ -1371,7 +1386,7 @@ impl<'a, 'b> Visitor<'b> for BuildReducedGraphVisitor<'a, 'b> {
// It's ok to use the variant's id as a ctor id since an
// error will be reported on any use of such resolution anyway.
let ctor_node_id = variant.data.ctor_id().unwrap_or(variant.id);
let ctor_def_id = self.r.definitions.local_def_id(ctor_node_id);
let ctor_def_id = self.r.definitions.local_def_id(ctor_node_id).to_def_id();
let ctor_kind = CtorKind::from_ast(&variant.data);
let ctor_res = Res::Def(DefKind::Ctor(CtorOf::Variant, ctor_kind), ctor_def_id);
self.r.define(parent, ident, ValueNS, (ctor_res, ctor_vis, variant.span, expn_id));
Expand Down
12 changes: 6 additions & 6 deletions src/librustc_resolve/late.rs
Original file line number Diff line number Diff line change
Expand Up @@ -745,7 +745,7 @@ impl<'a, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> {
debug!("resolve_adt");
self.with_current_self_item(item, |this| {
this.with_generic_param_rib(generics, ItemRibKind(HasGenericParams::Yes), |this| {
let item_def_id = this.r.definitions.local_def_id(item.id);
let item_def_id = this.r.definitions.local_def_id(item.id).to_def_id();
this.with_self_rib(Res::SelfTy(None, Some(item_def_id)), |this| {
visit::walk_item(this, item);
});
Expand Down Expand Up @@ -825,7 +825,7 @@ impl<'a, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> {
ItemKind::Trait(.., ref generics, ref bounds, ref trait_items) => {
// Create a new rib for the trait-wide type parameters.
self.with_generic_param_rib(generics, ItemRibKind(HasGenericParams::Yes), |this| {
let local_def_id = this.r.definitions.local_def_id(item.id);
let local_def_id = this.r.definitions.local_def_id(item.id).to_def_id();
this.with_self_rib(Res::SelfTy(Some(local_def_id), None), |this| {
this.visit_generics(generics);
walk_list!(this, visit_param_bound, bounds);
Expand Down Expand Up @@ -866,7 +866,7 @@ impl<'a, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> {
ItemKind::TraitAlias(ref generics, ref bounds) => {
// Create a new rib for the trait-wide type parameters.
self.with_generic_param_rib(generics, ItemRibKind(HasGenericParams::Yes), |this| {
let local_def_id = this.r.definitions.local_def_id(item.id);
let local_def_id = this.r.definitions.local_def_id(item.id).to_def_id();
this.with_self_rib(Res::SelfTy(Some(local_def_id), None), |this| {
this.visit_generics(generics);
walk_list!(this, visit_param_bound, bounds);
Expand Down Expand Up @@ -947,7 +947,7 @@ impl<'a, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> {
seen_bindings.entry(ident).or_insert(param.ident.span);

// Plain insert (no renaming).
let res = Res::Def(def_kind, self.r.definitions.local_def_id(param.id));
let res = Res::Def(def_kind, self.r.definitions.local_def_id(param.id).to_def_id());

match param.kind {
GenericParamKind::Type { .. } => {
Expand Down Expand Up @@ -1097,7 +1097,7 @@ impl<'a, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> {
this.with_self_rib(Res::SelfTy(None, None), |this| {
// Resolve the trait reference, if necessary.
this.with_optional_trait_ref(opt_trait_reference.as_ref(), |this, trait_id| {
let item_def_id = this.r.definitions.local_def_id(item_id);
let item_def_id = this.r.definitions.local_def_id(item_id).to_def_id();
this.with_self_rib(Res::SelfTy(trait_id, Some(item_def_id)), |this| {
if let Some(trait_ref) = opt_trait_reference.as_ref() {
// Resolve type arguments in the trait path.
Expand Down Expand Up @@ -1906,7 +1906,7 @@ impl<'a, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> {
if let StmtKind::Item(ref item) = stmt.kind {
if let ItemKind::MacroDef(..) = item.kind {
num_macro_definition_ribs += 1;
let res = self.r.definitions.local_def_id(item.id);
let res = self.r.definitions.local_def_id(item.id).to_def_id();
self.ribs[ValueNS].push(Rib::new(MacroDefinition(res)));
self.label_ribs.push(Rib::new(MacroDefinition(res)));
}
Expand Down
Loading