summaryrefslogtreecommitdiffstats
path: root/compiler/rustc_middle/src/hir/mod.rs
diff options
context:
space:
mode:
Diffstat (limited to 'compiler/rustc_middle/src/hir/mod.rs')
-rw-r--r--compiler/rustc_middle/src/hir/mod.rs31
1 files changed, 19 insertions, 12 deletions
diff --git a/compiler/rustc_middle/src/hir/mod.rs b/compiler/rustc_middle/src/hir/mod.rs
index 45a07fdd2..0da8fe9cc 100644
--- a/compiler/rustc_middle/src/hir/mod.rs
+++ b/compiler/rustc_middle/src/hir/mod.rs
@@ -11,7 +11,7 @@ use crate::ty::{EarlyBinder, ImplSubject, TyCtxt};
use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
use rustc_data_structures::sync::{par_for_each_in, DynSend, DynSync};
use rustc_hir::def::DefKind;
-use rustc_hir::def_id::{DefId, LocalDefId};
+use rustc_hir::def_id::{DefId, LocalDefId, LocalModDefId};
use rustc_hir::*;
use rustc_query_system::ich::StableHashingContext;
use rustc_span::{ExpnId, DUMMY_SP};
@@ -101,8 +101,22 @@ impl<'tcx> TyCtxt<'tcx> {
map::Map { tcx: self }
}
- pub fn parent_module(self, id: HirId) -> LocalDefId {
- self.parent_module_from_def_id(id.owner.def_id)
+ pub fn parent_module(self, id: HirId) -> LocalModDefId {
+ if !id.is_owner() && self.def_kind(id.owner) == DefKind::Mod {
+ LocalModDefId::new_unchecked(id.owner.def_id)
+ } else {
+ self.parent_module_from_def_id(id.owner.def_id)
+ }
+ }
+
+ pub fn parent_module_from_def_id(self, mut id: LocalDefId) -> LocalModDefId {
+ while let Some(parent) = self.opt_local_parent(id) {
+ id = parent;
+ if self.def_kind(id) == DefKind::Mod {
+ break;
+ }
+ }
+ LocalModDefId::new_unchecked(id)
}
pub fn impl_subject(self, def_id: DefId) -> EarlyBinder<ImplSubject<'tcx>> {
@@ -120,10 +134,6 @@ impl<'tcx> TyCtxt<'tcx> {
}
pub fn provide(providers: &mut Providers) {
- providers.parent_module_from_def_id = |tcx, id| {
- let hir = tcx.hir();
- hir.get_module_parent_node(hir.local_def_id_to_hir_id(id)).def_id
- };
providers.hir_crate_items = map::hir_crate_items;
providers.crate_hash = map::crate_hash;
providers.hir_module_items = map::hir_module_items;
@@ -154,18 +164,15 @@ pub fn provide(providers: &mut Providers) {
tcx.hir_crate(()).owners[id.def_id].as_owner().map_or(AttributeMap::EMPTY, |o| &o.attrs)
};
providers.def_span = |tcx, def_id| {
- let def_id = def_id;
let hir_id = tcx.hir().local_def_id_to_hir_id(def_id);
tcx.hir().opt_span(hir_id).unwrap_or(DUMMY_SP)
};
providers.def_ident_span = |tcx, def_id| {
- let def_id = def_id;
let hir_id = tcx.hir().local_def_id_to_hir_id(def_id);
tcx.hir().opt_ident_span(hir_id)
};
- providers.fn_arg_names = |tcx, id| {
+ providers.fn_arg_names = |tcx, def_id| {
let hir = tcx.hir();
- let def_id = id;
let hir_id = hir.local_def_id_to_hir_id(def_id);
if let Some(body_id) = hir.maybe_body_owned_by(def_id) {
tcx.arena.alloc_from_iter(hir.body_param_names(body_id))
@@ -180,7 +187,7 @@ pub fn provide(providers: &mut Providers) {
{
idents
} else {
- span_bug!(hir.span(hir_id), "fn_arg_names: unexpected item {:?}", id);
+ span_bug!(hir.span(hir_id), "fn_arg_names: unexpected item {:?}", def_id);
}
};
providers.opt_def_kind = |tcx, def_id| tcx.hir().opt_def_kind(def_id);