Skip to content

Commit bd02f4a

Browse files
committed
Add a parent_module query
1 parent 0174725 commit bd02f4a

File tree

8 files changed

+23
-12
lines changed

8 files changed

+23
-12
lines changed

src/librustc/hir/mod.rs

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

99
use crate::ty::query::Providers;
1010
use crate::ty::TyCtxt;
11-
use rustc_hir::def_id::LOCAL_CRATE;
11+
use rustc_hir::def_id::{DefId, LOCAL_CRATE};
1212
use rustc_hir::print;
1313
use rustc_hir::Crate;
14+
use rustc_hir::HirId;
1415
use std::ops::Deref;
1516

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

5156
pub fn provide(providers: &mut Providers<'_>) {
57+
providers.parent_module = |tcx, id| {
58+
let hir = tcx.hir();
59+
hir.get_module_parent(hir.as_local_hir_id(id).unwrap())
60+
};
5261
providers.hir_crate = |tcx, _| tcx.hir_map.untracked_krate();
5362
map::provide(providers);
5463
}

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(_: 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
@@ -301,9 +301,7 @@ impl Visibility {
301301
Res::Err => Visibility::Public,
302302
def => Visibility::Restricted(def.def_id()),
303303
},
304-
hir::VisibilityKind::Inherited => {
305-
Visibility::Restricted(tcx.hir().get_module_parent(id))
306-
}
304+
hir::VisibilityKind::Inherited => Visibility::Restricted(tcx.hir_id_parent_module(id)),
307305
}
308306
}
309307

@@ -2968,7 +2966,7 @@ impl<'tcx> TyCtxt<'tcx> {
29682966
Some(actual_expansion) => {
29692967
self.hir().definitions().parent_module_of_macro_def(actual_expansion)
29702968
}
2971-
None => self.hir().get_module_parent(block),
2969+
None => self.hir_id_parent_module(block),
29722970
};
29732971
(ident, scope)
29742972
}

src/librustc_lint/unused.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for UnusedResults {
127127
plural_len: usize,
128128
) -> bool {
129129
if ty.is_unit()
130-
|| cx.tcx.is_ty_uninhabited_from(cx.tcx.hir().get_module_parent(expr.hir_id), ty)
130+
|| cx.tcx.is_ty_uninhabited_from(cx.tcx.hir_id_parent_module(expr.hir_id), ty)
131131
{
132132
return true;
133133
}

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.hir_id_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.hir_id_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.hir_id_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.hir_id_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.hir_id_parent_module(self.body_id);
431431
let is_accessible = field.vis.is_accessible_from(scope, self.tcx);
432432

433433
if is_accessible {
@@ -686,7 +686,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
686686
mut msg: String,
687687
candidates: Vec<DefId>,
688688
) {
689-
let module_did = self.tcx.hir().get_module_parent(self.body_id);
689+
let module_did = self.tcx.hir_id_parent_module(self.body_id);
690690
let module_id = self.tcx.hir().as_local_hir_id(module_did).unwrap();
691691
let krate = self.tcx.hir().krate();
692692
let (span, found_use) = UsePlacementFinder::check(self.tcx, krate, module_id);

0 commit comments

Comments
 (0)