rust-lang / rust-lang/rust

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

Open
#132,199 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-diagnostics A-suggestion-diagnostics D-incorrect F-precise_capturing T-compiler
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

Description

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 (c1db4dc24 2024-10-25)
binary: rustc
commit-hash: c1db4dc24267a707409c9bf2e67cf3c7323975c8
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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the issue with the provided Rust snippet on the reported rustc 1.84.0-nightly version, then inspect the compiler diagnostic path for E0700 and use<...> lifetime suggestions. Done means the diagnostic suggests naming the distinct lifetimes and produces the shown corrected signature without iterative or duplicate '_ captures.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.