Skip to content

Commit 31114ac

Browse files
alexcrichtonthestinger
authored andcommitted
Require extern "Rust" fn main() exactly
1 parent b727b9e commit 31114ac

File tree

5 files changed

+27
-15
lines changed

5 files changed

+27
-15
lines changed

src/librustc/middle/typeck/mod.rs

+13-10
Original file line numberDiff line numberDiff line change
@@ -319,16 +319,19 @@ fn check_main_fn_ty(ccx: &CrateCtxt,
319319
}
320320
_ => ()
321321
}
322-
let mut ok = ty::type_is_nil(fn_ty.sig.output);
323-
let num_args = fn_ty.sig.inputs.len();
324-
ok &= num_args == 0u;
325-
if !ok {
326-
tcx.sess.span_err(
327-
main_span,
328-
fmt!("Wrong type in main function: found `%s`, \
329-
expected `fn() -> ()`",
330-
ppaux::ty_to_str(tcx, main_t)));
331-
}
322+
let se_ty = ty::mk_bare_fn(tcx, ty::BareFnTy {
323+
purity: ast::impure_fn,
324+
abis: abi::AbiSet::Rust(),
325+
sig: ty::FnSig {
326+
bound_lifetime_names: opt_vec::Empty,
327+
inputs: ~[],
328+
output: ty::mk_nil()
329+
}
330+
});
331+
332+
require_same_types(tcx, None, false, main_span, main_t, se_ty,
333+
|| fmt!("main function expects type: `%s`",
334+
ppaux::ty_to_str(ccx.tcx, se_ty)));
332335
}
333336
_ => {
334337
tcx.sess.span_bug(main_span,

src/test/compile-fail/bad-main.rs

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

11-
// error-pattern:expected `fn()
12-
13-
fn main(x: int) { }
11+
fn main(x: int) { } //~ ERROR: main function expects type
+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// Copyright 2013 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
extern fn main() {} //~ ERROR: main function expects type

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

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

1111
fn main() -> char {
12-
//~^ ERROR Wrong type in main function: found `extern "Rust" fn() -> char`
12+
//~^ ERROR: main function expects type
1313
}

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 Wrong type in main function: found `extern "Rust" fn(S)`
17+
//~^ ERROR: main function expects type
1818
}

0 commit comments

Comments
 (0)