rust-lang / rust-lang/rust

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

Open
#146,016 0 comments 0 reactions 1 assignee View on GitHub

@chenyukang is already working on this.

Since Sep 13, 2026.

A-diagnostics A-enum A-thread T-compiler
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

Description

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).

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.