Skip to content

Suggestion to use Arc<T> is too indiscriminate #114687

Closed
@dtolnay

Description

@dtolnay

Code

fn main() {
    let ptr = &false as *const bool;

    std::thread::spawn(move || println!("{:p}", ptr)).join().unwrap();
}

Current output

error[E0277]: `*const bool` cannot be sent between threads safely
 --> src/main.rs:4:24
  |
4 |     std::thread::spawn(move || println!("{:p}", ptr)).join().unwrap();
  |     ------------------ -------^^^^^^^^^^^^^^^^^^^^^^
  |     |                  |
  |     |                  `*const bool` cannot be sent between threads safely
  |     |                  within this `[closure@src/main.rs:4:24: 4:31]`
  |     required by a bound introduced by this call
  |
  = help: within `[closure@src/main.rs:4:24: 4:31]`, the trait `Send` is not implemented for `*const bool`
  = note: consider using `std::sync::Arc<*const bool>`; for more information visit <https://doc.rust-lang.org/book/ch16-03-shared-state.html>
note: required because it's used within this closure
 --> src/main.rs:4:24
  |
4 |     std::thread::spawn(move || println!("{:p}", ptr)).join().unwrap();
  |                        ^^^^^^^
note: required by a bound in `spawn`
 --> /rustc/08d00b40aef2017fe6dba3ff7d6476efa0c10888/library/std/src/thread/mod.rs:680:1

Desired output

No response

Rationale and extra context

Filing to track the hesitation from #88936 (review).

[@nagisa]— Use of Arc is not always applicable, as both Send and Sync impls for Arc<T> require T: Send + Sync. Unless we can verify T bounds here, I don't think this on clause adds any more clarity over the default note?

[@estebank]— Thats fair, but was concerned the default would be too verbose for a case where we could be more straightforward. I can change it.

[@nagisa]— Well, we can always change this later too. r=me if you don't want to change it.

If T is not Send then Arc<T> is also not Send. Suggesting Arc<T> whenever Send is not implemented seems misguided.

Other cases

No response

Anything else?

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsD-incorrectDiagnostics: A diagnostic that is giving misleading or incorrect information.D-papercutDiagnostics: An error or lint that needs small tweaks.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