Skip to content

treat coroutines like implicitly UnsafePinned (and therefore UnsafeCell) - #165300

Draft
RalfJung wants to merge 1 commit into
rust-lang:mainfrom
RalfJung:coroutine-implicit-unsafe-pinned
Draft

RalfJung wants to merge 1 commit into
rust-lang:mainfrom
RalfJung:coroutine-implicit-unsafe-pinned

Conversation

@RalfJung

@RalfJung RalfJung commented Nov 1, 2026 •

Copy link
Copy Markdown
Member

This is meant to finally properly fix #63818 by treating all corotuines as big UnsafePinned/UnsafeCell in their own right, without actually changing the types of the fields.

This does mean that references created to those fields still behave according to the normal aliasing rules. I think that's fine? The concern here is a self-referential future like

let mut field = 0;
let mutref = &mut field;
something.await;
dbg!(*mutref);

Nothing must alias mutref for its lifetime. Today we have aliases in the form of &mut to the future itself. This PR basically removes those aliases by not attaching any assumptions to those outer &mut.

Status: incomplete!
The Freeze and UnsafeUnpin traits don't honor the new logic yet.
Also RA seems to have some logic that checks for UnsafeCell (for layout computation, IIUC) which may need updating.

Zulip discussion

@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. labels Nov 1, 2026
@rust-log-analyzer

This comment has been minimized.

@RalfJung
RalfJung force-pushed the coroutine-implicit-unsafe-pinned branch from 39fd433 to f83ff53 Compare November 1, 2026 21:52
@rustbot rustbot added the T-clippy Relevant to the Clippy team. label Nov 1, 2026
@rust-log-analyzer

This comment has been minimized.

@rust-bors

This comment has been minimized.

@RalfJung
RalfJung force-pushed the coroutine-implicit-unsafe-pinned branch 2 times, most recently from 193d78f to 7be97e3 Compare November 13, 2026 19:47
@rustbot rustbot added the A-LLVM Area: Code generation parts specific to LLVM. Both correctness bugs and optimization-related issues. label Nov 13, 2026
@RalfJung
RalfJung force-pushed the coroutine-implicit-unsafe-pinned branch from 7be97e3 to 193d78f Compare November 13, 2026 19:47
@lcnr

lcnr commented Nov 24, 2026

Copy link
Copy Markdown
Contributor

the builtin impls need to be disabled here

ty::Coroutine(def_id, _)
if self
.cx()
.is_trait_lang_item(goal.predicate.def_id(), SolverTraitLangItem::Unpin) =>
{
match self.cx().coroutine_movability(def_id) {
Movability::Static => Some(Err(NoSolution.into())),
Movability::Movable => Some(
self.probe_builtin_trait_candidate(BuiltinImplSource::Misc).enter(|ecx| {
ecx.evaluate_added_goals_and_make_canonical_response(Certainty::Yes)
}),
),
}
}

and for old solver

ty::Coroutine(coroutine_def_id, _) => {
if self.tcx().is_lang_item(def_id, LangItem::Unpin) {
match self.tcx().coroutine_movability(coroutine_def_id) {
hir::Movability::Static => {
// Immovable coroutines are never `Unpin`, so
// suppress the normal auto-impl candidate for it.
}
hir::Movability::Movable => {
// Movable coroutines are always `Unpin`, so add an
// unconditional builtin candidate with no sub-obligations.
candidates.vec.push(BuiltinCandidate);
}
}
} else {
if self.should_stall_coroutine(coroutine_def_id) {
candidates.ambiguous = true;
} else {
// Coroutines implement all other auto traits normally.
candidates.vec.push(AutoImplCandidate);
}
}
}

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

A-LLVM Area: Code generation parts specific to LLVM. Both correctness bugs and optimization-related issues. S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. 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.

Resolve unsound interaction between noalias and self-referential data (incl. generators, async fn)

4 participants