diff options
| author | Paul Trojahn <paul.trojahn@gmail.com> | 2021-07-26 20:26:23 +0200 |
|---|---|---|
| committer | Paul Trojahn <paul.trojahn@gmail.com> | 2021-09-06 13:18:05 +0200 |
| commit | 50e5f90c925b411bac7de0ec3ec239d7d321b5a2 (patch) | |
| tree | 605d5f1093e938a80377d0b942425f6bcf5e81a9 /src | |
| parent | d19d864e79531ad702e4f44b2635da6c50cc5eb0 (diff) | |
Suggest deriving traits if possible
This only applies to builtin derives as I don't think there is a clean way to get the available derives in typeck. Closes #85851
Diffstat (limited to 'src')
7 files changed, 148 insertions, 0 deletions
diff --git a/src/test/ui/derives/derive-assoc-type-not-impl.stderr b/src/test/ui/derives/derive-assoc-type-not-impl.stderr index fd993d0f9d8..1080f947732 100644 --- a/src/test/ui/derives/derive-assoc-type-not-impl.stderr +++ b/src/test/ui/derives/derive-assoc-type-not-impl.stderr @@ -19,6 +19,10 @@ LL | Bar::<NotClone> { x: 1 }.clone(); = help: items from traits can only be used if the trait is implemented and in scope = note: the following trait defines an item `clone`, perhaps you need to implement it: candidate #1: `Clone` +help: consider annotating `NotClone` with `#[derive(Clone)]` + | +LL | #[derive(Clone)] + | error: aborting due to previous error diff --git a/src/test/ui/mismatched_types/method-help-unsatisfied-bound.stderr b/src/test/ui/mismatched_types/method-help-unsatisfied-bound.stderr index 1030061b2d1..596b7bfe79c 100644 --- a/src/test/ui/mismatched_types/method-help-unsatisfied-bound.stderr +++ b/src/test/ui/mismatched_types/method-help-unsatisfied-bound.stderr @@ -9,6 +9,10 @@ LL | a.unwrap(); | = note: the following trait bounds were not satisfied: `Foo: Debug` +help: consider annotating `Foo` with `#[derive(Debug)]` + | +LL | #[derive(Debug)] + | error: aborting due to previous error diff --git a/src/test/ui/suggestions/derive-trait-for-method-call.rs b/src/test/ui/suggestions/derive-trait-for-method-call.rs new file mode 100644 index 00000000000..25043da52aa --- /dev/null +++ b/src/test/ui/suggestions/derive-trait-for-method-call.rs @@ -0,0 +1,44 @@ +use std::time::Instant; + +enum Enum { + First +} + +#[derive(Clone)] +enum CloneEnum { + First +} + +struct Struct { +} + +#[derive(Clone)] +struct CloneStruct { +} + +struct Foo<X, Y> (X, Y); +impl<X: Clone + Default + , Y: Clone + Default> Foo<X, Y> { + fn test(&self) -> (X, Y) { + (self.0, self.1) + } +} + +fn test1() { + let x = Foo(Enum::First, CloneEnum::First); + let y = x.test(); + //~^the method `test` exists for struct `Foo<Enum, CloneEnum>`, but its trait bounds were not satisfied [E0599] +} + +fn test2() { + let x = Foo(Struct{}, CloneStruct{}); + let y = x.test(); + //~^the method `test` exists for struct `Foo<Struct, CloneStruct>`, but its trait bounds were not satisfied [E0599] +} + +fn test3() { + let x = Foo(Vec::<Enum>::new(), Instant::now()); + let y = x.test(); + //~^the method `test` exists for struct `Foo<Vec<Enum>, Instant>`, but its trait bounds were not satisfied [E0599] +} + +fn main() {} diff --git a/src/test/ui/suggestions/derive-trait-for-method-call.stderr b/src/test/ui/suggestions/derive-trait-for-method-call.stderr new file mode 100644 index 00000000000..97fc1134a94 --- /dev/null +++ b/src/test/ui/suggestions/derive-trait-for-method-call.stderr @@ -0,0 +1,84 @@ +error[E0599]: the method `test` exists for struct `Foo<Enum, CloneEnum>`, but its trait bounds were not satisfied + --> $DIR/derive-trait-for-method-call.rs:28:15 + | +LL | enum Enum { + | --------- + | | + | doesn't satisfy `Enum: Clone` + | doesn't satisfy `Enum: Default` +... +LL | enum CloneEnum { + | -------------- doesn't satisfy `CloneEnum: Default` +... +LL | struct Foo<X, Y> (X, Y); + | ------------------------ method `test` not found for this +... +LL | let y = x.test(); + | ^^^^ method cannot be called on `Foo<Enum, CloneEnum>` due to unsatisfied trait bounds + | + = note: the following trait bounds were not satisfied: + `Enum: Clone` + `Enum: Default` + `CloneEnum: Default` +help: consider annotating `Enum` with `#[derive(Clone)]` + | +LL | #[derive(Clone)] + | + +error[E0599]: the method `test` exists for struct `Foo<Struct, CloneStruct>`, but its trait bounds were not satisfied + --> $DIR/derive-trait-for-method-call.rs:34:15 + | +LL | struct Struct { + | ------------- + | | + | doesn't satisfy `Struct: Clone` + | doesn't satisfy `Struct: Default` +... +LL | struct CloneStruct { + | ------------------ doesn't satisfy `CloneStruct: Default` +... +LL | struct Foo<X, Y> (X, Y); + | ------------------------ method `test` not found for this +... +LL | let y = x.test(); + | ^^^^ method cannot be called on `Foo<Struct, CloneStruct>` due to unsatisfied trait bounds + | + = note: the following trait bounds were not satisfied: + `Struct: Clone` + `Struct: Default` + `CloneStruct: Default` +help: consider annotating `CloneStruct` with `#[derive(Default)]` + | +LL | #[derive(Default)] + | +help: consider annotating `Struct` with `#[derive(Clone, Default)]` + | +LL | #[derive(Clone, Default)] + | + +error[E0599]: the method `test` exists for struct `Foo<Vec<Enum>, Instant>`, but its trait bounds were not satisfied + --> $DIR/derive-trait-for-method-call.rs:40:15 + | +LL | struct Foo<X, Y> (X, Y); + | ------------------------ method `test` not found for this +... +LL | let y = x.test(); + | ^^^^ method cannot be called on `Foo<Vec<Enum>, Instant>` due to unsatisfied trait bounds + | + ::: $SRC_DIR/std/src/time.rs:LL:COL + | +LL | pub struct Instant(time::Instant); + | ---------------------------------- doesn't satisfy `Instant: Default` + | + ::: $SRC_DIR/alloc/src/vec/mod.rs:LL:COL + | +LL | pub struct Vec<T, #[unstable(feature = "allocator_api", issue = "32838")] A: Allocator = Global> { + | ------------------------------------------------------------------------------------------------ doesn't satisfy `Vec<Enum>: Clone` + | + = note: the following trait bounds were not satisfied: + `Vec<Enum>: Clone` + `Instant: Default` + +error: aborting due to 3 previous errors + +For more information about this error, try `rustc --explain E0599`. diff --git a/src/test/ui/union/union-derive-clone.mirunsafeck.stderr b/src/test/ui/union/union-derive-clone.mirunsafeck.stderr index b52117cd19c..146a627bcde 100644 --- a/src/test/ui/union/union-derive-clone.mirunsafeck.stderr +++ b/src/test/ui/union/union-derive-clone.mirunsafeck.stderr @@ -16,6 +16,10 @@ LL | let w = u.clone(); = note: the following trait bounds were not satisfied: `CloneNoCopy: Copy` which is required by `U5<CloneNoCopy>: Clone` +help: consider annotating `CloneNoCopy` with `#[derive(Copy)]` + | +LL | #[derive(Copy)] + | error[E0277]: the trait bound `U1: Copy` is not satisfied --> $DIR/union-derive-clone.rs:6:10 diff --git a/src/test/ui/union/union-derive-clone.thirunsafeck.stderr b/src/test/ui/union/union-derive-clone.thirunsafeck.stderr index b52117cd19c..146a627bcde 100644 --- a/src/test/ui/union/union-derive-clone.thirunsafeck.stderr +++ b/src/test/ui/union/union-derive-clone.thirunsafeck.stderr @@ -16,6 +16,10 @@ LL | let w = u.clone(); = note: the following trait bounds were not satisfied: `CloneNoCopy: Copy` which is required by `U5<CloneNoCopy>: Clone` +help: consider annotating `CloneNoCopy` with `#[derive(Copy)]` + | +LL | #[derive(Copy)] + | error[E0277]: the trait bound `U1: Copy` is not satisfied --> $DIR/union-derive-clone.rs:6:10 diff --git a/src/test/ui/unique-pinned-nocopy.stderr b/src/test/ui/unique-pinned-nocopy.stderr index 8e0804ebf9b..02ce371c8d5 100644 --- a/src/test/ui/unique-pinned-nocopy.stderr +++ b/src/test/ui/unique-pinned-nocopy.stderr @@ -21,6 +21,10 @@ LL | | >(Unique<T>, A); = help: items from traits can only be used if the trait is implemented and in scope = note: the following trait defines an item `clone`, perhaps you need to implement it: candidate #1: `Clone` +help: consider annotating `R` with `#[derive(Clone)]` + | +LL | #[derive(Clone)] + | error: aborting due to previous error |
