Skip to content

Error for using a by-ref closure when by-move was needed could be improved #24909

Closed
@seanmonstar

Description

@seanmonstar
use std::thread;
use std::sync::mpsc::channel;

fn main() {
    let (tx, _rx) = channel();
    thread::spawn(|| tx.send(()).unwrap());
}

Errors with:

<anon>:6:5: 6:18 error: the trait `core::marker::Sync` is not implemented for the type `core::cell::UnsafeCell<std::sync::mpsc::Flavor<()>>` [E0277]
<anon>:6     thread::spawn(|| tx.send(()).unwrap());
             ^~~~~~~~~~~~~
<anon>:6:5: 6:18 note: `core::cell::UnsafeCell<std::sync::mpsc::Flavor<()>>` cannot be shared between threads safely
<anon>:6     thread::spawn(|| tx.send(()).unwrap());

When the real solution here is to add the move keyword to the closure, but the error message leaves everyone quite confused.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-closuresArea: Closures (`|…| { … }`)A-diagnosticsArea: Messages for errors, warnings, and lintsC-enhancementCategory: An issue proposing an enhancement or a PR with one.D-newcomer-roadblockDiagnostics: Confusing error or lint; hard to understand for new users.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions