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

Error when fn keyword is missing could be more explicit #125446

Closed
hds opened this issue May 23, 2024 · 3 comments · Fixed by #127419
Closed

Error when fn keyword is missing could be more explicit #125446

hds opened this issue May 23, 2024 · 3 comments · Fixed by #127419
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-parser Area: The parsing of Rust source code to an AST C-enhancement Category: An issue proposing an enhancement or a PR with one. D-newcomer-roadblock Diagnostics: Confusing error or lint; hard to understand for new users. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@hds
Copy link

hds commented May 23, 2024

Code

whoops() {}

fn main() {
    whoops();
}

Current output

error: expected one of `!` or `::`, found `(`
 --> src/main.rs:1:7
  |
1 | whoops() {}
  |       ^ expected one of `!` or `::`

error: could not compile `playground` (bin "playground") due to 1 previous error

Desired output

error: function definition is missing `fn` keyword
 --> src/main.rs:1:7
  |
1 | whoops() {}
  | ^ should be `fn whoops()`

Rationale and extra context

The current error appears to be based entirely on the syntax tree. However, adding a ! or :: in this case won't provide compilable code by itself.

It may be possible to determine that the incorrect code "looks like" a function definition which is missing the fn keyword in front of it (the author doesn't have the knowledge to say for sure). Especially in cases where the context doesn't permit calling a function (i.e. not inside another function).

This would provide immediately actionable diagnostics.

Other cases

A missing struct or enum keyword could be differentiated by the contents of the braces, or potentially from the case of the first character of the name (assuming it's correct).

Missing struct:

Whoops {
    value: u64,
}

Missing enum:

Whoops {
    OptionA,
    OptionB,
}

A unit struct is somewhat more ambiguous (if the name is capitalized correctly then there is a hint):

Whoops {}

Rust Version

rustc 1.78.0 (9b00956e5 2024-04-29)
binary: rustc
commit-hash: 9b00956e56009bab2aa15d7bff10916599e3d6d6
commit-date: 2024-04-29
host: x86_64-apple-darwin
release: 1.78.0
LLVM version: 18.1.2

Anything else?

No response

@hds hds 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 May 23, 2024
@veera-sivarajan
Copy link
Contributor

Similar to #65381

@veera-sivarajan
Copy link
Contributor

@rustbot label +A-parser +C-enhancement +D-newcomer-roadblock

@rustbot rustbot added A-parser Area: The parsing of Rust source code to an AST C-enhancement Category: An issue proposing an enhancement or a PR with one. D-newcomer-roadblock Diagnostics: Confusing error or lint; hard to understand for new users. labels May 23, 2024
@gurry
Copy link
Contributor

gurry commented May 24, 2024

@rustbot claim

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 A-parser Area: The parsing of Rust source code to an AST C-enhancement Category: An issue proposing an enhancement or a PR with one. D-newcomer-roadblock Diagnostics: Confusing error or lint; hard to understand for new users. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants