Skip to content

incorrect suggestion for use<...> bound that captures multiple lifetimes #132199

Closed

Description

@m4rch3n1ng

Code

struct T;

impl T {
	fn iter(&self, t: &T) -> impl Iterator {
		std::iter::once((self, t))
	}
}

Current output

cargo check currently suggests

error[E0700]: hidden type for `impl std::iter::Iterator` captures lifetime that does not appear in bounds
 --> src/main.rs:5:3
  |
4 |     fn iter(&self, t: &T) -> impl Iterator {
  |             -----            ------------- opaque type defined here
  |             |
  |             hidden type `std::iter::Once<(&T, &T)>` captures the anonymous lifetime defined here
5 |         std::iter::once((self, t))
  |         ^^^^^^^^^^^^^^^^^^^^^^^^^^
  |
help: add a `use<...>` bound to explicitly capture `'_`
  |
4 |     fn iter(&self, t: &T) -> impl Iterator + use<'_> {
  |                                            +++++++++

if i apply that suggestion it still complains about another implicitly captured lifetime and suggests this.

help: add `'_` to the `use<...>` bound to explicitly capture it
  |
4 |     fn iter(&self, t: &T) -> impl Iterator + use<'_, '_> {
  |                                                    ++++

if i apply this suggestion the code doesn't compile either with this error

error: cannot capture parameter `'_` twice
 --> src/main.rs:8:47
  |
4 |     fn iter(&self, t: &T) -> impl Iterator + use<'_, '_> {
  |                                                  ^^  -- parameter captured again here

and it still complains about a captured lifetime that doesn't appear in the bounds with another helpful suggestion

help: add `'_` to the `use<...>` bound to explicitly capture it
  |
4 |     fn iter(&self, t: &T) -> impl Iterator + use<'_, '_, '_> {
  |                                                        ++++

Desired output

help: add a `use<...>` bound to explicitly capture the lifetimes
  |
4 |     fn iter<'a, 'b>(&'a self, t: &'b T) -> impl Iterator + use<'a, 'b> {
  |            ++++++++  +++          +++                    +++++++++++++

Rationale and extra context

No response

Other cases

No response

Rust Version

rustc 1.84.0-nightly (c1db4dc 2024-10-25)
binary: rustc
commit-hash: c1db4dc
commit-date: 2024-10-25
host: x86_64-unknown-linux-gnu
release: 1.84.0-nightly
LLVM version: 19.1.1

Anything else?

No response

Activity

  1. added
    A-diagnosticsArea: Messages for errors, warnings, and lints
    T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.
    on Oct 26, 2024
  2. added
    A-suggestion-diagnosticsArea: Suggestions generated by the compiler applied by `cargo fix`
    D-incorrectDiagnostics: A diagnostic that is giving misleading or incorrect information.
    on Oct 27, 2024
  3. added a commit that references this issue on Sep 29, 2026
  4. added a commit that references this issue on Sep 29, 2026
  5. added a commit that references this issue on Oct 23, 2026
  6. added a commit that references this issue on Oct 23, 2026
  7. added a commit that references this issue on Oct 23, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsA-suggestion-diagnosticsArea: Suggestions generated by the compiler applied by `cargo fix`D-incorrectDiagnostics: A diagnostic that is giving misleading or incorrect information.F-precise_capturing`#![feature(precise_capturing)]`T-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