Skip to content

Add Wrap MIR projection - #166666

Open
Jules-Bertholet wants to merge 3 commits into
rust-lang:mainfrom
Jules-Bertholet:transmute-projection
Open

Jules-Bertholet wants to merge 3 commits into
rust-lang:mainfrom
Jules-Bertholet:transmute-projection

Conversation

@Jules-Bertholet

@Jules-Bertholet Jules-Bertholet commented Nov 27, 2026 •

Copy link
Copy Markdown
Contributor

Split out from #165010

Adds a new Wrap MIR projection to wrap a place with a particular type constructor.

Currently, the only constructor supported is MaybeDangling, needed for #165010. MaybeUninit could be added as another option in the future (it would be handy for safely reading union fields).

r? @RalfJung

@rustbot

rustbot commented Nov 27, 2026

Copy link
Copy Markdown
Collaborator

Some changes occurred to the CTFE / Miri interpreter

cc @rust-lang/miri

clippy is developed in its own repository. If possible, consider making this change to rust-lang/rust-clippy instead.

cc @rust-lang/clippy

This PR changes MIR

cc @oli-obk, @RalfJung, @JakobDegen, @vakaras

This PR changes rustc_public

cc @oli-obk, @celinval, @ouz-a, @makai410

Some changes occurred to constck

cc @fee1-dead

Some changes occurred to the CTFE machinery

cc @RalfJung, @oli-obk, @lcnr

rustc_codegen_cranelift is developed in its own repository. If possible, consider making this change to rust-lang/rustc_codegen_cranelift instead.

cc @bjorn3

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-clippy Relevant to the Clippy team. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Nov 27, 2026
@rustbot

rustbot commented Nov 27, 2026

Copy link
Copy Markdown
Collaborator

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

@RalfJung

Copy link
Copy Markdown
Member

Cc @oli-obk as the reviewer of #130514 -- is there any pitfall around this MIR projection I should be aware of, where just making it a more general Transmute could become a problem?

@RalfJung RalfJung left a comment •

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.

MIR syntax, interpreter, and so on all look good.
But there's a borrowck diff here that doesn't look trivial and I don't know that code at all.

View changes since this review

@Jules-Bertholet
Jules-Bertholet marked this pull request as draft November 27, 2026 23:01
@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 Nov 27, 2026
@Jules-Bertholet
Jules-Bertholet force-pushed the transmute-projection branch 2 times, most recently from e3f408f to ac05035 Compare November 28, 2026 04:22
@Jules-Bertholet Jules-Bertholet changed the title Replace UnwrapUnsafeBinder MIR projection with Transmute Add Wrap MIR projection Nov 28, 2026
@Jules-Bertholet

Copy link
Copy Markdown
Contributor Author

@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 Nov 28, 2026
@Jules-Bertholet
Jules-Bertholet marked this pull request as ready for review November 28, 2026 04:31
@RalfJung

Copy link
Copy Markdown
Member

My proposal would have been to keep UnwrapUnsafeBinder and add a Transmute projection. Borrowck would disallow Transmute projections (just like it disallows most regular transmutes). Do you think that would work? Or do you need the projection to exist pre-borrowck?

@Jules-Bertholet

Copy link
Copy Markdown
Contributor Author

Or do you need the projection to exist pre-borrowck?

Not for #165010, but other potential uses of this projection I have in mind would need it.

@RalfJung

Copy link
Copy Markdown
Member

Pre-borrowck transmutes are extremely dangerous... well just like using mem::transmute I guess, as input and output lifetimes are entirely unrelated. That's basically never what we want.
Anyway you could have it panic in borrowck for now and then relax that later if needed.

@RalfJung RalfJung left a comment •

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.

Thanks! I still like the idea of a Transmute projection, but I can also live with this, modulo the comments I left.

View changes since this review

Comment thread compiler/rustc_borrowck/src/lib.rs Resolved
Comment thread compiler/rustc_middle/src/mir/syntax.rs Resolved
| ProjectionElem::Wrap(_) => {
// We don't capture array-access projections.
// We can stop here as arrays are captured completely.
// `Wrap` projections currently have no HIR equivalent.

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.

It is entirely unclear to me why it would be okay to just break the loop on Wrap. Can you make this bug! instead?

@Jules-Bertholet Jules-Bertholet Nov 29, 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.

If Wrap is only accepted after borrowck, then bug! should work fine. There's nothing inherently wrong with breaking, though, it just signals that the Wrap MIR projection can't correspond to any part of a HIR closure capture path.

@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 Nov 29, 2026
@rustbot

rustbot commented Nov 29, 2026

Copy link
Copy Markdown
Collaborator

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

Jules-Bertholet and others added 3 commits November 29, 2026 16:44
For now, as it's not needed.
Co-authored-by: RalfJung <post@ralfj.de>
@Jules-Bertholet

Copy link
Copy Markdown
Contributor Author

@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 Nov 29, 2026

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-review Status: Awaiting review from the assignee but also interested parties. T-clippy Relevant to the Clippy team. 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.

3 participants