Skip to content

Commit 0d2a000

Browse files
committed
Suggest derivable trait on E0277
1 parent 60e50fc commit 0d2a000

File tree

62 files changed

+310
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

62 files changed

+310
-0
lines changed

compiler/rustc_trait_selection/src/traits/error_reporting/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -536,6 +536,7 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
536536
);
537537
self.note_version_mismatch(&mut err, &trait_ref);
538538
self.suggest_remove_await(&obligation, &mut err);
539+
self.suggest_derive(&mut err, trait_predicate);
539540

540541
if Some(trait_ref.def_id()) == tcx.lang_items().try_trait() {
541542
self.suggest_await_before_try(

compiler/rustc_trait_selection/src/traits/error_reporting/suggestions.rs

+35
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,8 @@ pub trait InferCtxtExt<'tcx> {
189189
err: &mut Diagnostic,
190190
trait_ref: &ty::PolyTraitRef<'tcx>,
191191
);
192+
193+
fn suggest_derive(&self, err: &mut Diagnostic, trait_pred: ty::PolyTraitPredicate<'tcx>);
192194
}
193195

194196
fn predicate_constraint(generics: &hir::Generics<'_>, pred: String) -> (Span, String) {
@@ -2589,6 +2591,39 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
25892591
_ => {}
25902592
}
25912593
}
2594+
2595+
fn suggest_derive(&self, err: &mut Diagnostic, trait_pred: ty::PolyTraitPredicate<'tcx>) {
2596+
if let Some(diagnostic_name) = self.tcx.get_diagnostic_name(trait_pred.def_id()) {
2597+
let adt = match trait_pred.skip_binder().self_ty().ty_adt_def() {
2598+
Some(adt) if adt.did().is_local() => adt,
2599+
_ => return,
2600+
};
2601+
let can_derive = match diagnostic_name {
2602+
sym::Default => !adt.is_enum(),
2603+
sym::Eq
2604+
| sym::PartialEq
2605+
| sym::Ord
2606+
| sym::PartialOrd
2607+
| sym::Clone
2608+
| sym::Copy
2609+
| sym::Hash
2610+
| sym::Debug => true,
2611+
_ => false,
2612+
};
2613+
if can_derive {
2614+
err.span_suggestion_verbose(
2615+
self.tcx.def_span(adt.did()).shrink_to_lo(),
2616+
&format!(
2617+
"consider annotating `{}` with `#[derive({})]`",
2618+
trait_pred.skip_binder().self_ty().to_string(),
2619+
diagnostic_name.to_string(),
2620+
),
2621+
format!("#[derive({})]\n", diagnostic_name.to_string()),
2622+
Applicability::MaybeIncorrect,
2623+
);
2624+
}
2625+
}
2626+
}
25922627
}
25932628

25942629
/// Collect all the returned expressions within the input expression.

src/test/ui/array-slice-vec/repeat_empty_ok.stderr

+8
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@ LL | let headers = [Header{value: &[]}; 128];
55
| ^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `Copy` is not implemented for `Header<'_>`
66
|
77
= note: the `Copy` trait is required because the repeated element will be copied
8+
help: consider annotating `Header<'_>` with `#[derive(Copy)]`
9+
|
10+
LL | #[derive(Copy)]
11+
|
812

913
error[E0277]: the trait bound `Header<'_>: Copy` is not satisfied
1014
--> $DIR/repeat_empty_ok.rs:13:19
@@ -13,6 +17,10 @@ LL | let headers = [Header{value: &[0]}; 128];
1317
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `Copy` is not implemented for `Header<'_>`
1418
|
1519
= note: the `Copy` trait is required because the repeated element will be copied
20+
help: consider annotating `Header<'_>` with `#[derive(Copy)]`
21+
|
22+
LL | #[derive(Copy)]
23+
|
1624

1725
error: aborting due to 2 previous errors
1826

src/test/ui/associated-types/defaults-suitability.stderr

+8
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@ note: required by a bound in `Tr::Ty`
99
|
1010
LL | type Ty: Clone = NotClone;
1111
| ^^^^^ required by this bound in `Tr::Ty`
12+
help: consider annotating `NotClone` with `#[derive(Clone)]`
13+
|
14+
LL | #[derive(Clone)]
15+
|
1216

1317
error[E0277]: the trait bound `NotClone: Clone` is not satisfied
1418
--> $DIR/defaults-suitability.rs:22:15
@@ -24,6 +28,10 @@ LL | Self::Ty: Clone,
2428
LL | {
2529
LL | type Ty = NotClone;
2630
| -- required by a bound in this
31+
help: consider annotating `NotClone` with `#[derive(Clone)]`
32+
|
33+
LL | #[derive(Clone)]
34+
|
2735

2836
error[E0277]: the trait bound `T: Clone` is not satisfied
2937
--> $DIR/defaults-suitability.rs:28:23

src/test/ui/consts/const-blocks/trait-error.stderr

+4
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ LL | [Foo(String::new()); 4];
77
= help: the following implementations were found:
88
<Foo<T> as Copy>
99
= note: the `Copy` trait is required because the repeated element will be copied
10+
help: consider annotating `Foo<String>` with `#[derive(Copy)]`
11+
|
12+
LL | #[derive(Copy)]
13+
|
1014

1115
error: aborting due to previous error
1216

src/test/ui/derives/derives-span-Clone-enum-struct-variant.stderr

+4
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ LL | x: Error
88
| ^^^^^^^^ the trait `Clone` is not implemented for `Error`
99
|
1010
= note: this error originates in the derive macro `Clone` (in Nightly builds, run with -Z macro-backtrace for more info)
11+
help: consider annotating `Error` with `#[derive(Clone)]`
12+
|
13+
LL | #[derive(Clone)]
14+
|
1115

1216
error: aborting due to previous error
1317

src/test/ui/derives/derives-span-Clone-enum.stderr

+4
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ LL | Error
88
| ^^^^^ the trait `Clone` is not implemented for `Error`
99
|
1010
= note: this error originates in the derive macro `Clone` (in Nightly builds, run with -Z macro-backtrace for more info)
11+
help: consider annotating `Error` with `#[derive(Clone)]`
12+
|
13+
LL | #[derive(Clone)]
14+
|
1115

1216
error: aborting due to previous error
1317

src/test/ui/derives/derives-span-Clone-struct.stderr

+4
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ LL | x: Error
88
| ^^^^^^^^ the trait `Clone` is not implemented for `Error`
99
|
1010
= note: this error originates in the derive macro `Clone` (in Nightly builds, run with -Z macro-backtrace for more info)
11+
help: consider annotating `Error` with `#[derive(Clone)]`
12+
|
13+
LL | #[derive(Clone)]
14+
|
1115

1216
error: aborting due to previous error
1317

src/test/ui/derives/derives-span-Clone-tuple-struct.stderr

+4
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ LL | Error
88
| ^^^^^ the trait `Clone` is not implemented for `Error`
99
|
1010
= note: this error originates in the derive macro `Clone` (in Nightly builds, run with -Z macro-backtrace for more info)
11+
help: consider annotating `Error` with `#[derive(Clone)]`
12+
|
13+
LL | #[derive(Clone)]
14+
|
1115

1216
error: aborting due to previous error
1317

src/test/ui/derives/derives-span-Debug-enum-struct-variant.stderr

+4
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ LL | x: Error
1010
= help: the trait `Debug` is not implemented for `Error`
1111
= note: add `#[derive(Debug)]` to `Error` or manually `impl Debug for Error`
1212
= note: this error originates in the derive macro `Debug` (in Nightly builds, run with -Z macro-backtrace for more info)
13+
help: consider annotating `Error` with `#[derive(Debug)]`
14+
|
15+
LL | #[derive(Debug)]
16+
|
1317

1418
error: aborting due to previous error
1519

src/test/ui/derives/derives-span-Debug-enum.stderr

+4
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ LL | Error
1010
= help: the trait `Debug` is not implemented for `Error`
1111
= note: add `#[derive(Debug)]` to `Error` or manually `impl Debug for Error`
1212
= note: this error originates in the derive macro `Debug` (in Nightly builds, run with -Z macro-backtrace for more info)
13+
help: consider annotating `Error` with `#[derive(Debug)]`
14+
|
15+
LL | #[derive(Debug)]
16+
|
1317

1418
error: aborting due to previous error
1519

src/test/ui/derives/derives-span-Debug-struct.stderr

+4
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ LL | x: Error
1010
= help: the trait `Debug` is not implemented for `Error`
1111
= note: add `#[derive(Debug)]` to `Error` or manually `impl Debug for Error`
1212
= note: this error originates in the derive macro `Debug` (in Nightly builds, run with -Z macro-backtrace for more info)
13+
help: consider annotating `Error` with `#[derive(Debug)]`
14+
|
15+
LL | #[derive(Debug)]
16+
|
1317

1418
error: aborting due to previous error
1519

src/test/ui/derives/derives-span-Debug-tuple-struct.stderr

+4
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ LL | Error
1010
= help: the trait `Debug` is not implemented for `Error`
1111
= note: add `#[derive(Debug)]` to `Error` or manually `impl Debug for Error`
1212
= note: this error originates in the derive macro `Debug` (in Nightly builds, run with -Z macro-backtrace for more info)
13+
help: consider annotating `Error` with `#[derive(Debug)]`
14+
|
15+
LL | #[derive(Debug)]
16+
|
1317

1418
error: aborting due to previous error
1519

src/test/ui/derives/derives-span-Default-struct.stderr

+4
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ LL | x: Error
88
| ^^^^^^^^ the trait `Default` is not implemented for `Error`
99
|
1010
= note: this error originates in the derive macro `Default` (in Nightly builds, run with -Z macro-backtrace for more info)
11+
help: consider annotating `Error` with `#[derive(Default)]`
12+
|
13+
LL | #[derive(Default)]
14+
|
1115

1216
error: aborting due to previous error
1317

src/test/ui/derives/derives-span-Default-tuple-struct.stderr

+4
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ LL | Error
88
| ^^^^^ the trait `Default` is not implemented for `Error`
99
|
1010
= note: this error originates in the derive macro `Default` (in Nightly builds, run with -Z macro-backtrace for more info)
11+
help: consider annotating `Error` with `#[derive(Default)]`
12+
|
13+
LL | #[derive(Default)]
14+
|
1115

1216
error: aborting due to previous error
1317

src/test/ui/derives/derives-span-Eq-enum-struct-variant.stderr

+4
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ note: required by a bound in `AssertParamIsEq`
1313
LL | pub struct AssertParamIsEq<T: Eq + ?Sized> {
1414
| ^^ required by this bound in `AssertParamIsEq`
1515
= note: this error originates in the derive macro `Eq` (in Nightly builds, run with -Z macro-backtrace for more info)
16+
help: consider annotating `Error` with `#[derive(Eq)]`
17+
|
18+
LL | #[derive(Eq)]
19+
|
1620

1721
error: aborting due to previous error
1822

src/test/ui/derives/derives-span-Eq-enum.stderr

+4
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ note: required by a bound in `AssertParamIsEq`
1313
LL | pub struct AssertParamIsEq<T: Eq + ?Sized> {
1414
| ^^ required by this bound in `AssertParamIsEq`
1515
= note: this error originates in the derive macro `Eq` (in Nightly builds, run with -Z macro-backtrace for more info)
16+
help: consider annotating `Error` with `#[derive(Eq)]`
17+
|
18+
LL | #[derive(Eq)]
19+
|
1620

1721
error: aborting due to previous error
1822

src/test/ui/derives/derives-span-Eq-struct.stderr

+4
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ note: required by a bound in `AssertParamIsEq`
1313
LL | pub struct AssertParamIsEq<T: Eq + ?Sized> {
1414
| ^^ required by this bound in `AssertParamIsEq`
1515
= note: this error originates in the derive macro `Eq` (in Nightly builds, run with -Z macro-backtrace for more info)
16+
help: consider annotating `Error` with `#[derive(Eq)]`
17+
|
18+
LL | #[derive(Eq)]
19+
|
1620

1721
error: aborting due to previous error
1822

src/test/ui/derives/derives-span-Eq-tuple-struct.stderr

+4
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ note: required by a bound in `AssertParamIsEq`
1313
LL | pub struct AssertParamIsEq<T: Eq + ?Sized> {
1414
| ^^ required by this bound in `AssertParamIsEq`
1515
= note: this error originates in the derive macro `Eq` (in Nightly builds, run with -Z macro-backtrace for more info)
16+
help: consider annotating `Error` with `#[derive(Eq)]`
17+
|
18+
LL | #[derive(Eq)]
19+
|
1620

1721
error: aborting due to previous error
1822

src/test/ui/derives/derives-span-Hash-enum-struct-variant.stderr

+4
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ LL | x: Error
88
| ^^^^^^^^ the trait `Hash` is not implemented for `Error`
99
|
1010
= note: this error originates in the derive macro `Hash` (in Nightly builds, run with -Z macro-backtrace for more info)
11+
help: consider annotating `Error` with `#[derive(Hash)]`
12+
|
13+
LL | #[derive(Hash)]
14+
|
1115

1216
error: aborting due to previous error
1317

src/test/ui/derives/derives-span-Hash-enum.stderr

+4
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ LL | Error
88
| ^^^^^ the trait `Hash` is not implemented for `Error`
99
|
1010
= note: this error originates in the derive macro `Hash` (in Nightly builds, run with -Z macro-backtrace for more info)
11+
help: consider annotating `Error` with `#[derive(Hash)]`
12+
|
13+
LL | #[derive(Hash)]
14+
|
1115

1216
error: aborting due to previous error
1317

src/test/ui/derives/derives-span-Hash-struct.stderr

+4
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ LL | x: Error
88
| ^^^^^^^^ the trait `Hash` is not implemented for `Error`
99
|
1010
= note: this error originates in the derive macro `Hash` (in Nightly builds, run with -Z macro-backtrace for more info)
11+
help: consider annotating `Error` with `#[derive(Hash)]`
12+
|
13+
LL | #[derive(Hash)]
14+
|
1115

1216
error: aborting due to previous error
1317

src/test/ui/derives/derives-span-Hash-tuple-struct.stderr

+4
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ LL | Error
88
| ^^^^^ the trait `Hash` is not implemented for `Error`
99
|
1010
= note: this error originates in the derive macro `Hash` (in Nightly builds, run with -Z macro-backtrace for more info)
11+
help: consider annotating `Error` with `#[derive(Hash)]`
12+
|
13+
LL | #[derive(Hash)]
14+
|
1115

1216
error: aborting due to previous error
1317

src/test/ui/derives/derives-span-Ord-enum-struct-variant.stderr

+4
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ LL | x: Error
88
| ^^^^^^^^ the trait `Ord` is not implemented for `Error`
99
|
1010
= note: this error originates in the derive macro `Ord` (in Nightly builds, run with -Z macro-backtrace for more info)
11+
help: consider annotating `Error` with `#[derive(Ord)]`
12+
|
13+
LL | #[derive(Ord)]
14+
|
1115

1216
error: aborting due to previous error
1317

src/test/ui/derives/derives-span-Ord-enum.stderr

+4
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ LL | Error
88
| ^^^^^ the trait `Ord` is not implemented for `Error`
99
|
1010
= note: this error originates in the derive macro `Ord` (in Nightly builds, run with -Z macro-backtrace for more info)
11+
help: consider annotating `Error` with `#[derive(Ord)]`
12+
|
13+
LL | #[derive(Ord)]
14+
|
1115

1216
error: aborting due to previous error
1317

src/test/ui/derives/derives-span-Ord-struct.stderr

+4
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ LL | x: Error
88
| ^^^^^^^^ the trait `Ord` is not implemented for `Error`
99
|
1010
= note: this error originates in the derive macro `Ord` (in Nightly builds, run with -Z macro-backtrace for more info)
11+
help: consider annotating `Error` with `#[derive(Ord)]`
12+
|
13+
LL | #[derive(Ord)]
14+
|
1115

1216
error: aborting due to previous error
1317

src/test/ui/derives/derives-span-Ord-tuple-struct.stderr

+4
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ LL | Error
88
| ^^^^^ the trait `Ord` is not implemented for `Error`
99
|
1010
= note: this error originates in the derive macro `Ord` (in Nightly builds, run with -Z macro-backtrace for more info)
11+
help: consider annotating `Error` with `#[derive(Ord)]`
12+
|
13+
LL | #[derive(Ord)]
14+
|
1115

1216
error: aborting due to previous error
1317

src/test/ui/derives/derives-span-PartialOrd-enum-struct-variant.stderr

+4
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@ LL | x: Error
99
|
1010
= help: the trait `PartialOrd` is not implemented for `Error`
1111
= note: this error originates in the derive macro `PartialOrd` (in Nightly builds, run with -Z macro-backtrace for more info)
12+
help: consider annotating `Error` with `#[derive(PartialOrd)]`
13+
|
14+
LL | #[derive(PartialOrd)]
15+
|
1216

1317
error: aborting due to previous error
1418

src/test/ui/derives/derives-span-PartialOrd-enum.stderr

+4
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@ LL | Error
99
|
1010
= help: the trait `PartialOrd` is not implemented for `Error`
1111
= note: this error originates in the derive macro `PartialOrd` (in Nightly builds, run with -Z macro-backtrace for more info)
12+
help: consider annotating `Error` with `#[derive(PartialOrd)]`
13+
|
14+
LL | #[derive(PartialOrd)]
15+
|
1216

1317
error: aborting due to previous error
1418

src/test/ui/derives/derives-span-PartialOrd-struct.stderr

+4
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@ LL | x: Error
99
|
1010
= help: the trait `PartialOrd` is not implemented for `Error`
1111
= note: this error originates in the derive macro `PartialOrd` (in Nightly builds, run with -Z macro-backtrace for more info)
12+
help: consider annotating `Error` with `#[derive(PartialOrd)]`
13+
|
14+
LL | #[derive(PartialOrd)]
15+
|
1216

1317
error: aborting due to previous error
1418

src/test/ui/derives/derives-span-PartialOrd-tuple-struct.stderr

+4
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@ LL | Error
99
|
1010
= help: the trait `PartialOrd` is not implemented for `Error`
1111
= note: this error originates in the derive macro `PartialOrd` (in Nightly builds, run with -Z macro-backtrace for more info)
12+
help: consider annotating `Error` with `#[derive(PartialOrd)]`
13+
|
14+
LL | #[derive(PartialOrd)]
15+
|
1216

1317
error: aborting due to previous error
1418

0 commit comments

Comments
 (0)