Skip to content

Commit 79c6450

Browse files
committed
Don't suggest .into_iter() on iterators
1 parent 5b20c45 commit 79c6450

File tree

3 files changed

+55
-0
lines changed

3 files changed

+55
-0
lines changed

compiler/rustc_hir_typeck/src/method/suggest.rs

+9
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,15 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
152152
return false;
153153
}
154154

155+
// Does the `ty` not already implement `Iterator`? (#127511)
156+
if let Some(iterator_trait) = self.tcx.get_diagnostic_item(sym::Iterator)
157+
&& self
158+
.type_implements_trait(iterator_trait, [ty], self.param_env)
159+
.must_apply_modulo_regions()
160+
{
161+
return false;
162+
}
163+
155164
match *ty.peel_refs().kind() {
156165
ty::Param(param) => {
157166
let generics = self.tcx.generics_of(self.body_id);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
//! regression test for #127511: don't suggest `.into_iter()` on iterators
2+
3+
trait Missing {}
4+
trait HasMethod {
5+
fn foo(self);
6+
}
7+
impl<T: Iterator + Missing> HasMethod for T {
8+
fn foo(self) {}
9+
}
10+
11+
fn get_iter() -> impl Iterator {
12+
core::iter::once(())
13+
}
14+
15+
fn main() {
16+
get_iter().foo();
17+
//~^ ERROR the method `foo` exists for opaque type `impl Iterator`, but its trait bounds were not satisfied [E0599]
18+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
error[E0599]: the method `foo` exists for opaque type `impl Iterator`, but its trait bounds were not satisfied
2+
--> $DIR/iterator-does-not-need-into-iter.rs:16:16
3+
|
4+
LL | get_iter().foo();
5+
| ^^^ method cannot be called on `impl Iterator` due to unsatisfied trait bounds
6+
|
7+
note: the following trait bounds were not satisfied:
8+
`&impl Iterator: Iterator`
9+
`&impl Iterator: Missing`
10+
`&mut impl Iterator: Missing`
11+
`impl Iterator: Missing`
12+
--> $DIR/iterator-does-not-need-into-iter.rs:7:9
13+
|
14+
LL | impl<T: Iterator + Missing> HasMethod for T {
15+
| ^^^^^^^^ ^^^^^^^ --------- -
16+
| | |
17+
| | unsatisfied trait bound introduced here
18+
| unsatisfied trait bound introduced here
19+
= help: items from traits can only be used if the trait is implemented and in scope
20+
note: `HasMethod` defines an item `foo`, perhaps you need to implement it
21+
--> $DIR/iterator-does-not-need-into-iter.rs:4:1
22+
|
23+
LL | trait HasMethod {
24+
| ^^^^^^^^^^^^^^^
25+
26+
error: aborting due to 1 previous error
27+
28+
For more information about this error, try `rustc --explain E0599`.

0 commit comments

Comments
 (0)