rust-lang / rust-lang/rust

Incorrect trait bound handling for `IntoIterator<IntoIter = I, Item = T>`

Open
#137,185 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-associated-items A-trait-system C-bug T-types
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

Description

I tried this code:

pub struct Positive<F>(F);

struct MyStruct<F, I> {
    first: Option<F>,
    iter: I,
}

impl<F, I> MyStruct<F, I> {
    fn new<T>(iter: T) -> Self
    where
        T: IntoIterator<IntoIter = I, Item = Positive<F>>,
    {
        let mut iter = iter.into_iter();
        let first = iter.next().map(|Positive(t)| t);
        
        Self { iter, first }
    }
}

I expected it to compile just like this does:

pub struct Positive<F>(F);

struct MyStruct<F, I> {
    first: Option<F>,
    iter: I,
}

impl<F, I> MyStruct<F, I> {
    fn new<T>(iter: T) -> Self
    where
        T: IntoIterator<IntoIter = I>,
        I: Iterator<Item = Positive<F>>,
    {
        let mut iter = iter.into_iter();
        let first = iter.next().map(|Positive(t)| t);
        
        Self { iter, first }
    }
}

Instead, it complains:

error[E0599]: no method named `next` found for type parameter `I` in the current scope
  --> src/main.rs:15:26
   |
8  | impl<F, I> MyStruct<F, I> {
   |         - method `next` not found for this type parameter
...
15 |         let first = iter.next().map(|Positive(t)| t);
   |                          ^^^^ method not found in `I`
   |
   = help: items from traits can only be used if the type parameter is bounded by the trait
help: the following trait defines an item `next`, perhaps you need to restrict type parameter `I` with it:
   |
8  | impl<F, I: Iterator> MyStruct<F, I> {
   |          ++++++++++
Meta

rustc --version --verbose:

~> rustc --version --verbose
rustc 1.82.0 (f6e511eec 2024-10-15)
binary: rustc
commit-hash: f6e511eec7342f59a25f7c0534f1dbea00d01b14
commit-date: 2024-10-15
host: x86_64-unknown-linux-gnu
release: 1.82.0
LLVM version: 19.1.1

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the issue in src/main.rs with rustc 1.82.0, comparing the failing IntoIterator bound with the working separate Iterator bound. Start by examining how the compiler handles the associated IntoIter and Item constraints; done means the reported example compiles as expected without requiring an additional Iterator bound.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.