Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
Tehforsch opened this issue Apr 28, 2025 · 2 comments
Assignees
Labels
A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@Tehforsch
Copy link

Tehforsch commented Apr 28, 2025

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.

@Tehforsch Tehforsch added A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Apr 28, 2025
@rustbot rustbot self-assigned this Apr 29, 2025
@xizheyin
Copy link
Contributor

@rustbot claim

@rustbot rustbot assigned xizheyin and unassigned rustbot Apr 29, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

3 participants