-
-
Notifications
You must be signed in to change notification settings - Fork 15.5k
Add accurate targeting for imperfect derives diagnostic in E0277
#159363
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
raushan728
wants to merge
1
commit into
rust-lang:main
Choose a base branch
from
raushan728:accurate-targeting-157117
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| // Test that the "imperfect derives" note is emitted for associated types, | ||
| // `Rc<T>`, and coinductive types. | ||
|
|
||
| use std::rc::Rc; | ||
|
|
||
| trait Trait { | ||
| type Assoc: Clone; | ||
| } | ||
|
|
||
| #[derive(Clone)] | ||
| struct AssocStruct<T: Trait> { | ||
| field: T::Assoc, | ||
| } | ||
|
|
||
| struct NonClone; | ||
| impl Trait for NonClone { | ||
| type Assoc = u32; | ||
| } | ||
|
|
||
| #[derive(Clone)] | ||
| struct RcStruct<T> { | ||
| field: Rc<T>, | ||
| } | ||
|
|
||
| #[derive(Clone)] | ||
| struct List<T> { | ||
| value: Rc<T>, | ||
| next: Option<Box<List<T>>>, | ||
| } | ||
|
|
||
| fn require_clone<T: Clone>() {} | ||
|
|
||
| fn main() { | ||
| require_clone::<AssocStruct<NonClone>>(); | ||
| //~^ ERROR the trait bound `NonClone: Clone` is not satisfied | ||
|
|
||
| require_clone::<RcStruct<NonClone>>(); | ||
| //~^ ERROR the trait bound `NonClone: Clone` is not satisfied | ||
|
|
||
| require_clone::<List<NonClone>>(); | ||
| //~^ ERROR the trait bound `NonClone: Clone` is not satisfied | ||
| } |
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,81 @@ | ||
| error[E0277]: the trait bound `NonClone: Clone` is not satisfied | ||
| --> $DIR/imperfect-derive-advanced.rs:34:21 | ||
| | | ||
| LL | require_clone::<AssocStruct<NonClone>>(); | ||
| | ^^^^^^^^^^^^^^^^^^^^^ the trait `Clone` is not implemented for `NonClone` | ||
| | | ||
| note: required for `AssocStruct<NonClone>` to implement `Clone` | ||
| --> $DIR/imperfect-derive-advanced.rs:11:8 | ||
| | | ||
| LL | #[derive(Clone)] | ||
| | ----- in this derive macro expansion | ||
| LL | struct AssocStruct<T: Trait> { | ||
| | ^^^^^^^^^^^ - type parameter would need to implement `Clone` | ||
| = help: consider manually implementing `Clone` to avoid undesired bounds caused by "imperfect derives" | ||
| = note: to learn more, visit <https://github.com/rust-lang/rust/issues/26925> | ||
| note: required by a bound in `require_clone` | ||
| --> $DIR/imperfect-derive-advanced.rs:31:21 | ||
| | | ||
| LL | fn require_clone<T: Clone>() {} | ||
| | ^^^^^ required by this bound in `require_clone` | ||
| help: consider annotating `NonClone` with `#[derive(Clone)]` | ||
| | | ||
| LL + #[derive(Clone)] | ||
| LL | struct NonClone; | ||
| | | ||
|
|
||
| error[E0277]: the trait bound `NonClone: Clone` is not satisfied | ||
| --> $DIR/imperfect-derive-advanced.rs:37:21 | ||
| | | ||
| LL | require_clone::<RcStruct<NonClone>>(); | ||
| | ^^^^^^^^^^^^^^^^^^ the trait `Clone` is not implemented for `NonClone` | ||
| | | ||
| note: required for `RcStruct<NonClone>` to implement `Clone` | ||
| --> $DIR/imperfect-derive-advanced.rs:21:8 | ||
| | | ||
| LL | #[derive(Clone)] | ||
| | ----- in this derive macro expansion | ||
| LL | struct RcStruct<T> { | ||
| | ^^^^^^^^ - type parameter would need to implement `Clone` | ||
| = help: consider manually implementing `Clone` to avoid undesired bounds caused by "imperfect derives" | ||
| = note: to learn more, visit <https://github.com/rust-lang/rust/issues/26925> | ||
| note: required by a bound in `require_clone` | ||
| --> $DIR/imperfect-derive-advanced.rs:31:21 | ||
| | | ||
| LL | fn require_clone<T: Clone>() {} | ||
| | ^^^^^ required by this bound in `require_clone` | ||
| help: consider annotating `NonClone` with `#[derive(Clone)]` | ||
| | | ||
| LL + #[derive(Clone)] | ||
| LL | struct NonClone; | ||
| | | ||
|
|
||
| error[E0277]: the trait bound `NonClone: Clone` is not satisfied | ||
| --> $DIR/imperfect-derive-advanced.rs:40:21 | ||
| | | ||
| LL | require_clone::<List<NonClone>>(); | ||
| | ^^^^^^^^^^^^^^ the trait `Clone` is not implemented for `NonClone` | ||
| | | ||
| note: required for `List<NonClone>` to implement `Clone` | ||
| --> $DIR/imperfect-derive-advanced.rs:26:8 | ||
| | | ||
| LL | #[derive(Clone)] | ||
| | ----- in this derive macro expansion | ||
| LL | struct List<T> { | ||
| | ^^^^ - type parameter would need to implement `Clone` | ||
| = help: consider manually implementing `Clone` to avoid undesired bounds caused by "imperfect derives" | ||
| = note: to learn more, visit <https://github.com/rust-lang/rust/issues/26925> | ||
| note: required by a bound in `require_clone` | ||
| --> $DIR/imperfect-derive-advanced.rs:31:21 | ||
| | | ||
| LL | fn require_clone<T: Clone>() {} | ||
| | ^^^^^ required by this bound in `require_clone` | ||
| help: consider annotating `NonClone` with `#[derive(Clone)]` | ||
| | | ||
| LL + #[derive(Clone)] | ||
| LL | struct NonClone; | ||
| | | ||
|
|
||
| error: aborting due to 3 previous errors | ||
|
|
||
| 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| // Test that the "imperfect derives" note is emitted when the derive bound | ||
| // is genuinely unnecessary (e.g., `PhantomData<T>`). | ||
|
|
||
| use std::marker::PhantomData; | ||
|
|
||
| #[derive(Clone)] | ||
| struct S<T>(PhantomData<T>); | ||
|
|
||
| struct X; | ||
|
|
||
| fn require_clone<T: Clone>(_t: T) {} | ||
|
|
||
| fn main() { | ||
| require_clone(S::<X>(PhantomData)); | ||
| //~^ ERROR the trait bound `S<X>: Clone` is not satisfied | ||
| } |
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,30 @@ | ||
| error[E0277]: the trait bound `S<X>: Clone` is not satisfied | ||
| --> $DIR/imperfect-derive-phantom.rs:14:26 | ||
| | | ||
| LL | require_clone(S::<X>(PhantomData)); | ||
| | ------------- ^^^^^^^^^^^ the trait `Clone` is not implemented for `S<X>` | ||
| | | | ||
| | required by a bound introduced by this call | ||
| | | ||
| note: required for `S<X>` to implement `Clone` | ||
| --> $DIR/imperfect-derive-phantom.rs:7:8 | ||
| | | ||
| LL | #[derive(Clone)] | ||
| | ----- in this derive macro expansion | ||
| LL | struct S<T>(PhantomData<T>); | ||
| | ^ - type parameter would need to implement `Clone` | ||
| = help: consider manually implementing `Clone` to avoid undesired bounds caused by "imperfect derives" | ||
| = note: to learn more, visit <https://github.com/rust-lang/rust/issues/26925> | ||
| note: required by a bound in `require_clone` | ||
| --> $DIR/imperfect-derive-phantom.rs:11:21 | ||
| | | ||
| LL | fn require_clone<T: Clone>(_t: T) {} | ||
| | ^^^^^ required by this bound in `require_clone` | ||
| help: consider borrowing here | ||
| | | ||
| LL | require_clone(S::<X>(&PhantomData)); | ||
| | + | ||
|
|
||
| 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
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.
Uh oh!
There was an error while loading. Please reload this page.