Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
seanmonstar opened this issue Apr 28, 2015 · 4 comments
Closed
Labels
A-closures Area: closures (`|args| { .. }`) A-diagnostics Area: Messages for errors, warnings, and lints C-enhancement Category: An issue proposing an enhancement or a PR with one. D-newcomer-roadblock Diagnostics: Confusing error or lint; hard to understand for new users. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@seanmonstar
Copy link
Contributor

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.

@steveklabnik steveklabnik added the A-diagnostics Area: Messages for errors, warnings, and lints label Apr 30, 2015
@steveklabnik
Copy link
Member

Triage: the error is now fancier, but this is still a quite bad diagnostic:

error[E0277]: the trait bound `std::sync::mpsc::Sender<()>: std::marker::Sync` is not satisfied
 --> <anon>:6:5
  |
6 |     thread::spawn(|| tx.send(()).unwrap());
  |     ^^^^^^^^^^^^^ the trait `std::marker::Sync` is not implemented for `std::sync::mpsc::Sender<()>`
  |
  = note: `std::sync::mpsc::Sender<()>` cannot be shared between threads safely

@steveklabnik steveklabnik added the T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. label Mar 9, 2017
@Mark-Simulacrum Mark-Simulacrum added the A-closures Area: closures (`|args| { .. }`) label May 31, 2017
@Mark-Simulacrum Mark-Simulacrum added the C-enhancement Category: An issue proposing an enhancement or a PR with one. label Jul 22, 2017
bors added a commit that referenced this issue Mar 15, 2018
Reword E0044 and message for `!Send` types

 - Reword E0044 help.
 - Change error message for types that don't implement `Send`

CC #45092, #46678, #24909, #33307.
@estebank
Copy link
Contributor

Current output:

error[E0277]: `std::sync::mpsc::Sender<()>` cannot be shared between threads safely
 --> src/main.rs:6:5
  |
6 |     thread::spawn(|| tx.send(()).unwrap());
  |     ^^^^^^^^^^^^^ `std::sync::mpsc::Sender<()>` cannot be shared between threads safely
  |
  = help: the trait `std::marker::Sync` is not implemented for `std::sync::mpsc::Sender<()>`
  = note: required because of the requirements on the impl of `std::marker::Send` for `&std::sync::mpsc::Sender<()>`
  = note: required because it appears within the type `[closure@src/main.rs:6:19: 6:42 tx:&std::sync::mpsc::Sender<()>]`
  = note: required by `std::thread::spawn`

@estebank estebank added the D-newcomer-roadblock Diagnostics: Confusing error or lint; hard to understand for new users. label Oct 4, 2019
@Spoonbender
Copy link

Triage: no change

@wcampbell0x2a
Copy link
Contributor

@estebank this looks fixed:

error[E0373]: closure may outlive the current function, but it borrows `tx`, which is owned by the current function
 --> src/main.rs:6:19
  |
6 |     thread::spawn(|| tx.send(()).unwrap());
  |                   ^^ -- `tx` is borrowed here
  |                   |
  |                   may outlive borrowed value `tx`
  |
note: function requires argument type to outlive `'static`
 --> src/main.rs:6:5
  |
6 |     thread::spawn(|| tx.send(()).unwrap());
  |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
help: to force the closure to take ownership of `tx` (and any other referenced variables), use the `move` keyword
  |
6 |     thread::spawn(move || tx.send(()).unwrap());
  |                   ++++

@estebank estebank closed this as completed Oct 4, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-closures Area: closures (`|args| { .. }`) A-diagnostics Area: Messages for errors, warnings, and lints C-enhancement Category: An issue proposing an enhancement or a PR with one. D-newcomer-roadblock Diagnostics: Confusing error or lint; hard to understand for new users. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

6 participants