diagnostics: suggest type annotation for closure params on HRTB FnOnce mismatch#158701
Conversation
|
r? @mejrs rustbot has assigned @mejrs. Use Why was this reviewer chosen?The reviewer was selected based on:
|
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
There was a problem hiding this comment.
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
| err.multipart_suggestion( | ||
| "consider adding an explicit type annotation to the closure \ | ||
| parameter to resolve the lifetime ambiguity", | ||
| suggestions, |
There was a problem hiding this comment.
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.
| 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)
There was a problem hiding this comment.
Thanks , changed the suggestion message added a .fixed file too!
|
Reminder, once the PR becomes ready for a review, use |
…ure HRTB annotation
|
@rustbot ready |
This comment has been minimized.
This comment has been minimized.
|
The job Click to see the possible cause of the failure (guessed by this bot) |
… 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.
… 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.
…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)
…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)
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! |
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.
|
Hell yeah, thanks @Rohan-Singla! |
Fixes #158393
Problem
When a closure passed to a function requiring
for<'a> FnOnce(&'a mut [u8])hasunannotated parameters (e.g.
|buf|), the compiler emits the confusing error: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 thecompiler does not currently provide any guidance.
What
This PR adds a diagnostic suggestion in
placeholder_error.rsthat detectswhen:
FnOnce,FnMut, orFn)When these conditions are met, the compiler now emits the following help
message:
Testing
A UI test has been added covering the exact reproduction case from the
reported issue.