rust-lang / rust-lang/rust

"wrap the `u32` in a closure with no arguments" doesn't always make sense

Open
#124,074 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-diagnostics T-compiler
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

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. 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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.