From b6b00578dbd6ef6724b104160744339e9736fb1e Mon Sep 17 00:00:00 2001 From: marmeladema Date: Wed, 8 Apr 2020 15:42:53 +0100 Subject: [PATCH] librustc_middle: return LocalDefId instead of DefId in get_parent_did --- src/librustc_middle/hir/map/mod.rs | 5 ++--- src/librustc_mir/const_eval/fn_queries.rs | 6 +----- src/librustc_passes/reachable.rs | 8 ++++---- src/librustc_privacy/lib.rs | 7 ++++--- src/librustc_span/def_id.rs | 5 +++++ src/librustc_typeck/astconv.rs | 2 +- src/librustc_typeck/collect.rs | 2 +- src/librustc_typeck/collect/type_of.rs | 14 ++++++++------ 8 files changed, 26 insertions(+), 23 deletions(-) diff --git a/src/librustc_middle/hir/map/mod.rs b/src/librustc_middle/hir/map/mod.rs index 6d6cbda5d5ffa..3eaacb54d5b42 100644 --- a/src/librustc_middle/hir/map/mod.rs +++ b/src/librustc_middle/hir/map/mod.rs @@ -720,9 +720,8 @@ impl<'hir> Map<'hir> { scope } - // FIXME(eddyb) this function can and should return `LocalDefId`. - pub fn get_parent_did(&self, id: HirId) -> DefId { - self.local_def_id(self.get_parent_item(id)) + pub fn get_parent_did(&self, id: HirId) -> LocalDefId { + self.local_def_id(self.get_parent_item(id)).expect_local() } pub fn get_foreign_abi(&self, hir_id: HirId) -> Abi { diff --git a/src/librustc_mir/const_eval/fn_queries.rs b/src/librustc_mir/const_eval/fn_queries.rs index 8ae4f9f4a0d6e..f1dff4fceb498 100644 --- a/src/librustc_mir/const_eval/fn_queries.rs +++ b/src/librustc_mir/const_eval/fn_queries.rs @@ -84,11 +84,7 @@ pub fn is_min_const_fn(tcx: TyCtxt<'_>, def_id: DefId) -> bool { pub fn is_parent_const_impl_raw(tcx: TyCtxt<'_>, hir_id: hir::HirId) -> bool { let parent_id = tcx.hir().get_parent_did(hir_id); - if !parent_id.is_top_level_module() { - is_const_impl_raw(tcx, parent_id.expect_local()) - } else { - false - } + if !parent_id.is_top_level_module() { is_const_impl_raw(tcx, parent_id) } else { false } } /// Checks whether the function has a `const` modifier or, in case it is an intrinsic, whether diff --git a/src/librustc_passes/reachable.rs b/src/librustc_passes/reachable.rs index c0ae6519d2e56..dcf50d7c08990 100644 --- a/src/librustc_passes/reachable.rs +++ b/src/librustc_passes/reachable.rs @@ -10,7 +10,7 @@ use rustc_data_structures::sync::Lrc; use rustc_hir as hir; use rustc_hir::def::{DefKind, Res}; use rustc_hir::def_id::LOCAL_CRATE; -use rustc_hir::def_id::{CrateNum, DefId}; +use rustc_hir::def_id::{CrateNum, DefId, LocalDefId}; use rustc_hir::intravisit::{self, NestedVisitorMap, Visitor}; use rustc_hir::itemlikevisit::ItemLikeVisitor; use rustc_hir::{HirIdSet, Node}; @@ -42,7 +42,7 @@ fn item_might_be_inlined(tcx: TyCtxt<'tcx>, item: &hir::Item<'_>, attrs: Codegen fn method_might_be_inlined( tcx: TyCtxt<'_>, impl_item: &hir::ImplItem<'_>, - impl_src: DefId, + impl_src: LocalDefId, ) -> bool { let codegen_fn_attrs = tcx.codegen_fn_attrs(impl_item.hir_id.owner.to_def_id()); let generics = tcx.generics_of(tcx.hir().local_def_id(impl_item.hir_id)); @@ -54,7 +54,7 @@ fn method_might_be_inlined( return true; } } - if let Some(impl_hir_id) = tcx.hir().as_local_hir_id(impl_src) { + if let Some(impl_hir_id) = tcx.hir().as_local_hir_id(impl_src.to_def_id()) { match tcx.hir().find(impl_hir_id) { Some(Node::Item(item)) => item_might_be_inlined(tcx, &item, codegen_fn_attrs), Some(..) | None => span_bug!(impl_item.span, "impl did is not an item"), @@ -171,7 +171,7 @@ impl<'a, 'tcx> ReachableContext<'a, 'tcx> { if generics.requires_monomorphization(self.tcx) || attrs.requests_inline() { true } else { - let impl_did = self.tcx.hir().get_parent_did(hir_id); + let impl_did = self.tcx.hir().get_parent_did(hir_id).to_def_id(); // Check the impl. If the generics on the self // type of the impl require inlining, this method // does too. diff --git a/src/librustc_privacy/lib.rs b/src/librustc_privacy/lib.rs index 34470c2ded5f9..a6d880667adf2 100644 --- a/src/librustc_privacy/lib.rs +++ b/src/librustc_privacy/lib.rs @@ -248,7 +248,7 @@ fn def_id_visibility<'tcx>( } } Node::TraitItem(..) | Node::Variant(..) => { - return def_id_visibility(tcx, tcx.hir().get_parent_did(hir_id)); + return def_id_visibility(tcx, tcx.hir().get_parent_did(hir_id).to_def_id()); } Node::ImplItem(impl_item) => { match tcx.hir().get(tcx.hir().get_parent_item(hir_id)) { @@ -270,7 +270,7 @@ fn def_id_visibility<'tcx>( let (mut ctor_vis, mut span, mut descr) = def_id_visibility(tcx, parent_did); - let adt_def = tcx.adt_def(tcx.hir().get_parent_did(hir_id)); + let adt_def = tcx.adt_def(tcx.hir().get_parent_did(hir_id).to_def_id()); let ctor_did = tcx.hir().local_def_id(vdata.ctor_hir_id().unwrap()); let variant = adt_def.variant_with_ctor_id(ctor_did); @@ -309,7 +309,8 @@ fn def_id_visibility<'tcx>( // If the structure is marked as non_exhaustive then lower the // visibility to within the crate. if ctor_vis == ty::Visibility::Public { - let adt_def = tcx.adt_def(tcx.hir().get_parent_did(hir_id)); + let adt_def = + tcx.adt_def(tcx.hir().get_parent_did(hir_id).to_def_id()); if adt_def.non_enum_variant().is_field_list_non_exhaustive() { ctor_vis = ty::Visibility::Restricted(DefId::local(CRATE_DEF_INDEX)); diff --git a/src/librustc_span/def_id.rs b/src/librustc_span/def_id.rs index 73b46d753d787..fad9f2f613012 100644 --- a/src/librustc_span/def_id.rs +++ b/src/librustc_span/def_id.rs @@ -224,6 +224,11 @@ impl LocalDefId { pub fn to_def_id(self) -> DefId { DefId { krate: LOCAL_CRATE, index: self.local_def_index } } + + #[inline] + pub fn is_top_level_module(self) -> bool { + self.local_def_index == CRATE_DEF_INDEX + } } impl fmt::Debug for LocalDefId { diff --git a/src/librustc_typeck/astconv.rs b/src/librustc_typeck/astconv.rs index ed7ec1c3b10da..83e56e3c96a96 100644 --- a/src/librustc_typeck/astconv.rs +++ b/src/librustc_typeck/astconv.rs @@ -2371,7 +2371,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o { let parent_def_id = def_id .and_then(|def_id| tcx.hir().as_local_hir_id(def_id)) - .map(|hir_id| tcx.hir().get_parent_did(hir_id)); + .map(|hir_id| tcx.hir().get_parent_did(hir_id).to_def_id()); debug!("qpath_to_ty: parent_def_id={:?}", parent_def_id); diff --git a/src/librustc_typeck/collect.rs b/src/librustc_typeck/collect.rs index c5e9a288c9ce8..5414a343c68e1 100644 --- a/src/librustc_typeck/collect.rs +++ b/src/librustc_typeck/collect.rs @@ -1513,7 +1513,7 @@ fn fn_sig(tcx: TyCtxt<'_>, def_id: DefId) -> ty::PolyFnSig<'_> { } Ctor(data) | Variant(hir::Variant { data, .. }) if data.ctor_hir_id().is_some() => { - let ty = tcx.type_of(tcx.hir().get_parent_did(hir_id)); + let ty = tcx.type_of(tcx.hir().get_parent_did(hir_id).to_def_id()); let inputs = data.fields().iter().map(|f| tcx.type_of(tcx.hir().local_def_id(f.hir_id))); ty::Binder::bind(tcx.mk_fn_sig( diff --git a/src/librustc_typeck/collect/type_of.rs b/src/librustc_typeck/collect/type_of.rs index d45c82700689a..985f66694b671 100644 --- a/src/librustc_typeck/collect/type_of.rs +++ b/src/librustc_typeck/collect/type_of.rs @@ -59,14 +59,14 @@ pub(super) fn type_of(tcx: TyCtxt<'_>, def_id: DefId) -> Ty<'_> { } } ImplItemKind::OpaqueTy(_) => { - if tcx.impl_trait_ref(tcx.hir().get_parent_did(hir_id)).is_none() { + if tcx.impl_trait_ref(tcx.hir().get_parent_did(hir_id).to_def_id()).is_none() { report_assoc_ty_on_inherent_impl(tcx, item.span); } find_opaque_ty_constraints(tcx, def_id) } ImplItemKind::TyAlias(ref ty) => { - if tcx.impl_trait_ref(tcx.hir().get_parent_did(hir_id)).is_none() { + if tcx.impl_trait_ref(tcx.hir().get_parent_did(hir_id).to_def_id()).is_none() { report_assoc_ty_on_inherent_impl(tcx, item.span); } @@ -177,7 +177,7 @@ pub(super) fn type_of(tcx: TyCtxt<'_>, def_id: DefId) -> Ty<'_> { Node::Ctor(&ref def) | Node::Variant(Variant { data: ref def, .. }) => match *def { VariantData::Unit(..) | VariantData::Struct(..) => { - tcx.type_of(tcx.hir().get_parent_did(hir_id)) + tcx.type_of(tcx.hir().get_parent_did(hir_id).to_def_id()) } VariantData::Tuple(..) => { let substs = InternalSubsts::identity_for_item(tcx, def_id); @@ -207,9 +207,11 @@ pub(super) fn type_of(tcx: TyCtxt<'_>, def_id: DefId) -> Ty<'_> { tcx.types.usize } - Node::Variant(Variant { disr_expr: Some(ref e), .. }) if e.hir_id == hir_id => { - tcx.adt_def(tcx.hir().get_parent_did(hir_id)).repr.discr_type().to_ty(tcx) - } + Node::Variant(Variant { disr_expr: Some(ref e), .. }) if e.hir_id == hir_id => tcx + .adt_def(tcx.hir().get_parent_did(hir_id).to_def_id()) + .repr + .discr_type() + .to_ty(tcx), Node::Ty(&Ty { kind: TyKind::Path(_), .. }) | Node::Expr(&Expr { kind: ExprKind::Struct(..), .. })