Skip to content

Diagnostics for closure capture not implementing auto trait does not show _which_ capture caused it #161371

Description

@ds84182

Code

fn main() {
    let foo = std::ptr::null_mut::<()>();
    std::thread::spawn(move || {
        unsafe { foo.read(); }
    });
}

Current output

Compiling playground v0.0.1 (/playground)
error[E0277]: `*mut ()` cannot be sent between threads safely
   --> src/main.rs:3:24
    |
  3 |       std::thread::spawn(move || {
    |       ------------------ ^------
    |       |                  |
    |  _____|__________________within this `{closure@src/main.rs:3:24: 3:31}`
    | |     |
    | |     required by a bound introduced by this call
  4 | |         unsafe { foo.read(); }
  5 | |     });
    | |_____^ `*mut ()` cannot be sent between threads safely
    |
    = help: within `{closure@src/main.rs:3:24: 3:31}`, the trait `Send` is not implemented for `*mut ()`
note: required because it's used within this closure
   --> src/main.rs:3:24
    |
  3 |     std::thread::spawn(move || {
    |                        ^^^^^^^
note: required by a bound in `spawn`
   --> /playground/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/std/src/thread/functions.rs:128:8
    |
125 | pub fn spawn<F, T>(f: F) -> JoinHandle<T>
    |        ----- required by a bound in this function
...
128 |     F: Send + 'static,
    |        ^^^^ required by this bound in `spawn`

For more information about this error, try `rustc --explain E0277`.
error: could not compile `playground` (bin "playground") due to 1 previous error

Desired output

...
note: required because `foo` is used within this closure
...

Rationale and extra context

While it is obvious in this context, it is not obvious when a closure captures many variables.

Other cases

Rust Version

Playground in 1.97.1 as well as 1.100.0-nightly (2026-08-18 e71c0f1e3395b10a8c33)

Anything else?

I couldn't find an existing issue for this. It might exist but GitHub search sucks.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-auto-traitsArea: auto traits (e.g., `auto trait Send {}`)A-closuresArea: Closures (`|…| { … }`)A-diagnosticsArea: Messages for errors, warnings, and lintsC-enhancementCategory: An issue proposing an enhancement or a PR with one.D-terseDiagnostics: An error or lint that doesn't give enough information about the problem at hand.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