Skip to content

replace TypingEnv::post_analysis() with mir::Body::typing_env() - #164077

Open
adwinwhite wants to merge 1 commit into
rust-lang:mainfrom
adwinwhite:phase-hack
Open

adwinwhite wants to merge 1 commit into
rust-lang:mainfrom
adwinwhite:phase-hack

Conversation

@adwinwhite

@adwinwhite adwinwhite commented Oct 10, 2026 •

Copy link
Copy Markdown
Contributor

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.

This PR adds a field to mir::Body to track the typing mode.

r? lcnr

@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Oct 10, 2026
@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

Comment thread compiler/rustc_middle/src/mir/mod.rs Outdated
match self.phase {
MirPhase::Built | MirPhase::Analysis(_) => TypingEnv::new(
match self.typing_mode {
BodyTypingMode::Borrowck | BodyTypingMode::PostBorrowck => TypingEnv::new(

@lcnr lcnr Oct 12, 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.

when do we encounter this?

View changes since the review

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.

The initial mode is BodyTypingMode::Borrowck. After mir_borrowck, we set it to PostBorrowck.

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.

promote_consts pass would use BodyTypingMode::Borrowck since it's run before borrowck.

/// us to see the difference and forego optimization on the inlined promoted items.
pub phase: MirPhase,

pub typing_mode: BodyTypingMode,

@lcnr lcnr Oct 12, 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.

comment please :>

View changes since the review

Comment thread compiler/rustc_middle/src/mir/mod.rs Outdated
StableHash,
TypeFoldable,
TypeVisitable
)]

@lcnr lcnr Oct 12, 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.

should also have a comment, or maybe just change the field to go "see [BodyTypingMode] for more info"

also, mind splitting the derive in two so that it doesn't get spread out like this

View changes since the review

@adwinwhite
adwinwhite force-pushed the phase-hack branch 2 times, most recently from 5e23058 to 23a44a9 Compare October 19, 2026 07:51
@adwinwhite
adwinwhite marked this pull request as ready for review October 19, 2026 07:53
@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Oct 19, 2026
@rustbot

rustbot commented Oct 19, 2026

Copy link
Copy Markdown
Collaborator

Some changes occurred to MIR optimizations

cc @rust-lang/wg-mir-opt

@rustbot rustbot removed the S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. label Oct 19, 2026
@rust-bors

This comment has been minimized.

Comment thread compiler/rustc_middle/src/mir/mod.rs Outdated
#[derive(Copy, Clone, PartialEq, Debug)]
#[derive(TyEncodable, TyDecodable, StableHash, TypeFoldable, TypeVisitable)]
pub enum BodyTypingMode {
Borrowck,

@lcnr lcnr Oct 29, 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.

Suggested change
Borrowck,
PostTypeckThroughBorrowck,

? 🤔

View changes since the review

Comment thread compiler/rustc_middle/src/mir/mod.rs Outdated
// 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.
TypingEnv::post_analysis(tcx, self.source.def_id())

@lcnr lcnr Oct 29, 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.

should this be the case or could we change drop glue and fn ptr shims to just start out in TypingMode::PostAnalysis?

View changes since the review

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.

We already use PostAnalysis mode for them. I guess this branch is actually useless.

Comment thread compiler/rustc_mir_transform/src/lib.rs Outdated

debug!("runtime_mir_cleanup({:?})", did);
run_runtime_cleanup_passes(tcx, body);
assert!(body.typing_mode == BodyTypingMode::PostAnalysis, "{:?}", body.typing_mode);

@lcnr lcnr Oct 29, 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.

why no assert_eq! here?

also gives free good ICE diagnostics 🤔

View changes since the review

// We try to guard against future MIR building procedures that don't go through the usual
// lowering. The final validation is expected to run in `TypingMode::PostAnalysis`.
if new_phase == MirPhase::Runtime(RuntimePhase::Optimized) {
assert_eq!(body.typing_mode, BodyTypingMode::PostAnalysis,);

@lcnr lcnr Oct 29, 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.

Suggested change
assert_eq!(body.typing_mode, BodyTypingMode::PostAnalysis,);
assert_eq!(body.typing_mode, BodyTypingMode::PostAnalysis);

View changes since the review

@lcnr

lcnr commented Oct 29, 2026

Copy link
Copy Markdown
Contributor

@rustbot author

@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Oct 29, 2026
@rustbot

rustbot commented Oct 29, 2026

Copy link
Copy Markdown
Collaborator

Reminder, once the PR becomes ready for a review, use @rustbot ready.

@rustbot

rustbot commented Oct 31, 2026

Copy link
Copy Markdown
Collaborator

This PR was rebased onto a different main commit. Here's a range-diff highlighting what actually changed.

Rebasing is a normal part of keeping PRs up to date, so no action is needed—this note is just to help reviewers.

@rust-log-analyzer

This comment has been minimized.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants