-
-
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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 | ||
| } |
| 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> { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Doesn't this span need to point at
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Confirmed, this is existing span issue. Feel free to file a separate issue for it if you think it's worth tracking. |
||
| | ^^^^ - 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`. | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| // Test that the "imperfect derives" note is emitted for Copy when the derive | ||
| // bound is genuinely unnecessary (e.g., `PhantomData<T>`). | ||
|
|
||
| use std::marker::PhantomData; | ||
|
|
||
| #[derive(Copy, Clone)] | ||
| struct X<T>(PhantomData<T>); | ||
|
|
||
| struct Y; // does not implement Copy | ||
|
|
||
| fn require_copy<T: Copy>(_t: T) {} | ||
|
|
||
| fn main() { | ||
| require_copy(X::<Y>(PhantomData)); | ||
| //~^ ERROR the trait bound `X<Y>: Copy` is not satisfied | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| error[E0277]: the trait bound `X<Y>: Copy` is not satisfied | ||
| --> $DIR/imperfect-derive-copy.rs:14:25 | ||
| | | ||
| LL | require_copy(X::<Y>(PhantomData)); | ||
| | ------------ ^^^^^^^^^^^ the trait `Copy` is not implemented for `X<Y>` | ||
| | | | ||
| | required by a bound introduced by this call | ||
| | | ||
| note: required for `X<Y>` to implement `Copy` | ||
| --> $DIR/imperfect-derive-copy.rs:7:8 | ||
| | | ||
| LL | #[derive(Copy, Clone)] | ||
| | ---- in this derive macro expansion | ||
| LL | struct X<T>(PhantomData<T>); | ||
| | ^ - type parameter would need to implement `Copy` | ||
| = help: consider manually implementing `Copy` 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_copy` | ||
| --> $DIR/imperfect-derive-copy.rs:11:20 | ||
| | | ||
| LL | fn require_copy<T: Copy>(_t: T) {} | ||
| | ^^^^ required by this bound in `require_copy` | ||
| help: consider borrowing here | ||
| | | ||
| LL | require_copy(X::<Y>(&PhantomData)); | ||
| | + | ||
|
|
||
| error: aborting due to 1 previous error | ||
|
|
||
| For more information about this error, try `rustc --explain E0277`. |
| 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 | ||
| } |
| 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>` | ||
|
Comment on lines
+4
to
+5
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this span also looks a bit off 🤔 |
||
| | | | ||
| | 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`. | ||
Uh oh!
There was an error while loading. Please reload this page.