Skip to content

UnsafePinned: implement coroutine lowering changes#152946

Open
b-naber wants to merge 6 commits intorust-lang:mainfrom
b-naber:coroutine-unsafe-pinned
Open

UnsafePinned: implement coroutine lowering changes#152946
b-naber wants to merge 6 commits intorust-lang:mainfrom
b-naber:coroutine-unsafe-pinned

Conversation

@b-naber
Copy link
Copy Markdown
Contributor

@b-naber b-naber commented Feb 21, 2026

View all comments

Implements the lowering part for #125735

This introduces a visitor that looks for self-referential fields in the coroutine struct and wraps all of these types into UnsafePinned. I've also included a commit where we change the condition for using noalias. That change is based on my understanding of the RFC, but is likely wrong given the change in #151365. I've included it for now to further discuss this.

cc @RalfJung

@rustbot
Copy link
Copy Markdown
Collaborator

rustbot commented Feb 21, 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 Feb 21, 2026
@rustbot
Copy link
Copy Markdown
Collaborator

rustbot commented Feb 21, 2026

r? @mati865

rustbot has assigned @mati865.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

Why was this reviewer chosen?

The reviewer was selected based on:

  • Owners of files modified in this PR: compiler, mir, mir-opt
  • compiler, mir, mir-opt expanded to 68 candidates
  • Random selection from 15 candidates

@rust-log-analyzer

This comment has been minimized.

@rust-cloud-vms rust-cloud-vms Bot force-pushed the coroutine-unsafe-pinned branch from 50482bd to b226e46 Compare February 21, 2026 20:58
Comment thread compiler/rustc_ty_utils/src/abi.rs Outdated
Comment thread compiler/rustc_ty_utils/src/abi.rs Outdated
Zalathar added a commit to Zalathar/rust that referenced this pull request Feb 23, 2026
…rk-Simulacrum

extend unpin noalias tests to cover mutable references

rust-lang#152946 made a change to the logic for this attribute that the test should have flagged as problematic -- but the test only checked `Box`, not `&mut`, and those have independent code paths. So extend the test to also cover `&mut`.

@b-naber would be nice if you could confirm that the added tests do fail with your PR.
rust-timer added a commit that referenced this pull request Feb 23, 2026
Rollup merge of #152970 - RalfJung:unpin-noalias-tests, r=Mark-Simulacrum

extend unpin noalias tests to cover mutable references

#152946 made a change to the logic for this attribute that the test should have flagged as problematic -- but the test only checked `Box`, not `&mut`, and those have independent code paths. So extend the test to also cover `&mut`.

@b-naber would be nice if you could confirm that the added tests do fail with your PR.
@mati865
Copy link
Copy Markdown
Member

mati865 commented Feb 23, 2026

I'm not a good fit for this code, perhaps r? @RalfJung ?

Feel free to reroll

@rustbot rustbot assigned RalfJung and unassigned mati865 Feb 23, 2026
@rustbot
Copy link
Copy Markdown
Collaborator

rustbot commented Feb 23, 2026

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

@RalfJung
Copy link
Copy Markdown
Member

Definitely not, I don't know the coroutine lowering code at all.

@rustbot reroll

@rustbot rustbot assigned adwinwhite and unassigned RalfJung Feb 23, 2026
@b-naber
Copy link
Copy Markdown
Contributor Author

b-naber commented Feb 23, 2026

@cjgillot Could you maybe review this?

@rust-cloud-vms rust-cloud-vms Bot force-pushed the coroutine-unsafe-pinned branch from b226e46 to d8b3c3d Compare February 23, 2026 17:46
@rustbot
Copy link
Copy Markdown
Collaborator

rustbot commented Feb 23, 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.

@rust-log-analyzer

This comment has been minimized.

@rust-cloud-vms rust-cloud-vms Bot force-pushed the coroutine-unsafe-pinned branch from d8b3c3d to 3d40415 Compare February 23, 2026 20:28
@cjgillot cjgillot self-assigned this Feb 24, 2026
RalfJung pushed a commit to RalfJung/miri that referenced this pull request Feb 24, 2026
…crum

extend unpin noalias tests to cover mutable references

rust-lang/rust#152946 made a change to the logic for this attribute that the test should have flagged as problematic -- but the test only checked `Box`, not `&mut`, and those have independent code paths. So extend the test to also cover `&mut`.

@b-naber would be nice if you could confirm that the added tests do fail with your PR.
@adwinwhite adwinwhite removed their assignment Mar 4, 2026
@b-naber
Copy link
Copy Markdown
Contributor Author

b-naber commented Mar 6, 2026

@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 Mar 6, 2026
@b-naber
Copy link
Copy Markdown
Contributor Author

b-naber commented Mar 11, 2026

I am very confused. I didn't expect anything nearly that complicated.^^ The compiler can already figure out that a generator has a self-referential borrow and hence cannot be gen move. Doesn't that same analysis let us figure out which fields could be borrowed across a yield point, and hence should be UnsafePinned?

(I just realized I have no idea what any of that does with raw pointers.^^)

Sorry for the confusion. I think you are right, the MaybeBorrowedLocals analysis should be sufficient to infer all self-referential fields. If a local is kept alive due to an outstanding borrow then it must be a self-referential field.

That analyis also takes into account raw pointers. We only take the information from that analysis into account when calculating the live_locals if the coroutine is !movable though. I haven't investigated the segfault example though and don't know whether the reason why that segfaults is actually related to this.

@RalfJung
Copy link
Copy Markdown
Member

RalfJung commented Mar 11, 2026 via email

@rust-cloud-vms rust-cloud-vms Bot force-pushed the coroutine-unsafe-pinned branch from 3d40415 to 30322eb Compare March 18, 2026 19:59
@rust-log-analyzer

This comment has been minimized.

@RalfJung
Copy link
Copy Markdown
Member

That test failing is a great sign! Quoting from the test description

//! FIXME: This test should pass! However, `async fn` does not yet use `UnsafePinned`.
//! This is a regression test for <https://github.com/rust-lang/rust/issues/137750>:
//! `UnsafePinned` must include the effects of `UnsafeCell`.

@rustbot
Copy link
Copy Markdown
Collaborator

rustbot commented Mar 18, 2026

The Miri subtree was changed

cc @rust-lang/miri

Comment thread src/tools/miri/tests/fail/async-shared-mutable.rs
//! FIXME: This test should pass! However, `async fn` does not yet use `UnsafePinned`.
//! This is a regression test for <https://github.com/rust-lang/rust/issues/137750>:
//! `UnsafePinned` must include the effects of `UnsafeCell`.
// run-pass
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.

This comment does nothing, please remove it. (Why did you even add this? Even in the rustc ui test suite this would do nothing. It's spelled // @run-pass there. But the Miri test suite is a little different.)

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.

There's another test that uses this: https://github.com/rust-lang/rust/blob/main/src/tools/miri/tests/pass/packed-struct-dyn-trait.rs, which is why I used this.

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.

Ah, not sure how that snuck in, thanks for pointing that out. But you'll notice most pass tests don't have such an annotation.

@rust-cloud-vms rust-cloud-vms Bot force-pushed the coroutine-unsafe-pinned branch 2 times, most recently from 3733432 to 4998cf2 Compare March 18, 2026 22:29
@rust-log-analyzer

This comment has been minimized.

@rustbot
Copy link
Copy Markdown
Collaborator

rustbot commented Mar 21, 2026

Some changes occurred in src/tools/clippy

cc @rust-lang/clippy

@rustbot rustbot added the T-clippy Relevant to the Clippy team. label Mar 21, 2026
@b-naber
Copy link
Copy Markdown
Contributor Author

b-naber commented Mar 21, 2026

@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 Mar 21, 2026
@rust-bors

This comment has been minimized.

@rust-cloud-vms rust-cloud-vms Bot force-pushed the coroutine-unsafe-pinned branch from f5e7edd to 3f2a21b Compare May 8, 2026 17:40
@rust-log-analyzer

This comment has been minimized.

@rust-cloud-vms rust-cloud-vms Bot force-pushed the coroutine-unsafe-pinned branch from 3f2a21b to 8129bdb Compare May 8, 2026 17:58
@rust-log-analyzer

This comment has been minimized.

@rust-cloud-vms rust-cloud-vms Bot force-pushed the coroutine-unsafe-pinned branch from 8129bdb to 4df37a6 Compare May 8, 2026 18:12
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.

8 participants