Skip to content

Commit 360e42d

Browse files
committed
Auto merge of #69380 - Zoxc:parent-module, r=michaelwoerister
Use a query to get parent modules Split out from #69015 / #68944. r? @michaelwoerister
2 parents 6b2983a + 98251d8 commit 360e42d

File tree

10 files changed

+25
-21
lines changed

10 files changed

+25
-21
lines changed

src/librustc/hir/map/mod.rs

+1-7
Original file line numberDiff line numberDiff line change
@@ -746,15 +746,9 @@ impl<'hir> Map<'hir> {
746746
hir_id
747747
}
748748

749-
/// Returns the `DefId` of `id`'s nearest module parent, or `id` itself if no
750-
/// module parent is in this map.
751-
pub fn get_module_parent(&self, id: HirId) -> DefId {
752-
self.local_def_id(self.get_module_parent_node(id))
753-
}
754-
755749
/// Returns the `HirId` of `id`'s nearest module parent, or `id` itself if no
756750
/// module parent is in this map.
757-
pub fn get_module_parent_node(&self, hir_id: HirId) -> HirId {
751+
pub(super) fn get_module_parent_node(&self, hir_id: HirId) -> HirId {
758752
for (hir_id, node) in self.parent_iter(hir_id) {
759753
if let Node::Item(&Item { kind: ItemKind::Mod(_), .. }) = node {
760754
return hir_id;

src/librustc/hir/mod.rs

+10-1
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,10 @@ pub mod map;
77

88
use crate::ty::query::Providers;
99
use crate::ty::TyCtxt;
10-
use rustc_hir::def_id::LOCAL_CRATE;
10+
use rustc_hir::def_id::{DefId, LOCAL_CRATE};
1111
use rustc_hir::print;
1212
use rustc_hir::Crate;
13+
use rustc_hir::HirId;
1314
use std::ops::Deref;
1415

1516
/// A wrapper type which allows you to access HIR.
@@ -45,9 +46,17 @@ impl<'tcx> TyCtxt<'tcx> {
4546
pub fn hir(self) -> Hir<'tcx> {
4647
Hir { tcx: self, map: &self.hir_map }
4748
}
49+
50+
pub fn parent_module(self, id: HirId) -> DefId {
51+
self.parent_module_from_def_id(DefId::local(id.owner))
52+
}
4853
}
4954

5055
pub fn provide(providers: &mut Providers<'_>) {
56+
providers.parent_module_from_def_id = |tcx, id| {
57+
let hir = tcx.hir();
58+
hir.local_def_id(hir.get_module_parent_node(hir.as_local_hir_id(id).unwrap()))
59+
};
5160
providers.hir_crate = |tcx, _| tcx.hir_map.untracked_krate();
5261
map::provide(providers);
5362
}

src/librustc/query/mod.rs

+4
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,10 @@ rustc_queries! {
9898
eval_always
9999
desc { "computing the lint levels for items in this crate" }
100100
}
101+
102+
query parent_module_from_def_id(_: DefId) -> DefId {
103+
eval_always
104+
}
101105
}
102106

103107
Codegen {

src/librustc/ty/mod.rs

+2-4
Original file line numberDiff line numberDiff line change
@@ -385,9 +385,7 @@ impl Visibility {
385385
Res::Err => Visibility::Public,
386386
def => Visibility::Restricted(def.def_id()),
387387
},
388-
hir::VisibilityKind::Inherited => {
389-
Visibility::Restricted(tcx.hir().get_module_parent(id))
390-
}
388+
hir::VisibilityKind::Inherited => Visibility::Restricted(tcx.parent_module(id)),
391389
}
392390
}
393391

@@ -3087,7 +3085,7 @@ impl<'tcx> TyCtxt<'tcx> {
30873085
Some(actual_expansion) => {
30883086
self.hir().definitions().parent_module_of_macro_def(actual_expansion)
30893087
}
3090-
None => self.hir().get_module_parent(block),
3088+
None => self.parent_module(block),
30913089
};
30923090
(ident, scope)
30933091
}

src/librustc_lint/unused.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -124,8 +124,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for UnusedResults {
124124
descr_post: &str,
125125
plural_len: usize,
126126
) -> bool {
127-
if ty.is_unit()
128-
|| cx.tcx.is_ty_uninhabited_from(cx.tcx.hir().get_module_parent(expr.hir_id), ty)
127+
if ty.is_unit() || cx.tcx.is_ty_uninhabited_from(cx.tcx.parent_module(expr.hir_id), ty)
129128
{
130129
return true;
131130
}

src/librustc_mir_build/hair/pattern/check_match.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ impl<'tcx> MatchVisitor<'_, 'tcx> {
142142
}
143143

144144
fn check_in_cx(&self, hir_id: HirId, f: impl FnOnce(MatchCheckCtxt<'_, 'tcx>)) {
145-
let module = self.tcx.hir().get_module_parent(hir_id);
145+
let module = self.tcx.parent_module(hir_id);
146146
MatchCheckCtxt::create_and_enter(self.tcx, self.param_env, module, |cx| f(cx));
147147
}
148148

src/librustc_passes/liveness.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1125,7 +1125,7 @@ impl<'a, 'tcx> Liveness<'a, 'tcx> {
11251125
}
11261126

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

11381138
hir::ExprKind::MethodCall(.., ref args) => {
1139-
let m = self.ir.tcx.hir().get_module_parent(expr.hir_id);
1139+
let m = self.ir.tcx.parent_module(expr.hir_id);
11401140
let succ = if self.ir.tcx.is_ty_uninhabited_from(m, self.tables.expr_ty(expr)) {
11411141
self.s.exit_ln
11421142
} else {

src/librustc_privacy/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -327,7 +327,7 @@ fn def_id_visibility<'tcx>(
327327
}
328328
Node::Expr(expr) => {
329329
return (
330-
ty::Visibility::Restricted(tcx.hir().get_module_parent(expr.hir_id)),
330+
ty::Visibility::Restricted(tcx.parent_module(expr.hir_id)),
331331
expr.span,
332332
"private",
333333
);

src/librustc_typeck/check/method/suggest.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -427,7 +427,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
427427
});
428428

429429
if let Some((field, field_ty)) = field_receiver {
430-
let scope = self.tcx.hir().get_module_parent(self.body_id);
430+
let scope = self.tcx.parent_module(self.body_id);
431431
let is_accessible = field.vis.is_accessible_from(scope, self.tcx);
432432

433433
if is_accessible {
@@ -824,7 +824,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
824824
mut msg: String,
825825
candidates: Vec<DefId>,
826826
) {
827-
let module_did = self.tcx.hir().get_module_parent(self.body_id);
827+
let module_did = self.tcx.parent_module(self.body_id);
828828
let module_id = self.tcx.hir().as_local_hir_id(module_did).unwrap();
829829
let krate = self.tcx.hir().krate();
830830
let (span, found_use) = UsePlacementFinder::check(self.tcx, krate, module_id);

src/librustdoc/passes/collect_intra_doc_links.rs

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

0 commit comments

Comments
 (0)