Skip to content

Commit 78f9759

Browse files
committed
Only point at methods that might be relevant
1 parent aff0ab4 commit 78f9759

File tree

4 files changed

+81
-19
lines changed

4 files changed

+81
-19
lines changed

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

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3008,7 +3008,17 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
30083008
let Some(prev_assoc_in_method) = assocs.peek() else {
30093009
for entry in assocs_in_method {
30103010
let Some((span, (assoc, ty))) = entry else { continue; };
3011-
primary_spans.push(span);
3011+
if type_diffs.iter().any(|diff| {
3012+
let Sorts(expected_found) = diff else { return false; };
3013+
self.can_eq(param_env, expected_found.found, ty).is_ok()
3014+
}) {
3015+
// FIXME: this doesn't quite work for `Iterator::collect`
3016+
// because we have `Vec<i32>` and `()`, but we'd want `i32`
3017+
// to point at the `.into_iter()` call, but as long as we
3018+
// still point at the other method calls that might have
3019+
// introduced the issue, this is fine for now.
3020+
primary_spans.push(span);
3021+
}
30123022
span_labels.push((
30133023
span,
30143024
format!("`{assoc}` is `{ty}` here"),
@@ -3022,7 +3032,12 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
30223032
match (entry, prev_entry) {
30233033
(Some((span, (assoc, ty))), Some((_, (_, prev_ty)))) => {
30243034
if ty != *prev_ty {
3025-
primary_spans.push(span);
3035+
if type_diffs.iter().any(|diff| {
3036+
let Sorts(expected_found) = diff else { return false; };
3037+
self.can_eq(param_env, expected_found.found, ty).is_ok()
3038+
}) {
3039+
primary_spans.push(span);
3040+
}
30263041
span_labels.push((
30273042
span,
30283043
format!("`{assoc}` changed to `{ty}` here"),

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,13 @@ LL | let sr2: Vec<(u32, _, _)> = sr.iter().map(|(faction, th_sender, th_rece
2323
= help: the trait `FromIterator<()>` is not implemented for `Vec<(u32, _, _)>`
2424
= help: the trait `FromIterator<T>` is implemented for `Vec<T>`
2525
note: the method call chain might not have had the expected associated types
26-
--> $DIR/issue-34334.rs:5:36
26+
--> $DIR/issue-34334.rs:5:43
2727
|
2828
LL | let sr: Vec<(u32, _, _) = vec![];
2929
| ------ this expression has type `Vec<(_, _, _)>`
3030
...
3131
LL | let sr2: Vec<(u32, _, _)> = sr.iter().map(|(faction, th_sender, th_receiver)| {}).collect();
32-
| ^^^^^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ `std::iter::Iterator::Item` changed to `()` here
32+
| ------ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ `std::iter::Iterator::Item` changed to `()` here
3333
| |
3434
| `std::iter::Iterator::Item` is `&(_, _, _)` here
3535
note: required by a bound in `collect`

src/test/ui/iterators/invalid-iterator-chain.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,16 @@ fn main() {
1717
.map(|x| { x; })
1818
.sum::<i32>(),
1919
);
20+
println!(
21+
"{}",
22+
vec![0, 1] //~ ERROR E0277
23+
.iter()
24+
.map(|x| x * 2)
25+
.map(|x| x as f64)
26+
.filter(|x| *x > 0.0)
27+
.map(|x| { x + 1.0 })
28+
.sum::<i32>(),
29+
);
2030
println!("{}", vec![0, 1].iter().map(|x| { x; }).sum::<i32>()); //~ ERROR E0277
2131
println!("{}", vec![(), ()].iter().sum::<i32>()); //~ ERROR E0277
2232
let a = vec![0];

src/test/ui/iterators/invalid-iterator-chain.stderr

Lines changed: 52 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@ LL | println!("{}", scores.sum::<i32>());
1010
<i32 as Sum<&'a i32>>
1111
<i32 as Sum>
1212
note: the method call chain might not have had the expected associated types
13-
--> $DIR/invalid-iterator-chain.rs:3:10
13+
--> $DIR/invalid-iterator-chain.rs:4:10
1414
|
1515
LL | let scores = vec![(0, 0)]
1616
| ------------ this expression has type `Vec<({integer}, {integer})>`
1717
LL | .iter()
18-
| ^^^^^^ `std::iter::Iterator::Item` is `&({integer}, {integer})` here
18+
| ------ `std::iter::Iterator::Item` is `&({integer}, {integer})` here
1919
LL | .map(|(a, b)| {
2020
| __________^
2121
LL | | a + b;
@@ -45,18 +45,18 @@ LL | .sum::<i32>(),
4545
<i32 as Sum<&'a i32>>
4646
<i32 as Sum>
4747
note: the method call chain might not have had the expected associated types
48-
--> $DIR/invalid-iterator-chain.rs:11:14
48+
--> $DIR/invalid-iterator-chain.rs:12:14
4949
|
5050
LL | vec![0, 1]
5151
| ---------- this expression has type `Vec<{integer}>`
5252
LL | .iter()
53-
| ^^^^^^ `std::iter::Iterator::Item` is `&{integer}` here
53+
| ------ `std::iter::Iterator::Item` is `&{integer}` here
5454
LL | .map(|x| x * 2)
5555
| ^^^^^^^^^^^^^^ `std::iter::Iterator::Item` changed to `{integer}` here
5656
LL | .map(|x| x as f64)
57-
| ^^^^^^^^^^^^^^^^^ `std::iter::Iterator::Item` changed to `f64` here
57+
| ----------------- `std::iter::Iterator::Item` changed to `f64` here
5858
LL | .map(|x| x as i64)
59-
| ^^^^^^^^^^^^^^^^^ `std::iter::Iterator::Item` changed to `i64` here
59+
| ----------------- `std::iter::Iterator::Item` changed to `i64` here
6060
LL | .filter(|x| *x > 0)
6161
| ------------------ `std::iter::Iterator::Item` remains `i64` here
6262
LL | .map(|x| { x + 1 })
@@ -69,8 +69,45 @@ note: required by a bound in `std::iter::Iterator::sum`
6969
LL | S: Sum<Self::Item>,
7070
| ^^^^^^^^^^^^^^^ required by this bound in `std::iter::Iterator::sum`
7171

72+
error[E0277]: the trait bound `i32: Sum<f64>` is not satisfied
73+
--> $DIR/invalid-iterator-chain.rs:22:9
74+
|
75+
LL | / vec![0, 1]
76+
LL | | .iter()
77+
LL | | .map(|x| x * 2)
78+
LL | | .map(|x| x as f64)
79+
LL | | .filter(|x| *x > 0.0)
80+
LL | | .map(|x| { x + 1.0 })
81+
| |_________________________________^ the trait `Sum<f64>` is not implemented for `i32`
82+
LL | .sum::<i32>(),
83+
| --- required by a bound introduced by this call
84+
|
85+
= help: the following other types implement trait `Sum<A>`:
86+
<i32 as Sum<&'a i32>>
87+
<i32 as Sum>
88+
note: the method call chain might not have had the expected associated types
89+
--> $DIR/invalid-iterator-chain.rs:24:14
90+
|
91+
LL | vec![0, 1]
92+
| ---------- this expression has type `Vec<{integer}>`
93+
LL | .iter()
94+
| ------ `std::iter::Iterator::Item` is `&{integer}` here
95+
LL | .map(|x| x * 2)
96+
| ^^^^^^^^^^^^^^ `std::iter::Iterator::Item` changed to `{integer}` here
97+
LL | .map(|x| x as f64)
98+
| ^^^^^^^^^^^^^^^^^ `std::iter::Iterator::Item` changed to `f64` here
99+
LL | .filter(|x| *x > 0.0)
100+
| -------------------- `std::iter::Iterator::Item` remains `f64` here
101+
LL | .map(|x| { x + 1.0 })
102+
| -------------------- `std::iter::Iterator::Item` remains `f64` here
103+
note: required by a bound in `std::iter::Iterator::sum`
104+
--> $SRC_DIR/core/src/iter/traits/iterator.rs:LL:COL
105+
|
106+
LL | S: Sum<Self::Item>,
107+
| ^^^^^^^^^^^^^^^ required by this bound in `std::iter::Iterator::sum`
108+
72109
error[E0277]: the trait bound `i32: Sum<()>` is not satisfied
73-
--> $DIR/invalid-iterator-chain.rs:20:20
110+
--> $DIR/invalid-iterator-chain.rs:30:20
74111
|
75112
LL | println!("{}", vec![0, 1].iter().map(|x| { x; }).sum::<i32>());
76113
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ --- required by a bound introduced by this call
@@ -81,10 +118,10 @@ LL | println!("{}", vec![0, 1].iter().map(|x| { x; }).sum::<i32>());
81118
<i32 as Sum<&'a i32>>
82119
<i32 as Sum>
83120
note: the method call chain might not have had the expected associated types
84-
--> $DIR/invalid-iterator-chain.rs:20:31
121+
--> $DIR/invalid-iterator-chain.rs:30:38
85122
|
86123
LL | println!("{}", vec![0, 1].iter().map(|x| { x; }).sum::<i32>());
87-
| ---------- ^^^^^^ ^^^^^^^^^^^^^^^ `std::iter::Iterator::Item` changed to `()` here
124+
| ---------- ------ ^^^^^^^^^^^^^^^ `std::iter::Iterator::Item` changed to `()` here
88125
| | |
89126
| | `std::iter::Iterator::Item` is `&{integer}` here
90127
| this expression has type `Vec<{integer}>`
@@ -95,7 +132,7 @@ LL | S: Sum<Self::Item>,
95132
| ^^^^^^^^^^^^^^^ required by this bound in `std::iter::Iterator::sum`
96133

97134
error[E0277]: the trait bound `i32: Sum<&()>` is not satisfied
98-
--> $DIR/invalid-iterator-chain.rs:21:20
135+
--> $DIR/invalid-iterator-chain.rs:31:20
99136
|
100137
LL | println!("{}", vec![(), ()].iter().sum::<i32>());
101138
| ^^^^^^^^^^^^^^^^^^^ --- required by a bound introduced by this call
@@ -106,7 +143,7 @@ LL | println!("{}", vec![(), ()].iter().sum::<i32>());
106143
<i32 as Sum<&'a i32>>
107144
<i32 as Sum>
108145
note: the method call chain might not have had the expected associated types
109-
--> $DIR/invalid-iterator-chain.rs:21:33
146+
--> $DIR/invalid-iterator-chain.rs:31:33
110147
|
111148
LL | println!("{}", vec![(), ()].iter().sum::<i32>());
112149
| ------------ ^^^^^^ `std::iter::Iterator::Item` is `&()` here
@@ -119,7 +156,7 @@ LL | S: Sum<Self::Item>,
119156
| ^^^^^^^^^^^^^^^ required by this bound in `std::iter::Iterator::sum`
120157

121158
error[E0277]: a value of type `Vec<i32>` cannot be built from an iterator over elements of type `()`
122-
--> $DIR/invalid-iterator-chain.rs:30:23
159+
--> $DIR/invalid-iterator-chain.rs:40:23
123160
|
124161
LL | let g: Vec<i32> = f.collect();
125162
| ^ ------- required by a bound introduced by this call
@@ -129,12 +166,12 @@ LL | let g: Vec<i32> = f.collect();
129166
= help: the trait `FromIterator<()>` is not implemented for `Vec<i32>`
130167
= help: the trait `FromIterator<T>` is implemented for `Vec<T>`
131168
note: the method call chain might not have had the expected associated types
132-
--> $DIR/invalid-iterator-chain.rs:23:15
169+
--> $DIR/invalid-iterator-chain.rs:36:15
133170
|
134171
LL | let a = vec![0];
135172
| ------- this expression has type `Vec<{integer}>`
136173
LL | let b = a.into_iter();
137-
| ^^^^^^^^^^^ `std::iter::Iterator::Item` is `{integer}` here
174+
| ----------- `std::iter::Iterator::Item` is `{integer}` here
138175
LL | let c = b.map(|x| x + 1);
139176
| -------------- `std::iter::Iterator::Item` remains `{integer}` here
140177
LL | let d = c.filter(|x| *x > 10 );
@@ -152,6 +189,6 @@ note: required by a bound in `collect`
152189
LL | fn collect<B: FromIterator<Self::Item>>(self) -> B
153190
| ^^^^^^^^^^^^^^^^^^^^^^^^ required by this bound in `collect`
154191

155-
error: aborting due to 5 previous errors
192+
error: aborting due to 6 previous errors
156193

157194
For more information about this error, try `rustc --explain E0277`.

0 commit comments

Comments
 (0)