Skip to content

Commit 07fe04c

Browse files
committed
Auto merge of #38819 - GuillaumeGomez:main_func_wrong_type, r=GuillaumeGomez
Add a distinct error code and description for "main function has wron… …g type"
2 parents 5158501 + 97b9c8b commit 07fe04c

File tree

7 files changed

+34
-7
lines changed

7 files changed

+34
-7
lines changed

Diff for: src/librustc/diagnostics.rs

+24
Original file line numberDiff line numberDiff line change
@@ -1694,6 +1694,30 @@ To understand better how closures work in Rust, read:
16941694
https://doc.rust-lang.org/book/closures.html
16951695
"##,
16961696

1697+
E0580: r##"
1698+
The `main` function was incorrectly declared.
1699+
1700+
Erroneous code example:
1701+
1702+
```compile_fail,E0580
1703+
fn main() -> i32 { // error: main function has wrong type
1704+
0
1705+
}
1706+
```
1707+
1708+
The `main` function prototype should never take arguments or return type.
1709+
Example:
1710+
1711+
```
1712+
fn main() {
1713+
// your code
1714+
}
1715+
```
1716+
1717+
If you want to get command-line arguments, use `std::env::args`. To exit with a
1718+
specified exit code, use `std::process::exit`.
1719+
"##,
1720+
16971721
}
16981722

16991723

Diff for: src/librustc/infer/error_reporting.rs

+5-2
Original file line numberDiff line numberDiff line change
@@ -629,10 +629,13 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
629629
let mut diag = match trace.cause.code {
630630
ObligationCauseCode::IfExpressionWithNoElse => {
631631
struct_span_err!(self.tcx.sess, span, E0317, "{}", failure_str)
632-
},
632+
}
633+
ObligationCauseCode::MainFunctionType => {
634+
struct_span_err!(self.tcx.sess, span, E0580, "{}", failure_str)
635+
}
633636
_ => {
634637
struct_span_err!(self.tcx.sess, span, E0308, "{}", failure_str)
635-
},
638+
}
636639
};
637640
self.note_type_err(&mut diag, &trace.cause, None, Some(trace.values), terr);
638641
diag

Diff for: src/test/compile-fail/E0308-3.rs renamed to src/test/compile-fail/E0580.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,4 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11-
fn main() -> i32 { 0 } //~ ERROR E0308
11+
fn main() -> i32 { 0 } //~ ERROR E0580

Diff for: src/test/compile-fail/bad-main.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,4 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11-
fn main(x: isize) { } //~ ERROR: main function has wrong type
11+
fn main(x: isize) { } //~ ERROR: main function has wrong type [E0580]

Diff for: src/test/compile-fail/extern-main-fn.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,4 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11-
extern fn main() {} //~ ERROR: main function has wrong type
11+
extern fn main() {} //~ ERROR: main function has wrong type [E0580]

Diff for: src/test/compile-fail/main-wrong-type-2.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,6 @@
99
// except according to those terms.
1010

1111
fn main() -> char {
12-
//~^ ERROR: main function has wrong type
12+
//~^ ERROR: main function has wrong type [E0580]
1313
' '
1414
}

Diff for: src/test/compile-fail/main-wrong-type.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,5 @@ struct S {
1414
}
1515

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

0 commit comments

Comments
 (0)