Skip to content

don't suggest non-deriveable traits for unions#154118

Merged
rust-bors[bot] merged 1 commit intorust-lang:mainfrom
malezjaa:dont-suggest-some-traits-for-unions
Mar 21, 2026
Merged

don't suggest non-deriveable traits for unions#154118
rust-bors[bot] merged 1 commit intorust-lang:mainfrom
malezjaa:dont-suggest-some-traits-for-unions

Conversation

@malezjaa
Copy link
Copy Markdown
Contributor

@malezjaa malezjaa commented Mar 19, 2026

Fixes #137587

Before, the compiler suggested adding #[derive(Debug)] (other traits too) for unions,
which is misleading because some traits can't be automatically derived.

Only traits that are still suggested are Copy and Clone.

I noticed the error label changed after removing the suggestion. I hope this isn't a big deal, but let me know if that's an issue.

original example:

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);
}

before:

error[E0277]: `Union` doesn't implement `Debug`                                                                       
  --> src\main.rs:13:5
   |
13 |     assert_eq!(Union { member: 0 }, 0);
   |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 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 {
   |

after (the message doesn't suggest adding #[derive(Debug)] to unions)

error[E0277]: `Union` doesn't implement `Debug`                                                                       
  --> src\main.rs:13:5
   |
13 |     assert_eq!(Union { member: 0 }, 0);
   |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ unsatisfied trait bound
   |
help: the trait `Debug` is not implemented for `Union`
  --> src\main.rs:2:1
   |
 2 | union Union {
   | ^^^^^^^^^^^
   = note: manually `impl Debug for Union`

@rustbot
Copy link
Copy Markdown
Collaborator

rustbot commented Mar 19, 2026

Some changes occurred in compiler/rustc_attr_parsing

cc @jdonszelmann, @JonathanBrouwer

@rustbot rustbot added A-attributes Area: Attributes (`#[…]`, `#![…]`) S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-libs Relevant to the library team, which will review and decide on the PR/issue. labels Mar 19, 2026
@rustbot
Copy link
Copy Markdown
Collaborator

rustbot commented Mar 19, 2026

r? @dingxiangfei2009

rustbot has assigned @dingxiangfei2009.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

Why was this reviewer chosen?

The reviewer was selected based on:

  • Owners of files modified in this PR: compiler, types
  • compiler, types expanded to 69 candidates
  • Random selection from 14 candidates

@malezjaa malezjaa changed the title don't suggest non-deriveable for unions don't suggest non-deriveable traits for unions Mar 19, 2026
@Kivooeo
Copy link
Copy Markdown
Member

Kivooeo commented Mar 20, 2026

Can you please add a test to reflect new behaviour

@malezjaa malezjaa force-pushed the dont-suggest-some-traits-for-unions branch from 8fd2da8 to ccaae21 Compare March 20, 2026 08:24
@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Mar 20, 2026
@rustbot
Copy link
Copy Markdown
Collaborator

rustbot commented Mar 20, 2026

Reminder, once the PR becomes ready for a review, use @rustbot ready.

@JonathanBrouwer
Copy link
Copy Markdown
Contributor

cc @mejrs
if you feel like it, could you also take a look?

@malezjaa malezjaa force-pushed the dont-suggest-some-traits-for-unions branch from ccaae21 to dd844f3 Compare March 20, 2026 20:03
Copy link
Copy Markdown
Contributor

@mejrs mejrs left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The changes to on_unimplemented look good. I have some suggestions.

Please also update this test

#[diagnostic::on_unimplemented(
message = "{from_desugaring}{direct}{cause}{integral}{integer}",
//~^WARN there is no parameter `from_desugaring` on trait `Baz`
//~|WARN there is no parameter `direct` on trait `Baz`
//~|WARN there is no parameter `cause` on trait `Baz`
//~|WARN there is no parameter `integral` on trait `Baz`
//~|WARN there is no parameter `integer` on trait `Baz`
label = "{float}{_Self}{crate_local}{Trait}{ItemContext}{This}"
//~^WARN there is no parameter `float` on trait `Baz`
//~|WARN there is no parameter `_Self` on trait `Baz`
//~|WARN there is no parameter `crate_local` on trait `Baz`
//~|WARN there is no parameter `Trait` on trait `Baz`
//~|WARN there is no parameter `ItemContext` on trait `Baz`
//~|WARN there is no parameter `This` on trait `Baz`
)]
trait Baz {}

View changes since this review

@rust-log-analyzer

This comment has been minimized.

@malezjaa
Copy link
Copy Markdown
Contributor Author

I forgot to rebless the test. Will do that in a sec

@malezjaa
Copy link
Copy Markdown
Contributor Author

obraz I updated this test (do_not_accept_options_of_the_internal_rustc_attribute.rs). The diagnostic uses raw identifier for struct and enum, but displays union normally. Is that expected?

@malezjaa malezjaa force-pushed the dont-suggest-some-traits-for-unions branch from dd844f3 to 927e225 Compare March 20, 2026 21:19
@mejrs
Copy link
Copy Markdown
Contributor

mejrs commented Mar 20, 2026

I updated this test (do_not_accept_options_of_the_internal_rustc_attribute.rs). The diagnostic uses raw identifier for struct and enum, but displays union normally. Is that expected?

Interesting. I hadn't realized it does this with keywords. It does that with other keywords too.

#[diagnostic::on_unimplemented(message = "{impl}")]
trait Baz{}
warning: there is no parameter `r#impl` on trait `Baz`

Do note that union is a weak keyword; that's probably why it doesn't do it for union.

It at least is consistent:

fn main(){
    println!("{impl} {union}")
}
error[E0425]: cannot find value `r#impl` in this scope
 --> src/main.rs:2:16
  |
2 |     println!("{impl} {union}")
  |                ^^^^ not found in this scope

error[E0425]: cannot find value `union` in this scope
 --> src/main.rs:2:23
  |
2 |     println!("{impl} {union}")
  |                       ^^^^^ not found in this scope

so I suppose we should leave it this way.

Don't suggest non-deriveable traits for unions. This also adds enum, struct and union markers to rustc_on_unimplemented
@malezjaa malezjaa force-pushed the dont-suggest-some-traits-for-unions branch from 927e225 to 27212bb Compare March 20, 2026 22:39
@JonathanBrouwer
Copy link
Copy Markdown
Contributor

It is a bit weird the r# prefix is there, it could be intended but not sure, but regardless out of scope for this PR.

@JonathanBrouwer
Copy link
Copy Markdown
Contributor

@bors r+ rollup

@rust-bors
Copy link
Copy Markdown
Contributor

rust-bors bot commented Mar 20, 2026

📌 Commit 27212bb has been approved by JonathanBrouwer

It is now in the queue for this repository.

@rust-bors rust-bors bot added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Mar 20, 2026
JonathanBrouwer added a commit to JonathanBrouwer/rust that referenced this pull request Mar 20, 2026
…for-unions, r=JonathanBrouwer

don't suggest non-deriveable traits for unions

cc rust-lang#137587

Before, the compiler suggested adding `#[derive(Debug)]` (other traits too) for unions,
which is misleading because some traits can't be automatically derived.

Only traits that are still suggested are Copy and Clone.

I noticed the error label changed after removing the suggestion. I hope this isn't a big deal, but let me know if that's an issue.

original example:
```rs
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);
}
```

before:
```
error[E0277]: `Union` doesn't implement `Debug`
  --> src\main.rs:13:5
   |
13 |     assert_eq!(Union { member: 0 }, 0);
   |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 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 {
   |
```

after (the message doesn't suggest adding #[derive(Debug)] to unions)
```
error[E0277]: `Union` doesn't implement `Debug`
  --> src\main.rs:13:5
   |
13 |     assert_eq!(Union { member: 0 }, 0);
   |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ unsatisfied trait bound
   |
help: the trait `Debug` is not implemented for `Union`
  --> src\main.rs:2:1
   |
 2 | union Union {
   | ^^^^^^^^^^^
   = note: manually `impl Debug for Union`
```
@JonathanBrouwer
Copy link
Copy Markdown
Contributor

@malezjaa For in the future, if you put "Fixes #..." rather than "CC #..." it will automatically close the issue :)
I fixed it in the PR description for this PR

@malezjaa
Copy link
Copy Markdown
Contributor Author

Thank you both for the help with this PR :D

rust-bors bot pushed a commit that referenced this pull request Mar 21, 2026
…uwer

Rollup of 6 pull requests

Successful merges:

 - #154154 (Emit fewer errors for incorrect rtn and `=` -> `:` typos in bindings)
 - #154155 (tests/ui/async-await/drop-option-future.rs: New regression test)
 - #146961 (Allow passing `expr` metavariable as `cfg` predicate)
 - #154118 (don't suggest non-deriveable traits for unions)
 - #154120 (Start migrating `DecorateDiagCompat::Builtin` items to `DecorateDiagCompat::Dynamic`)
 - #154156 (Moving issue-52049 to borrowck)
rust-bors bot pushed a commit that referenced this pull request Mar 21, 2026
…uwer

Rollup of 6 pull requests

Successful merges:

 - #154154 (Emit fewer errors for incorrect rtn and `=` -> `:` typos in bindings)
 - #154155 (tests/ui/async-await/drop-option-future.rs: New regression test)
 - #146961 (Allow passing `expr` metavariable as `cfg` predicate)
 - #154118 (don't suggest non-deriveable traits for unions)
 - #154120 (Start migrating `DecorateDiagCompat::Builtin` items to `DecorateDiagCompat::Dynamic`)
 - #154156 (Moving issue-52049 to borrowck)
@rust-bors rust-bors bot merged commit 70d1c58 into rust-lang:main Mar 21, 2026
11 checks passed
@rustbot rustbot added this to the 1.96.0 milestone Mar 21, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-attributes Area: Attributes (`#[…]`, `#![…]`) S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-libs Relevant to the library team, which will review and decide on the PR/issue.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

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

7 participants