Skip to content

Commit 027a232

Browse files
committed
Auto merge of #95487 - cjgillot:menhir, r=oli-obk
Avoid accessing HIR from MIR passes `hir_owner_nodes` contains a lot of information, and the query result is typically dirty. This forces dependent queries to be re-executed needlessly. This PR refactors some accesses to HIR to go through more targeted queries that yield the same result. Based on #95435 and #95436
2 parents 18f32b7 + bbacfcb commit 027a232

File tree

17 files changed

+89
-110
lines changed

17 files changed

+89
-110
lines changed

compiler/rustc_borrowck/src/lib.rs

Lines changed: 11 additions & 10 deletions
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

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -828,10 +828,8 @@ fn for_each_late_bound_region_defined_on<'tcx>(
828828
mut f: impl FnMut(ty::Region<'tcx>),
829829
) {
830830
if let Some((owner, late_bounds)) = tcx.is_late_bound_map(fn_def_id.expect_local()) {
831-
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);
831+
for &region_def_id in late_bounds.iter() {
832+
let name = tcx.item_name(region_def_id.to_def_id());
835833
let liberated_region = tcx.mk_region(ty::ReFree(ty::FreeRegion {
836834
scope: owner.to_def_id(),
837835
bound_region: ty::BoundRegionKind::BrNamed(region_def_id.to_def_id(), name),

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

Lines changed: 2 additions & 6 deletions
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

Lines changed: 2 additions & 8 deletions
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

Lines changed: 8 additions & 0 deletions
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_middle/src/middle/resolve_lifetime.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ pub struct ResolveLifetimes {
6464
/// Set of lifetime def ids that are late-bound; a region can
6565
/// be late-bound if (a) it does NOT appear in a where-clause and
6666
/// (b) it DOES appear in the arguments.
67-
pub late_bound: FxHashMap<LocalDefId, FxHashSet<ItemLocalId>>,
67+
pub late_bound: FxHashMap<LocalDefId, FxHashSet<LocalDefId>>,
6868

6969
pub late_bound_vars: FxHashMap<LocalDefId, FxHashMap<ItemLocalId, Vec<ty::BoundVariableKind>>>,
7070
}

compiler/rustc_middle/src/query/mod.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1502,8 +1502,7 @@ rustc_queries! {
15021502
Option<&'tcx FxHashMap<ItemLocalId, Region>> {
15031503
desc { "looking up a named region" }
15041504
}
1505-
query is_late_bound_map(_: LocalDefId) ->
1506-
Option<(LocalDefId, &'tcx FxHashSet<ItemLocalId>)> {
1505+
query is_late_bound_map(_: LocalDefId) -> Option<(LocalDefId, &'tcx FxHashSet<LocalDefId>)> {
15071506
desc { "testing if a region is late bound" }
15081507
}
15091508
/// For a given item (like a struct), gets the default lifetimes to be used

compiler/rustc_middle/src/ty/context.rs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2772,11 +2772,6 @@ impl<'tcx> TyCtxt<'tcx> {
27722772
self.named_region_map(id.owner).and_then(|map| map.get(&id.local_id).cloned())
27732773
}
27742774

2775-
pub fn is_late_bound(self, id: HirId) -> bool {
2776-
self.is_late_bound_map(id.owner)
2777-
.map_or(false, |(owner, set)| owner == id.owner && set.contains(&id.local_id))
2778-
}
2779-
27802775
pub fn late_bound_vars(self, id: HirId) -> &'tcx List<ty::BoundVariableKind> {
27812776
self.mk_bound_variable_kinds(
27822777
self.late_bound_vars_map(id.owner)

compiler/rustc_mir_transform/src/const_prop.rs

Lines changed: 3 additions & 2 deletions
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

Lines changed: 1 addition & 1 deletion
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

0 commit comments

Comments
 (0)