Skip to content

Implement arbitrary casts in custom mir - #160860

Merged
rust-bors[bot] merged 2 commits into
rust-lang:mainfrom
maxdexh:custom-mir-casts
Sep 11, 2026
Merged

Implement arbitrary casts in custom mir#160860
rust-bors[bot] merged 2 commits into
rust-lang:mainfrom
maxdexh:custom-mir-casts

Conversation

@maxdexh

@maxdexh maxdexh commented Aug 10, 2026

Copy link
Copy Markdown
Member

View all comments

See zulip

Implement custom casts using an enum in intrinsics::mir that closely matches the CastKind enum in the compiler.

We have 5 different ways to cast in custom MIR, 3 with functions (which hard code a cast kind), 1 implicit coerce (which only does subtyping i think), 1 with as (which does a few more). They are all different and the error messages don't tell you what to do. Some are (i think) not possible at all.

This came up while testing miri. We currently have to guess the casting syntax until we find the one that generates the MIR we want. This PR changes that by adding a custom mir operation that allows picking an arbitrary cast.

r? RalfJung

@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. T-libs Relevant to the library team, which will review and decide on the PR/issue. labels Aug 10, 2026
@rustbot

rustbot commented Aug 10, 2026

Copy link
Copy Markdown
Collaborator

RalfJung is not on the review rotation at the moment.
They may take a while to respond.

@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

@maxdexh

This comment was marked as resolved.

@rust-bors

This comment was marked as resolved.

@RalfJung

Copy link
Copy Markdown
Member

I am on vacation this week, so this will have to wait a bit. Or maybe @saethlin has time to look at it earlier.

@maxdexh

maxdexh commented Aug 11, 2026

Copy link
Copy Markdown
Member Author

This is not done yet anyway, I still gotta figure out how to unflatten the enum and write some tests. But thanks for letting me know :)

Comment thread tests/ui/hygiene/unpretty-debug-lifetimes.stdout
@maxdexh

maxdexh commented Aug 16, 2026

Copy link
Copy Markdown
Member Author

I completely forgot about this thing, I'll get back to it later or tomorrow.

@rust-log-analyzer

This comment has been minimized.

@maxdexh
maxdexh force-pushed the custom-mir-casts branch 2 times, most recently from 1b4e3de to a85dda1 Compare August 18, 2026 09:24
@maxdexh
maxdexh marked this pull request as ready for review August 18, 2026 09:46
@rustbot

rustbot commented Aug 18, 2026

Copy link
Copy Markdown
Collaborator

Some changes occurred to the intrinsics. Make sure the CTFE / Miri interpreter
gets adapted for the changes, if necessary.

cc @rust-lang/miri, @RalfJung, @oli-obk, @lcnr

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

@maxdexh maxdexh Aug 18, 2026

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Note: If this is named Unsize, then std::marker::Unsized doesn't get abbreviated to Unsize in diagnostics anymore.

View changes since the review

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

then std::marker::Unsized doesn't get abbreviated to Unsize in diagnostics anymore.

Did you mean "abbreviated to Unsized"?

Please file an issue for this... it is strange how adding unstable items changes diagnostics for stable code.

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.

to my knowledge we already do this correctly, it affects nightly code, but a stable compiler will not consider unstable items in the diagnostics

@RalfJung

RalfJung commented Sep 8, 2026

Copy link
Copy Markdown
Member

I'm afraid this is a bit too much for me right now, sorry.
r? @oli-obk

@rustbot rustbot assigned oli-obk and unassigned RalfJung Sep 8, 2026

@oli-obk oli-obk left a comment

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.

@rust-bors

rust-bors Bot commented Sep 10, 2026

Copy link
Copy Markdown
Contributor

📌 Commit a85dda1 has been approved by oli-obk

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.

Reason for tree closure: git config problem again

@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-review Status: Awaiting review from the assignee but also interested parties. labels Sep 10, 2026
JonathanBrouwer added a commit to JonathanBrouwer/rust that referenced this pull request Sep 10, 2026
Implement arbitrary casts in custom mir

[See zulip](https://rust-lang.zulipchat.com/#narrow/channel/131828-t-compiler/topic/extending.20custom.20MIR/with/613727602)

Implement custom casts using an enum in `intrinsics::mir` that closely matches the `CastKind` enum in the compiler.

We have 5 different ways to cast in custom MIR, 3 with functions (which hard code a cast kind), 1 implicit coerce (which only does subtyping i think), 1 with `as` (which does a few more). They are all different and the error messages don't tell you what to do. Some are (i think) not possible at all.

This came up [while testing miri](https://rust-lang.zulipchat.com/#narrow/channel/136281-t-opsem/topic/Valdity.20checks.20for.20references/near/613662285). We currently have to guess the casting syntax until we find the one that generates the MIR we want. This PR changes that by adding a custom mir operation that allows picking an arbitrary cast.

r? RalfJung
@JonathanBrouwer

Copy link
Copy Markdown
Member

💔 I suspect this PR failed tests as part of a rollup
@bors r-

After fixing the problem, consider running a try job for the failed job before re-approving.

Link to failure: #162596 (comment)

@rust-bors rust-bors Bot 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-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. labels Sep 10, 2026
@rust-bors

rust-bors Bot commented Sep 10, 2026

Copy link
Copy Markdown
Contributor

This pull request was unapproved.

This PR was contained in a rollup (#162596), which was unapproved.

View changes since this unapproval

@maxdexh

maxdexh commented Sep 10, 2026

Copy link
Copy Markdown
Member Author

Yeah, the symbol numbering stuff again...

@maxdexh

maxdexh commented Sep 10, 2026

Copy link
Copy Markdown
Member Author

So this is the same thing as what happened earlier: #160860 (comment)

For some reason, adding symbols sometimes causes the output on that test to gain an extra line break (at least that's what I think is happening)

@rustbot

rustbot commented Sep 10, 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.

@maxdexh

maxdexh commented Sep 10, 2026

Copy link
Copy Markdown
Member Author

I just blessed the output, idk what else I can do about this /shrug
@rustbot ready

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

Copy link
Copy Markdown
Member

That test has

// Don't break whenever Symbol numbering changes
//@ normalize-stdout: "\d+#" -> "0#"

So there's a lot more that changes in that test and it gets normalized away so we don't see it. The normalization is incomplete however so we see the newline change.

@oli-obk

oli-obk commented Sep 11, 2026

Copy link
Copy Markdown
Contributor

@bors r+

@rust-bors

rust-bors Bot commented Sep 11, 2026

Copy link
Copy Markdown
Contributor

📌 Commit 676862d has been approved by oli-obk

It is now in the queue for this repository.

@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-review Status: Awaiting review from the assignee but also interested parties. labels Sep 11, 2026
Zalathar added a commit to Zalathar/rust that referenced this pull request Sep 11, 2026
Implement arbitrary casts in custom mir

[See zulip](https://rust-lang.zulipchat.com/#narrow/channel/131828-t-compiler/topic/extending.20custom.20MIR/with/613727602)

Implement custom casts using an enum in `intrinsics::mir` that closely matches the `CastKind` enum in the compiler.

We have 5 different ways to cast in custom MIR, 3 with functions (which hard code a cast kind), 1 implicit coerce (which only does subtyping i think), 1 with `as` (which does a few more). They are all different and the error messages don't tell you what to do. Some are (i think) not possible at all.

This came up [while testing miri](https://rust-lang.zulipchat.com/#narrow/channel/136281-t-opsem/topic/Valdity.20checks.20for.20references/near/613662285). We currently have to guess the casting syntax until we find the one that generates the MIR we want. This PR changes that by adding a custom mir operation that allows picking an arbitrary cast.

r? RalfJung
rust-bors Bot pushed a commit that referenced this pull request Sep 11, 2026
…uwer

Rollup of 11 pull requests

Successful merges:

 - #162520 (Refactor `HygieneEncodeContext`)
 - #154373 (Skip linting unused braces for FunctionArg and MethodArg context for 2024 later )
 - #160023 (Refactor the way cg_ssa handles indirect returns (returns via `sret`) — Take 2)
 - #160860 (Implement arbitrary casts in custom mir)
 - #161482 (Use attribute parser for `#[non_exhaustive]` attribute check)
 - #161867 (deeper `rustc_builtin_macros` cleanups)
 - #162099 (small refactor of doc attribute arguments warnings)
 - #162541 (split `macroless_generic_const_args` in two)
 - #162549 (The fuchsia team maintains `riscv64gc-unknown-fuchsia`)
 - #162577 (Fix `i686-pc-windows-msvc` platform support docs and target spec metadata)
 - #162624 (regression test for opaque field projection in closure capture)
@rust-bors
rust-bors Bot merged commit 9e9dc37 into rust-lang:main Sep 11, 2026
13 checks passed
@rustbot rustbot added this to the 1.100.0 milestone Sep 11, 2026
rust-bors Bot pushed a commit that referenced this pull request Sep 11, 2026
Rollup merge of #160860 - maxdexh:custom-mir-casts, r=oli-obk

Implement arbitrary casts in custom mir

[See zulip](https://rust-lang.zulipchat.com/#narrow/channel/131828-t-compiler/topic/extending.20custom.20MIR/with/613727602)

Implement custom casts using an enum in `intrinsics::mir` that closely matches the `CastKind` enum in the compiler.

We have 5 different ways to cast in custom MIR, 3 with functions (which hard code a cast kind), 1 implicit coerce (which only does subtyping i think), 1 with `as` (which does a few more). They are all different and the error messages don't tell you what to do. Some are (i think) not possible at all.

This came up [while testing miri](https://rust-lang.zulipchat.com/#narrow/channel/136281-t-opsem/topic/Valdity.20checks.20for.20references/near/613662285). We currently have to guess the casting syntax until we find the one that generates the MIR we want. This PR changes that by adding a custom mir operation that allows picking an arbitrary cast.

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

Labels

S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-libs Relevant to the library team, which will review and decide on the PR/issue.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants