Skip to content

diagnostics: suggest type annotation for closure params on HRTB FnOnce mismatch#158701

Merged
rust-bors[bot] merged 5 commits into
rust-lang:mainfrom
Rohan-Singla:fix/#158393
Jul 8, 2026
Merged

diagnostics: suggest type annotation for closure params on HRTB FnOnce mismatch#158701
rust-bors[bot] merged 5 commits into
rust-lang:mainfrom
Rohan-Singla:fix/#158393

Conversation

@Rohan-Singla

@Rohan-Singla Rohan-Singla commented Jul 2, 2026

Copy link
Copy Markdown
Contributor

Fixes #158393

Problem

When a closure passed to a function requiring for<'a> FnOnce(&'a mut [u8]) has
unannotated parameters (e.g. |buf|), the compiler emits the confusing error:

implementation of `FnOnce` is not general enough

without explaining how to fix it.

In many cases, simply adding an explicit type annotation to the closure
parameter (e.g. |buf: &mut [u8]|) resolves the lifetime ambiguity, but the
compiler does not currently provide any guidance.

What

This PR adds a diagnostic suggestion in placeholder_error.rs that detects
when:

  • the mismatched trait is an Fn-trait (FnOnce, FnMut, or Fn)
  • the self type is a local closure
  • one or more closure parameters do not have explicit type annotations

When these conditions are met, the compiler now emits the following help
message:

help: consider adding an explicit type annotation to the closure parameter
      to resolve the lifetime ambiguity
  |
  |     let outer_closure = |buf: &mut [u8]| {
  |                             +++++++++++

Testing

A UI test has been added covering the exact reproduction case from the
reported issue.

@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 Jul 2, 2026
@rustbot

rustbot commented Jul 2, 2026

Copy link
Copy Markdown
Collaborator

r? @mejrs

rustbot has assigned @mejrs.
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 20 candidates

@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

@mejrs mejrs left a comment

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.

Thanks, this looks mostly good to me. Does this suggestion fix the errors in all the affected ui tests? Can you add rustfix tests for those that are?

Also it seems that you need to bless more tests.

@rustbot author

View changes since this review

err.multipart_suggestion(
"consider adding an explicit type annotation to the closure \
parameter to resolve the lifetime ambiguity",
suggestions,

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.

So the thing is that I'm not sure how often this actually fixes the error. This reads a lot like "if you add a type annotation, this will fix the error". We should not overpromise.

Suggested change
suggestions,
"consider adding an explicit type annotation to the closure's argument",

(also, should be argument/arguments depending on if there's more than one suggestion)

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.

Thanks , changed the suggestion message added a .fixed file too!

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

rustbot commented Jul 4, 2026

Copy link
Copy Markdown
Collaborator

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

@Rohan-Singla

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 Jul 5, 2026
@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer

Copy link
Copy Markdown
Collaborator

The job x86_64-gnu-tools failed! Check out the build log: (web) (plain enhanced) (plain)

Click to see the possible cause of the failure (guessed by this bot)
...............................F.................. (50/144)
.................................................. (100/144)
............................................       (144/144)

======== tests/rustdoc-gui/go-to-collapsed-elem.goml ========

[ERROR] line 40
    at `tests/rustdoc-gui/go-to-collapsed-elem.goml` line 21: Error: Node is detached from document: for command `click: "//*[@id='search']//a[@href='../test_docs/struct.Foo.html#method.must_use']"`
    at <file:///checkout/obj/build/x86_64-unknown-linux-gnu/test/rustdoc-gui/doc/test_docs/struct.Foo.html?search=t_use>


<= doc-ui tests done: 143 succeeded, 1 failed, 0 filtered out

Error: ()

@mejrs mejrs left a comment

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.

Thanks!

I think the CI failure is spurious?

@bors r+ rollup

View changes since this review

@rust-bors

rust-bors Bot commented Jul 7, 2026

Copy link
Copy Markdown
Contributor

📌 Commit 049b3a3 has been approved by mejrs

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 Jul 7, 2026
JonathanBrouwer added a commit to JonathanBrouwer/rust that referenced this pull request Jul 7, 2026
… r=mejrs

 diagnostics: suggest type annotation for closure params on HRTB FnOnce mismatch

Fixes rust-lang#158393

## Problem

When a closure passed to a function requiring `for<'a> FnOnce(&'a mut [u8])` has
unannotated parameters (e.g. `|buf|`), the compiler emits the confusing error:

```text
implementation of `FnOnce` is not general enough
```

without explaining how to fix it.

In many cases, simply adding an explicit type annotation to the closure
parameter (e.g. `|buf: &mut [u8]|`) resolves the lifetime ambiguity, but the
compiler does not currently provide any guidance.

## What

This PR adds a diagnostic suggestion in `placeholder_error.rs` that detects
when:

- the mismatched trait is an Fn-trait (`FnOnce`, `FnMut`, or `Fn`)
- the self type is a local closure
- one or more closure parameters do not have explicit type annotations

When these conditions are met, the compiler now emits the following help
message:

```text
help: consider adding an explicit type annotation to the closure parameter
      to resolve the lifetime ambiguity
  |
  |     let outer_closure = |buf: &mut [u8]| {
  |                             +++++++++++
```

## Testing

A UI test has been added covering the exact reproduction case from the
reported issue.
JonathanBrouwer added a commit to JonathanBrouwer/rust that referenced this pull request Jul 7, 2026
… r=mejrs

 diagnostics: suggest type annotation for closure params on HRTB FnOnce mismatch

Fixes rust-lang#158393

## Problem

When a closure passed to a function requiring `for<'a> FnOnce(&'a mut [u8])` has
unannotated parameters (e.g. `|buf|`), the compiler emits the confusing error:

```text
implementation of `FnOnce` is not general enough
```

without explaining how to fix it.

In many cases, simply adding an explicit type annotation to the closure
parameter (e.g. `|buf: &mut [u8]|`) resolves the lifetime ambiguity, but the
compiler does not currently provide any guidance.

## What

This PR adds a diagnostic suggestion in `placeholder_error.rs` that detects
when:

- the mismatched trait is an Fn-trait (`FnOnce`, `FnMut`, or `Fn`)
- the self type is a local closure
- one or more closure parameters do not have explicit type annotations

When these conditions are met, the compiler now emits the following help
message:

```text
help: consider adding an explicit type annotation to the closure parameter
      to resolve the lifetime ambiguity
  |
  |     let outer_closure = |buf: &mut [u8]| {
  |                             +++++++++++
```

## Testing

A UI test has been added covering the exact reproduction case from the
reported issue.
rust-bors Bot pushed a commit that referenced this pull request Jul 7, 2026
…uwer

Rollup of 18 pull requests

Successful merges:

 - #157385 (Enable Enzyme on x86_64-apple)
 - #157561 (rustdoc: do not include extra stuff in span)
 - #158179 (std: unconditionally use `preadv`/`pwritev` on AArch64 macOS)
 - #158617 (allow mGCA const arguments to fall back to anon consts)
 - #158621 (disallow `extern "custom"` on wasm and spirv targets)
 - #158690 (delegation: support mapping of all arguments with `Self` type)
 - #158696 (Rename some `body_id` to `body_def_id`)
 - #158697 (Fixes for QNX SDP 8)
 - #158760 (Clarify that `LocalKey::try_with` may return `AccessError`)
 - #157801 (Rewrite safety requirements for `Allocator` impls)
 - #158333 (Fix typetree generation for differentiated functions)
 - #158646 (powerpc64le_unknown_freebsd.rs: link with -lgcc)
 - #158701 ( diagnostics: suggest type annotation for closure params on HRTB FnOnce mismatch)
 - #158791 (Avoid unused braces lint for macro generated arguments)
 - #158802 (Use `ci-mirrors` in `armhf-gnu` for {busybox, ubuntu rootfs} artifacts)
 - #158841 (Avoid final override ICE for RPITIT associated types)
 - #158889 (tests: catch up with LLVM returning f128 on the stack)
 - #158905 (delegation: add constraints to new generic args)
rust-bors Bot pushed a commit that referenced this pull request Jul 7, 2026
…uwer

Rollup of 19 pull requests

Successful merges:

 - #156016 (view-types: store view types in the AST)
 - #157385 (Enable Enzyme on x86_64-apple)
 - #158179 (std: unconditionally use `preadv`/`pwritev` on AArch64 macOS)
 - #158621 (disallow `extern "custom"` on wasm and spirv targets)
 - #158690 (delegation: support mapping of all arguments with `Self` type)
 - #158696 (Rename some `body_id` to `body_def_id`)
 - #158697 (Fixes for QNX SDP 8)
 - #158760 (Clarify that `LocalKey::try_with` may return `AccessError`)
 - #157801 (Rewrite safety requirements for `Allocator` impls)
 - #158085 (rustdoc: Fix sidebar heading order)
 - #158333 (Fix typetree generation for differentiated functions)
 - #158646 (powerpc64le_unknown_freebsd.rs: link with -lgcc)
 - #158701 ( diagnostics: suggest type annotation for closure params on HRTB FnOnce mismatch)
 - #158791 (Avoid unused braces lint for macro generated arguments)
 - #158802 (Use `ci-mirrors` in `armhf-gnu` for {busybox, ubuntu rootfs} artifacts)
 - #158841 (Avoid final override ICE for RPITIT associated types)
 - #158889 (tests: catch up with LLVM returning f128 on the stack)
 - #158905 (delegation: add constraints to new generic args)
 - #158922 (tests: clean up over-constraint on LLVM feature count)

Failed merges:

 - #158617 (allow mGCA const arguments to fall back to anon consts)
@Rohan-Singla

Copy link
Copy Markdown
Contributor Author

Thanks!

I think the CI failure is spurious?

@bors r+ rollup

View changes since this review

Yeah i believe so think so i suspected its due to all tests are not blessed but i did so now , thanks for the rollup!

@rust-bors
rust-bors Bot merged commit b8df403 into rust-lang:main Jul 8, 2026
9 of 13 checks passed
@rustbot rustbot added this to the 1.99.0 milestone Jul 8, 2026
rust-timer added a commit that referenced this pull request Jul 8, 2026
Rollup merge of #158701 - Rohan-Singla:fix/#158393, r=mejrs

 diagnostics: suggest type annotation for closure params on HRTB FnOnce mismatch

Fixes #158393

## Problem

When a closure passed to a function requiring `for<'a> FnOnce(&'a mut [u8])` has
unannotated parameters (e.g. `|buf|`), the compiler emits the confusing error:

```text
implementation of `FnOnce` is not general enough
```

without explaining how to fix it.

In many cases, simply adding an explicit type annotation to the closure
parameter (e.g. `|buf: &mut [u8]|`) resolves the lifetime ambiguity, but the
compiler does not currently provide any guidance.

## What

This PR adds a diagnostic suggestion in `placeholder_error.rs` that detects
when:

- the mismatched trait is an Fn-trait (`FnOnce`, `FnMut`, or `Fn`)
- the self type is a local closure
- one or more closure parameters do not have explicit type annotations

When these conditions are met, the compiler now emits the following help
message:

```text
help: consider adding an explicit type annotation to the closure parameter
      to resolve the lifetime ambiguity
  |
  |     let outer_closure = |buf: &mut [u8]| {
  |                             +++++++++++
```

## Testing

A UI test has been added covering the exact reproduction case from the
reported issue.
@jamesmunns

Copy link
Copy Markdown
Contributor

Hell yeah, thanks @Rohan-Singla!

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.

Compiler suggests HRTB for closure when only type annotation needed

5 participants