Skip to content

Commit 717294f

Browse files
committed
Inform the user which trait is meant in the diagnostic itself instead of relying on the span making it obvious
1 parent cb26b35 commit 717294f

File tree

15 files changed

+40
-27
lines changed

15 files changed

+40
-27
lines changed

compiler/rustc_hir_typeck/src/method/suggest.rs

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1853,7 +1853,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
18531853
)],
18541854
) {
18551855
let mut derives = Vec::<(String, Span, Symbol)>::new();
1856-
let mut traits = Vec::<Span>::new();
1856+
let mut traits = Vec::new();
18571857
for (pred, _, _) in unsatisfied_predicates {
18581858
let ty::PredicateKind::Clause(ty::Clause::Trait(trait_pred)) = pred.kind().skip_binder() else { continue };
18591859
let adt = match trait_pred.self_ty().ty_adt_def() {
@@ -1892,10 +1892,10 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
18921892
}
18931893
derives.push((self_name, self_span, diagnostic_name));
18941894
} else {
1895-
traits.push(self.tcx.def_span(trait_pred.def_id()));
1895+
traits.push(trait_pred.def_id());
18961896
}
18971897
} else {
1898-
traits.push(self.tcx.def_span(trait_pred.def_id()));
1898+
traits.push(trait_pred.def_id());
18991899
}
19001900
}
19011901
traits.sort();
@@ -1918,10 +1918,23 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
19181918

19191919
let len = traits.len();
19201920
if len > 0 {
1921-
let span: MultiSpan = traits.into();
1921+
let span =
1922+
MultiSpan::from_spans(traits.iter().map(|&did| self.tcx.def_span(did)).collect());
1923+
let mut names = format!("`{}`", self.tcx.def_path_str(traits[0]));
1924+
for (i, &did) in traits.iter().enumerate().skip(1) {
1925+
if len > 2 {
1926+
names.push_str(", ");
1927+
}
1928+
if i == len - 1 {
1929+
names.push_str(" and ");
1930+
}
1931+
names.push('`');
1932+
names.push_str(&self.tcx.def_path_str(did));
1933+
names.push('`');
1934+
}
19221935
err.span_note(
19231936
span,
1924-
&format!("the following trait{} must be implemented", pluralize!(len),),
1937+
&format!("the trait{} {} must be implemented", pluralize!(len), names),
19251938
);
19261939
}
19271940

src/test/ui/binop/issue-28837.stderr

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ note: an implementation of `Add<_>` might be missing for `A`
1111
|
1212
LL | struct A;
1313
| ^^^^^^^^ must implement `Add<_>`
14-
note: the following trait must be implemented
14+
note: the trait `Add` must be implemented
1515
--> $SRC_DIR/core/src/ops/arith.rs:LL:COL
1616

1717
error[E0369]: cannot subtract `A` from `A`
@@ -27,7 +27,7 @@ note: an implementation of `Sub<_>` might be missing for `A`
2727
|
2828
LL | struct A;
2929
| ^^^^^^^^ must implement `Sub<_>`
30-
note: the following trait must be implemented
30+
note: the trait `Sub` must be implemented
3131
--> $SRC_DIR/core/src/ops/arith.rs:LL:COL
3232

3333
error[E0369]: cannot multiply `A` by `A`
@@ -43,7 +43,7 @@ note: an implementation of `Mul<_>` might be missing for `A`
4343
|
4444
LL | struct A;
4545
| ^^^^^^^^ must implement `Mul<_>`
46-
note: the following trait must be implemented
46+
note: the trait `Mul` must be implemented
4747
--> $SRC_DIR/core/src/ops/arith.rs:LL:COL
4848

4949
error[E0369]: cannot divide `A` by `A`
@@ -59,7 +59,7 @@ note: an implementation of `Div<_>` might be missing for `A`
5959
|
6060
LL | struct A;
6161
| ^^^^^^^^ must implement `Div<_>`
62-
note: the following trait must be implemented
62+
note: the trait `Div` must be implemented
6363
--> $SRC_DIR/core/src/ops/arith.rs:LL:COL
6464

6565
error[E0369]: cannot mod `A` by `A`
@@ -75,7 +75,7 @@ note: an implementation of `Rem<_>` might be missing for `A`
7575
|
7676
LL | struct A;
7777
| ^^^^^^^^ must implement `Rem<_>`
78-
note: the following trait must be implemented
78+
note: the trait `Rem` must be implemented
7979
--> $SRC_DIR/core/src/ops/arith.rs:LL:COL
8080

8181
error[E0369]: no implementation for `A & A`
@@ -91,7 +91,7 @@ note: an implementation of `BitAnd<_>` might be missing for `A`
9191
|
9292
LL | struct A;
9393
| ^^^^^^^^ must implement `BitAnd<_>`
94-
note: the following trait must be implemented
94+
note: the trait `BitAnd` must be implemented
9595
--> $SRC_DIR/core/src/ops/bit.rs:LL:COL
9696

9797
error[E0369]: no implementation for `A | A`
@@ -107,7 +107,7 @@ note: an implementation of `BitOr<_>` might be missing for `A`
107107
|
108108
LL | struct A;
109109
| ^^^^^^^^ must implement `BitOr<_>`
110-
note: the following trait must be implemented
110+
note: the trait `BitOr` must be implemented
111111
--> $SRC_DIR/core/src/ops/bit.rs:LL:COL
112112

113113
error[E0369]: no implementation for `A << A`
@@ -123,7 +123,7 @@ note: an implementation of `Shl<_>` might be missing for `A`
123123
|
124124
LL | struct A;
125125
| ^^^^^^^^ must implement `Shl<_>`
126-
note: the following trait must be implemented
126+
note: the trait `Shl` must be implemented
127127
--> $SRC_DIR/core/src/ops/bit.rs:LL:COL
128128

129129
error[E0369]: no implementation for `A >> A`
@@ -139,7 +139,7 @@ note: an implementation of `Shr<_>` might be missing for `A`
139139
|
140140
LL | struct A;
141141
| ^^^^^^^^ must implement `Shr<_>`
142-
note: the following trait must be implemented
142+
note: the trait `Shr` must be implemented
143143
--> $SRC_DIR/core/src/ops/bit.rs:LL:COL
144144

145145
error[E0369]: binary operation `==` cannot be applied to type `A`

src/test/ui/binop/issue-3820.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ note: an implementation of `Mul<_>` might be missing for `Thing`
1111
|
1212
LL | struct Thing {
1313
| ^^^^^^^^^^^^ must implement `Mul<_>`
14-
note: the following trait must be implemented
14+
note: the trait `Mul` must be implemented
1515
--> $SRC_DIR/core/src/ops/arith.rs:LL:COL
1616

1717
error: aborting due to previous error

src/test/ui/destructuring-assignment/note-unsupported.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ note: an implementation of `AddAssign<_>` might be missing for `S`
4949
|
5050
LL | struct S { x: u8, y: u8 }
5151
| ^^^^^^^^ must implement `AddAssign<_>`
52-
note: the following trait must be implemented
52+
note: the trait `AddAssign` must be implemented
5353
--> $SRC_DIR/core/src/ops/arith.rs:LL:COL
5454

5555
error[E0067]: invalid left-hand side of assignment

src/test/ui/error-festival.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ note: an implementation of `Not` might be missing for `Question`
4141
|
4242
LL | enum Question {
4343
| ^^^^^^^^^^^^^ must implement `Not`
44-
note: the following trait must be implemented
44+
note: the trait `Not` must be implemented
4545
--> $SRC_DIR/core/src/ops/bit.rs:LL:COL
4646

4747
error[E0604]: only `u8` can be cast as `char`, not `u32`

src/test/ui/impl-trait/issues/issue-62742.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ LL | pub struct SafeImpl<T: ?Sized, A: Raw<T>>(PhantomData<(A, T)>);
2525
|
2626
= note: the following trait bounds were not satisfied:
2727
`RawImpl<()>: Raw<()>`
28-
note: the following trait must be implemented
28+
note: the trait `Raw` must be implemented
2929
--> $DIR/issue-62742.rs:12:1
3030
|
3131
LL | pub trait Raw<T: ?Sized> {

src/test/ui/issues/issue-14091-2.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ note: an implementation of `Not` might be missing for `BytePos`
99
|
1010
LL | pub struct BytePos(pub u32);
1111
| ^^^^^^^^^^^^^^^^^^ must implement `Not`
12-
note: the following trait must be implemented
12+
note: the trait `Not` must be implemented
1313
--> $SRC_DIR/core/src/ops/bit.rs:LL:COL
1414
= note: this error originates in the macro `assert` (in Nightly builds, run with -Z macro-backtrace for more info)
1515

src/test/ui/methods/method-call-err-msg.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ LL | .take()
6161
= note: the following trait bounds were not satisfied:
6262
`Foo: Iterator`
6363
which is required by `&mut Foo: Iterator`
64-
note: the following trait must be implemented
64+
note: the trait `Iterator` must be implemented
6565
--> $SRC_DIR/core/src/iter/traits/iterator.rs:LL:COL
6666
= help: items from traits can only be used if the trait is implemented and in scope
6767
= note: the following trait defines an item `take`, perhaps you need to implement it:

src/test/ui/mismatched_types/assignment-operator-unimplemented.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ note: an implementation of `AddAssign<_>` might be missing for `Foo`
1111
|
1212
LL | struct Foo;
1313
| ^^^^^^^^^^ must implement `AddAssign<_>`
14-
note: the following trait must be implemented
14+
note: the trait `AddAssign` must be implemented
1515
--> $SRC_DIR/core/src/ops/arith.rs:LL:COL
1616

1717
error: aborting due to previous error

src/test/ui/or-patterns/or-patterns-syntactic-fail.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ note: an implementation of `BitOr<_>` might be missing for `E`
3535
|
3636
LL | enum E { A, B }
3737
| ^^^^^^ must implement `BitOr<_>`
38-
note: the following trait must be implemented
38+
note: the trait `BitOr` must be implemented
3939
--> $SRC_DIR/core/src/ops/bit.rs:LL:COL
4040

4141
error: aborting due to 5 previous errors

0 commit comments

Comments
 (0)