Description
Code
fn handle<F>() where F: FnOnce -> () {}
Current output
error: expected one of `(`, `+`, `,`, `::`, `<`, or `{`, found `->`
--> src/main.rs:1:32
|
1 | fn handle<F>() where F: FnOnce -> () {}
| ^^ expected one of `(`, `+`, `,`, `::`, `<`, or `{`
Desired output
error: expected `(`, found `->`
--> src/main.rs:1:32
|
1 | fn handle<F>() where F: FnOnce -> () {}
| ^ `Fn*`-family trait bounds must be followed by parentheses
help: try using parenthesis here:
|
1 | fn handle<F>() where F: FnOnce() -> () {}
| ^^
Rationale and extra context
It feels like rustc should be able to determine that a trait followed by a ->
is a Fn*
-family trait bound and be able to suggest this behavior. If that is too general it could just check for Fn
, FnOnce
, FnMut
.
Other cases
Not that i know of...
Very similar is if parenthesis are missing from regular fn
declarations:
error: expected one of `(` or `<`, found `->`
--> src/lib.rs:3:9
|
3 | fn main -> () {}
| ^^ expected one of `(` or `<`
Anything else?
Also happens on nightly according to Rust Playground.
I'd like to try my hand at implementing this diagnostic, but I'll likely need a little bit of locational guidance. A naive code search show that the message is emitted by this code is the parser diagnostics, but I'm not sure if the adjustment would need to be made within that or from where it is invoked in this case.
rust/compiler/rustc_parse/src/parser/diagnostics.rs
Lines 384 to 388 in c528357
Edit: Updated to use the wording "Fn*
-family trait bounds" rather than "HRTBs".