-
-
Notifications
You must be signed in to change notification settings - Fork 15k
Avoid redundant note when a #[derive] is already suggested #157126
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Dnreikronos
wants to merge
2
commits into
rust-lang:main
Choose a base branch
from
Dnreikronos:avoid-redundant-derive-note-on-unimplemented
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 15 additions & 0 deletions
15
tests/ui/derives/redundant-derive-note-on-unimplemented.rs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| // Regression test for the redundant `note: add `#[derive(Debug)]` to `X` or manually | ||
| // `impl Debug for X`` that was emitted alongside the `consider annotating X with | ||
| // `#[derive(Debug)]`` suggestion. When the derive suggestion is shown, the note is | ||
| // redundant and should be suppressed. | ||
| // | ||
| // See https://github.com/rust-lang/rust/issues/157118 | ||
|
|
||
| #[derive(Debug)] | ||
| struct S<T>(T); | ||
|
|
||
| struct X; | ||
|
|
||
| fn main() { | ||
| println!("{:?}", S(X)); //~ ERROR `X` doesn't implement `Debug` | ||
| } |
31 changes: 31 additions & 0 deletions
31
tests/ui/derives/redundant-derive-note-on-unimplemented.stderr
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| error[E0277]: `X` doesn't implement `Debug` | ||
| --> $DIR/redundant-derive-note-on-unimplemented.rs:14:22 | ||
| | | ||
| LL | println!("{:?}", S(X)); | ||
| | ---- ^^^^ `X` cannot be formatted using `{:?}` because it doesn't implement `Debug` | ||
| | | | ||
| | required by this formatting parameter | ||
| | | ||
| = help: the trait `Debug` is not implemented for `X` | ||
| help: the trait `Debug` is implemented for `S<T>` | ||
| --> $DIR/redundant-derive-note-on-unimplemented.rs:8:10 | ||
| | | ||
| LL | #[derive(Debug)] | ||
| | ^^^^^ | ||
| note: required for `S<X>` to implement `Debug` | ||
| --> $DIR/redundant-derive-note-on-unimplemented.rs:9:8 | ||
| | | ||
| LL | #[derive(Debug)] | ||
| | ----- in this derive macro expansion | ||
| LL | struct S<T>(T); | ||
| | ^ - type parameter would need to implement `Debug` | ||
| = help: consider manually implementing `Debug` to avoid undesired bounds | ||
| help: consider annotating `X` with `#[derive(Debug)]` | ||
| | | ||
| LL + #[derive(Debug)] | ||
| LL | struct X; | ||
| | | ||
|
|
||
| error: aborting due to 1 previous error | ||
|
|
||
| For more information about this error, try `rustc --explain E0277`. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just to confirm, what is the difference between the output in this PR and the output if we remove the following lines? Are there some cases where the note is useful? It'd be a good idea to include that context in the comment or at least in this PR.
rust/library/core/src/fmt/mod.rs
Lines 1039 to 1043 in 6368fd5
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, good call flagging this. Short version: deleting those lines would kill the note everywhere, but it's only redundant some of the time, so I skip it conditionally instead.
suggest_deriveshowsconsider annotating X with #[derive(..)]only whencan_suggest_deriveholds: a crate-local ADT, not a union, and all fields already implement the trait. When that fires, the note and the suggestion are saying the same thing, so the note is just clutter (that's what the stderr diffs are trimming).When
can_suggest_deriveis false there's no suggestion at all, and then the note is the only nudge the user gets. Easiest case to picture is a local struct with a field that isn'tDebug:No
consider annotatingline here, sinceOuter's field isn'tDebug, so dropping the lines outright would leave this one with nothing actionable. Same story whenmain_trait_predicate != leaf_trait_predicate: the note's built from the main predicate and the suggestion from the leaf, so they don't overlap.Happy to pull this into the PR description too. Thanks for pushing on it! :)