Skip to content

Diagnostic when Fn*-family trait bounds is missing parens could be improved #108109

Closed

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.

pub(super) fn expected_one_of_not_found(
&mut self,
edible: &[TokenKind],
inedible: &[TokenKind],
) -> PResult<'a, bool /* recovered */> {


Edit: Updated to use the wording "Fn*-family trait bounds" rather than "HRTBs".

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsA-parserArea: The parsing of Rust source code to an ASTT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions