1
Fork 0

Inform the user which trait is meant in the diagnostic itself instead of relying on the span making it obvious

This commit is contained in:
Oli Scherer 2022-12-12 11:55:46 +00:00
parent cb26b35b12
commit 717294fa04
15 changed files with 40 additions and 27 deletions

View file

@ -1853,7 +1853,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
)], )],
) { ) {
let mut derives = Vec::<(String, Span, Symbol)>::new(); let mut derives = Vec::<(String, Span, Symbol)>::new();
let mut traits = Vec::<Span>::new(); let mut traits = Vec::new();
for (pred, _, _) in unsatisfied_predicates { for (pred, _, _) in unsatisfied_predicates {
let ty::PredicateKind::Clause(ty::Clause::Trait(trait_pred)) = pred.kind().skip_binder() else { continue }; let ty::PredicateKind::Clause(ty::Clause::Trait(trait_pred)) = pred.kind().skip_binder() else { continue };
let adt = match trait_pred.self_ty().ty_adt_def() { let adt = match trait_pred.self_ty().ty_adt_def() {
@ -1892,10 +1892,10 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
} }
derives.push((self_name, self_span, diagnostic_name)); derives.push((self_name, self_span, diagnostic_name));
} else { } else {
traits.push(self.tcx.def_span(trait_pred.def_id())); traits.push(trait_pred.def_id());
} }
} else { } else {
traits.push(self.tcx.def_span(trait_pred.def_id())); traits.push(trait_pred.def_id());
} }
} }
traits.sort(); traits.sort();
@ -1918,10 +1918,23 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
let len = traits.len(); let len = traits.len();
if len > 0 { if len > 0 {
let span: MultiSpan = traits.into(); let span =
MultiSpan::from_spans(traits.iter().map(|&did| self.tcx.def_span(did)).collect());
let mut names = format!("`{}`", self.tcx.def_path_str(traits[0]));
for (i, &did) in traits.iter().enumerate().skip(1) {
if len > 2 {
names.push_str(", ");
}
if i == len - 1 {
names.push_str(" and ");
}
names.push('`');
names.push_str(&self.tcx.def_path_str(did));
names.push('`');
}
err.span_note( err.span_note(
span, span,
&format!("the following trait{} must be implemented", pluralize!(len),), &format!("the trait{} {} must be implemented", pluralize!(len), names),
); );
} }

View file

@ -11,7 +11,7 @@ note: an implementation of `Add<_>` might be missing for `A`
| |
LL | struct A; LL | struct A;
| ^^^^^^^^ must implement `Add<_>` | ^^^^^^^^ must implement `Add<_>`
note: the following trait must be implemented note: the trait `Add` must be implemented
--> $SRC_DIR/core/src/ops/arith.rs:LL:COL --> $SRC_DIR/core/src/ops/arith.rs:LL:COL
error[E0369]: cannot subtract `A` from `A` error[E0369]: cannot subtract `A` from `A`
@ -27,7 +27,7 @@ note: an implementation of `Sub<_>` might be missing for `A`
| |
LL | struct A; LL | struct A;
| ^^^^^^^^ must implement `Sub<_>` | ^^^^^^^^ must implement `Sub<_>`
note: the following trait must be implemented note: the trait `Sub` must be implemented
--> $SRC_DIR/core/src/ops/arith.rs:LL:COL --> $SRC_DIR/core/src/ops/arith.rs:LL:COL
error[E0369]: cannot multiply `A` by `A` error[E0369]: cannot multiply `A` by `A`
@ -43,7 +43,7 @@ note: an implementation of `Mul<_>` might be missing for `A`
| |
LL | struct A; LL | struct A;
| ^^^^^^^^ must implement `Mul<_>` | ^^^^^^^^ must implement `Mul<_>`
note: the following trait must be implemented note: the trait `Mul` must be implemented
--> $SRC_DIR/core/src/ops/arith.rs:LL:COL --> $SRC_DIR/core/src/ops/arith.rs:LL:COL
error[E0369]: cannot divide `A` by `A` error[E0369]: cannot divide `A` by `A`
@ -59,7 +59,7 @@ note: an implementation of `Div<_>` might be missing for `A`
| |
LL | struct A; LL | struct A;
| ^^^^^^^^ must implement `Div<_>` | ^^^^^^^^ must implement `Div<_>`
note: the following trait must be implemented note: the trait `Div` must be implemented
--> $SRC_DIR/core/src/ops/arith.rs:LL:COL --> $SRC_DIR/core/src/ops/arith.rs:LL:COL
error[E0369]: cannot mod `A` by `A` error[E0369]: cannot mod `A` by `A`
@ -75,7 +75,7 @@ note: an implementation of `Rem<_>` might be missing for `A`
| |
LL | struct A; LL | struct A;
| ^^^^^^^^ must implement `Rem<_>` | ^^^^^^^^ must implement `Rem<_>`
note: the following trait must be implemented note: the trait `Rem` must be implemented
--> $SRC_DIR/core/src/ops/arith.rs:LL:COL --> $SRC_DIR/core/src/ops/arith.rs:LL:COL
error[E0369]: no implementation for `A & A` error[E0369]: no implementation for `A & A`
@ -91,7 +91,7 @@ note: an implementation of `BitAnd<_>` might be missing for `A`
| |
LL | struct A; LL | struct A;
| ^^^^^^^^ must implement `BitAnd<_>` | ^^^^^^^^ must implement `BitAnd<_>`
note: the following trait must be implemented note: the trait `BitAnd` must be implemented
--> $SRC_DIR/core/src/ops/bit.rs:LL:COL --> $SRC_DIR/core/src/ops/bit.rs:LL:COL
error[E0369]: no implementation for `A | A` error[E0369]: no implementation for `A | A`
@ -107,7 +107,7 @@ note: an implementation of `BitOr<_>` might be missing for `A`
| |
LL | struct A; LL | struct A;
| ^^^^^^^^ must implement `BitOr<_>` | ^^^^^^^^ must implement `BitOr<_>`
note: the following trait must be implemented note: the trait `BitOr` must be implemented
--> $SRC_DIR/core/src/ops/bit.rs:LL:COL --> $SRC_DIR/core/src/ops/bit.rs:LL:COL
error[E0369]: no implementation for `A << A` error[E0369]: no implementation for `A << A`
@ -123,7 +123,7 @@ note: an implementation of `Shl<_>` might be missing for `A`
| |
LL | struct A; LL | struct A;
| ^^^^^^^^ must implement `Shl<_>` | ^^^^^^^^ must implement `Shl<_>`
note: the following trait must be implemented note: the trait `Shl` must be implemented
--> $SRC_DIR/core/src/ops/bit.rs:LL:COL --> $SRC_DIR/core/src/ops/bit.rs:LL:COL
error[E0369]: no implementation for `A >> A` error[E0369]: no implementation for `A >> A`
@ -139,7 +139,7 @@ note: an implementation of `Shr<_>` might be missing for `A`
| |
LL | struct A; LL | struct A;
| ^^^^^^^^ must implement `Shr<_>` | ^^^^^^^^ must implement `Shr<_>`
note: the following trait must be implemented note: the trait `Shr` must be implemented
--> $SRC_DIR/core/src/ops/bit.rs:LL:COL --> $SRC_DIR/core/src/ops/bit.rs:LL:COL
error[E0369]: binary operation `==` cannot be applied to type `A` error[E0369]: binary operation `==` cannot be applied to type `A`

View file

@ -11,7 +11,7 @@ note: an implementation of `Mul<_>` might be missing for `Thing`
| |
LL | struct Thing { LL | struct Thing {
| ^^^^^^^^^^^^ must implement `Mul<_>` | ^^^^^^^^^^^^ must implement `Mul<_>`
note: the following trait must be implemented note: the trait `Mul` must be implemented
--> $SRC_DIR/core/src/ops/arith.rs:LL:COL --> $SRC_DIR/core/src/ops/arith.rs:LL:COL
error: aborting due to previous error error: aborting due to previous error

View file

@ -49,7 +49,7 @@ note: an implementation of `AddAssign<_>` might be missing for `S`
| |
LL | struct S { x: u8, y: u8 } LL | struct S { x: u8, y: u8 }
| ^^^^^^^^ must implement `AddAssign<_>` | ^^^^^^^^ must implement `AddAssign<_>`
note: the following trait must be implemented note: the trait `AddAssign` must be implemented
--> $SRC_DIR/core/src/ops/arith.rs:LL:COL --> $SRC_DIR/core/src/ops/arith.rs:LL:COL
error[E0067]: invalid left-hand side of assignment error[E0067]: invalid left-hand side of assignment

View file

@ -41,7 +41,7 @@ note: an implementation of `Not` might be missing for `Question`
| |
LL | enum Question { LL | enum Question {
| ^^^^^^^^^^^^^ must implement `Not` | ^^^^^^^^^^^^^ must implement `Not`
note: the following trait must be implemented note: the trait `Not` must be implemented
--> $SRC_DIR/core/src/ops/bit.rs:LL:COL --> $SRC_DIR/core/src/ops/bit.rs:LL:COL
error[E0604]: only `u8` can be cast as `char`, not `u32` error[E0604]: only `u8` can be cast as `char`, not `u32`

View file

@ -25,7 +25,7 @@ LL | pub struct SafeImpl<T: ?Sized, A: Raw<T>>(PhantomData<(A, T)>);
| |
= note: the following trait bounds were not satisfied: = note: the following trait bounds were not satisfied:
`RawImpl<()>: Raw<()>` `RawImpl<()>: Raw<()>`
note: the following trait must be implemented note: the trait `Raw` must be implemented
--> $DIR/issue-62742.rs:12:1 --> $DIR/issue-62742.rs:12:1
| |
LL | pub trait Raw<T: ?Sized> { LL | pub trait Raw<T: ?Sized> {

View file

@ -9,7 +9,7 @@ note: an implementation of `Not` might be missing for `BytePos`
| |
LL | pub struct BytePos(pub u32); LL | pub struct BytePos(pub u32);
| ^^^^^^^^^^^^^^^^^^ must implement `Not` | ^^^^^^^^^^^^^^^^^^ must implement `Not`
note: the following trait must be implemented note: the trait `Not` must be implemented
--> $SRC_DIR/core/src/ops/bit.rs:LL:COL --> $SRC_DIR/core/src/ops/bit.rs:LL:COL
= note: this error originates in the macro `assert` (in Nightly builds, run with -Z macro-backtrace for more info) = note: this error originates in the macro `assert` (in Nightly builds, run with -Z macro-backtrace for more info)

View file

@ -61,7 +61,7 @@ LL | .take()
= note: the following trait bounds were not satisfied: = note: the following trait bounds were not satisfied:
`Foo: Iterator` `Foo: Iterator`
which is required by `&mut Foo: Iterator` which is required by `&mut Foo: Iterator`
note: the following trait must be implemented note: the trait `Iterator` must be implemented
--> $SRC_DIR/core/src/iter/traits/iterator.rs:LL:COL --> $SRC_DIR/core/src/iter/traits/iterator.rs:LL:COL
= help: items from traits can only be used if the trait is implemented and in scope = help: items from traits can only be used if the trait is implemented and in scope
= note: the following trait defines an item `take`, perhaps you need to implement it: = note: the following trait defines an item `take`, perhaps you need to implement it:

View file

@ -11,7 +11,7 @@ note: an implementation of `AddAssign<_>` might be missing for `Foo`
| |
LL | struct Foo; LL | struct Foo;
| ^^^^^^^^^^ must implement `AddAssign<_>` | ^^^^^^^^^^ must implement `AddAssign<_>`
note: the following trait must be implemented note: the trait `AddAssign` must be implemented
--> $SRC_DIR/core/src/ops/arith.rs:LL:COL --> $SRC_DIR/core/src/ops/arith.rs:LL:COL
error: aborting due to previous error error: aborting due to previous error

View file

@ -35,7 +35,7 @@ note: an implementation of `BitOr<_>` might be missing for `E`
| |
LL | enum E { A, B } LL | enum E { A, B }
| ^^^^^^ must implement `BitOr<_>` | ^^^^^^ must implement `BitOr<_>`
note: the following trait must be implemented note: the trait `BitOr` must be implemented
--> $SRC_DIR/core/src/ops/bit.rs:LL:COL --> $SRC_DIR/core/src/ops/bit.rs:LL:COL
error: aborting due to 5 previous errors error: aborting due to 5 previous errors

View file

@ -26,7 +26,7 @@ note: an implementation of `Add<_>` might be missing for `World`
| |
LL | enum World { LL | enum World {
| ^^^^^^^^^^ must implement `Add<_>` | ^^^^^^^^^^ must implement `Add<_>`
note: the following trait must be implemented note: the trait `Add` must be implemented
--> $SRC_DIR/core/src/ops/arith.rs:LL:COL --> $SRC_DIR/core/src/ops/arith.rs:LL:COL
error[E0369]: cannot add `String` to `&str` error[E0369]: cannot add `String` to `&str`

View file

@ -27,7 +27,7 @@ LL | default impl<T> Foo for T {
| ^^^^^^^^^^^^^^^^---^^^^^- | ^^^^^^^^^^^^^^^^---^^^^^-
| | | |
| unsatisfied trait bound introduced here | unsatisfied trait bound introduced here
note: the following trait must be implemented note: the trait `Foo` must be implemented
--> $DIR/specialization-trait-not-implemented.rs:7:1 --> $DIR/specialization-trait-not-implemented.rs:7:1
| |
LL | trait Foo { LL | trait Foo {

View file

@ -20,7 +20,7 @@ LL | let y = x.test();
`Enum: Clone` `Enum: Clone`
`Enum: Default` `Enum: Default`
`CloneEnum: Default` `CloneEnum: Default`
note: the following trait must be implemented note: the trait `Default` must be implemented
--> $SRC_DIR/core/src/default.rs:LL:COL --> $SRC_DIR/core/src/default.rs:LL:COL
help: consider annotating `Enum` with `#[derive(Clone)]` help: consider annotating `Enum` with `#[derive(Clone)]`
| |

View file

@ -11,7 +11,7 @@ note: an implementation of `Add<_>` might be missing for `Wrapper<T>`
| |
LL | struct Wrapper<T>(T); LL | struct Wrapper<T>(T);
| ^^^^^^^^^^^^^^^^^ must implement `Add<_>` | ^^^^^^^^^^^^^^^^^ must implement `Add<_>`
note: the following trait must be implemented note: the trait `Add` must be implemented
--> $SRC_DIR/core/src/ops/arith.rs:LL:COL --> $SRC_DIR/core/src/ops/arith.rs:LL:COL
help: consider introducing a `where` clause, but there might be an alternative better way to express this requirement help: consider introducing a `where` clause, but there might be an alternative better way to express this requirement
| |

View file

@ -33,7 +33,7 @@ note: an implementation of `std::ops::Neg` might be missing for `Z`
| |
LL | struct Z; LL | struct Z;
| ^^^^^^^^ must implement `std::ops::Neg` | ^^^^^^^^ must implement `std::ops::Neg`
note: the following trait must be implemented note: the trait `std::ops::Neg` must be implemented
--> $SRC_DIR/core/src/ops/arith.rs:LL:COL --> $SRC_DIR/core/src/ops/arith.rs:LL:COL
error[E0308]: mismatched types error[E0308]: mismatched types