Skip to content

Commit 0c835b0

Browse files
committed
Auto merge of #70909 - marmeladema:issue70853/librustc_hir-local-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 #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
2 parents 94d3463 + 6ae3888 commit 0c835b0

File tree

8 files changed

+67
-57
lines changed

8 files changed

+67
-57
lines changed

src/librustc_ast_lowering/item.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
259259
hir::ItemKind::Const(ty, body_id)
260260
}
261261
ItemKind::Fn(_, FnSig { ref decl, header }, ref generics, ref body) => {
262-
let fn_def_id = self.resolver.definitions().local_def_id(id).expect_local();
262+
let fn_def_id = self.resolver.definitions().local_def_id(id);
263263
self.with_new_scopes(|this| {
264264
this.current_item = Some(ident.span);
265265

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

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

647647
fn lower_foreign_item(&mut self, i: &ForeignItem) -> hir::ForeignItem<'hir> {
648-
let def_id = self.resolver.definitions().local_def_id(i.id).expect_local();
648+
let def_id = self.resolver.definitions().local_def_id(i.id);
649649
hir::ForeignItem {
650650
hir_id: self.lower_node_id(i.id),
651651
ident: i.ident,
@@ -746,7 +746,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
746746
}
747747

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

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

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

816816
let (generics, kind) = match &i.kind {
817817
AssocItemKind::Const(_, ty, expr) => {

src/librustc_ast_lowering/lib.rs

+5-9
Original file line numberDiff line numberDiff line change
@@ -464,8 +464,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
464464
| ItemKind::Enum(_, ref generics)
465465
| ItemKind::TyAlias(_, ref generics, ..)
466466
| ItemKind::Trait(_, _, ref generics, ..) => {
467-
let def_id =
468-
self.lctx.resolver.definitions().local_def_id(item.id).expect_local();
467+
let def_id = self.lctx.resolver.definitions().local_def_id(item.id);
469468
let count = generics
470469
.params
471470
.iter()
@@ -600,7 +599,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
600599
.item_local_id_counters
601600
.insert(owner, HIR_ID_COUNTER_LOCKED)
602601
.unwrap_or_else(|| panic!("no `item_local_id_counters` entry for {:?}", owner));
603-
let def_id = self.resolver.definitions().local_def_id(owner).expect_local();
602+
let def_id = self.resolver.definitions().local_def_id(owner);
604603
self.current_hir_id_owner.push((def_id, counter));
605604
let ret = f(self);
606605
let (new_def_id, new_counter) = self.current_hir_id_owner.pop().unwrap();
@@ -1280,8 +1279,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
12801279
}
12811280
ImplTraitContext::Universal(in_band_ty_params) => {
12821281
// Add a definition for the in-band `Param`.
1283-
let def_id =
1284-
self.resolver.definitions().local_def_id(def_node_id).expect_local();
1282+
let def_id = self.resolver.definitions().local_def_id(def_node_id);
12851283

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

1372-
let opaque_ty_def_id =
1373-
self.resolver.definitions().local_def_id(opaque_ty_node_id).expect_local();
1370+
let opaque_ty_def_id = self.resolver.definitions().local_def_id(opaque_ty_node_id);
13741371

13751372
self.allocate_hir_id_counter(opaque_ty_node_id);
13761373

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

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

1802-
let opaque_ty_def_id =
1803-
self.resolver.definitions().local_def_id(opaque_ty_node_id).expect_local();
1799+
let opaque_ty_def_id = self.resolver.definitions().local_def_id(opaque_ty_node_id);
18041800

18051801
self.allocate_hir_id_counter(opaque_ty_node_id);
18061802

src/librustc_hir/definitions.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -326,10 +326,9 @@ impl Definitions {
326326
self.node_id_to_def_id.get(&node).copied()
327327
}
328328

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

335334
#[inline]

src/librustc_middle/ty/context.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -1162,17 +1162,17 @@ impl<'tcx> TyCtxt<'tcx> {
11621162
maybe_unused_trait_imports: resolutions
11631163
.maybe_unused_trait_imports
11641164
.into_iter()
1165-
.map(|id| definitions.local_def_id(id))
1165+
.map(|id| definitions.local_def_id(id).to_def_id())
11661166
.collect(),
11671167
maybe_unused_extern_crates: resolutions
11681168
.maybe_unused_extern_crates
11691169
.into_iter()
1170-
.map(|(id, sp)| (definitions.local_def_id(id), sp))
1170+
.map(|(id, sp)| (definitions.local_def_id(id).to_def_id(), sp))
11711171
.collect(),
11721172
glob_map: resolutions
11731173
.glob_map
11741174
.into_iter()
1175-
.map(|(id, names)| (definitions.local_def_id(id), names))
1175+
.map(|(id, names)| (definitions.local_def_id(id).to_def_id(), names))
11761176
.collect(),
11771177
extern_prelude: resolutions.extern_prelude,
11781178
untracked_crate: krate,

src/librustc_resolve/build_reduced_graph.rs

+42-27
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ use rustc_errors::{struct_span_err, Applicability};
2626
use rustc_expand::base::SyntaxExtension;
2727
use rustc_expand::expand::AstFragment;
2828
use rustc_hir::def::{self, *};
29-
use rustc_hir::def_id::{DefId, CRATE_DEF_INDEX, LOCAL_CRATE};
29+
use rustc_hir::def_id::{DefId, CRATE_DEF_INDEX};
3030
use rustc_metadata::creader::LoadedMacro;
3131
use rustc_middle::bug;
3232
use rustc_middle::hir::exports::Export;
@@ -96,7 +96,7 @@ impl<'a> Resolver<'a> {
9696
}
9797

9898
crate fn get_module(&mut self, def_id: DefId) -> Module<'a> {
99-
if def_id.krate == LOCAL_CRATE {
99+
if let Some(def_id) = def_id.as_local() {
100100
return self.module_map[&def_id];
101101
}
102102

@@ -675,12 +675,18 @@ impl<'a, 'b> BuildReducedGraphVisitor<'a, 'b> {
675675

676676
ItemKind::Mod(..) => {
677677
let def_id = self.r.definitions.local_def_id(item.id);
678-
let module_kind = ModuleKind::Def(DefKind::Mod, def_id, ident.name);
678+
let module_kind = ModuleKind::Def(DefKind::Mod, def_id.to_def_id(), ident.name);
679679
let module = self.r.arenas.alloc_module(ModuleData {
680680
no_implicit_prelude: parent.no_implicit_prelude || {
681681
attr::contains_name(&item.attrs, sym::no_implicit_prelude)
682682
},
683-
..ModuleData::new(Some(parent), module_kind, def_id, expansion, item.span)
683+
..ModuleData::new(
684+
Some(parent),
685+
module_kind,
686+
def_id.to_def_id(),
687+
expansion,
688+
item.span,
689+
)
684690
});
685691
self.r.define(parent, ident, TypeNS, (module, vis, sp, expansion));
686692
self.r.module_map.insert(def_id, module);
@@ -691,15 +697,18 @@ impl<'a, 'b> BuildReducedGraphVisitor<'a, 'b> {
691697

692698
// These items live in the value namespace.
693699
ItemKind::Static(..) => {
694-
let res = Res::Def(DefKind::Static, self.r.definitions.local_def_id(item.id));
700+
let res =
701+
Res::Def(DefKind::Static, self.r.definitions.local_def_id(item.id).to_def_id());
695702
self.r.define(parent, ident, ValueNS, (res, vis, sp, expansion));
696703
}
697704
ItemKind::Const(..) => {
698-
let res = Res::Def(DefKind::Const, self.r.definitions.local_def_id(item.id));
705+
let res =
706+
Res::Def(DefKind::Const, self.r.definitions.local_def_id(item.id).to_def_id());
699707
self.r.define(parent, ident, ValueNS, (res, vis, sp, expansion));
700708
}
701709
ItemKind::Fn(..) => {
702-
let res = Res::Def(DefKind::Fn, self.r.definitions.local_def_id(item.id));
710+
let res =
711+
Res::Def(DefKind::Fn, self.r.definitions.local_def_id(item.id).to_def_id());
703712
self.r.define(parent, ident, ValueNS, (res, vis, sp, expansion));
704713

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

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

735744
ItemKind::TraitAlias(..) => {
736-
let res = Res::Def(DefKind::TraitAlias, self.r.definitions.local_def_id(item.id));
745+
let res = Res::Def(
746+
DefKind::TraitAlias,
747+
self.r.definitions.local_def_id(item.id).to_def_id(),
748+
);
737749
self.r.define(parent, ident, TypeNS, (res, vis, sp, expansion));
738750
}
739751

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

@@ -773,15 +785,15 @@ impl<'a, 'b> BuildReducedGraphVisitor<'a, 'b> {
773785
}
774786
let ctor_res = Res::Def(
775787
DefKind::Ctor(CtorOf::Struct, CtorKind::from_ast(vdata)),
776-
self.r.definitions.local_def_id(ctor_node_id),
788+
self.r.definitions.local_def_id(ctor_node_id).to_def_id(),
777789
);
778790
self.r.define(parent, ident, ValueNS, (ctor_res, ctor_vis, sp, expansion));
779791
self.r.struct_constructors.insert(def_id, (ctor_res, ctor_vis));
780792
}
781793
}
782794

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

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

792804
ItemKind::Trait(..) => {
793-
let def_id = self.r.definitions.local_def_id(item.id);
805+
let def_id = self.r.definitions.local_def_id(item.id).to_def_id();
794806

795807
// Add all the items within to a new module.
796808
let module_kind = ModuleKind::Def(DefKind::Trait, def_id, ident.name);
@@ -815,15 +827,18 @@ impl<'a, 'b> BuildReducedGraphVisitor<'a, 'b> {
815827
/// Constructs the reduced graph for one foreign item.
816828
fn build_reduced_graph_for_foreign_item(&mut self, item: &ForeignItem) {
817829
let (res, ns) = match item.kind {
818-
ForeignItemKind::Fn(..) => {
819-
(Res::Def(DefKind::Fn, self.r.definitions.local_def_id(item.id)), ValueNS)
820-
}
821-
ForeignItemKind::Static(..) => {
822-
(Res::Def(DefKind::Static, self.r.definitions.local_def_id(item.id)), ValueNS)
823-
}
824-
ForeignItemKind::TyAlias(..) => {
825-
(Res::Def(DefKind::ForeignTy, self.r.definitions.local_def_id(item.id)), TypeNS)
826-
}
830+
ForeignItemKind::Fn(..) => (
831+
Res::Def(DefKind::Fn, self.r.definitions.local_def_id(item.id).to_def_id()),
832+
ValueNS,
833+
),
834+
ForeignItemKind::Static(..) => (
835+
Res::Def(DefKind::Static, self.r.definitions.local_def_id(item.id).to_def_id()),
836+
ValueNS,
837+
),
838+
ForeignItemKind::TyAlias(..) => (
839+
Res::Def(DefKind::ForeignTy, self.r.definitions.local_def_id(item.id).to_def_id()),
840+
TypeNS,
841+
),
827842
ForeignItemKind::MacCall(_) => unreachable!(),
828843
};
829844
let parent = self.parent_scope.module;
@@ -1121,7 +1136,7 @@ impl<'a, 'b> BuildReducedGraphVisitor<'a, 'b> {
11211136
_ => unreachable!(),
11221137
};
11231138

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

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

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

@@ -1371,7 +1386,7 @@ impl<'a, 'b> Visitor<'b> for BuildReducedGraphVisitor<'a, 'b> {
13711386
// It's ok to use the variant's id as a ctor id since an
13721387
// error will be reported on any use of such resolution anyway.
13731388
let ctor_node_id = variant.data.ctor_id().unwrap_or(variant.id);
1374-
let ctor_def_id = self.r.definitions.local_def_id(ctor_node_id);
1389+
let ctor_def_id = self.r.definitions.local_def_id(ctor_node_id).to_def_id();
13751390
let ctor_kind = CtorKind::from_ast(&variant.data);
13761391
let ctor_res = Res::Def(DefKind::Ctor(CtorOf::Variant, ctor_kind), ctor_def_id);
13771392
self.r.define(parent, ident, ValueNS, (ctor_res, ctor_vis, variant.span, expn_id));

src/librustc_resolve/late.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -745,7 +745,7 @@ impl<'a, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> {
745745
debug!("resolve_adt");
746746
self.with_current_self_item(item, |this| {
747747
this.with_generic_param_rib(generics, ItemRibKind(HasGenericParams::Yes), |this| {
748-
let item_def_id = this.r.definitions.local_def_id(item.id);
748+
let item_def_id = this.r.definitions.local_def_id(item.id).to_def_id();
749749
this.with_self_rib(Res::SelfTy(None, Some(item_def_id)), |this| {
750750
visit::walk_item(this, item);
751751
});
@@ -825,7 +825,7 @@ impl<'a, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> {
825825
ItemKind::Trait(.., ref generics, ref bounds, ref trait_items) => {
826826
// Create a new rib for the trait-wide type parameters.
827827
self.with_generic_param_rib(generics, ItemRibKind(HasGenericParams::Yes), |this| {
828-
let local_def_id = this.r.definitions.local_def_id(item.id);
828+
let local_def_id = this.r.definitions.local_def_id(item.id).to_def_id();
829829
this.with_self_rib(Res::SelfTy(Some(local_def_id), None), |this| {
830830
this.visit_generics(generics);
831831
walk_list!(this, visit_param_bound, bounds);
@@ -866,7 +866,7 @@ impl<'a, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> {
866866
ItemKind::TraitAlias(ref generics, ref bounds) => {
867867
// Create a new rib for the trait-wide type parameters.
868868
self.with_generic_param_rib(generics, ItemRibKind(HasGenericParams::Yes), |this| {
869-
let local_def_id = this.r.definitions.local_def_id(item.id);
869+
let local_def_id = this.r.definitions.local_def_id(item.id).to_def_id();
870870
this.with_self_rib(Res::SelfTy(Some(local_def_id), None), |this| {
871871
this.visit_generics(generics);
872872
walk_list!(this, visit_param_bound, bounds);
@@ -947,7 +947,7 @@ impl<'a, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> {
947947
seen_bindings.entry(ident).or_insert(param.ident.span);
948948

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

952952
match param.kind {
953953
GenericParamKind::Type { .. } => {
@@ -1097,7 +1097,7 @@ impl<'a, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> {
10971097
this.with_self_rib(Res::SelfTy(None, None), |this| {
10981098
// Resolve the trait reference, if necessary.
10991099
this.with_optional_trait_ref(opt_trait_reference.as_ref(), |this, trait_id| {
1100-
let item_def_id = this.r.definitions.local_def_id(item_id);
1100+
let item_def_id = this.r.definitions.local_def_id(item_id).to_def_id();
11011101
this.with_self_rib(Res::SelfTy(trait_id, Some(item_def_id)), |this| {
11021102
if let Some(trait_ref) = opt_trait_reference.as_ref() {
11031103
// Resolve type arguments in the trait path.
@@ -1906,7 +1906,7 @@ impl<'a, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> {
19061906
if let StmtKind::Item(ref item) = stmt.kind {
19071907
if let ItemKind::MacroDef(..) = item.kind {
19081908
num_macro_definition_ribs += 1;
1909-
let res = self.r.definitions.local_def_id(item.id);
1909+
let res = self.r.definitions.local_def_id(item.id).to_def_id();
19101910
self.ribs[ValueNS].push(Rib::new(MacroDefinition(res)));
19111911
self.label_ribs.push(Rib::new(MacroDefinition(res)));
19121912
}

0 commit comments

Comments
 (0)