Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 14 additions & 19 deletions compiler/rustc_middle/src/mir/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -415,37 +415,32 @@ impl<'tcx> Body<'tcx> {
pub fn typing_env(&self, tcx: TyCtxt<'tcx>) -> TypingEnv<'tcx> {
if tcx.use_typing_mode_post_typeck_through_borrowck() {
match self.phase {
MirPhase::Built | MirPhase::Analysis(AnalysisPhase::Initial)
if let Some(def_id) = self.source.def_id().as_local() =>
{
// `analysis_cleanup_passes` should use `PostBorrowck`, but doing so would
// complicate the code, and none of those passes seems to care.
MirPhase::Built | MirPhase::Analysis(AnalysisPhase::Initial) => {
let local_def_id = self
.source
.def_id()
.as_local()
.expect("We only build MIR for local bodies");
TypingEnv::new(
tcx.param_env(self.source.def_id()),
ty::TypingMode::borrowck(tcx, def_id),
ty::TypingMode::borrowck(tcx, local_def_id),
)
}
MirPhase::Analysis(AnalysisPhase::PostCleanup)
if let Some(def_id) = self.source.def_id().as_local() =>
{
TypingEnv::new(
tcx.param_env(self.source.def_id()),
ty::TypingMode::post_borrowck_analysis(tcx, def_id),
)
}
MirPhase::Built | MirPhase::Analysis(_) => {
// This branch happens for drop glue and fn ptr shims.
// FIXME: why do we do any of this analysis on drop glue etc?
// This should ideally all be skipped.
MirPhase::Analysis(AnalysisPhase::PostCleanup) | MirPhase::Runtime(_) => {
TypingEnv::post_analysis(tcx, self.source.def_id())
}
MirPhase::Runtime(_) => TypingEnv::post_analysis(tcx, self.source.def_id()),
}
} else {
match self.phase {
MirPhase::Built | MirPhase::Analysis(_) => TypingEnv::new(
MirPhase::Built | MirPhase::Analysis(AnalysisPhase::Initial) => TypingEnv::new(
tcx.param_env(self.source.def_id()),
ty::TypingMode::non_body_analysis(),
),
MirPhase::Runtime(_) => TypingEnv::post_analysis(tcx, self.source.def_id()),
MirPhase::Analysis(AnalysisPhase::PostCleanup) | MirPhase::Runtime(_) => {
TypingEnv::post_analysis(tcx, self.source.def_id())
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use rustc_middle::mir::*;
use rustc_middle::ty::{self, TyCtxt};
use rustc_middle::ty::TyCtxt;
use tracing::debug;

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

for (bb, data) in body.basic_blocks.iter_enumerated() {
let loc = Location { block: bb, statement_index: data.statements.len() };
Expand Down
4 changes: 1 addition & 3 deletions compiler/rustc_mir_transform/src/elaborate_drops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,7 @@ impl<'tcx> crate::MirPass<'tcx> for ElaborateDrops {
#[instrument(level = "trace", skip(self, tcx, body))]
fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
debug!("elaborate_drops({:?} @ {:?})", body.source, body.span);
// FIXME(#132279): This is used during the phase transition from analysis
// to runtime, so we have to manually specify the correct typing mode.
let typing_env = ty::TypingEnv::post_analysis(tcx, body.source.def_id());
let typing_env = body.typing_env(tcx);
// For types that do not need dropping, the behaviour is trivial. So we only need to track
// init/uninit for types that do need dropping.
let move_data = MoveData::gather_moves(body, tcx, |ty| ty.needs_drop(tcx, typing_env));
Expand Down
4 changes: 1 addition & 3 deletions compiler/rustc_mir_transform/src/known_panics_lint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -181,9 +181,7 @@ impl<'tcx> ty::layout::HasTypingEnv<'tcx> for ConstPropagator<'_, 'tcx> {
impl<'mir, 'tcx> ConstPropagator<'mir, 'tcx> {
fn new(body: &'mir Body<'tcx>, tcx: TyCtxt<'tcx>) -> ConstPropagator<'mir, 'tcx> {
let def_id = body.source.def_id();
// FIXME(#132279): This is used during the phase transition from analysis
// to runtime, so we have to manually specify the correct typing mode.
let typing_env = ty::TypingEnv::post_analysis(tcx, body.source.def_id());
let typing_env = body.typing_env(tcx);
let can_const_prop = CanConstProp::check(tcx, typing_env, body);
let ecx = InterpCx::new(tcx, tcx.def_span(def_id), typing_env, DummyMachine);

Expand Down
6 changes: 4 additions & 2 deletions compiler/rustc_mir_transform/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -647,15 +647,17 @@ fn run_analysis_cleanup_passes<'tcx>(tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
];

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

@jdonszelmann jdonszelmann Nov 10, 2026 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can't this be part of the other run_passes call?

View changes since the review

@adwinwhite adwinwhite Nov 11, 2026 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It can't be.
PostAnalysisNormalize needs to be run and validated in TypingMode::PostAnalysis which happens after the other run_passes call.

}

/// Returns the sequence of passes that lowers analysis to runtime MIR.
fn run_runtime_lowering_passes<'tcx>(tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
let passes: &[&dyn MirPass<'tcx>] = &[
// These next passes must be executed together.
&add_call_guards::CriticalCallEdges,
// Must be done before drop elaboration because we need to drop opaque types, too.
&post_analysis_normalize::PostAnalysisNormalize,
// Calling this after `PostAnalysisNormalize` ensures that we don't deal with opaque types.
&add_subtyping_projections::Subtyper,
&elaborate_drops::ElaborateDrops,
Expand Down
12 changes: 8 additions & 4 deletions compiler/rustc_mir_transform/src/pass_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use std::sync::atomic::Ordering;
use rustc_attr_ir::OptimizeAttr;
use rustc_data_structures::fx::{FxHashMap, FxIndexSet};
use rustc_hir::def_id::DefId;
use rustc_middle::mir::{Body, MirDumper, MirPhase, RuntimePhase};
use rustc_middle::mir::{AnalysisPhase, Body, MirDumper, MirPhase, RuntimePhase};
use rustc_middle::ty::TyCtxt;
use rustc_session::Session;
use rustc_session::config::OptLevel;
Expand Down Expand Up @@ -393,9 +393,13 @@ fn run_passes_inner<'tcx>(

dump_mir_for_phase_change(tcx, body);

let validate =
(validate_each & tcx.sess.opts.unstable_opts.validate_mir & !body.should_skip())
|| new_phase == MirPhase::Runtime(RuntimePhase::Optimized);
let validate = (validate_each

@jdonszelmann jdonszelmann Nov 10, 2026 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

feels weird to use single & here haha, especially with the || below

View changes since the review

&& tcx.sess.opts.unstable_opts.validate_mir
&& !body.should_skip()
// Skip validating the body in `TypingMode::PostAnalysis` because we haven't
// revealed opaques yet.
&& (new_phase != MirPhase::Analysis(AnalysisPhase::PostCleanup)))
|| new_phase == MirPhase::Runtime(RuntimePhase::Optimized);
let lint = tcx.sess.opts.unstable_opts.lint_mir & !body.should_skip();
if validate {
validate_body(tcx, body, format!("after phase change to {}", new_phase.name()));
Expand Down
4 changes: 1 addition & 3 deletions compiler/rustc_mir_transform/src/post_analysis_normalize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,7 @@ pub(super) struct PostAnalysisNormalize;

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

Expand Down
2 changes: 2 additions & 0 deletions compiler/rustc_mir_transform/src/shim.rs
Original file line number Diff line number Diff line change
Expand Up @@ -395,6 +395,8 @@ fn new_body<'tcx>(
);
// Shims do not directly mention any consts.
body.set_required_consts(Vec::new());
// We don't do any analysis on shims.
body.phase = MirPhase::Runtime(RuntimePhase::Initial);
body
}

Expand Down
Loading