Skip to content

Commit 5d181d6

Browse files
Rollup merge of #164077 - adwinwhite:phase-hack, r=jdonszelmann
replace `TypingEnv::post_analysis()` with `mir::Body::typing_env()` This PR fixes the correspondence between typing modes and MIR phases so that some passes don't have to fix that inconsistency themselves. Similar to #163745 We should discourage constructing a typing env by hand when it can be derived from context. This simplifies reasoning and reduces the mistake of not resetting rigidness markers across typing envs. r? lcnr
2 parents 438355c + 9549f82 commit 5d181d6

8 files changed

Lines changed: 33 additions & 38 deletions

File tree

‎compiler/rustc_middle/src/mir/mod.rs‎

Lines changed: 14 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -414,37 +414,32 @@ impl<'tcx> Body<'tcx> {
414414
pub fn typing_env(&self, tcx: TyCtxt<'tcx>) -> TypingEnv<'tcx> {
415415
if tcx.use_typing_mode_post_typeck_through_borrowck() {
416416
match self.phase {
417-
MirPhase::Built | MirPhase::Analysis(AnalysisPhase::Initial)
418-
if let Some(def_id) = self.source.def_id().as_local() =>
419-
{
417+
// `analysis_cleanup_passes` should use `PostBorrowck`, but doing so would
418+
// complicate the code, and none of those passes seems to care.
419+
MirPhase::Built | MirPhase::Analysis(AnalysisPhase::Initial) => {
420+
let local_def_id = self
421+
.source
422+
.def_id()
423+
.as_local()
424+
.expect("We only build MIR for local bodies");
420425
TypingEnv::new(
421426
tcx.param_env(self.source.def_id()),
422-
ty::TypingMode::borrowck(tcx, def_id),
427+
ty::TypingMode::borrowck(tcx, local_def_id),
423428
)
424429
}
425-
MirPhase::Analysis(AnalysisPhase::PostCleanup)
426-
if let Some(def_id) = self.source.def_id().as_local() =>
427-
{
428-
TypingEnv::new(
429-
tcx.param_env(self.source.def_id()),
430-
ty::TypingMode::post_borrowck_analysis(tcx, def_id),
431-
)
432-
}
433-
MirPhase::Built | MirPhase::Analysis(_) => {
434-
// This branch happens for drop glue and fn ptr shims.
435-
// FIXME: why do we do any of this analysis on drop glue etc?
436-
// This should ideally all be skipped.
430+
MirPhase::Analysis(AnalysisPhase::PostCleanup) | MirPhase::Runtime(_) => {
437431
TypingEnv::post_analysis(tcx, self.source.def_id())
438432
}
439-
MirPhase::Runtime(_) => TypingEnv::post_analysis(tcx, self.source.def_id()),
440433
}
441434
} else {
442435
match self.phase {
443-
MirPhase::Built | MirPhase::Analysis(_) => TypingEnv::new(
436+
MirPhase::Built | MirPhase::Analysis(AnalysisPhase::Initial) => TypingEnv::new(
444437
tcx.param_env(self.source.def_id()),
445438
ty::TypingMode::non_body_analysis(),
446439
),
447-
MirPhase::Runtime(_) => TypingEnv::post_analysis(tcx, self.source.def_id()),
440+
MirPhase::Analysis(AnalysisPhase::PostCleanup) | MirPhase::Runtime(_) => {
441+
TypingEnv::post_analysis(tcx, self.source.def_id())
442+
}
448443
}
449444
}
450445
}

‎compiler/rustc_mir_transform/src/add_moves_for_packed_drops.rs‎

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use rustc_middle::mir::*;
2-
use rustc_middle::ty::{self, TyCtxt};
2+
use rustc_middle::ty::TyCtxt;
33
use tracing::debug;
44

55
use crate::patch::MirPatch;
@@ -41,9 +41,7 @@ impl<'tcx> crate::MirPass<'tcx> for AddMovesForPackedDrops {
4141
fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
4242
debug!("add_moves_for_packed_drops({:?} @ {:?})", body.source, body.span);
4343
let mut patch = MirPatch::new(body);
44-
// FIXME(#132279): This is used during the phase transition from analysis
45-
// to runtime, so we have to manually specify the correct typing mode.
46-
let typing_env = ty::TypingEnv::post_analysis(tcx, body.source.def_id());
44+
let typing_env = body.typing_env(tcx);
4745

4846
for (bb, data) in body.basic_blocks.iter_enumerated() {
4947
let loc = Location { block: bb, statement_index: data.statements.len() };

‎compiler/rustc_mir_transform/src/elaborate_drops.rs‎

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,7 @@ impl<'tcx> crate::MirPass<'tcx> for ElaborateDrops {
5252
#[instrument(level = "trace", skip(self, tcx, body))]
5353
fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
5454
debug!("elaborate_drops({:?} @ {:?})", body.source, body.span);
55-
// FIXME(#132279): This is used during the phase transition from analysis
56-
// to runtime, so we have to manually specify the correct typing mode.
57-
let typing_env = ty::TypingEnv::post_analysis(tcx, body.source.def_id());
55+
let typing_env = body.typing_env(tcx);
5856
// For types that do not need dropping, the behaviour is trivial. So we only need to track
5957
// init/uninit for types that do need dropping.
6058
let move_data = MoveData::gather_moves(body, tcx, |ty| ty.needs_drop(tcx, typing_env));

‎compiler/rustc_mir_transform/src/known_panics_lint.rs‎

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -181,9 +181,7 @@ impl<'tcx> ty::layout::HasTypingEnv<'tcx> for ConstPropagator<'_, 'tcx> {
181181
impl<'mir, 'tcx> ConstPropagator<'mir, 'tcx> {
182182
fn new(body: &'mir Body<'tcx>, tcx: TyCtxt<'tcx>) -> ConstPropagator<'mir, 'tcx> {
183183
let def_id = body.source.def_id();
184-
// FIXME(#132279): This is used during the phase transition from analysis
185-
// to runtime, so we have to manually specify the correct typing mode.
186-
let typing_env = ty::TypingEnv::post_analysis(tcx, body.source.def_id());
184+
let typing_env = body.typing_env(tcx);
187185
let can_const_prop = CanConstProp::check(tcx, typing_env, body);
188186
let ecx = InterpCx::new(tcx, tcx.def_span(def_id), typing_env, DummyMachine);
189187

‎compiler/rustc_mir_transform/src/lib.rs‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -649,15 +649,17 @@ fn run_analysis_cleanup_passes<'tcx>(tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
649649
];
650650

651651
pm::run_passes(tcx, body, passes, Some(MirPhase::Analysis(AnalysisPhase::PostCleanup)));
652+
// `PostAnalysisNormalize` is the pass that puts a body into `TypingMode::PostAnalysis`.
653+
// It has to be run here because moving it earlier or later would cause `validate-mir`
654+
// to fail due to mismatched typing envs.
655+
pm::run_passes(tcx, body, &[&post_analysis_normalize::PostAnalysisNormalize], None);
652656
}
653657

654658
/// Returns the sequence of passes that lowers analysis to runtime MIR.
655659
fn run_runtime_lowering_passes<'tcx>(tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
656660
let passes: &[&dyn MirPass<'tcx>] = &[
657661
// These next passes must be executed together.
658662
&add_call_guards::CriticalCallEdges,
659-
// Must be done before drop elaboration because we need to drop opaque types, too.
660-
&post_analysis_normalize::PostAnalysisNormalize,
661663
// Calling this after `PostAnalysisNormalize` ensures that we don't deal with opaque types.
662664
&add_subtyping_projections::Subtyper,
663665
&elaborate_drops::ElaborateDrops,

‎compiler/rustc_mir_transform/src/pass_manager.rs‎

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use std::sync::atomic::Ordering;
55
use rustc_attr_ir::OptimizeAttr;
66
use rustc_data_structures::fx::{FxHashMap, FxIndexSet};
77
use rustc_hir::def_id::DefId;
8-
use rustc_middle::mir::{Body, MirDumper, MirPhase, RuntimePhase};
8+
use rustc_middle::mir::{AnalysisPhase, Body, MirDumper, MirPhase, RuntimePhase};
99
use rustc_middle::ty::TyCtxt;
1010
use rustc_session::Session;
1111
use rustc_session::config::OptLevel;
@@ -397,9 +397,13 @@ fn run_passes_inner<'tcx>(
397397

398398
dump_mir_for_phase_change(tcx, body);
399399

400-
let validate =
401-
(validate_each & tcx.sess.opts.unstable_opts.validate_mir & !body.should_skip())
402-
|| new_phase == MirPhase::Runtime(RuntimePhase::Optimized);
400+
let validate = (validate_each
401+
&& tcx.sess.opts.unstable_opts.validate_mir
402+
&& !body.should_skip()
403+
// Skip validating the body in `TypingMode::PostAnalysis` because we haven't
404+
// revealed opaques yet.
405+
&& (new_phase != MirPhase::Analysis(AnalysisPhase::PostCleanup)))
406+
|| new_phase == MirPhase::Runtime(RuntimePhase::Optimized);
403407
let lint = tcx.sess.opts.unstable_opts.lint_mir & !body.should_skip();
404408
if validate {
405409
validate_body(tcx, body, format!("after phase change to {}", new_phase.name()));

‎compiler/rustc_mir_transform/src/post_analysis_normalize.rs‎

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,7 @@ pub(super) struct PostAnalysisNormalize;
1212

1313
impl<'tcx> crate::MirPass<'tcx> for PostAnalysisNormalize {
1414
fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
15-
// FIXME(#132279): This is used during the phase transition from analysis
16-
// to runtime, so we have to manually specify the correct typing mode.
17-
let typing_env = ty::TypingEnv::post_analysis(tcx, body.source.def_id());
15+
let typing_env = body.typing_env(tcx);
1816
PostAnalysisNormalizeVisitor { tcx, typing_env }.visit_body_preserves_cfg(body);
1917
}
2018

‎compiler/rustc_mir_transform/src/shim.rs‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -402,6 +402,8 @@ fn new_body<'tcx>(
402402
);
403403
// Shims do not directly mention any consts.
404404
body.set_required_consts(Vec::new());
405+
// We don't do any analysis on shims.
406+
body.phase = MirPhase::Runtime(RuntimePhase::Initial);
405407
body
406408
}
407409

0 commit comments

Comments
 (0)