rust-lang / rust-lang/rust

Coroutine/Future `Send` bounds are too restrictive when taking references to `!Sync` values

Open
#132,382 4 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

A-coroutines C-bug E-needs-investigation T-compiler T-lang WG-async
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

Description

I tried this code:

fn main() {
    let foo: &dyn Send = &async {
        let cell = &std::cell::Cell::new(1234);
        async {}.await;
        cell.set(5678);
    };
}

I expected to see this happen: Should compile successfully, like it does without the reference to Cell.

Instead, this happened: Compilation fails. Removing the intermediate reference to the Cell allows this to compile.


This is a fundamental limitation between Send and coroutine yield points. A Cell owned by a coroutine will not observe any data races as references to that Cell cannot (soundly) escape the coroutine across yield points. Yield points could use an alternative autotrait that represents "thread pinning".

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the minimal Rust reproducer in the issue and compare it with the version that removes the reference to Cell. Trace how Send bounds are inferred across the async yield point, then determine whether the behavior can be changed without compromising soundness. Done means the reproducer compiles as expected with appropriate compiler coverage.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
compilers
Issue type
Bug
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.