Skip to content

Add a distinct error code and description for "main function has wron… #38819

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

Merged
merged 2 commits into from
Jan 26, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions src/librustc/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1694,6 +1694,30 @@ To understand better how closures work in Rust, read:
https://doc.rust-lang.org/book/closures.html
"##,

E0580: r##"
The `main` function was incorrectly declared.

Erroneous code example:

```compile_fail,E0580
fn main() -> i32 { // error: main function has wrong type
0
}
```

The `main` function prototype should never take arguments or return type.
Example:

```
fn main() {
// your code
}
```

If you want to get command-line arguments, use `std::env::args`. To exit with a
specified exit code, use `std::process::exit`.
"##,

}


Expand Down
7 changes: 5 additions & 2 deletions src/librustc/infer/error_reporting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -629,10 +629,13 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
let mut diag = match trace.cause.code {
ObligationCauseCode::IfExpressionWithNoElse => {
struct_span_err!(self.tcx.sess, span, E0317, "{}", failure_str)
},
}
ObligationCauseCode::MainFunctionType => {
struct_span_err!(self.tcx.sess, span, E0580, "{}", failure_str)
}
_ => {
struct_span_err!(self.tcx.sess, span, E0308, "{}", failure_str)
},
}
};
self.note_type_err(&mut diag, &trace.cause, None, Some(trace.values), terr);
diag
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.

fn main() -> i32 { 0 } //~ ERROR E0308
fn main() -> i32 { 0 } //~ ERROR E0580
2 changes: 1 addition & 1 deletion src/test/compile-fail/bad-main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.

fn main(x: isize) { } //~ ERROR: main function has wrong type
fn main(x: isize) { } //~ ERROR: main function has wrong type [E0580]
2 changes: 1 addition & 1 deletion src/test/compile-fail/extern-main-fn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.

extern fn main() {} //~ ERROR: main function has wrong type
extern fn main() {} //~ ERROR: main function has wrong type [E0580]
2 changes: 1 addition & 1 deletion src/test/compile-fail/main-wrong-type-2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@
// except according to those terms.

fn main() -> char {
//~^ ERROR: main function has wrong type
//~^ ERROR: main function has wrong type [E0580]
' '
}
2 changes: 1 addition & 1 deletion src/test/compile-fail/main-wrong-type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,5 @@ struct S {
}

fn main(foo: S) {
//~^ ERROR: main function has wrong type
//~^ ERROR: main function has wrong type [E0580]
}