From 717294fa04dacaa82e8efbad9021c8970bca4bb0 Mon Sep 17 00:00:00 2001 From: Oli Scherer Date: Mon, 12 Dec 2022 11:55:46 +0000 Subject: [PATCH] Inform the user which trait is meant in the diagnostic itself instead of relying on the span making it obvious --- .../rustc_hir_typeck/src/method/suggest.rs | 23 +++++++++++++++---- src/test/ui/binop/issue-28837.stderr | 18 +++++++-------- src/test/ui/binop/issue-3820.stderr | 2 +- .../note-unsupported.stderr | 2 +- src/test/ui/error-festival.stderr | 2 +- .../ui/impl-trait/issues/issue-62742.stderr | 2 +- src/test/ui/issues/issue-14091-2.stderr | 2 +- .../ui/methods/method-call-err-msg.stderr | 2 +- .../assignment-operator-unimplemented.stderr | 2 +- .../or-patterns-syntactic-fail.stderr | 2 +- src/test/ui/span/issue-39018.stderr | 2 +- ...pecialization-trait-not-implemented.stderr | 2 +- .../derive-trait-for-method-call.stderr | 2 +- .../restrict-type-not-param.stderr | 2 +- .../ui/type/type-ascription-precedence.stderr | 2 +- 15 files changed, 40 insertions(+), 27 deletions(-) diff --git a/compiler/rustc_hir_typeck/src/method/suggest.rs b/compiler/rustc_hir_typeck/src/method/suggest.rs index db93cfab2c0..2d3b4663f06 100644 --- a/compiler/rustc_hir_typeck/src/method/suggest.rs +++ b/compiler/rustc_hir_typeck/src/method/suggest.rs @@ -1853,7 +1853,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { )], ) { let mut derives = Vec::<(String, Span, Symbol)>::new(); - let mut traits = Vec::::new(); + let mut traits = Vec::new(); for (pred, _, _) in unsatisfied_predicates { 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() { @@ -1892,10 +1892,10 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { } derives.push((self_name, self_span, diagnostic_name)); } else { - traits.push(self.tcx.def_span(trait_pred.def_id())); + traits.push(trait_pred.def_id()); } } else { - traits.push(self.tcx.def_span(trait_pred.def_id())); + traits.push(trait_pred.def_id()); } } traits.sort(); @@ -1918,10 +1918,23 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { let len = traits.len(); 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( span, - &format!("the following trait{} must be implemented", pluralize!(len),), + &format!("the trait{} {} must be implemented", pluralize!(len), names), ); } diff --git a/src/test/ui/binop/issue-28837.stderr b/src/test/ui/binop/issue-28837.stderr index 89355edf74d..6e236ca5296 100644 --- a/src/test/ui/binop/issue-28837.stderr +++ b/src/test/ui/binop/issue-28837.stderr @@ -11,7 +11,7 @@ note: an implementation of `Add<_>` might be missing for `A` | LL | struct A; | ^^^^^^^^ 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 error[E0369]: cannot subtract `A` from `A` @@ -27,7 +27,7 @@ note: an implementation of `Sub<_>` might be missing for `A` | LL | struct A; | ^^^^^^^^ 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 error[E0369]: cannot multiply `A` by `A` @@ -43,7 +43,7 @@ note: an implementation of `Mul<_>` might be missing for `A` | LL | struct A; | ^^^^^^^^ 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 error[E0369]: cannot divide `A` by `A` @@ -59,7 +59,7 @@ note: an implementation of `Div<_>` might be missing for `A` | LL | struct A; | ^^^^^^^^ 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 error[E0369]: cannot mod `A` by `A` @@ -75,7 +75,7 @@ note: an implementation of `Rem<_>` might be missing for `A` | LL | struct A; | ^^^^^^^^ 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 error[E0369]: no implementation for `A & A` @@ -91,7 +91,7 @@ note: an implementation of `BitAnd<_>` might be missing for `A` | LL | struct A; | ^^^^^^^^ 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 error[E0369]: no implementation for `A | A` @@ -107,7 +107,7 @@ note: an implementation of `BitOr<_>` might be missing for `A` | LL | struct A; | ^^^^^^^^ 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 error[E0369]: no implementation for `A << A` @@ -123,7 +123,7 @@ note: an implementation of `Shl<_>` might be missing for `A` | LL | struct A; | ^^^^^^^^ 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 error[E0369]: no implementation for `A >> A` @@ -139,7 +139,7 @@ note: an implementation of `Shr<_>` might be missing for `A` | LL | struct A; | ^^^^^^^^ 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 error[E0369]: binary operation `==` cannot be applied to type `A` diff --git a/src/test/ui/binop/issue-3820.stderr b/src/test/ui/binop/issue-3820.stderr index 9bf178d1f85..c313ed6037f 100644 --- a/src/test/ui/binop/issue-3820.stderr +++ b/src/test/ui/binop/issue-3820.stderr @@ -11,7 +11,7 @@ note: an implementation of `Mul<_>` might be missing for `Thing` | LL | struct Thing { | ^^^^^^^^^^^^ 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 error: aborting due to previous error diff --git a/src/test/ui/destructuring-assignment/note-unsupported.stderr b/src/test/ui/destructuring-assignment/note-unsupported.stderr index 3b546115a50..8a88332b73e 100644 --- a/src/test/ui/destructuring-assignment/note-unsupported.stderr +++ b/src/test/ui/destructuring-assignment/note-unsupported.stderr @@ -49,7 +49,7 @@ note: an implementation of `AddAssign<_>` might be missing for `S` | LL | struct S { x: u8, y: u8 } | ^^^^^^^^ 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 error[E0067]: invalid left-hand side of assignment diff --git a/src/test/ui/error-festival.stderr b/src/test/ui/error-festival.stderr index 5ff7ec952c1..fe9956b70bd 100644 --- a/src/test/ui/error-festival.stderr +++ b/src/test/ui/error-festival.stderr @@ -41,7 +41,7 @@ note: an implementation of `Not` might be missing for `Question` | LL | enum Question { | ^^^^^^^^^^^^^ 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 error[E0604]: only `u8` can be cast as `char`, not `u32` diff --git a/src/test/ui/impl-trait/issues/issue-62742.stderr b/src/test/ui/impl-trait/issues/issue-62742.stderr index 34f4dc2cef3..bc342dc4689 100644 --- a/src/test/ui/impl-trait/issues/issue-62742.stderr +++ b/src/test/ui/impl-trait/issues/issue-62742.stderr @@ -25,7 +25,7 @@ LL | pub struct SafeImpl>(PhantomData<(A, T)>); | = note: the following trait bounds were not satisfied: `RawImpl<()>: Raw<()>` -note: the following trait must be implemented +note: the trait `Raw` must be implemented --> $DIR/issue-62742.rs:12:1 | LL | pub trait Raw { diff --git a/src/test/ui/issues/issue-14091-2.stderr b/src/test/ui/issues/issue-14091-2.stderr index 2e164f7c53e..f8375d4ef90 100644 --- a/src/test/ui/issues/issue-14091-2.stderr +++ b/src/test/ui/issues/issue-14091-2.stderr @@ -9,7 +9,7 @@ note: an implementation of `Not` might be missing for `BytePos` | LL | pub struct BytePos(pub u32); | ^^^^^^^^^^^^^^^^^^ 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 = note: this error originates in the macro `assert` (in Nightly builds, run with -Z macro-backtrace for more info) diff --git a/src/test/ui/methods/method-call-err-msg.stderr b/src/test/ui/methods/method-call-err-msg.stderr index 13d07ea2e49..3f4e647491e 100644 --- a/src/test/ui/methods/method-call-err-msg.stderr +++ b/src/test/ui/methods/method-call-err-msg.stderr @@ -61,7 +61,7 @@ LL | .take() = note: the following trait bounds were not satisfied: `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 = 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: diff --git a/src/test/ui/mismatched_types/assignment-operator-unimplemented.stderr b/src/test/ui/mismatched_types/assignment-operator-unimplemented.stderr index 53937d29077..2393791a9b2 100644 --- a/src/test/ui/mismatched_types/assignment-operator-unimplemented.stderr +++ b/src/test/ui/mismatched_types/assignment-operator-unimplemented.stderr @@ -11,7 +11,7 @@ note: an implementation of `AddAssign<_>` might be missing for `Foo` | LL | struct Foo; | ^^^^^^^^^^ 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 error: aborting due to previous error diff --git a/src/test/ui/or-patterns/or-patterns-syntactic-fail.stderr b/src/test/ui/or-patterns/or-patterns-syntactic-fail.stderr index a65ad0bb785..10d42b7e3c0 100644 --- a/src/test/ui/or-patterns/or-patterns-syntactic-fail.stderr +++ b/src/test/ui/or-patterns/or-patterns-syntactic-fail.stderr @@ -35,7 +35,7 @@ note: an implementation of `BitOr<_>` might be missing for `E` | LL | enum E { A, B } | ^^^^^^ 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 error: aborting due to 5 previous errors diff --git a/src/test/ui/span/issue-39018.stderr b/src/test/ui/span/issue-39018.stderr index fd15f2ba9d4..5d4d692b2cf 100644 --- a/src/test/ui/span/issue-39018.stderr +++ b/src/test/ui/span/issue-39018.stderr @@ -26,7 +26,7 @@ note: an implementation of `Add<_>` might be missing for `World` | LL | enum World { | ^^^^^^^^^^ 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 error[E0369]: cannot add `String` to `&str` diff --git a/src/test/ui/specialization/defaultimpl/specialization-trait-not-implemented.stderr b/src/test/ui/specialization/defaultimpl/specialization-trait-not-implemented.stderr index 33ca7a2c210..37788612f43 100644 --- a/src/test/ui/specialization/defaultimpl/specialization-trait-not-implemented.stderr +++ b/src/test/ui/specialization/defaultimpl/specialization-trait-not-implemented.stderr @@ -27,7 +27,7 @@ LL | default impl Foo for T { | ^^^^^^^^^^^^^^^^---^^^^^- | | | 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 | LL | trait Foo { diff --git a/src/test/ui/suggestions/derive-trait-for-method-call.stderr b/src/test/ui/suggestions/derive-trait-for-method-call.stderr index f07fd54e07c..14e8a2675dd 100644 --- a/src/test/ui/suggestions/derive-trait-for-method-call.stderr +++ b/src/test/ui/suggestions/derive-trait-for-method-call.stderr @@ -20,7 +20,7 @@ LL | let y = x.test(); `Enum: Clone` `Enum: 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 help: consider annotating `Enum` with `#[derive(Clone)]` | diff --git a/src/test/ui/suggestions/restrict-type-not-param.stderr b/src/test/ui/suggestions/restrict-type-not-param.stderr index cf31c9ebcc7..5434472ceec 100644 --- a/src/test/ui/suggestions/restrict-type-not-param.stderr +++ b/src/test/ui/suggestions/restrict-type-not-param.stderr @@ -11,7 +11,7 @@ note: an implementation of `Add<_>` might be missing for `Wrapper` | LL | struct Wrapper(T); | ^^^^^^^^^^^^^^^^^ 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 help: consider introducing a `where` clause, but there might be an alternative better way to express this requirement | diff --git a/src/test/ui/type/type-ascription-precedence.stderr b/src/test/ui/type/type-ascription-precedence.stderr index ecf5845ca55..edc5aeffdcd 100644 --- a/src/test/ui/type/type-ascription-precedence.stderr +++ b/src/test/ui/type/type-ascription-precedence.stderr @@ -33,7 +33,7 @@ note: an implementation of `std::ops::Neg` might be missing for `Z` | LL | struct Z; | ^^^^^^^^ 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 error[E0308]: mismatched types