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
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
@rust-bors

This comment has been minimized.

Dnreikronos and others added 2 commits August 10, 2026 14:12
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 1fccd4c to 7b0d9e4 Compare August 10, 2026 17:13
@rustbot

rustbot commented Aug 10, 2026

Copy link
Copy Markdown
Collaborator

This PR was rebased onto a different main commit. Here's a range-diff highlighting what actually changed.

Rebasing is a normal part of keeping PRs up to date, so no action is needed—this note is just to help reviewers.

@JonathanBrouwer

JonathanBrouwer commented Aug 10, 2026

Copy link
Copy Markdown
Contributor

This looks cool but I might not have time to review this in the next 2 weeks as it would require me to do some reading about how for<> binders are implemented
@rustbot reroll

@rustbot rustbot assigned mejrs and unassigned JonathanBrouwer Aug 10, 2026
@JonathanBrouwer

JonathanBrouwer commented Aug 10, 2026

Copy link
Copy Markdown
Contributor

I will do a perf run, just in case, since this does some extra work
@bors try @rust-timer queue

@rust-timer

This comment has been minimized.

@rustbot rustbot added the S-waiting-on-perf Status: Waiting on a perf run to be completed. label Aug 10, 2026
@rust-bors

This comment has been minimized.

rust-bors Bot pushed a commit that referenced this pull request Aug 10, 2026
…er_sugg, r=<try>

diagnostics: Suggest fn binding type for unstable closure for<> binders
@rust-bors

rust-bors Bot commented Aug 10, 2026

Copy link
Copy Markdown
Contributor

☀️ Try build successful (CI)
Build commit: 44fa895 (44fa895cdd61615981ffd0c4e535670c534b839a)
Base parent: ef20314 (ef20314466010b8b9259ec5f86230c530ca08661)

@rust-timer

This comment has been minimized.

@rust-timer

Copy link
Copy Markdown
Collaborator

Finished benchmarking commit (44fa895): comparison URL.

Overall result: no relevant changes - no action needed

Benchmarking means the PR may be perf-sensitive. Consider adding rollup=never if this change is not fit for rolling up.

@rustbot label: -S-waiting-on-perf -perf-regression

Instruction count

This perf run didn't have relevant results for this metric.

Max RSS (memory usage)

Results (secondary -0.6%)

A less reliable metric. May be of interest, but not used to determine the overall result above.

mean range count
Regressions ❌
(primary)
- - 0
Regressions ❌
(secondary)
1.0% [0.4%, 2.7%] 5
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
-1.4% [-5.7%, -0.4%] 9
All ❌✅ (primary) - - 0

Cycles

Results (primary 2.6%, secondary -0.2%)

A less reliable metric. May be of interest, but not used to determine the overall result above.

mean range count
Regressions ❌
(primary)
2.6% [2.6%, 2.6%] 1
Regressions ❌
(secondary)
0.8% [0.4%, 2.3%] 10
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
-0.8% [-3.0%, -0.4%] 17
All ❌✅ (primary) 2.6% [2.6%, 2.6%] 1

Binary size

This perf run didn't have relevant results for this metric.

Bootstrap: 455.478s -> 454.351s (-0.25%)
Artifact size: 398.50 MiB -> 398.45 MiB (-0.01%)

@rustbot rustbot removed the S-waiting-on-perf Status: Waiting on a perf run to be completed. label Aug 10, 2026
@oli-obk

This comment was marked as resolved.

@oli-obk oli-obk closed this Aug 13, 2026
@rustbot rustbot removed the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Aug 13, 2026
@oli-obk oli-obk reopened this Aug 13, 2026
@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Aug 13, 2026
@rust-bors

rust-bors Bot commented Aug 14, 2026

Copy link
Copy Markdown
Contributor

☔ The latest upstream changes (presumably #161075) made this pull request unmergeable. Please resolve the merge conflicts by rebasing.

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

6 participants