Skip to content

Commit 97f8f4a

Browse files
committed
Auto merge of rust-lang#13311 - lowr:fix/for-loop-item-resolution, r=Veykril
fix: infer for-loop item type with `IntoIterator` and `Iterator` Part of rust-lang#13299 We've been inferring the type of the yielded values in for-loop as `<T as IntoIterator>::Item`. We infer the correct type most of the time when we normalize the projection type, but it turns out not always. We should infer the type as `<<T as IntoIterator>::IntoIter as Iterator>::Item`. When one specifies `IntoIter` assoc type of `IntoIterator` but not `Item` in generic bounds, we fail to normalize `<T as IntoIterator>::Item` (even though `IntoIter` is defined like so: `type IntoIter: Iterator<Item = Self::Item>` - rustc does *not* normalize projections based on other projection's bound I believe; see [this playground](https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=e88e19385094cb98fadbf647b4c2082e)). Note that this doesn't fully fix # 13299 - given the following code, chalk can normalize `<I as IntoIterator>::IntoIter` to `S`, but cannot normalize `<S as Iterator>::Item` to `i32`. ```rust struct S; impl Iterator for S { type Item = i32; /* ... */ } fn f<I: IntoIterator<IntoIter = S>>(it: I) { for elem in it {} //^^^^{unknown} } ``` This is because chalk finds multiple answers that satisfy the query `AliasEq(<S as Iterator>::Item = ?X`: `?X = i32` and `?X = <I as IntoIterator>::Item` - which are supposed to be the same type due to the aforementioned bound on `IntoIter` but chalk is unable to figure it out.
2 parents ad752bd + 6d8903a commit 97f8f4a

File tree

5 files changed

+29
-3
lines changed

5 files changed

+29
-3
lines changed

crates/hir-expand/src/name.rs

+1
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,7 @@ pub mod known {
263263
Iterator,
264264
IntoIterator,
265265
Item,
266+
IntoIter,
266267
Try,
267268
Ok,
268269
Future,

crates/hir-ty/src/infer.rs

+6
Original file line numberDiff line numberDiff line change
@@ -883,6 +883,12 @@ impl<'a> InferenceContext<'a> {
883883
fn resolve_into_iter_item(&self) -> Option<TypeAliasId> {
884884
let path = path![core::iter::IntoIterator];
885885
let trait_ = self.resolver.resolve_known_trait(self.db.upcast(), &path)?;
886+
self.db.trait_data(trait_).associated_type_by_name(&name![IntoIter])
887+
}
888+
889+
fn resolve_iterator_item(&self) -> Option<TypeAliasId> {
890+
let path = path![core::iter::Iterator];
891+
let trait_ = self.resolver.resolve_known_trait(self.db.upcast(), &path)?;
886892
self.db.trait_data(trait_).associated_type_by_name(&name![Item])
887893
}
888894

crates/hir-ty/src/infer/expr.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -207,8 +207,10 @@ impl<'a> InferenceContext<'a> {
207207
}
208208
&Expr::For { iterable, body, pat, label } => {
209209
let iterable_ty = self.infer_expr(iterable, &Expectation::none());
210-
let pat_ty =
210+
let into_iter_ty =
211211
self.resolve_associated_type(iterable_ty, self.resolve_into_iter_item());
212+
let pat_ty =
213+
self.resolve_associated_type(into_iter_ty, self.resolve_iterator_item());
212214

213215
self.infer_pat(pat, &pat_ty, BindingMode::default());
214216
self.with_breakable_ctx(BreakableKind::Loop, self.err_ty(), label, |this| {

crates/hir-ty/src/tests/traits.rs

+11-1
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,10 @@ fn test() {
279279
pub mod iter {
280280
pub trait IntoIterator {
281281
type Item;
282+
type IntoIter: Iterator<Item = Self::Item>;
283+
}
284+
pub trait Iterator {
285+
type Item;
282286
}
283287
}
284288
pub mod prelude {
@@ -297,7 +301,13 @@ pub mod collections {
297301
}
298302
299303
impl<T> IntoIterator for Vec<T> {
300-
type Item=T;
304+
type Item = T;
305+
type IntoIter = IntoIter<T>;
306+
}
307+
308+
struct IntoIter<T> {}
309+
impl<T> Iterator for IntoIter<T> {
310+
type Item = T;
301311
}
302312
}
303313
"#,

crates/ide/src/inlay_hints.rs

+8-1
Original file line numberDiff line numberDiff line change
@@ -2024,7 +2024,14 @@ impl<T> Vec<T> {
20242024
}
20252025
20262026
impl<T> IntoIterator for Vec<T> {
2027-
type Item=T;
2027+
type Item = T;
2028+
type IntoIter = IntoIter<T>;
2029+
}
2030+
2031+
struct IntoIter<T> {}
2032+
2033+
impl<T> Iterator for IntoIter<T> {
2034+
type Item = T;
20282035
}
20292036
20302037
fn main() {

0 commit comments

Comments
 (0)