Skip to content

Unhelpful suggestion to borrow when an iterator of wrong type is passed to a method with a trait bound (E0277) #140390

Open
@Tehforsch

Description

@Tehforsch

Code

struct Foo;

trait Trait {}

impl Trait for Foo {}

pub fn foo<T: Trait>(_: impl Iterator<Item = T>) {
    todo!()
}

fn main() {
    let list = vec![Foo, Foo, Foo];
    foo(list.iter());
}

Current output

error[E0277]: the trait bound `&Foo: Trait` is not satisfied
  --> src/main.rs:13:5
   |
13 |     foo(list.iter());
   |     ^^^^^^^^^^^^^^^^ the trait `Trait` is not implemented for `&Foo`
   |
note: required by a bound in `foo`
  --> src/main.rs:7:15
   |
7  | pub fn foo<T: Trait>(_: impl Iterator<Item = T>) {
   |               ^^^^^ required by this bound in `foo`
help: consider borrowing here
   |
13 |     &foo(list.iter());
   |     +

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

Rationale and extra context

The suggestion to add a borrow is unhelpful (and if the borrow is actually added, the new suggestion will be to add yet another borrow).

Rust Version

rustc 1.86.0 (05f9846f8 2025-03-31)
binary: rustc
commit-hash: 05f9846f893b09a1be1fc8560e33fc3c815cfecb
commit-date: 2025-03-31
host: x86_64-unknown-linux-gnu
release: 1.86.0
LLVM version: 19.1.7

Anything else?

A similar suggestion also appears in #134805 and #132041 but the context is a bit different, so I wasn't sure where to post this.

Metadata

Metadata

Assignees

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