Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit 2838b8e

Browse files
committed
Point at method call when it is the source of the bound error
1 parent ce486d5 commit 2838b8e

29 files changed

+96
-164
lines changed

compiler/rustc_errors/src/diagnostic.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -370,7 +370,11 @@ impl Diagnostic {
370370
self.set_span(after);
371371
for span_label in before.span_labels() {
372372
if let Some(label) = span_label.label {
373-
self.span.push_span_label(after, label);
373+
if span_label.is_primary {
374+
self.span.push_span_label(after, label);
375+
} else {
376+
self.span.push_span_label(span_label.span, label);
377+
}
374378
}
375379
}
376380
self

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3108,6 +3108,16 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
31083108
point_at_chain(expr);
31093109
}
31103110
}
3111+
let call_node = hir.find(call_hir_id);
3112+
if let Some(Node::Expr(hir::Expr {
3113+
kind: hir::ExprKind::MethodCall(path, rcvr, ..),
3114+
..
3115+
})) = call_node
3116+
{
3117+
if Some(rcvr.span) == err.span.primary_span() {
3118+
err.replace_span_with(path.ident.span);
3119+
}
3120+
}
31113121
if let Some(Node::Expr(hir::Expr {
31123122
kind:
31133123
hir::ExprKind::Call(hir::Expr { span, .. }, _)

src/test/ui/generic-associated-types/issue-101020.stderr

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
error[E0277]: the trait bound `for<'a> &'a mut (): Foo<&'a mut ()>` is not satisfied
2-
--> $DIR/issue-101020.rs:31:5
2+
--> $DIR/issue-101020.rs:31:22
33
|
44
LL | (&mut EmptyIter).consume(());
5-
| ^^^^^^^^^^^^^^^^ ------- required by a bound introduced by this call
6-
| |
7-
| the trait `for<'a> Foo<&'a mut ()>` is not implemented for `&'a mut ()`
5+
| ^^^^^^^ the trait `for<'a> Foo<&'a mut ()>` is not implemented for `&'a mut ()`
86
|
97
note: required for `&'a mut ()` to implement `for<'a> FuncInput<'a, &'a mut ()>`
108
--> $DIR/issue-101020.rs:27:20

src/test/ui/issues/issue-20162.stderr

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
error[E0277]: the trait bound `X: Ord` is not satisfied
2-
--> $DIR/issue-20162.rs:5:5
2+
--> $DIR/issue-20162.rs:5:7
33
|
44
LL | b.sort();
5-
| ^ ---- required by a bound introduced by this call
6-
| |
7-
| the trait `Ord` is not implemented for `X`
5+
| ^^^^ the trait `Ord` is not implemented for `X`
86
|
97
note: required by a bound in `slice::<impl [T]>::sort`
108
--> $SRC_DIR/alloc/src/slice.rs:LL:COL

src/test/ui/issues/issue-31173.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,11 @@ pub fn get_tok(it: &mut IntoIter<u8>) {
44
let mut found_e = false;
55

66
let temp: Vec<u8> = it
7-
//~^ ERROR to be an iterator that yields `&_`, but it yields `u8`
87
.take_while(|&x| {
98
found_e = true;
109
false
1110
})
12-
.cloned()
11+
.cloned() //~ ERROR to be an iterator that yields `&_`, but it yields `u8`
1312
.collect(); //~ ERROR the method
1413
}
1514

src/test/ui/issues/issue-31173.stderr

Lines changed: 12 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,8 @@
1-
error[E0271]: expected `TakeWhile<&mut std::vec::IntoIter<u8>, [closure@$DIR/issue-31173.rs:8:21: 8:25]>` to be an iterator that yields `&_`, but it yields `u8`
2-
--> $DIR/issue-31173.rs:6:25
3-
|
4-
LL | let temp: Vec<u8> = it
5-
| _________________________^
6-
LL | |
7-
LL | | .take_while(|&x| {
8-
LL | | found_e = true;
9-
LL | | false
10-
LL | | })
11-
| |__________^ expected reference, found `u8`
12-
LL | .cloned()
13-
| ------ required by a bound introduced by this call
1+
error[E0271]: expected `TakeWhile<&mut std::vec::IntoIter<u8>, [closure@$DIR/issue-31173.rs:7:21: 7:25]>` to be an iterator that yields `&_`, but it yields `u8`
2+
--> $DIR/issue-31173.rs:11:10
3+
|
4+
LL | .cloned()
5+
| ^^^^^^ expected reference, found `u8`
146
|
157
= note: expected reference `&_`
168
found type `u8`
@@ -20,11 +12,11 @@ note: required by a bound in `cloned`
2012
LL | Self: Sized + Iterator<Item = &'a T>,
2113
| ^^^^^^^^^^^^ required by this bound in `Iterator::cloned`
2214

23-
error[E0599]: the method `collect` exists for struct `Cloned<TakeWhile<&mut std::vec::IntoIter<u8>, [closure@$DIR/issue-31173.rs:8:21: 8:25]>>`, but its trait bounds were not satisfied
24-
--> $DIR/issue-31173.rs:13:10
15+
error[E0599]: the method `collect` exists for struct `Cloned<TakeWhile<&mut std::vec::IntoIter<u8>, [closure@$DIR/issue-31173.rs:7:21: 7:25]>>`, but its trait bounds were not satisfied
16+
--> $DIR/issue-31173.rs:12:10
2517
|
2618
LL | .collect();
27-
| ^^^^^^^ method cannot be called on `Cloned<TakeWhile<&mut std::vec::IntoIter<u8>, [closure@$DIR/issue-31173.rs:8:21: 8:25]>>` due to unsatisfied trait bounds
19+
| ^^^^^^^ method cannot be called on `Cloned<TakeWhile<&mut std::vec::IntoIter<u8>, [closure@$DIR/issue-31173.rs:7:21: 7:25]>>` due to unsatisfied trait bounds
2820
|
2921
::: $SRC_DIR/core/src/iter/adapters/take_while.rs:LL:COL
3022
|
@@ -37,10 +29,10 @@ LL | pub struct Cloned<I> {
3729
| -------------------- doesn't satisfy `_: Iterator`
3830
|
3931
= note: the following trait bounds were not satisfied:
40-
`<TakeWhile<&mut std::vec::IntoIter<u8>, [closure@$DIR/issue-31173.rs:8:21: 8:25]> as Iterator>::Item = &_`
41-
which is required by `Cloned<TakeWhile<&mut std::vec::IntoIter<u8>, [closure@$DIR/issue-31173.rs:8:21: 8:25]>>: Iterator`
42-
`Cloned<TakeWhile<&mut std::vec::IntoIter<u8>, [closure@$DIR/issue-31173.rs:8:21: 8:25]>>: Iterator`
43-
which is required by `&mut Cloned<TakeWhile<&mut std::vec::IntoIter<u8>, [closure@$DIR/issue-31173.rs:8:21: 8:25]>>: Iterator`
32+
`<TakeWhile<&mut std::vec::IntoIter<u8>, [closure@$DIR/issue-31173.rs:7:21: 7:25]> as Iterator>::Item = &_`
33+
which is required by `Cloned<TakeWhile<&mut std::vec::IntoIter<u8>, [closure@$DIR/issue-31173.rs:7:21: 7:25]>>: Iterator`
34+
`Cloned<TakeWhile<&mut std::vec::IntoIter<u8>, [closure@$DIR/issue-31173.rs:7:21: 7:25]>>: Iterator`
35+
which is required by `&mut Cloned<TakeWhile<&mut std::vec::IntoIter<u8>, [closure@$DIR/issue-31173.rs:7:21: 7:25]>>: Iterator`
4436

4537
error: aborting due to 2 previous errors
4638

src/test/ui/issues/issue-33941.stderr

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
error[E0271]: expected `std::collections::hash_map::Iter<'_, _, _>` to be an iterator that yields `&_`, but it yields `(&_, &_)`
2-
--> $DIR/issue-33941.rs:6:14
2+
--> $DIR/issue-33941.rs:6:36
33
|
44
LL | for _ in HashMap::new().iter().cloned() {}
5-
| ^^^^^^^^^^^^^^^^^^^^^ ------ required by a bound introduced by this call
6-
| |
7-
| expected reference, found tuple
5+
| ^^^^^^ expected reference, found tuple
86
|
97
= note: expected reference `&_`
108
found tuple `(&_, &_)`

src/test/ui/issues/issue-34334.stderr

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,10 @@ LL | let sr: Vec<(u32, _, _)> = vec![];
1313
| +
1414

1515
error[E0277]: a value of type `Vec<(u32, _, _)>` cannot be built from an iterator over elements of type `()`
16-
--> $DIR/issue-34334.rs:5:33
16+
--> $DIR/issue-34334.rs:5:87
1717
|
1818
LL | let sr2: Vec<(u32, _, _)> = sr.iter().map(|(faction, th_sender, th_receiver)| {}).collect();
19-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ------- required by a bound introduced by this call
20-
| |
21-
| value of type `Vec<(u32, _, _)>` cannot be built from `std::iter::Iterator<Item=()>`
19+
| ^^^^^^^ value of type `Vec<(u32, _, _)>` cannot be built from `std::iter::Iterator<Item=()>`
2220
|
2321
= help: the trait `FromIterator<()>` is not implemented for `Vec<(u32, _, _)>`
2422
= help: the trait `FromIterator<T>` is implemented for `Vec<T>`

src/test/ui/issues/issue-66923-show-error-for-correct-call.stderr

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
error[E0277]: a value of type `Vec<f64>` cannot be built from an iterator over elements of type `&f64`
2-
--> $DIR/issue-66923-show-error-for-correct-call.rs:8:24
2+
--> $DIR/issue-66923-show-error-for-correct-call.rs:8:39
33
|
44
LL | let x2: Vec<f64> = x1.into_iter().collect();
5-
| ^^^^^^^^^^^^^^ ------- required by a bound introduced by this call
6-
| |
7-
| value of type `Vec<f64>` cannot be built from `std::iter::Iterator<Item=&f64>`
5+
| ^^^^^^^ value of type `Vec<f64>` cannot be built from `std::iter::Iterator<Item=&f64>`
86
|
97
= help: the trait `FromIterator<&f64>` is not implemented for `Vec<f64>`
108
= help: the trait `FromIterator<T>` is implemented for `Vec<T>`
@@ -22,12 +20,10 @@ LL | fn collect<B: FromIterator<Self::Item>>(self) -> B
2220
| ^^^^^^^^^^^^^^^^^^^^^^^^ required by this bound in `Iterator::collect`
2321

2422
error[E0277]: a value of type `Vec<f64>` cannot be built from an iterator over elements of type `&f64`
25-
--> $DIR/issue-66923-show-error-for-correct-call.rs:12:14
23+
--> $DIR/issue-66923-show-error-for-correct-call.rs:12:29
2624
|
2725
LL | let x3 = x1.into_iter().collect::<Vec<f64>>();
28-
| ^^^^^^^^^^^^^^ ------- required by a bound introduced by this call
29-
| |
30-
| value of type `Vec<f64>` cannot be built from `std::iter::Iterator<Item=&f64>`
26+
| ^^^^^^^ value of type `Vec<f64>` cannot be built from `std::iter::Iterator<Item=&f64>`
3127
|
3228
= help: the trait `FromIterator<&f64>` is not implemented for `Vec<f64>`
3329
= help: the trait `FromIterator<T>` is implemented for `Vec<T>`

src/test/ui/iterators/collect-into-array.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,4 @@ fn main() {
33
//~^ ERROR an array of type `[u32; 10]` cannot be built directly from an iterator
44
//~| NOTE try collecting into a `Vec<{integer}>`, then using `.try_into()`
55
//~| NOTE required by a bound in `collect`
6-
//~| NOTE required by a bound introduced by this call
76
}

0 commit comments

Comments
 (0)