Closed
Description
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
.
Activity
saethlin commentedon Dec 9, 2023
The generic in question is probably the lifetime parameter,
fn main(hmm: &'static i32) {}
produces the better diagnostic.compiler-errors commentedon Dec 9, 2023
I think we can just invert the check for generic params and the signature comparison here, so the signature comparison happens first.
mu001999 commentedon Dec 10, 2023
Why is this only emitted when
cargo run
, not even whencargo build
?cargo run
:cargo build
:saethlin commentedon Dec 10, 2023
Your
cargo run
invocation is building a bin crate, and yourcargo 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.Auto merge of rust-lang#119047 - mu001999:fix/118772, r=wesleywiser