Skip to content

"main function is not allowed to have generic parameters" when the parameter is a reference #118772

New issue

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

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

Already on GitHub? # to your account

Closed
wyattscarpenter opened this issue Dec 9, 2023 · 4 comments · Fixed by #119047
Labels
A-diagnostics Area: Messages for errors, warnings, and lints D-confusing Diagnostics: Confusing error or lint that should be reworked. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@wyattscarpenter
Copy link

Code

fn main(hmm: &i32) {
  println!("Hello, world!");
}

Current output

error[E0131]: `main` function is not allowed to have generic parameters
  --> src\bin\8.rs:22:8
   |
22 | fn main(hmm: &i32) {
   |        ^ `main` cannot have generic parameters

For more information about this error, try `rustc --explain E0131`.

Desired output

error[E0580]: `main` function has wrong type
  --> src\bin\8.rs:22:1
   |
22 | fn main(hmm: i32) {
   | ^^^^^^^^^^^^^^^^^ incorrect number of function parameters
   |
   = note: expected signature `fn()`
              found signature `fn(i32)`

For more information about this error, try `rustc --explain E0580`.

Rationale and extra context

It is less clear to display the E0131 message for the situation, because it it's about generic parameters. Eg to quote from rustc --explain E0131:

fn main() { // error: main function is not allowed to have generic parameters

Whereas rustc --explain E0580 explains:

The main function prototype should never take arguments.

Other cases

No response

Anything else?

Additionally, neither error is output when the command run is cargo test; which is probably erroneous behavior, as check, clippy, build, and run.

@wyattscarpenter wyattscarpenter 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 Dec 9, 2023
@rustbot rustbot added the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Dec 9, 2023
@saethlin
Copy link
Member

saethlin commented Dec 9, 2023

The generic in question is probably the lifetime parameter, fn main(hmm: &'static i32) {} produces the better diagnostic.

@saethlin saethlin removed the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Dec 9, 2023
@fmease fmease added the D-confusing Diagnostics: Confusing error or lint that should be reworked. label Dec 9, 2023
@compiler-errors
Copy link
Member

I think we can just invert the check for generic params and the signature comparison here, so the signature comparison happens first.

@mu001999
Copy link
Contributor

mu001999 commented Dec 10, 2023

Why is this only emitted when cargo run, not even when cargo build?

cargo run:

error[E0131]: `main` function is not allowed to have generic parameters
 --> src/main.rs:1:8
  |
1 | fn main(hmm: &i32) {
  |        ^ `main` cannot have generic parameters

For more information about this error, try `rustc --explain E0131`.
error: could not compile `playground` (bin "playground") due to 1 previous error

cargo build:

warning: unused variable: `hmm`
 --> src/lib.rs:1:9
  |
1 | fn main(hmm: &i32) {
  |         ^^^ help: if this is intentional, prefix it with an underscore: `_hmm`
  |
  = note: `#[warn(unused_variables)]` on by default

warning: function `main` is never used
 --> src/lib.rs:1:4
  |
1 | fn main(hmm: &i32) {
  |    ^^^^
  |
  = note: `#[warn(dead_code)]` on by default

warning: `playground` (lib) generated 2 warnings (run `cargo fix --lib -p playground` to apply 1 suggestion)
    Finished dev [unoptimized + debuginfo] target(s) in 0.47s

@saethlin
Copy link
Member

Your cargo run invocation is building a bin crate, and your cargo build crate is building a library crate; that's the difference. fn main is special in a bin crate, and in a library crate it's just an ordinary function with no requirements on its signature.

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints D-confusing Diagnostics: Confusing error or lint that should be reworked. 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.

6 participants