Skip to content

Commit 5d5ab8b

Browse files
committed
Use single label for method not found due to unmet bound
1 parent a4419a7 commit 5d5ab8b

13 files changed

+37
-63
lines changed

compiler/rustc_hir_typeck/src/method/suggest.rs

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -459,22 +459,13 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
459459
);
460460
}
461461

462-
let ty_span = match rcvr_ty.kind() {
462+
let mut ty_span = match rcvr_ty.kind() {
463463
ty::Param(param_type) => {
464464
Some(param_type.span_from_generics(self.tcx, self.body_id.to_def_id()))
465465
}
466466
ty::Adt(def, _) if def.did().is_local() => Some(tcx.def_span(def.did())),
467467
_ => None,
468468
};
469-
if let Some(span) = ty_span {
470-
err.span_label(
471-
span,
472-
format!(
473-
"{item_kind} `{item_name}` not found for this {}",
474-
rcvr_ty.prefix_string(self.tcx)
475-
),
476-
);
477-
}
478469

479470
if let SelfSource::MethodCall(rcvr_expr) = source {
480471
self.suggest_fn_call(&mut err, rcvr_expr, rcvr_ty, |output_ty| {
@@ -1201,13 +1192,33 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
12011192
if !tcx.sess.source_map().is_span_accessible(span) {
12021193
continue;
12031194
}
1195+
let pre = if Some(span) == ty_span {
1196+
ty_span.take();
1197+
format!(
1198+
"{item_kind} `{item_name}` not found for this {} because it ",
1199+
rcvr_ty.prefix_string(self.tcx)
1200+
)
1201+
} else {
1202+
String::new()
1203+
};
12041204
let msg = match &bounds[..] {
1205-
[bound] => format!("doesn't satisfy {bound}"),
1206-
[bounds @ .., last] => format!("doesn't satisfy {} or {last}", bounds.join(", ")),
1205+
[bound] => format!("{pre}doesn't satisfy {bound}"),
1206+
[bounds @ .., last] => {
1207+
format!("{pre}doesn't satisfy {} or {last}", bounds.join(", "))
1208+
}
12071209
[] => unreachable!(),
12081210
};
12091211
err.span_label(span, msg);
12101212
}
1213+
if let Some(span) = ty_span {
1214+
err.span_label(
1215+
span,
1216+
format!(
1217+
"{item_kind} `{item_name}` not found for this {}",
1218+
rcvr_ty.prefix_string(self.tcx)
1219+
),
1220+
);
1221+
}
12111222

12121223
if rcvr_ty.is_numeric() && rcvr_ty.is_fresh() || restrict_type_params {
12131224
} else {

tests/ui/derives/derive-assoc-type-not-impl.stderr

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,7 @@ error[E0599]: the method `clone` exists for struct `Bar<NotClone>`, but its trai
22
--> $DIR/derive-assoc-type-not-impl.rs:18:30
33
|
44
LL | struct Bar<T: Foo> {
5-
| ------------------
6-
| |
7-
| method `clone` not found for this struct
8-
| doesn't satisfy `Bar<NotClone>: Clone`
5+
| ------------------ method `clone` not found for this struct because it doesn't satisfy `Bar<NotClone>: Clone`
96
...
107
LL | struct NotClone;
118
| --------------- doesn't satisfy `NotClone: Clone`

tests/ui/derives/deriving-with-repr-packed-2.stderr

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,7 @@ error[E0599]: the method `clone` exists for struct `Foo<NonCopy>`, but its trait
22
--> $DIR/deriving-with-repr-packed-2.rs:18:11
33
|
44
LL | pub struct Foo<T>(T, T, T);
5-
| -----------------
6-
| |
7-
| method `clone` not found for this struct
8-
| doesn't satisfy `Foo<NonCopy>: Clone`
5+
| ----------------- method `clone` not found for this struct because it doesn't satisfy `Foo<NonCopy>: Clone`
96
LL |
107
LL | struct NonCopy;
118
| -------------- doesn't satisfy `NonCopy: Clone` or `NonCopy: Copy`

tests/ui/generic-associated-types/issue-119942-unsatisified-gat-bound-during-assoc-ty-selection.stderr

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,7 @@ error[E0599]: the size for values of type `Node<i32, RcFamily>` cannot be known
1919
--> $DIR/issue-119942-unsatisified-gat-bound-during-assoc-ty-selection.rs:31:35
2020
|
2121
LL | enum Node<T, P: PointerFamily> {
22-
| ------------------------------
23-
| |
24-
| variant or associated item `new` not found for this enum
25-
| doesn't satisfy `Node<i32, RcFamily>: Sized`
22+
| ------------------------------ variant or associated item `new` not found for this enum because it doesn't satisfy `Node<i32, RcFamily>: Sized`
2623
...
2724
LL | let mut list = RcNode::<i32>::new();
2825
| ^^^ doesn't have a size known at compile-time

tests/ui/generic-associated-types/method-unsatisfied-assoc-type-predicate.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,7 @@ impl<T: X<Y<i32> = i32>> M for T {}
1616
//~| NOTE
1717

1818
struct S;
19-
//~^ NOTE method `f` not found for this
20-
//~| NOTE doesn't satisfy `<S as X>::Y<i32> = i32` or `S: M`
19+
//~^ NOTE method `f` not found for this struct because it doesn't satisfy `<S as X>::Y<i32> = i32` or `S: M`
2120

2221
impl X for S {
2322
type Y<T> = bool;

tests/ui/generic-associated-types/method-unsatisfied-assoc-type-predicate.stderr

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
11
error[E0599]: the method `f` exists for struct `S`, but its trait bounds were not satisfied
2-
--> $DIR/method-unsatisfied-assoc-type-predicate.rs:27:7
2+
--> $DIR/method-unsatisfied-assoc-type-predicate.rs:26:7
33
|
44
LL | struct S;
5-
| --------
6-
| |
7-
| method `f` not found for this struct
8-
| doesn't satisfy `<S as X>::Y<i32> = i32` or `S: M`
5+
| -------- method `f` not found for this struct because it doesn't satisfy `<S as X>::Y<i32> = i32` or `S: M`
96
...
107
LL | a.f();
118
| ^ method cannot be called on `S` due to unsatisfied trait bounds

tests/ui/higher-ranked/trait-bounds/issue-30786.stderr

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,7 @@ error[E0599]: the method `filterx` exists for struct `Map<Repeat, {closure@issue
22
--> $DIR/issue-30786.rs:120:22
33
|
44
LL | pub struct Map<S, F> {
5-
| --------------------
6-
| |
7-
| method `filterx` not found for this struct
8-
| doesn't satisfy `_: StreamExt`
5+
| -------------------- method `filterx` not found for this struct because it doesn't satisfy `_: StreamExt`
96
...
107
LL | let filter = map.filterx(|x: &_| true);
118
| ^^^^^^^ method cannot be called on `Map<Repeat, {[email protected]:119:27}>` due to unsatisfied trait bounds
@@ -23,10 +20,7 @@ error[E0599]: the method `countx` exists for struct `Filter<Map<Repeat, fn(&u64)
2320
--> $DIR/issue-30786.rs:132:24
2421
|
2522
LL | pub struct Filter<S, F> {
26-
| -----------------------
27-
| |
28-
| method `countx` not found for this struct
29-
| doesn't satisfy `_: StreamExt`
23+
| ----------------------- method `countx` not found for this struct because it doesn't satisfy `_: StreamExt`
3024
...
3125
LL | let count = filter.countx();
3226
| ^^^^^^ method cannot be called due to unsatisfied trait bounds

tests/ui/methods/method-call-err-msg.stderr

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,7 @@ error[E0599]: `Foo` is not an iterator
4949
--> $DIR/method-call-err-msg.rs:19:7
5050
|
5151
LL | pub struct Foo;
52-
| --------------
53-
| |
54-
| method `take` not found for this struct
55-
| doesn't satisfy `Foo: Iterator`
52+
| -------------- method `take` not found for this struct because it doesn't satisfy `Foo: Iterator`
5653
...
5754
LL | / y.zero()
5855
LL | | .take()

tests/ui/specialization/defaultimpl/specialization-trait-not-implemented.stderr

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,7 @@ error[E0599]: the method `foo_one` exists for struct `MyStruct`, but its trait b
1212
--> $DIR/specialization-trait-not-implemented.rs:22:29
1313
|
1414
LL | struct MyStruct;
15-
| ---------------
16-
| |
17-
| method `foo_one` not found for this struct
18-
| doesn't satisfy `MyStruct: Foo`
15+
| --------------- method `foo_one` not found for this struct because it doesn't satisfy `MyStruct: Foo`
1916
...
2017
LL | println!("{}", MyStruct.foo_one());
2118
| ^^^^^^^ method cannot be called on `MyStruct` due to unsatisfied trait bounds

tests/ui/trait-bounds/impl-derived-implicit-sized-bound-2.stderr

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,7 @@ error[E0599]: the method `get` exists for struct `Victim<'_, Self>`, but its tra
22
--> $DIR/impl-derived-implicit-sized-bound-2.rs:28:19
33
|
44
LL | struct Victim<'a, T: Perpetrator + ?Sized> {
5-
| ------------------------------------------
6-
| |
7-
| method `get` not found for this struct
8-
| doesn't satisfy `Victim<'_, Self>: VictimTrait`
5+
| ------------------------------------------ method `get` not found for this struct because it doesn't satisfy `Victim<'_, Self>: VictimTrait`
96
...
107
LL | self.getter().get();
118
| ^^^ method cannot be called on `Victim<'_, Self>` due to unsatisfied trait bounds

tests/ui/trait-bounds/impl-derived-implicit-sized-bound.stderr

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,7 @@ error[E0599]: the method `get` exists for struct `Victim<'_, Self>`, but its tra
22
--> $DIR/impl-derived-implicit-sized-bound.rs:31:19
33
|
44
LL | struct Victim<'a, T: Perpetrator + ?Sized>
5-
| ------------------------------------------
6-
| |
7-
| method `get` not found for this struct
8-
| doesn't satisfy `Victim<'_, Self>: VictimTrait`
5+
| ------------------------------------------ method `get` not found for this struct because it doesn't satisfy `Victim<'_, Self>: VictimTrait`
96
...
107
LL | self.getter().get();
118
| ^^^ method cannot be called on `Victim<'_, Self>` due to unsatisfied trait bounds

tests/ui/typeck/derive-sugg-arg-arity.stderr

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,7 @@ error[E0599]: the function or associated item `partial_cmp` exists for struct `A
22
--> $DIR/derive-sugg-arg-arity.rs:5:23
33
|
44
LL | pub struct A;
5-
| ------------
6-
| |
7-
| function or associated item `partial_cmp` not found for this struct
8-
| doesn't satisfy `A: Iterator` or `A: PartialOrd<_>`
5+
| ------------ function or associated item `partial_cmp` not found for this struct because it doesn't satisfy `A: Iterator` or `A: PartialOrd<_>`
96
...
107
LL | _ => match A::partial_cmp() {},
118
| ^^^^^^^^^^^ function or associated item cannot be called on `A` due to unsatisfied trait bounds

tests/ui/union/union-derive-clone.stderr

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,7 @@ error[E0599]: the method `clone` exists for union `U5<CloneNoCopy>`, but its tra
1717
--> $DIR/union-derive-clone.rs:35:15
1818
|
1919
LL | union U5<T> {
20-
| -----------
21-
| |
22-
| method `clone` not found for this union
23-
| doesn't satisfy `U5<CloneNoCopy>: Clone`
20+
| ----------- method `clone` not found for this union because it doesn't satisfy `U5<CloneNoCopy>: Clone`
2421
...
2522
LL | struct CloneNoCopy;
2623
| ------------------ doesn't satisfy `CloneNoCopy: Copy`

0 commit comments

Comments
 (0)