Skip to content

Commit b6b0057

Browse files
committed
librustc_middle: return LocalDefId instead of DefId in get_parent_did
1 parent 555e024 commit b6b0057

File tree

8 files changed

+26
-23
lines changed

8 files changed

+26
-23
lines changed

src/librustc_middle/hir/map/mod.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -720,9 +720,8 @@ impl<'hir> Map<'hir> {
720720
scope
721721
}
722722

723-
// FIXME(eddyb) this function can and should return `LocalDefId`.
724-
pub fn get_parent_did(&self, id: HirId) -> DefId {
725-
self.local_def_id(self.get_parent_item(id))
723+
pub fn get_parent_did(&self, id: HirId) -> LocalDefId {
724+
self.local_def_id(self.get_parent_item(id)).expect_local()
726725
}
727726

728727
pub fn get_foreign_abi(&self, hir_id: HirId) -> Abi {

src/librustc_mir/const_eval/fn_queries.rs

+1-5
Original file line numberDiff line numberDiff line change
@@ -84,11 +84,7 @@ pub fn is_min_const_fn(tcx: TyCtxt<'_>, def_id: DefId) -> bool {
8484

8585
pub fn is_parent_const_impl_raw(tcx: TyCtxt<'_>, hir_id: hir::HirId) -> bool {
8686
let parent_id = tcx.hir().get_parent_did(hir_id);
87-
if !parent_id.is_top_level_module() {
88-
is_const_impl_raw(tcx, parent_id.expect_local())
89-
} else {
90-
false
91-
}
87+
if !parent_id.is_top_level_module() { is_const_impl_raw(tcx, parent_id) } else { false }
9288
}
9389

9490
/// Checks whether the function has a `const` modifier or, in case it is an intrinsic, whether

src/librustc_passes/reachable.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use rustc_data_structures::sync::Lrc;
1010
use rustc_hir as hir;
1111
use rustc_hir::def::{DefKind, Res};
1212
use rustc_hir::def_id::LOCAL_CRATE;
13-
use rustc_hir::def_id::{CrateNum, DefId};
13+
use rustc_hir::def_id::{CrateNum, DefId, LocalDefId};
1414
use rustc_hir::intravisit::{self, NestedVisitorMap, Visitor};
1515
use rustc_hir::itemlikevisit::ItemLikeVisitor;
1616
use rustc_hir::{HirIdSet, Node};
@@ -42,7 +42,7 @@ fn item_might_be_inlined(tcx: TyCtxt<'tcx>, item: &hir::Item<'_>, attrs: Codegen
4242
fn method_might_be_inlined(
4343
tcx: TyCtxt<'_>,
4444
impl_item: &hir::ImplItem<'_>,
45-
impl_src: DefId,
45+
impl_src: LocalDefId,
4646
) -> bool {
4747
let codegen_fn_attrs = tcx.codegen_fn_attrs(impl_item.hir_id.owner.to_def_id());
4848
let generics = tcx.generics_of(tcx.hir().local_def_id(impl_item.hir_id));
@@ -54,7 +54,7 @@ fn method_might_be_inlined(
5454
return true;
5555
}
5656
}
57-
if let Some(impl_hir_id) = tcx.hir().as_local_hir_id(impl_src) {
57+
if let Some(impl_hir_id) = tcx.hir().as_local_hir_id(impl_src.to_def_id()) {
5858
match tcx.hir().find(impl_hir_id) {
5959
Some(Node::Item(item)) => item_might_be_inlined(tcx, &item, codegen_fn_attrs),
6060
Some(..) | None => span_bug!(impl_item.span, "impl did is not an item"),
@@ -171,7 +171,7 @@ impl<'a, 'tcx> ReachableContext<'a, 'tcx> {
171171
if generics.requires_monomorphization(self.tcx) || attrs.requests_inline() {
172172
true
173173
} else {
174-
let impl_did = self.tcx.hir().get_parent_did(hir_id);
174+
let impl_did = self.tcx.hir().get_parent_did(hir_id).to_def_id();
175175
// Check the impl. If the generics on the self
176176
// type of the impl require inlining, this method
177177
// does too.

src/librustc_privacy/lib.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,7 @@ fn def_id_visibility<'tcx>(
248248
}
249249
}
250250
Node::TraitItem(..) | Node::Variant(..) => {
251-
return def_id_visibility(tcx, tcx.hir().get_parent_did(hir_id));
251+
return def_id_visibility(tcx, tcx.hir().get_parent_did(hir_id).to_def_id());
252252
}
253253
Node::ImplItem(impl_item) => {
254254
match tcx.hir().get(tcx.hir().get_parent_item(hir_id)) {
@@ -270,7 +270,7 @@ fn def_id_visibility<'tcx>(
270270
let (mut ctor_vis, mut span, mut descr) =
271271
def_id_visibility(tcx, parent_did);
272272

273-
let adt_def = tcx.adt_def(tcx.hir().get_parent_did(hir_id));
273+
let adt_def = tcx.adt_def(tcx.hir().get_parent_did(hir_id).to_def_id());
274274
let ctor_did = tcx.hir().local_def_id(vdata.ctor_hir_id().unwrap());
275275
let variant = adt_def.variant_with_ctor_id(ctor_did);
276276

@@ -309,7 +309,8 @@ fn def_id_visibility<'tcx>(
309309
// If the structure is marked as non_exhaustive then lower the
310310
// visibility to within the crate.
311311
if ctor_vis == ty::Visibility::Public {
312-
let adt_def = tcx.adt_def(tcx.hir().get_parent_did(hir_id));
312+
let adt_def =
313+
tcx.adt_def(tcx.hir().get_parent_did(hir_id).to_def_id());
313314
if adt_def.non_enum_variant().is_field_list_non_exhaustive() {
314315
ctor_vis =
315316
ty::Visibility::Restricted(DefId::local(CRATE_DEF_INDEX));

src/librustc_span/def_id.rs

+5
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,11 @@ impl LocalDefId {
224224
pub fn to_def_id(self) -> DefId {
225225
DefId { krate: LOCAL_CRATE, index: self.local_def_index }
226226
}
227+
228+
#[inline]
229+
pub fn is_top_level_module(self) -> bool {
230+
self.local_def_index == CRATE_DEF_INDEX
231+
}
227232
}
228233

229234
impl fmt::Debug for LocalDefId {

src/librustc_typeck/astconv.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2371,7 +2371,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
23712371

23722372
let parent_def_id = def_id
23732373
.and_then(|def_id| tcx.hir().as_local_hir_id(def_id))
2374-
.map(|hir_id| tcx.hir().get_parent_did(hir_id));
2374+
.map(|hir_id| tcx.hir().get_parent_did(hir_id).to_def_id());
23752375

23762376
debug!("qpath_to_ty: parent_def_id={:?}", parent_def_id);
23772377

src/librustc_typeck/collect.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1513,7 +1513,7 @@ fn fn_sig(tcx: TyCtxt<'_>, def_id: DefId) -> ty::PolyFnSig<'_> {
15131513
}
15141514

15151515
Ctor(data) | Variant(hir::Variant { data, .. }) if data.ctor_hir_id().is_some() => {
1516-
let ty = tcx.type_of(tcx.hir().get_parent_did(hir_id));
1516+
let ty = tcx.type_of(tcx.hir().get_parent_did(hir_id).to_def_id());
15171517
let inputs =
15181518
data.fields().iter().map(|f| tcx.type_of(tcx.hir().local_def_id(f.hir_id)));
15191519
ty::Binder::bind(tcx.mk_fn_sig(

src/librustc_typeck/collect/type_of.rs

+8-6
Original file line numberDiff line numberDiff line change
@@ -59,14 +59,14 @@ pub(super) fn type_of(tcx: TyCtxt<'_>, def_id: DefId) -> Ty<'_> {
5959
}
6060
}
6161
ImplItemKind::OpaqueTy(_) => {
62-
if tcx.impl_trait_ref(tcx.hir().get_parent_did(hir_id)).is_none() {
62+
if tcx.impl_trait_ref(tcx.hir().get_parent_did(hir_id).to_def_id()).is_none() {
6363
report_assoc_ty_on_inherent_impl(tcx, item.span);
6464
}
6565

6666
find_opaque_ty_constraints(tcx, def_id)
6767
}
6868
ImplItemKind::TyAlias(ref ty) => {
69-
if tcx.impl_trait_ref(tcx.hir().get_parent_did(hir_id)).is_none() {
69+
if tcx.impl_trait_ref(tcx.hir().get_parent_did(hir_id).to_def_id()).is_none() {
7070
report_assoc_ty_on_inherent_impl(tcx, item.span);
7171
}
7272

@@ -177,7 +177,7 @@ pub(super) fn type_of(tcx: TyCtxt<'_>, def_id: DefId) -> Ty<'_> {
177177

178178
Node::Ctor(&ref def) | Node::Variant(Variant { data: ref def, .. }) => match *def {
179179
VariantData::Unit(..) | VariantData::Struct(..) => {
180-
tcx.type_of(tcx.hir().get_parent_did(hir_id))
180+
tcx.type_of(tcx.hir().get_parent_did(hir_id).to_def_id())
181181
}
182182
VariantData::Tuple(..) => {
183183
let substs = InternalSubsts::identity_for_item(tcx, def_id);
@@ -207,9 +207,11 @@ pub(super) fn type_of(tcx: TyCtxt<'_>, def_id: DefId) -> Ty<'_> {
207207
tcx.types.usize
208208
}
209209

210-
Node::Variant(Variant { disr_expr: Some(ref e), .. }) if e.hir_id == hir_id => {
211-
tcx.adt_def(tcx.hir().get_parent_did(hir_id)).repr.discr_type().to_ty(tcx)
212-
}
210+
Node::Variant(Variant { disr_expr: Some(ref e), .. }) if e.hir_id == hir_id => tcx
211+
.adt_def(tcx.hir().get_parent_did(hir_id).to_def_id())
212+
.repr
213+
.discr_type()
214+
.to_ty(tcx),
213215

214216
Node::Ty(&Ty { kind: TyKind::Path(_), .. })
215217
| Node::Expr(&Expr { kind: ExprKind::Struct(..), .. })

0 commit comments

Comments
 (0)