Skip to content

Conversation

@A4-Tacks
Copy link
Member

Example

std example:

fn foo(nums: std::rc::Rc<[i32]>) {
    nums.$0
}

minicore example:

struct Foo;
impl Foo { fn iter(&self) -> Iter { Iter } }
impl IntoIterator for &Foo {
    type Item = ();
    type IntoIter = Iter;
    fn into_iter(self) -> Self::IntoIter { Iter }
}
struct Ref;
impl core::ops::Deref for Ref {
    type Target = Foo;
    fn deref(&self) -> &Self::Target { &Foo }
}
struct Iter;
impl Iterator for Iter {
    type Item = ();
    fn next(&mut self) -> Option<Self::Item> { None }
}
fn foo() {
    Ref.$0
}

Before this PR

me deref() (use core::ops::Deref)                 fn(&self) -> &<Self as Deref>::Target
me into_iter() (as IntoIterator)           fn(self) -> <Self as IntoIterator>::IntoIter
me iter()                                                             fn(&self) -> Iter

After this PR

me deref() (use core::ops::Deref)                 fn(&self) -> &<Self as Deref>::Target
me into_iter() (as IntoIterator)           fn(self) -> <Self as IntoIterator>::IntoIter
me iter()                                                             fn(&self) -> Iter
me iter().by_ref() (as Iterator)                             fn(&mut self) -> &mut Self
me iter().into_iter() (as IntoIterator)    fn(self) -> <Self as IntoIterator>::IntoIter
me iter().next() (as Iterator)        fn(&mut self) -> Option<<Self as Iterator>::Item>
me iter().nth(…) (as Iterator) fn(&mut self, usize) -> Option<<Self as Iterator>::Item>

Example
---

**std example**:

```rust
fn foo(nums: std::rc::Rc<[i32]>) {
    nums.$0
}
```

---

**minicore example**:

```rust
struct Foo;
impl Foo { fn iter(&self) -> Iter { Iter } }
impl IntoIterator for &Foo {
    type Item = ();
    type IntoIter = Iter;
    fn into_iter(self) -> Self::IntoIter { Iter }
}
struct Ref;
impl core::ops::Deref for Ref {
    type Target = Foo;
    fn deref(&self) -> &Self::Target { &Foo }
}
struct Iter;
impl Iterator for Iter {
    type Item = ();
    fn next(&mut self) -> Option<Self::Item> { None }
}
fn foo() {
    Ref.$0
}
```

**Before this PR**

```text
me deref() (use core::ops::Deref)                 fn(&self) -> &<Self as Deref>::Target
me into_iter() (as IntoIterator)           fn(self) -> <Self as IntoIterator>::IntoIter
me iter()                                                             fn(&self) -> Iter
```

**After this PR**

```text
me deref() (use core::ops::Deref)                 fn(&self) -> &<Self as Deref>::Target
me into_iter() (as IntoIterator)           fn(self) -> <Self as IntoIterator>::IntoIter
me iter()                                                             fn(&self) -> Iter
me iter().by_ref() (as Iterator)                             fn(&mut self) -> &mut Self
me iter().into_iter() (as IntoIterator)    fn(self) -> <Self as IntoIterator>::IntoIter
me iter().next() (as Iterator)        fn(&mut self) -> Option<<Self as Iterator>::Item>
me iter().nth(…) (as Iterator) fn(&mut self, usize) -> Option<<Self as Iterator>::Item>
```
@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Nov 22, 2025
// its return type, so we instead check for `<&Self as IntoIterator>::IntoIter`.
// Does <&receiver_ty as IntoIterator>::IntoIter` exist? Assume `iter` is valid
let iter = receiver_ty
.strip_references()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Won't this break cases where we have a type T that derefs to U where T has interesting impls but U does not?

I think the proper fix here is to walk the autoderef chain and find the first candidate that impls IntoIterator instead

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the proper fix here is to walk the autoderef chain and find the first candidate that impls IntoIterator instead

Can't .find_map(|ty| ty.into_iterator_iter(ctx.db)) do it?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

S-waiting-on-review Status: Awaiting review from the assignee but also interested parties.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants