]> git.lizzy.rs Git - rust.git/commitdiff
Add a distinct error code and description for "main function has wrong type"
authorGuillaume Gomez <guillaume1.gomez@gmail.com>
Wed, 4 Jan 2017 11:56:33 +0000 (12:56 +0100)
committerGuillaume Gomez <guillaume1.gomez@gmail.com>
Thu, 26 Jan 2017 10:12:46 +0000 (11:12 +0100)
src/librustc/diagnostics.rs
src/librustc/infer/error_reporting.rs
src/test/compile-fail/E0308-3.rs [deleted file]
src/test/compile-fail/E0579.rs [new file with mode: 0644]
src/test/compile-fail/bad-main.rs
src/test/compile-fail/extern-main-fn.rs
src/test/compile-fail/main-wrong-type-2.rs
src/test/compile-fail/main-wrong-type.rs

index 0d180e6ad76fda1be666cb41e66e7c0f53bd3083..6d94e1d725938f2b9c3930087b085b0dc26b7001 100644 (file)
@@ -1694,6 +1694,30 @@ fn main() {
 https://doc.rust-lang.org/book/closures.html
 "##,
 
+E0579: r##"
+The `main` function was incorrectly declared.
+
+Erroneous code example:
+
+```compile_fail,E0579
+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`.
+"##,
+
 }
 
 
index 095d2a78a94cd993e0b5a0597f07f50006404afc..2bed8148b9f8c8a4e5feb0319fa5218b414efd68 100644 (file)
@@ -629,10 +629,13 @@ pub fn report_and_explain_type_error(&self,
         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, E0579, "{}", 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
diff --git a/src/test/compile-fail/E0308-3.rs b/src/test/compile-fail/E0308-3.rs
deleted file mode 100644 (file)
index d7dca05..0000000
+++ /dev/null
@@ -1,11 +0,0 @@
-// Copyright 2016 The Rust Project Developers. See the COPYRIGHT
-// file at the top-level directory of this distribution and at
-// http://rust-lang.org/COPYRIGHT.
-//
-// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
-// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
-// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
-// option. This file may not be copied, modified, or distributed
-// except according to those terms.
-
-fn main() -> i32 { 0 } //~ ERROR E0308
diff --git a/src/test/compile-fail/E0579.rs b/src/test/compile-fail/E0579.rs
new file mode 100644 (file)
index 0000000..beb6c79
--- /dev/null
@@ -0,0 +1,11 @@
+// Copyright 2016 The Rust Project Developers. See the COPYRIGHT
+// file at the top-level directory of this distribution and at
+// http://rust-lang.org/COPYRIGHT.
+//
+// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
+// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
+// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
+// option. This file may not be copied, modified, or distributed
+// except according to those terms.
+
+fn main() -> i32 { 0 } //~ ERROR E0579
index 1253f7569e7e8e48ec2f9e315838d05b5c6940fa..6cd033da4bd711e7d7eac9381f8df1eeeb363eea 100644 (file)
@@ -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 [E0579]
index 11f299acefa874e55b5efa49b81eb345fd9350be..479b3e532c6b3baec316c7375388c2a3c53367da 100644 (file)
@@ -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 [E0579]
index 2878cbc7fc15488d6fcfb865390be929d419f7c9..da1455403c2e3d29c2e4558cecf0032618be0388 100644 (file)
@@ -9,6 +9,6 @@
 // except according to those terms.
 
 fn main() -> char {
-//~^ ERROR: main function has wrong type
+//~^ ERROR: main function has wrong type [E0579]
     ' '
 }
index 431b855d5177373cfcabd13a747e6f90dbe86e48..dc51777503569c381db611b03db84a0f9ec0f29c 100644 (file)
@@ -14,5 +14,5 @@ struct S {
 }
 
 fn main(foo: S) {
-//~^ ERROR: main function has wrong type
+//~^ ERROR: main function has wrong type [E0579]
 }