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

missing fn before main() -- error could be improved #65381

Closed
ultrasaurus opened this issue Oct 13, 2019 · 6 comments · Fixed by #127419
Closed

missing fn before main() -- error could be improved #65381

ultrasaurus opened this issue Oct 13, 2019 · 6 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

@ultrasaurus
Copy link

ultrasaurus commented Oct 13, 2019

As a newcomer to Rust, sometimes I forget fn before main() and the error message doesn't make sense in this instance:

Example file:

main() {
}

Error message

error: expected one of `!` or `::`, found `(`
 --> examples/tuple.rs:1:5
  |
1 | main() {
  |     ^ expected one of `!` or `::` here

suggested change:

"^ expected prior fn for a function declaration or suffix ! for a macro"

I'm not familiar with a case where :: would make sense here without other changes too.

@jonas-schievink jonas-schievink added 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. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Oct 13, 2019
@Centril Centril added the D-newcomer-roadblock Diagnostics: Confusing error or lint; hard to understand for new users. label Oct 13, 2019
@Centril
Copy link
Contributor

Centril commented Oct 13, 2019

cc @estebank @petrochenkov -- While this would be good to fix, it is probably difficult and I suspect it requires some thought to avoid erroring on e.g. main(); (a function call -- consider function defs as "statements"). Also, I would like to merge the function grammars (free, foreign, trait, impl) before adding a diagnostic here to ensure that they can still be merged.

@estebank
Copy link
Contributor

Note that we already do this for trait items. In those cases we already do some bounded lookahead to look for { or ; to make sure the suggestion is correct. In this case it would only look for {.

@MatsPalmgren
Copy link

Fwiw, I made this typo today:

268 |     pub const initial_value() -> Self {
    |                            ^ expected `:`

A better error message would be:

268 |     pub const initial_value() -> Self {
    |              ^ did you forget `fn` ?

@dbofmmbt
Copy link

I don't know if it fits here, but it would be good to improve the error messages for users using function, func, fun etc too. The message which appears for this kind of mistake is the same as the error message on this issue. IMO the current message may look very confusing for beginners and even for more experienced folks.

Example:

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

error: aborting due to previous error

error: could not compile `playground`

To learn more, run the command again with --verbose.

@estebank
Copy link
Contributor

estebank commented Aug 4, 2023

For the bare C-style main() {}, we still don't handle it, but for fun main() {} we do:

error: expected one of `!` or `::`, found `main`
 --> src/lib.rs:1:5
  |
1 | fun main() {}
  | --- ^^^^ expected one of `!` or `::`
  | |
  | help: write `fn` instead of `fun` to declare a function

@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.

7 participants