Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
RalfJung opened this issue Apr 17, 2024 · 0 comments
Open

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

RalfJung opened this issue Apr 17, 2024 · 0 comments
Labels
A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@RalfJung
Copy link
Member

RalfJung commented Apr 17, 2024

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.

@RalfJung RalfJung added A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Apr 17, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

1 participant