Skip to content

Wrong "consider borrowing here" suggestion with impl IntoIterator<Item = impl Borrow<str>> #155088

@tyilo

Description

@tyilo

Code

use std::borrow::Borrow;

fn foo(_v: impl IntoIterator<Item = impl Borrow<str>>) {}

fn main() {
    foo(&[String::from("a")]);
}

Current output

Compiling test-borrow v0.1.0 (/tmp/test-borrow)
error[E0277]: the trait bound `&String: Borrow<str>` is not satisfied
 --> src/main.rs:6:5
  |
6 |     foo(&[String::from("a")]);
  |     ^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `Borrow<str>` is not implemented for `&String`
  |
note: required by a bound in `foo`
 --> src/main.rs:3:42
  |
3 | fn foo(_v: impl IntoIterator<Item = impl Borrow<str>>) {}
  |                                          ^^^^^^^^^^^ required by this bound in `foo`
help: consider borrowing here
  |
6 |     &foo(&[String::from("a")]);
  |     +

For more information about this error, try `rustc --explain E0277`.
error: could not compile `test-borrow` (bin "test-borrow") due to 1 previous error

Desired output

Compiling test-borrow v0.1.0 (/tmp/test-borrow)
error[E0277]: the trait bound `&String: Borrow<str>` is not satisfied
 --> src/main.rs:6:5
  |
6 |     foo(&[String::from("a")]);
  |     ^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `Borrow<str>` is not implemented for `&String`
  |
note: required by a bound in `foo`
 --> src/main.rs:3:42
  |
3 | fn foo(_v: impl IntoIterator<Item = impl Borrow<str>>) {}
  |                                          ^^^^^^^^^^^ required by this bound in `foo`

For more information about this error, try `rustc --explain E0277`.
error: could not compile `test-borrow` (bin "test-borrow") due to 1 previous error

Rationale and extra context

If you apply the suggestion, it keeps suggestion adding more &'s:

help: consider borrowing here
  |
6 |     &&foo(&[String::from("a")]);
  |      +
help: consider borrowing here
  |
6 |     &&&foo(&[String::from("a")]);
  |       +
help: consider borrowing here
  |
6 |     &&&&foo(&[String::from("a")]);
  |        +

Other cases

Rust Version

$ rustc --version --verbose
rustc 1.94.1 (e408947bf 2026-03-25)
binary: rustc
commit-hash: e408947bfd200af42db322daf0fadfe7e26d3bd1
commit-date: 2026-03-25
host: x86_64-unknown-linux-gnu
release: 1.94.1
LLVM version: 21.1.8
$ rustc +nightly --version --verbose
rustc 1.96.0-nightly (f5eca4fcf 2026-04-09)
binary: rustc
commit-hash: f5eca4fcfa908d1e038afd19c6e746f075859130
commit-date: 2026-04-09
host: x86_64-unknown-linux-gnu
release: 1.96.0-nightly
LLVM version: 22.1.2

Anything else?

No response

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
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions