fix: infer for-loop item type with IntoIterator and Iterator

This commit is contained in:
Ryo Yoshida 2022-09-29 18:44:45 +09:00
parent ad752bd521
commit 6d8903ae5f
No known key found for this signature in database
GPG Key ID: E25698A930586171
5 changed files with 29 additions and 3 deletions

View File

@ -263,6 +263,7 @@ macro_rules! known_names {
Iterator,
IntoIterator,
Item,
IntoIter,
Try,
Ok,
Future,

View File

@ -883,6 +883,12 @@ fn resolve_lang_item(&self, name: Name) -> Option<LangItemTarget> {
fn resolve_into_iter_item(&self) -> Option<TypeAliasId> {
let path = path![core::iter::IntoIterator];
let trait_ = self.resolver.resolve_known_trait(self.db.upcast(), &path)?;
self.db.trait_data(trait_).associated_type_by_name(&name![IntoIter])
}
fn resolve_iterator_item(&self) -> Option<TypeAliasId> {
let path = path![core::iter::Iterator];
let trait_ = self.resolver.resolve_known_trait(self.db.upcast(), &path)?;
self.db.trait_data(trait_).associated_type_by_name(&name![Item])
}

View File

@ -207,8 +207,10 @@ fn infer_expr_inner(&mut self, tgt_expr: ExprId, expected: &Expectation) -> Ty {
}
&Expr::For { iterable, body, pat, label } => {
let iterable_ty = self.infer_expr(iterable, &Expectation::none());
let pat_ty =
let into_iter_ty =
self.resolve_associated_type(iterable_ty, self.resolve_into_iter_item());
let pat_ty =
self.resolve_associated_type(into_iter_ty, self.resolve_iterator_item());
self.infer_pat(pat, &pat_ty, BindingMode::default());
self.with_breakable_ctx(BreakableKind::Loop, self.err_ty(), label, |this| {

View File

@ -279,6 +279,10 @@ fn test() {
pub mod iter {
pub trait IntoIterator {
type Item;
type IntoIter: Iterator<Item = Self::Item>;
}
pub trait Iterator {
type Item;
}
}
pub mod prelude {
@ -298,6 +302,12 @@ pub fn push(&mut self, t: T) { }
impl<T> IntoIterator for Vec<T> {
type Item = T;
type IntoIter = IntoIter<T>;
}
struct IntoIter<T> {}
impl<T> Iterator for IntoIter<T> {
type Item = T;
}
}
"#,

View File

@ -2025,6 +2025,13 @@ pub fn push(&mut self, t: T) {}
impl<T> IntoIterator for Vec<T> {
type Item = T;
type IntoIter = IntoIter<T>;
}
struct IntoIter<T> {}
impl<T> Iterator for IntoIter<T> {
type Item = T;
}
fn main() {