Skip to content

Commit db03a2d

Browse files
committed
Avoid accessing HIR from MIR queries.
1 parent 341883d commit db03a2d

File tree

10 files changed

+35
-41
lines changed

10 files changed

+35
-41
lines changed

compiler/rustc_borrowck/src/lib.rs

+11-10
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ use rustc_data_structures::graph::dominators::Dominators;
2323
use rustc_errors::{Applicability, Diagnostic, DiagnosticBuilder, ErrorGuaranteed};
2424
use rustc_hir as hir;
2525
use rustc_hir::def_id::LocalDefId;
26-
use rustc_hir::Node;
2726
use rustc_index::bit_set::ChunkedBitSet;
2827
use rustc_index::vec::IndexVec;
2928
use rustc_infer::infer::{InferCtxt, TyCtxtInferExt};
@@ -288,14 +287,16 @@ fn do_mir_borrowck<'a, 'tcx>(
288287
.pass_name("borrowck")
289288
.iterate_to_fixpoint();
290289

291-
let def_hir_id = tcx.hir().local_def_id_to_hir_id(def.did);
292-
let movable_generator = !matches!(
293-
tcx.hir().get(def_hir_id),
294-
Node::Expr(&hir::Expr {
295-
kind: hir::ExprKind::Closure(.., Some(hir::Movability::Static)),
296-
..
297-
})
298-
);
290+
let movable_generator =
291+
// The first argument is the generator type passed by value
292+
if let Some(local) = body.local_decls.raw.get(1)
293+
// Get the interior types and substs which typeck computed
294+
&& let ty::Generator(_, _, hir::Movability::Static) = local.ty.kind()
295+
{
296+
false
297+
} else {
298+
true
299+
};
299300

300301
for (idx, move_data_results) in promoted_errors {
301302
let promoted_body = &promoted[idx];
@@ -385,7 +386,7 @@ fn do_mir_borrowck<'a, 'tcx>(
385386
let scope = mbcx.body.source_info(location).scope;
386387
let lint_root = match &mbcx.body.source_scopes[scope].local_data {
387388
ClearCrossCrate::Set(data) => data.lint_root,
388-
_ => def_hir_id,
389+
_ => tcx.hir().local_def_id_to_hir_id(def.did),
389390
};
390391

391392
// Span and message don't matter; we overwrite them below anyway

compiler/rustc_borrowck/src/universal_regions.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -829,12 +829,12 @@ fn for_each_late_bound_region_defined_on<'tcx>(
829829
) {
830830
if let Some((owner, late_bounds)) = tcx.is_late_bound_map(fn_def_id.expect_local()) {
831831
for &late_bound in late_bounds.iter() {
832-
let hir_id = HirId { owner, local_id: late_bound };
833-
let name = tcx.hir().name(hir_id);
834-
let region_def_id = tcx.hir().local_def_id(hir_id);
832+
let region_def_id =
833+
tcx.hir().local_def_id(HirId { owner, local_id: late_bound }).to_def_id();
834+
let name = tcx.item_name(region_def_id);
835835
let liberated_region = tcx.mk_region(ty::ReFree(ty::FreeRegion {
836836
scope: owner.to_def_id(),
837-
bound_region: ty::BoundRegionKind::BrNamed(region_def_id.to_def_id(), name),
837+
bound_region: ty::BoundRegionKind::BrNamed(region_def_id, name),
838838
}));
839839
f(liberated_region);
840840
}

compiler/rustc_const_eval/src/transform/check_consts/check.rs

+2-6
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,7 @@ impl<'mir, 'tcx> Checker<'mir, 'tcx> {
222222

223223
// `async` functions cannot be `const fn`. This is checked during AST lowering, so there's
224224
// no need to emit duplicate errors here.
225-
if is_async_fn(self.ccx) || body.generator.is_some() {
225+
if self.ccx.is_async() || body.generator.is_some() {
226226
tcx.sess.delay_span_bug(body.span, "`async` functions cannot be `const fn`");
227227
return;
228228
}
@@ -1056,12 +1056,8 @@ fn is_int_bool_or_char(ty: Ty<'_>) -> bool {
10561056
ty.is_bool() || ty.is_integral() || ty.is_char()
10571057
}
10581058

1059-
fn is_async_fn(ccx: &ConstCx<'_, '_>) -> bool {
1060-
ccx.fn_sig().map_or(false, |sig| sig.header.asyncness == hir::IsAsync::Async)
1061-
}
1062-
10631059
fn emit_unstable_in_stable_error(ccx: &ConstCx<'_, '_>, span: Span, gate: Symbol) {
1064-
let attr_span = ccx.fn_sig().map_or(ccx.body.span, |sig| sig.span.shrink_to_lo());
1060+
let attr_span = ccx.tcx.def_span(ccx.def_id()).shrink_to_lo();
10651061

10661062
ccx.tcx
10671063
.sess

compiler/rustc_const_eval/src/transform/check_consts/mod.rs

+2-8
Original file line numberDiff line numberDiff line change
@@ -61,14 +61,8 @@ impl<'mir, 'tcx> ConstCx<'mir, 'tcx> {
6161
&& is_const_stable_const_fn(self.tcx, self.def_id().to_def_id())
6262
}
6363

64-
/// Returns the function signature of the item being const-checked if it is a `fn` or `const fn`.
65-
pub fn fn_sig(&self) -> Option<&'tcx hir::FnSig<'tcx>> {
66-
// Get this from the HIR map instead of a query to avoid cycle errors.
67-
//
68-
// FIXME: Is this still an issue?
69-
let hir_map = self.tcx.hir();
70-
let hir_id = hir_map.local_def_id_to_hir_id(self.def_id());
71-
hir_map.fn_sig_by_hir_id(hir_id)
64+
fn is_async(&self) -> bool {
65+
self.tcx.asyncness(self.def_id()) == hir::IsAsync::Async
7266
}
7367
}
7468

compiler/rustc_hir/src/def.rs

+8
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,14 @@ impl DefKind {
223223
| DefKind::Impl => None,
224224
}
225225
}
226+
227+
#[inline]
228+
pub fn is_fn_like(self) -> bool {
229+
match self {
230+
DefKind::Fn | DefKind::AssocFn | DefKind::Closure | DefKind::Generator => true,
231+
_ => false,
232+
}
233+
}
226234
}
227235

228236
/// The resolution of a path or export.

compiler/rustc_mir_transform/src/const_prop.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,9 @@ impl<'tcx> MirPass<'tcx> for ConstProp {
7171
}
7272

7373
let def_id = body.source.def_id().expect_local();
74-
let is_fn_like = tcx.hir().get_by_def_id(def_id).fn_kind().is_some();
75-
let is_assoc_const = tcx.def_kind(def_id) == DefKind::AssocConst;
74+
let def_kind = tcx.def_kind(def_id);
75+
let is_fn_like = def_kind.is_fn_like();
76+
let is_assoc_const = def_kind == DefKind::AssocConst;
7677

7778
// Only run const prop on functions, methods, closures and associated constants
7879
if !is_fn_like && !is_assoc_const {

compiler/rustc_mir_transform/src/const_prop_lint.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ impl<'tcx> MirLint<'tcx> for ConstProp {
6767
}
6868

6969
let def_id = body.source.def_id().expect_local();
70-
let is_fn_like = tcx.hir().get_by_def_id(def_id).fn_kind().is_some();
70+
let is_fn_like = tcx.def_kind(def_id).is_fn_like();
7171
let is_assoc_const = tcx.def_kind(def_id) == DefKind::AssocConst;
7272

7373
// Only run const prop on functions, methods, closures and associated constants

compiler/rustc_mir_transform/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -366,7 +366,7 @@ fn mir_drops_elaborated_and_const_checked<'tcx>(
366366

367367
let mir_borrowck = tcx.mir_borrowck_opt_const_arg(def);
368368

369-
let is_fn_like = tcx.hir().get_by_def_id(def.did).fn_kind().is_some();
369+
let is_fn_like = tcx.def_kind(def.did).is_fn_like();
370370
if is_fn_like {
371371
let did = def.did.to_def_id();
372372
let def = ty::WithOptConstParam::unknown(did);

compiler/rustc_mir_transform/src/shim.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -725,9 +725,6 @@ fn build_call_shim<'tcx>(
725725
pub fn build_adt_ctor(tcx: TyCtxt<'_>, ctor_id: DefId) -> Body<'_> {
726726
debug_assert!(tcx.is_constructor(ctor_id));
727727

728-
let span =
729-
tcx.hir().span_if_local(ctor_id).unwrap_or_else(|| bug!("no span for ctor {:?}", ctor_id));
730-
731728
let param_env = tcx.param_env(ctor_id);
732729

733730
// Normalize the sig.
@@ -740,6 +737,8 @@ pub fn build_adt_ctor(tcx: TyCtxt<'_>, ctor_id: DefId) -> Body<'_> {
740737

741738
debug!("build_ctor: ctor_id={:?} sig={:?}", ctor_id, sig);
742739

740+
let span = tcx.def_span(ctor_id);
741+
743742
let local_decls = local_decls_for_sig(&sig, span);
744743

745744
let source_info = SourceInfo::outermost(span);

compiler/rustc_ty_utils/src/ty.rs

+1-6
Original file line numberDiff line numberDiff line change
@@ -414,12 +414,7 @@ fn issue33140_self_ty(tcx: TyCtxt<'_>, def_id: DefId) -> Option<Ty<'_>> {
414414
/// Check if a function is async.
415415
fn asyncness(tcx: TyCtxt<'_>, def_id: DefId) -> hir::IsAsync {
416416
let node = tcx.hir().get_by_def_id(def_id.expect_local());
417-
418-
let fn_kind = node.fn_kind().unwrap_or_else(|| {
419-
bug!("asyncness: expected fn-like node but got `{:?}`", def_id);
420-
});
421-
422-
fn_kind.asyncness()
417+
if let Some(fn_kind) = node.fn_kind() { fn_kind.asyncness() } else { hir::IsAsync::NotAsync }
423418
}
424419

425420
/// Don't call this directly: use ``tcx.conservative_is_privately_uninhabited`` instead.

0 commit comments

Comments
 (0)