"wrap the `u32` in a closure with no arguments" doesn't always make sense
Nobody has claimed this yet.
- Dominant language
- Rust
- Stars
- 119k
- Forks
- 16.1k
- PR merge metrics
- PR metrics pending
Description
Code
fn f<F: FnMut()>() {}
fn main() {
f::<u32>();
}
Current output
error[E0277]: expected a `FnMut()` closure, found `u32`
--> src/main.rs:3:9
|
3 | f::<u32>();
| ^^^ expected an `FnMut()` closure, found `u32`
|
= help: the trait `FnMut<()>` is not implemented for `u32`
= note: wrap the `u32` in a closure with no arguments: `|| { /* code */ }`
note: required by a bound in `f`
--> src/main.rs:1:9
|
1 | fn f<F: FnMut()>() {}
| ^^^^^^^ required by this bound in `f`
Desired output
The suggestion to "wrap" should not be shown, as there is no argument.
Rationale and extra context
"wrap the u32 in a closure with no arguments: || { /* code */ }" doesn't make sense as there is no code of that type.
Other cases
No response
Rust Version
Current nightly
Anything else?
Even if there is an argument, this is somewhat questionable:
fn f<F: FnMut()>(_: F) {}
fn main() {
f(0u32);
}
This suggests f(|| 0u32) but that's still ill-typed as the closure must have return value (). So the suggestion should probably only be shown if the type of the expression matches the return type of the closure.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Reproduce both Rust examples with the current nightly compiler and trace the diagnostic that emits the closure-wrapping suggestion. Done means the no-argument case no longer suggests wrapping, and suggestions are not emitted when the resulting closure return type would be invalid.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- rust
- Domain
- compilers
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 45/100