Skip to content

Commit

Permalink
Auto merge of rust-lang#70909 - marmeladema:issue70853/librustc_hir-l…
Browse files Browse the repository at this point in the history
…ocal-def-id, r=eddyb

librustc_hir: return LocalDefId instead of DefId in local_def_id

Its a first try to remove a few calls to `expect_local` and use `LocalDefId` instead of `DefId` where possible for rust-lang#70853

This adds some calls to `.to_def_id()` to get a `DefId` back when needed. I don't know if I should push `LocalDefId` even further and change, for example, `Res::Def` to accept a `LocalDefId` instead of a `DefId` as second argument.

cc @ecstatic-morse
  • Loading branch information
bors committed Apr 9, 2020
2 parents 94d3463 + 6ae3888 commit 0c835b0
Show file tree
Hide file tree
Showing 8 changed files with 67 additions and 57 deletions.
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(),
extern_prelude: resolutions.extern_prelude,
untracked_crate: krate,
Expand Down
69 changes: 42 additions & 27 deletions src/librustc_resolve/build_reduced_graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ use rustc_errors::{struct_span_err, Applicability};
use rustc_expand::base::SyntaxExtension;
use rustc_expand::expand::AstFragment;
use rustc_hir::def::{self, *};
use rustc_hir::def_id::{DefId, CRATE_DEF_INDEX, LOCAL_CRATE};
use rustc_hir::def_id::{DefId, CRATE_DEF_INDEX};
use rustc_metadata::creader::LoadedMacro;
use rustc_middle::bug;
use rustc_middle::hir::exports::Export;
Expand Down Expand Up @@ -96,7 +96,7 @@ impl<'a> Resolver<'a> {
}

crate fn get_module(&mut self, def_id: DefId) -> Module<'a> {
if def_id.krate == LOCAL_CRATE {
if let Some(def_id) = def_id.as_local() {
return self.module_map[&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

0 comments on commit 0c835b0

Please sign in to comment.