Skip to content

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

Open
@ds84182

Description

@ds84182

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".

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-coroutinesArea: CoroutinesC-bugCategory: This is a bug.E-needs-investigationCall for partcipation: This issues needs some investigation to determine current statusT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.T-langRelevant to the language team, which will review and decide on the PR/issue.WG-asyncWorking group: Async & await

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions