Auto merge of #69380 - Zoxc:parent-module, r=michaelwoerister

Use a query to get parent modules

Split out from https://github.com/rust-lang/rust/pull/69015 / https://github.com/rust-lang/rust/pull/68944.

r? @michaelwoerister
This commit is contained in:
bors 2020-03-01 14:21:03 +00:00
commit 360e42de82
10 changed files with 25 additions and 21 deletions

View File

@ -746,15 +746,9 @@ impl<'hir> Map<'hir> {
hir_id hir_id
} }
/// Returns the `DefId` of `id`'s nearest module parent, or `id` itself if no
/// module parent is in this map.
pub fn get_module_parent(&self, id: HirId) -> DefId {
self.local_def_id(self.get_module_parent_node(id))
}
/// Returns the `HirId` of `id`'s nearest module parent, or `id` itself if no /// Returns the `HirId` of `id`'s nearest module parent, or `id` itself if no
/// module parent is in this map. /// module parent is in this map.
pub fn get_module_parent_node(&self, hir_id: HirId) -> HirId { pub(super) fn get_module_parent_node(&self, hir_id: HirId) -> HirId {
for (hir_id, node) in self.parent_iter(hir_id) { for (hir_id, node) in self.parent_iter(hir_id) {
if let Node::Item(&Item { kind: ItemKind::Mod(_), .. }) = node { if let Node::Item(&Item { kind: ItemKind::Mod(_), .. }) = node {
return hir_id; return hir_id;

View File

@ -7,9 +7,10 @@ pub mod map;
use crate::ty::query::Providers; use crate::ty::query::Providers;
use crate::ty::TyCtxt; use crate::ty::TyCtxt;
use rustc_hir::def_id::LOCAL_CRATE; use rustc_hir::def_id::{DefId, LOCAL_CRATE};
use rustc_hir::print; use rustc_hir::print;
use rustc_hir::Crate; use rustc_hir::Crate;
use rustc_hir::HirId;
use std::ops::Deref; use std::ops::Deref;
/// A wrapper type which allows you to access HIR. /// A wrapper type which allows you to access HIR.
@ -45,9 +46,17 @@ impl<'tcx> TyCtxt<'tcx> {
pub fn hir(self) -> Hir<'tcx> { pub fn hir(self) -> Hir<'tcx> {
Hir { tcx: self, map: &self.hir_map } Hir { tcx: self, map: &self.hir_map }
} }
pub fn parent_module(self, id: HirId) -> DefId {
self.parent_module_from_def_id(DefId::local(id.owner))
}
} }
pub fn provide(providers: &mut Providers<'_>) { pub fn provide(providers: &mut Providers<'_>) {
providers.parent_module_from_def_id = |tcx, id| {
let hir = tcx.hir();
hir.local_def_id(hir.get_module_parent_node(hir.as_local_hir_id(id).unwrap()))
};
providers.hir_crate = |tcx, _| tcx.hir_map.untracked_krate(); providers.hir_crate = |tcx, _| tcx.hir_map.untracked_krate();
map::provide(providers); map::provide(providers);
} }

View File

@ -98,6 +98,10 @@ rustc_queries! {
eval_always eval_always
desc { "computing the lint levels for items in this crate" } desc { "computing the lint levels for items in this crate" }
} }
query parent_module_from_def_id(_: DefId) -> DefId {
eval_always
}
} }
Codegen { Codegen {

View File

@ -385,9 +385,7 @@ impl Visibility {
Res::Err => Visibility::Public, Res::Err => Visibility::Public,
def => Visibility::Restricted(def.def_id()), def => Visibility::Restricted(def.def_id()),
}, },
hir::VisibilityKind::Inherited => { hir::VisibilityKind::Inherited => Visibility::Restricted(tcx.parent_module(id)),
Visibility::Restricted(tcx.hir().get_module_parent(id))
}
} }
} }
@ -3087,7 +3085,7 @@ impl<'tcx> TyCtxt<'tcx> {
Some(actual_expansion) => { Some(actual_expansion) => {
self.hir().definitions().parent_module_of_macro_def(actual_expansion) self.hir().definitions().parent_module_of_macro_def(actual_expansion)
} }
None => self.hir().get_module_parent(block), None => self.parent_module(block),
}; };
(ident, scope) (ident, scope)
} }

View File

@ -124,8 +124,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for UnusedResults {
descr_post: &str, descr_post: &str,
plural_len: usize, plural_len: usize,
) -> bool { ) -> bool {
if ty.is_unit() if ty.is_unit() || cx.tcx.is_ty_uninhabited_from(cx.tcx.parent_module(expr.hir_id), ty)
|| cx.tcx.is_ty_uninhabited_from(cx.tcx.hir().get_module_parent(expr.hir_id), ty)
{ {
return true; return true;
} }

View File

@ -142,7 +142,7 @@ impl<'tcx> MatchVisitor<'_, 'tcx> {
} }
fn check_in_cx(&self, hir_id: HirId, f: impl FnOnce(MatchCheckCtxt<'_, 'tcx>)) { fn check_in_cx(&self, hir_id: HirId, f: impl FnOnce(MatchCheckCtxt<'_, 'tcx>)) {
let module = self.tcx.hir().get_module_parent(hir_id); let module = self.tcx.parent_module(hir_id);
MatchCheckCtxt::create_and_enter(self.tcx, self.param_env, module, |cx| f(cx)); MatchCheckCtxt::create_and_enter(self.tcx, self.param_env, module, |cx| f(cx));
} }

View File

@ -1125,7 +1125,7 @@ impl<'a, 'tcx> Liveness<'a, 'tcx> {
} }
hir::ExprKind::Call(ref f, ref args) => { hir::ExprKind::Call(ref f, ref args) => {
let m = self.ir.tcx.hir().get_module_parent(expr.hir_id); let m = self.ir.tcx.parent_module(expr.hir_id);
let succ = if self.ir.tcx.is_ty_uninhabited_from(m, self.tables.expr_ty(expr)) { let succ = if self.ir.tcx.is_ty_uninhabited_from(m, self.tables.expr_ty(expr)) {
self.s.exit_ln self.s.exit_ln
} else { } else {
@ -1136,7 +1136,7 @@ impl<'a, 'tcx> Liveness<'a, 'tcx> {
} }
hir::ExprKind::MethodCall(.., ref args) => { hir::ExprKind::MethodCall(.., ref args) => {
let m = self.ir.tcx.hir().get_module_parent(expr.hir_id); let m = self.ir.tcx.parent_module(expr.hir_id);
let succ = if self.ir.tcx.is_ty_uninhabited_from(m, self.tables.expr_ty(expr)) { let succ = if self.ir.tcx.is_ty_uninhabited_from(m, self.tables.expr_ty(expr)) {
self.s.exit_ln self.s.exit_ln
} else { } else {

View File

@ -327,7 +327,7 @@ fn def_id_visibility<'tcx>(
} }
Node::Expr(expr) => { Node::Expr(expr) => {
return ( return (
ty::Visibility::Restricted(tcx.hir().get_module_parent(expr.hir_id)), ty::Visibility::Restricted(tcx.parent_module(expr.hir_id)),
expr.span, expr.span,
"private", "private",
); );

View File

@ -427,7 +427,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
}); });
if let Some((field, field_ty)) = field_receiver { if let Some((field, field_ty)) = field_receiver {
let scope = self.tcx.hir().get_module_parent(self.body_id); let scope = self.tcx.parent_module(self.body_id);
let is_accessible = field.vis.is_accessible_from(scope, self.tcx); let is_accessible = field.vis.is_accessible_from(scope, self.tcx);
if is_accessible { if is_accessible {
@ -824,7 +824,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
mut msg: String, mut msg: String,
candidates: Vec<DefId>, candidates: Vec<DefId>,
) { ) {
let module_did = self.tcx.hir().get_module_parent(self.body_id); let module_did = self.tcx.parent_module(self.body_id);
let module_id = self.tcx.hir().as_local_hir_id(module_did).unwrap(); let module_id = self.tcx.hir().as_local_hir_id(module_did).unwrap();
let krate = self.tcx.hir().krate(); let krate = self.tcx.hir().krate();
let (span, found_use) = UsePlacementFinder::check(self.tcx, krate, module_id); let (span, found_use) = UsePlacementFinder::check(self.tcx, krate, module_id);

View File

@ -348,7 +348,7 @@ impl<'a, 'tcx> DocFolder for LinkCollector<'a, 'tcx> {
let parent_node = self.cx.as_local_hir_id(item.def_id).and_then(|hir_id| { let parent_node = self.cx.as_local_hir_id(item.def_id).and_then(|hir_id| {
// FIXME: this fails hard for impls in non-module scope, but is necessary for the // FIXME: this fails hard for impls in non-module scope, but is necessary for the
// current `resolve()` implementation. // current `resolve()` implementation.
match self.cx.tcx.hir().get_module_parent_node(hir_id) { match self.cx.as_local_hir_id(self.cx.tcx.parent_module(hir_id)).unwrap() {
id if id != hir_id => Some(id), id if id != hir_id => Some(id),
_ => None, _ => None,
} }