Skip to content

Bound on iterator suggests invalid code to call iter() on the impl  #121398

Open
@Dylan-DPC

Description

@Dylan-DPC

Code

pub struct Foo<I>  
      where I: Iterator, 
  { 
   
      iter: I, 
  } 
   
   
  pub trait Bar: Iterator + Sized {  
      fn sprawl(self) -> Foo<Self> { 
          Foo {  
              iter: self 
          } 
      } 
  } 
   
  impl<T> Bar for Vec<T>
  where 
      T: Iterator, 
  {} 
   

It suggests you to call `iter()` on `Vec<T>`  in the impl, which is invalid code

Current output

Error[E0277]: `Vec<T>` is not an iterator
  --> src/lib.rs:17:19
   |
17 |   impl<T> Bar for Vec<T>
   |                   ^^^^^^ `Vec<T>` is not an iterator; try calling `.into_iter()` or `.iter()`
   |
   = help: the trait `Iterator` is not implemented for `Vec<T>`
note: required by a bound in `Bar`
  --> src/lib.rs:9:18
   |
9  |   pub trait Bar: Iterator + Sized {  
   |                  ^^^^^^^^ required by this bound in `Bar`

For more information about this error, try `rustc --explain E0277`.

Rationale and extra context

Playground link

Rust Version

1.78.0 nightly, but also present on the latest nightly and stable

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions