Skip to content

do not suggest capturing '_ twice in use<...> for E0700 - #163471

Merged
rust-bors[bot] merged 2 commits into
rust-lang:mainfrom
Albab-Hasan:no-duplicate-anon-lifetime-capture-suggestion
Sep 29, 2026
Merged

rust-bors[bot] merged 2 commits into
rust-lang:mainfrom
Albab-Hasan:no-duplicate-anon-lifetime-capture-suggestion

Conversation

@Albab-Hasan

Copy link
Copy Markdown
Contributor

starting from use<'_> with a second anonymous lifetime to capture, E0700 suggests use<'_, '_>. that fails with:

error: cannot capture parameter `'_` twice

then the compiler suggests use<'_, '_, '_>. both AddPreciseCapturing::New and AddPreciseCapturing::Existing are machine-applicable (rustc_trait_selection/src/diagnostics.rs:1876 and :1888) so rustfix applies the broken edit.

the branch for an opaque without use<..> already skips lifetimes that are captured. this adds the same check to the branch for an existing use<..>.

the issue repro compiles clean on edition 2024 and needs 2021 so the test starts one step in with use<'_> already written. two extra cases check the guard stays narrow: use<> with one anonymous lifetime still gets use<'_> and a named lifetime still gets suggested by name.

one note. the guard removes the suggestion without replacing it so someone who already wrote use<'_> gets no path forward. a correct suggestion needs to name the elided lifetimes and suggest_precise_capturing cannot do that today. that the multipart suggestion this issue asks for will be the follow up.

#132199

r? @oli-obk

the test records what the compiler prints today: for an opaque that already has use<'_> and a second anonymous lifetime to capture, it suggested use<'_, '_>, which doesnt compile
suggest_precise_capturing was appending '_ to a use<..> list that already had it, suggesting use<'_, '_>, which rustc rejects with cannot capture parameter '_ twice

the branch for an opaque with an existing use<..> now bails out the same way the branch for an opaque without one does: both check if the lifetime is already captured
@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Sep 29, 2026
@Albab-Hasan Albab-Hasan changed the title do not suggest capturing '_ twice in use<...> for E0700 do not suggest capturing '_ twice in use<...> for E0700 Sep 29, 2026
@@ -1 +1 @@
error[E0700]: hidden type for `impl Sized` captures lifetime that does not appear in bounds

@oli-obk oli-obk Sep 29, 2026 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

would this test pass if rustfix were run on it? if so, you can make it a rustfix test to ensure this doesn't regress

View changes since the review

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just checked, it cant be a rustfix test. after the fix dup gets no suggestion, so the .fixed file still fails E0700 and compiletest requires it to compile.

@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Sep 29, 2026
@rustbot

rustbot commented Sep 29, 2026

Copy link
Copy Markdown
Collaborator

Reminder, once the PR becomes ready for a review, use @rustbot ready.

@Albab-Hasan

Copy link
Copy Markdown
Contributor Author

@rustbot ready

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Sep 29, 2026
@Albab-Hasan
Albab-Hasan requested a review from oli-obk September 29, 2026 10:37
@oli-obk

oli-obk commented Sep 29, 2026

Copy link
Copy Markdown
Contributor

Thanks for checking

@bors r+ rollup

@rust-bors

rust-bors Bot commented Sep 29, 2026

Copy link
Copy Markdown
Contributor

📌 Commit e939199 has been approved by oli-obk

It is now in the queue for this repository.

@rust-bors rust-bors Bot added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Sep 29, 2026
@Albab-Hasan

Copy link
Copy Markdown
Contributor Author

thanks for the review!

rust-bors Bot pushed a commit that referenced this pull request Sep 29, 2026
…uwer

Rollup of 4 pull requests

Successful merges:

 - #163024 (Add `Dir` equivalents of `fs::metadata` & `fs::symlink_metadata`)
 - #162839 (Bump min Emscripten version to 4.0, drop deprecated -sWASM_BIGINT for wasm32-unknown-emscripten)
 - #163370 (Fix incorrect typo suggestion for `struct field` shorthands)
 - #163471 (do not suggest capturing `'_` twice in `use<...>` for E0700)
@rust-bors
rust-bors Bot merged commit 2444fac into rust-lang:main Sep 29, 2026
13 checks passed
@rustbot rustbot added this to the 1.101.0 milestone Sep 29, 2026
rust-bors Bot pushed a commit that referenced this pull request Sep 29, 2026
Rollup merge of #163471 - Albab-Hasan:no-duplicate-anon-lifetime-capture-suggestion, r=oli-obk

do not suggest capturing `'_` twice in `use<...>` for E0700

starting from `use<'_>` with a second anonymous lifetime to capture, E0700 suggests `use<'_, '_>`. that fails with:

```
error: cannot capture parameter `'_` twice
```

then the compiler suggests `use<'_, '_, '_>`. both `AddPreciseCapturing::New` and `AddPreciseCapturing::Existing` are `machine-applicable` (rustc_trait_selection/src/diagnostics.rs:1876 and :1888) so rustfix applies the broken edit.

the branch for an opaque without `use<..>` already skips lifetimes that are captured. this adds the same check to the branch for an existing `use<..>`.

the issue repro compiles clean on edition 2024 and needs 2021 so the test starts one step in with `use<'_>` already written. two extra cases check the guard stays narrow: `use<>` with one anonymous lifetime still gets `use<'_>` and a named lifetime still gets suggested by name.

one note. the guard removes the suggestion without replacing it so someone who already wrote `use<'_>` gets no path forward. a correct suggestion needs to name the elided lifetimes and suggest_precise_capturing cannot do that today. that the multipart suggestion this issue asks for will be the follow up.

#132199

r? @oli-obk
pull Bot pushed a commit to LeeeeeeM/miri that referenced this pull request Sep 30, 2026
…uwer

Rollup of 4 pull requests

Successful merges:

 - rust-lang/rust#163024 (Add `Dir` equivalents of `fs::metadata` & `fs::symlink_metadata`)
 - rust-lang/rust#162839 (Bump min Emscripten version to 4.0, drop deprecated -sWASM_BIGINT for wasm32-unknown-emscripten)
 - rust-lang/rust#163370 (Fix incorrect typo suggestion for `struct field` shorthands)
 - rust-lang/rust#163471 (do not suggest capturing `'_` twice in `use<...>` for E0700)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants