Skip to content

diagnostics: Suggest fn binding type for unstable closure for<> binders - #160478

Open
Dnreikronos wants to merge 2 commits into
rust-lang:mainfrom
Dnreikronos:diagnostics/closure_lifetime_binder_sugg
Open

diagnostics: Suggest fn binding type for unstable closure for<> binders#160478
Dnreikronos wants to merge 2 commits into
rust-lang:mainfrom
Dnreikronos:diagnostics/closure_lifetime_binder_sugg

Conversation

@Dnreikronos

@Dnreikronos Dnreikronos commented Aug 4, 2026

Copy link
Copy Markdown
Contributor

Fixes #160431

for<'a> |x: &'a T| -> U { ... } currently just says "consider removing for<...>", which idk is kinda useless when someone landed there while fighting HRTB errors. This rewrites the obvious case to let cl: for<'a> fn(...) = |x| { ... } instead.

Visitor emits the gate so we have AST context for the rewrite, and we still keep a pre-expansion fallback for #[cfg(false)] etc. MachineApplicable only when we're pretty sure: by-value bindings, lifetime-only unbounded binders, no _ in the sig, no macro expansion. Capture checking is a conservative single-segment free-path heuristic since this runs pre-resolution/pre-typeck; when that looks uncertain we fall back to MaybeIncorrect (or just "consider removing"). imo that's the right tradeoff here: better a maybe-wrong help than rustfix auto-applying into E0308. fyi free-fn / None / Some cases also hit MaybeIncorrect for the same reason.

ltm if the heuristic feels too conservative and y'all want it tightened later.

@rustbot

rustbot commented Aug 4, 2026

Copy link
Copy Markdown
Collaborator

The parser was modified, potentially altering the grammar of (stable) Rust
which would be a breaking change.

cc @fmease

@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 Aug 4, 2026
@rustbot

rustbot commented Aug 4, 2026

Copy link
Copy Markdown
Collaborator

r? @JonathanBrouwer

rustbot has assigned @JonathanBrouwer.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

Why was this reviewer chosen?

The reviewer was selected based on:

  • Owners of files modified in this PR: compiler
  • compiler expanded to 75 candidates
  • Random selection from 19 candidates

@rustbot

This comment has been minimized.

@Dnreikronos
Dnreikronos force-pushed the diagnostics/closure_lifetime_binder_sugg branch from b3e5c81 to 8a03a04 Compare August 4, 2026 01:38
@Dnreikronos
Dnreikronos marked this pull request as draft August 4, 2026 01:42
@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 Aug 4, 2026
@rust-bors

This comment has been minimized.

Dnreikronos and others added 2 commits August 4, 2026 10:01
When `for<'a>` appears on a closure without `closure_lifetime_binder`,
suggest rewriting a simple let-bound closure to a stable
`for<'a> fn(...)` binding type annotation when that rewrite is safe.

Co-authored-by: Cursor <cursoragent@cursor.com>
Cover MachineApplicable rewrites, MaybeIncorrect capture cases, and
macro-expanded binders where structured suggestions must be suppressed.

Co-authored-by: Cursor <cursoragent@cursor.com>
@Dnreikronos
Dnreikronos force-pushed the diagnostics/closure_lifetime_binder_sugg branch from 8a03a04 to 1fccd4c Compare August 4, 2026 13:02
@Dnreikronos
Dnreikronos marked this pull request as ready for review August 4, 2026 13:08
@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 Aug 4, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

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.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

When encountering for<'a> in closure declaration, try to provide structured suggestion to use a binding with a type

3 participants