Skip to content

Better error reporting for Sync and type that impl !Sync #46678

Open
@gbip

Description

@gbip

It would make a lot more sense if when a type impl !Sync the compiler could report an other error than the error that comes from this impl block.

For example, take a look at this code :

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

struct Foo {}

impl Foo {
    fn do_something(&self) {
        println!("hello");
    }
}

fn main() {
    let (tx, rx) = channel::<Foo>();
    thread::spawn(move || rx.iter().map(|f| f.do_something()));
}

Which outputs :

17 |     thread::spawn(move || rx.iter().map(|f| f.do_something()));
   |     ^^^^^^^^^^^^^ `std::sync::mpsc::Receiver<Foo>` cannot be shared between threads safely
   |
   = help: the trait `std::marker::Sync` is not implemented for `std::sync::mpsc::Receiver<Foo>`
   = note: required because of the requirements on the impl of `std::marker::Send` for `&std::sync::mpsc::Receiver<Foo>`
   = note: required because it appears within the type `std::sync::mpsc::Iter<'_, Foo>`
   = note: required because it appears within the type `std::iter::Map<std::sync::mpsc::Iter<'_, Foo>, [closure@src/main.rs:17:41: 17:61]>`
   = note: required by `std::thread::spawn`

It would be way better if it could just say that Receiver is explicitly not Sync.

I know that this a bad example because the error is actually due to the fact that the compiler does not move rx because we are returning a value from the call to map(), but it was the only one that I had...

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsA-trait-systemArea: Trait systemC-enhancementCategory: An issue proposing an enhancement or a PR with one.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