Skip to content

Fix malformed transmute handling during mir build#154144

Open
Human9000-bit wants to merge 5 commits intorust-lang:mainfrom
Human9000-bit:const-prop-transmute-async-fix
Open

Fix malformed transmute handling during mir build#154144
Human9000-bit wants to merge 5 commits intorust-lang:mainfrom
Human9000-bit:const-prop-transmute-async-fix

Conversation

@Human9000-bit
Copy link
Copy Markdown
Contributor

@Human9000-bit Human9000-bit commented Mar 20, 2026

View all comments

Running dataflow const prop optimization on std::mem::transmute of mismatched sizes failed the assertion in interpreter and caused ICE.

So it's better to not let those transmutations into the interpreter at all

Fixes #149920

@rustbot
Copy link
Copy Markdown
Collaborator

rustbot commented Mar 20, 2026

Some changes occurred to MIR optimizations

cc @rust-lang/wg-mir-opt

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Mar 20, 2026
@rustbot

This comment was marked as outdated.

@rust-log-analyzer

This comment has been minimized.

@Human9000-bit Human9000-bit force-pushed the const-prop-transmute-async-fix branch from 8891ca8 to 192c426 Compare March 20, 2026 16:34
@rustbot

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

@Human9000-bit Human9000-bit force-pushed the const-prop-transmute-async-fix branch from 192c426 to e8c1abe Compare March 21, 2026 04:19
@rust-log-analyzer

This comment has been minimized.

@Human9000-bit Human9000-bit force-pushed the const-prop-transmute-async-fix branch from e8c1abe to 2635118 Compare March 21, 2026 05:12
@fmease
Copy link
Copy Markdown
Member

fmease commented Mar 31, 2026

r? mir

@rustbot rustbot assigned saethlin and unassigned fmease Mar 31, 2026
Comment thread compiler/rustc_mir_transform/src/dataflow_const_prop.rs Outdated
@Human9000-bit Human9000-bit force-pushed the const-prop-transmute-async-fix branch from 2635118 to 5ccfb12 Compare March 31, 2026 04:38
@rustbot

This comment has been minimized.

@@ -0,0 +1,12 @@
error[E0512]: cannot transmute between types of different sizes, or dependently-sized types
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.

This error is emitted in typeck iirc. It should be tainting the body. If it isnt, it's likely that tcx.dcx() is used instead of infcx.dcx()

Please check if we can avoid changing anything in const prop by properly tainting wherever this error is emitted

Copy link
Copy Markdown
Contributor Author

@Human9000-bit Human9000-bit Apr 4, 2026

Choose a reason for hiding this comment

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

At first glance there isn't infcx where the error is emitted, and no obvious way to get one

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.

Where is it being emitted? If it is within the typeck query, we can probably change something further up the call stack ( you can try using -Ztreat-err-as-bug to find out where the error is emitted and what the call stack is while emitting it

Copy link
Copy Markdown
Contributor Author

@Human9000-bit Human9000-bit Apr 4, 2026

Choose a reason for hiding this comment

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

It is emitted in check_transmutes ran by run_required_analyses:

if not_typeck_child {
tcx.ensure_ok().mir_borrowck(def_id);
tcx.ensure_ok().check_transmutes(def_id);
}

and the very error emission happens here:

} else {
err.note(format!("source type: `{}` ({})", from, skeleton_string(from, sk_from)));
err.note(format!("target type: `{}` ({})", to, skeleton_string(to, sk_to)));
err.emit();
}

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.

Oh wow. Didn't know that was changed (#145469)

I'll need to think on it. Unsure why it was necessary to live in a separate query

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.

Yes that would fix this optimizer ICE, but i didn't suggest that because i assumed we'd get the same ICE in CTFE, and the only fix for that is to run the query earlier, where it would likely cycle again

Copy link
Copy Markdown
Contributor Author

@Human9000-bit Human9000-bit Apr 6, 2026

Choose a reason for hiding this comment

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

We could check at the beginning of optimized_mir that check_transmutes does not emit an error.

I tried to do that at the start of run_pass of dataflow const prop. It ICEd for def_id being a typeck child.
So we can't just slap check_transmutes for every def_id (at least in mir opts)

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.

You can invoke is_typeck_child to invoke check_transmutes on the parent (there's also a method for getting the typeck parent).

This will fix the ICE (if you also make the check_transmute query return a Result<(), ErrorReported> and don't do anything if it's an error).

Tho at that point it may be better to do what cjgillot suggested and check it at the start of thr optimized_mir query and just return a dummy body or the unoptimized body.

I'm just sceptical it fixed the ICE in general as it should be possibe to produce the ICE in const eval by evaluating code that has the same transmute problem

Copy link
Copy Markdown
Contributor

@oli-obk oli-obk Apr 6, 2026

Choose a reason for hiding this comment

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

Hmm apparently not: https://play.rust-lang.org/?version=nightly&mode=release&edition=2024&gist=31291deac2b05c040502d266b33e2f69

It seems very weird that we report an error here, but that's probably an oversight. The ctfe error should be an ICE

Copy link
Copy Markdown
Contributor Author

@Human9000-bit Human9000-bit Apr 10, 2026

Choose a reason for hiding this comment

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

Apparently your example is getting caught in well-formedness checking during HIR analysis, which uses different methods of interpreter compared to dataflow const prop opt

@saethlin saethlin assigned oli-obk and unassigned saethlin Apr 13, 2026
@Human9000-bit Human9000-bit force-pushed the const-prop-transmute-async-fix branch from 5ccfb12 to fec9c70 Compare April 13, 2026 15:32
@rustbot

This comment has been minimized.

@Human9000-bit
Copy link
Copy Markdown
Contributor Author

Follow-up idea: I am thinking it would be nice to add arena_cache modifier for check_trasnmutes query: Result<(), ErrorGuaranteed> is 1 byte size, and the query is now being reused.
Though it requires Result<(), ErrorGuaranteed> impl ArenaCache, so that's why it should better be in another PR.

@JonathanBrouwer
Copy link
Copy Markdown
Contributor

@bors try @rust-timer queue

@rust-timer

This comment has been minimized.

@rustbot rustbot added the S-waiting-on-perf Status: Waiting on a perf run to be completed. label Apr 13, 2026
@rust-bors

This comment has been minimized.

rust-bors Bot pushed a commit that referenced this pull request Apr 13, 2026
… r=<try>

Fix dataflow const prop behavior when propagating malsized transmutes
@Human9000-bit
Copy link
Copy Markdown
Contributor Author

@oli-obk I tried the approach with checking transmutes another time before optimizing MIR, the perf results should be ready soon

Copy link
Copy Markdown
Contributor

@oli-obk oli-obk left a comment

Choose a reason for hiding this comment

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

I think you should be able to do your check even earlier (in mir_drops_elaborated_and_const_checked), as long as you do it after borrowck is ensured

View changes since this review

Comment thread compiler/rustc_mir_transform/src/lib.rs Outdated
Comment thread compiler/rustc_mir_transform/src/lib.rs Outdated
@rustbot rustbot removed the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Apr 14, 2026
@rustbot
Copy link
Copy Markdown
Collaborator

rustbot commented Apr 14, 2026

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

@rustbot rustbot added the S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. label Apr 14, 2026
@oli-obk
Copy link
Copy Markdown
Contributor

oli-obk commented Apr 14, 2026

thinking it would be nice to add arena_cache modifier for check_trasnmutes query: Result<(), ErrorGuaranteed> is 1 byte size, and the query is now being reused.
Though it requires Result<(), ErrorGuaranteed> impl ArenaCache, so that's why it should better be in another PR.

That query modifier is for large query results: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_middle/query/modifiers/struct.arena_cache.html

I think using it may even cause perf regressions

@Human9000-bit Human9000-bit force-pushed the const-prop-transmute-async-fix branch from fec9c70 to 67933dd Compare April 15, 2026 12:26
@rust-log-analyzer

This comment has been minimized.

@Human9000-bit Human9000-bit force-pushed the const-prop-transmute-async-fix branch from 67933dd to 51a2a9f Compare April 15, 2026 14:18
@rust-log-analyzer

This comment has been minimized.

@Human9000-bit
Copy link
Copy Markdown
Contributor Author

It looks like tainting a body inside mir_drops_elaborated_and_const_checked disables some const eval analysis later on.
The error disappears if I move transmutes check into optimized_mir_inner

@Human9000-bit
Copy link
Copy Markdown
Contributor Author

Though missing error messages don't seem very much informative to me, so we wouldn't lose much if we just bless these tests ig
@oli-obk what do you think?

@Human9000-bit Human9000-bit changed the title Fix dataflow const prop behavior when propagating malsized transmutes Fix mallformed transmute handling during mir build Apr 17, 2026
@Human9000-bit Human9000-bit changed the title Fix mallformed transmute handling during mir build Fix malformed transmute handling during mir build Apr 17, 2026
@rust-bors

This comment has been minimized.

@Human9000-bit Human9000-bit force-pushed the const-prop-transmute-async-fix branch from 51a2a9f to 9161e6f Compare April 21, 2026 11:54
@rustbot
Copy link
Copy Markdown
Collaborator

rustbot commented Apr 21, 2026

Some changes occurred in src/tools/cargo

cc @ehuss

@rustbot
Copy link
Copy Markdown
Collaborator

rustbot commented Apr 21, 2026

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.

@rustbot

This comment has been minimized.

@Human9000-bit Human9000-bit force-pushed the const-prop-transmute-async-fix branch 2 times, most recently from 55366e3 to bb85aa2 Compare April 21, 2026 12:06
@Human9000-bit Human9000-bit force-pushed the const-prop-transmute-async-fix branch from bb85aa2 to 98494cf Compare April 21, 2026 12:24
@rust-log-analyzer

This comment has been minimized.

@Human9000-bit Human9000-bit force-pushed the const-prop-transmute-async-fix branch from 98494cf to 48ed07a Compare April 21, 2026 15:20
@rust-log-analyzer

This comment has been minimized.

@Human9000-bit Human9000-bit force-pushed the const-prop-transmute-async-fix branch from 48ed07a to 694c138 Compare April 21, 2026 17:32
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.

ICE invalid immediate for given destination place: scalar value has wrong size

9 participants