Skip to content

Diagnostic improvement request: Enums that contain non-Send aliased types produce confusing error messages #146016

@mutexlox-signal

Description

@mutexlox-signal

Consider this code (also in this playground):

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

mod other {
    use std::ffi::c_void;

    pub type Bar = *const c_void;
}

use other::Bar;

enum Foo {
    Case(i32),
    Case2(Bar),
}
fn main() {
    let (tx, rx) = mpsc::channel();
    let h = thread::spawn(move || match rx.recv().unwrap() {
        Foo::Case(x) => println!("{:?}", x),
        Foo::Case2(b) => println!("{:?}", b),
    });
    tx.send(Foo::Case2(std::ptr::null()));
    h.join();
}

When I compile this with the latest nightly (1.91.0-nightly (2025-08-28 f2824da98d44c4a4e17b), from rust playground), I see this error, which mentions neither Case2 nor Bar. In this case, it's easy enough to see what the problem is, but in a more complex example -- e.g. if Bar comes from a crate whose implementation the user is unfamiliar with -- it becomes difficult to understand what the cause of the problem is.

   Compiling playground v0.0.1 (/playground)
error[E0277]: `*const c_void` cannot be sent between threads safely
   --> src/main.rs:18:27
    |
 18 |       let h = thread::spawn(move ||
    |  _____________-------------_^
    | |             |
    | |             required by a bound introduced by this call
 19 | |     match rx.recv().unwrap() {
 20 | |         Foo::Case(x) => println!("{:?}", x),
 21 | |         Foo::Case2(b) => println!("{:?}", b),
 22 | |     });
    | |_____^ `*const c_void` cannot be sent between threads safely
    |
    = help: within `Foo`, the trait `Send` is not implemented for `*const c_void`
note: required because it appears within the type `Foo`
   --> src/main.rs:12:6
    |
 12 | enum Foo {
    |      ^^^
    = note: required for `std::sync::mpsc::Receiver<Foo>` to implement `Send`
note: required because it's used within this closure
   --> src/main.rs:18:27
    |
 18 |     let h = thread::spawn(move ||
    |                           ^^^^^^^
note: required by a bound in `spawn`
   --> /playground/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/std/src/thread/mod.rs:726:8
    |
723 | pub fn spawn<F, T>(f: F) -> JoinHandle<T>
    |        ----- required by a bound in this function
...
726 |     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

It would be better if the error mentioned either Case2, Bar, or both. (Likely mentioning Case2 would suffice, if the type alias presents issues with providing more helpful diagnostics).

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsA-enumArea: Enums (discriminated unions, or more generally ADTs (algebraic data types))A-threadArea: `std::thread`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