Skip to content

Fix malformed transmute handling during mir build#154144

Merged
rust-bors[bot] merged 1 commit into
rust-lang:mainfrom
Human9000-bit:const-prop-transmute-async-fix
Jun 3, 2026
Merged

Fix malformed transmute handling during mir build#154144
rust-bors[bot] merged 1 commit into
rust-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

@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
@rust-bors

This comment has been minimized.

It used to just error on wrong transmutes, without any body tainting.
That resulted in ICE during subsequent MIR optimizations.
Fixed by tainting the body right before the MIR optimizations
@Human9000-bit Human9000-bit force-pushed the const-prop-transmute-async-fix branch from 694c138 to fa6e163 Compare June 1, 2026 06:40
@rustbot
Copy link
Copy Markdown
Collaborator

rustbot commented Jun 1, 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.

@cjgillot
Copy link
Copy Markdown
Contributor

cjgillot commented Jun 2, 2026

r=me if perf is clean

@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 Jun 2, 2026
@rust-bors

This comment has been minimized.

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

Fix malformed transmute handling during mir build
@rust-bors
Copy link
Copy Markdown
Contributor

rust-bors Bot commented Jun 3, 2026

☀️ Try build successful (CI)
Build commit: 3cd4ee9 (3cd4ee9bcdc9171640486660ba26e388f1c35ebe, parent: 20e19eeac0c548e31f84d3914e0ce17cde618119)

@rust-timer

This comment has been minimized.

@rust-timer
Copy link
Copy Markdown
Collaborator

Finished benchmarking commit (3cd4ee9): comparison URL.

Overall result: ✅ improvements - no action needed

Benchmarking means the PR may be perf-sensitive. Consider adding rollup=never if this change is not fit for rolling up.

@rustbot label: -S-waiting-on-perf -perf-regression

Instruction count

Our most reliable metric. Used to determine the overall result above. However, even this metric can be noisy.

mean range count
Regressions ❌
(primary)
- - 0
Regressions ❌
(secondary)
- - 0
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
-1.2% [-1.2%, -1.2%] 1
All ❌✅ (primary) - - 0

Max RSS (memory usage)

Results (primary -3.9%)

A less reliable metric. May be of interest, but not used to determine the overall result above.

mean range count
Regressions ❌
(primary)
- - 0
Regressions ❌
(secondary)
- - 0
Improvements ✅
(primary)
-3.9% [-7.5%, -1.0%] 3
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) -3.9% [-7.5%, -1.0%] 3

Cycles

Results (secondary 2.9%)

A less reliable metric. May be of interest, but not used to determine the overall result above.

mean range count
Regressions ❌
(primary)
- - 0
Regressions ❌
(secondary)
2.9% [2.9%, 2.9%] 1
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) - - 0

Binary size

This perf run didn't have relevant results for this metric.

Bootstrap: 511.673s -> 511.063s (-0.12%)
Artifact size: 400.63 MiB -> 400.67 MiB (0.01%)

@rustbot rustbot removed the S-waiting-on-perf Status: Waiting on a perf run to be completed. label Jun 3, 2026
@oli-obk
Copy link
Copy Markdown
Contributor

oli-obk commented Jun 3, 2026

@bors r=cjgillot

@rust-bors
Copy link
Copy Markdown
Contributor

rust-bors Bot commented Jun 3, 2026

📌 Commit fa6e163 has been approved by cjgillot

It is now in the queue for this repository.

🌲 The tree is currently closed for pull requests below priority 5. This pull request will be tested once the tree is reopened.

@rust-bors rust-bors Bot added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Jun 3, 2026
@JonathanBrouwer
Copy link
Copy Markdown
Contributor

@bors p=6
Allowing this to run with closed tree

@rust-bors

This comment has been minimized.

@rust-bors rust-bors Bot added merged-by-bors This PR was explicitly merged by bors. and removed S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. labels Jun 3, 2026
@rust-bors
Copy link
Copy Markdown
Contributor

rust-bors Bot commented Jun 3, 2026

☀️ Test successful - CI
Approved by: cjgillot
Duration: 3h 11m 6s
Pushing 53509ca to main...

@rust-bors rust-bors Bot merged commit 53509ca into rust-lang:main Jun 3, 2026
14 checks passed
@rustbot rustbot added this to the 1.98.0 milestone Jun 3, 2026
@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented Jun 3, 2026

What is this? This is an experimental post-merge analysis report that shows differences in test outcomes between the merged PR and its parent PR.

Comparing d595fce (parent) -> 53509ca (this PR)

Test differences

Show 4 test diffs

Stage 1

  • [ui] tests/ui/transmute/transmute-in-async-fn.rs: [missing] -> pass (J0)

Stage 2

  • [ui] tests/ui/transmute/transmute-in-async-fn.rs: [missing] -> pass (J1)

Additionally, 2 doctest diffs were found. These are ignored, as they are noisy.

Job group index

Test dashboard

Run

cargo run --manifest-path src/ci/citool/Cargo.toml -- \
    test-dashboard 53509ca37e3b507887607c2f4a7f23bd4838f099 --output-dir test-dashboard

And then open test-dashboard/index.html in your browser to see an overview of all executed tests.

Job duration changes

  1. dist-aarch64-apple: 2h 17m -> 1h 26m (-36.8%)
  2. dist-powerpc64le-linux-gnu: 1h 16m -> 1h 44m (+36.8%)
  3. dist-powerpc64-linux-musl: 1h 14m -> 1h 41m (+36.3%)
  4. dist-x86_64-apple: 2h 13m -> 1h 35m (-28.4%)
  5. x86_64-msvc-2: 1h 56m -> 2h 29m (+28.3%)
  6. dist-sparcv9-solaris: 1h 15m -> 1h 35m (+26.6%)
  7. x86_64-msvc-ext1: 2h 11m -> 1h 40m (-23.3%)
  8. x86_64-gnu-aux: 2h 19m -> 1h 49m (-21.3%)
  9. dist-various-2: 35m 24s -> 42m 30s (+20.1%)
  10. aarch64-apple: 3h 27m -> 2h 46m (-19.7%)
How to interpret the job duration changes?

Job durations can vary a lot, based on the actual runner instance
that executed the job, system noise, invalidated caches, etc. The table above is provided
mostly for t-infra members, for simpler debugging of potential CI slow-downs.

@rust-timer
Copy link
Copy Markdown
Collaborator

Finished benchmarking commit (53509ca): comparison URL.

Overall result: no relevant changes - no action needed

@rustbot label: -perf-regression

Instruction count

This perf run didn't have relevant results for this metric.

Max RSS (memory usage)

Results (primary -0.9%)

A less reliable metric. May be of interest, but not used to determine the overall result above.

mean range count
Regressions ❌
(primary)
1.9% [1.9%, 1.9%] 1
Regressions ❌
(secondary)
- - 0
Improvements ✅
(primary)
-3.8% [-3.8%, -3.8%] 1
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) -0.9% [-3.8%, 1.9%] 2

Cycles

Results (secondary 6.2%)

A less reliable metric. May be of interest, but not used to determine the overall result above.

mean range count
Regressions ❌
(primary)
- - 0
Regressions ❌
(secondary)
10.3% [1.3%, 19.7%] 4
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
-2.2% [-2.3%, -2.1%] 2
All ❌✅ (primary) - - 0

Binary size

This perf run didn't have relevant results for this metric.

Bootstrap: 509.701s -> 509.672s (-0.01%)
Artifact size: 400.67 MiB -> 400.72 MiB (0.01%)

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

Labels

merged-by-bors This PR was explicitly merged by bors. 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