Skip to content

consider annotating Union with #[derive(Debug)] is misleading #137587

@XAMPPRocky

Description

@XAMPPRocky

Code

union Union {
    member: usize,
}

impl PartialEq<u8> for Union {
    fn eq(&self, rhs: &u8) -> bool {
        unsafe { self.member == (*rhs).into() }
    }
}

fn main() {
    assert_eq!(Union { member: 0, }, 0);
}

Current output

error[E0277]: `Union` doesn't implement `Debug`
  --> src/main.rs:13:5
   |
13 |     assert_eq!(Union { member: 0, }, 0);
   |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ `Union` cannot be formatted using `{:?}`
   |
   = help: the trait `Debug` is not implemented for `Union`
   = note: add `#[derive(Debug)]` to `Union` or manually `impl Debug for Union`
   = note: this error originates in the macro `assert_eq` (in Nightly builds, run with -Z macro-backtrace for more info)
help: consider annotating `Union` with `#[derive(Debug)]`
   |
2  + #[derive(Debug)]
3  | union Union {
   |

For more information about this error, try `rustc --explain E0277`.

Desired output

error[E0277]: `Union` doesn't implement `Debug`
  --> src/main.rs:13:5
   |
13 |     assert_eq!(Union { member: 0, }, 0);
   |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ `Union` cannot be formatted using `{:?}`
   |
   = help: the trait `Debug` is not implemented for `Union`
   = note: add manual `impl Debug for Union`
   = note: this error originates in the macro `assert_eq` (in Nightly builds, run with -Z macro-backtrace for more info)

For more information about this error, try `rustc --explain E0277`.

Rationale and extra context

The help in the error should be removed when the type is a union, because if you follow that instruction, you will just run into the "this trait cannot be derived for unions" error.

Other cases

Rust Version

I tested it with stable in playground.

https://play.rust-lang.org/?version=stable&mode=debug&edition=2024&gist=baa42bb5a36216b734f9eb665173a70b

Anything else?

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions