Skip to content

Commit

Permalink
Fail on multiple declarations of main.
Browse files Browse the repository at this point in the history
Previously, when inserting the entry function, we only checked for
duplicate _definitions_ of `main`.  However, it's possible to cause
problems even only having a duplicate _declaration_. For example,
shadowing `main` using an extern block isn't caught by the current
check, and causes an assertion failure down the line in in LLVM code.
  • Loading branch information
jumbatm committed Feb 22, 2020
1 parent 0753459 commit a796af7
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 4 deletions.
4 changes: 2 additions & 2 deletions src/librustc_codegen_ssa/base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -437,10 +437,10 @@ pub fn maybe_create_entry_wrapper<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>>(
// listing.
let main_ret_ty = cx.tcx().erase_regions(&main_ret_ty.no_bound_vars().unwrap());

if cx.get_defined_value("main").is_some() {
if cx.get_declared_value("main").is_some() {
// FIXME: We should be smart and show a better diagnostic here.
cx.sess()
.struct_span_err(sp, "entry symbol `main` defined multiple times")
.struct_span_err(sp, "entry symbol `main` declared multiple times")
.help("did you use `#[no_mangle]` on `fn main`? Use `#[start]` instead")
.emit();
cx.sess().abort_if_errors();
Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/duplicate/dupe-symbols-7.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// build-fail

//
// error-pattern: entry symbol `main` defined multiple times
// error-pattern: entry symbol `main` declared multiple times

// FIXME https://github.com/rust-lang/rust/issues/59774
// normalize-stderr-test "thread.*panicked.*Metadata module not compiled.*\n" -> ""
Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/duplicate/dupe-symbols-7.stderr
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
error: entry symbol `main` defined multiple times
error: entry symbol `main` declared multiple times
--> $DIR/dupe-symbols-7.rs:12:1
|
LL | fn main(){}
Expand Down
12 changes: 12 additions & 0 deletions src/test/ui/duplicate/dupe-symbols-8.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// build-fail
// error-pattern: entry symbol `main` declared multiple times
//
// See #67946.

#![allow(warnings)]
fn main() {
extern "Rust" {
fn main();
}
unsafe { main(); }
}
15 changes: 15 additions & 0 deletions src/test/ui/duplicate/dupe-symbols-8.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
error: entry symbol `main` declared multiple times
--> $DIR/dupe-symbols-8.rs:7:1
|
LL | / fn main() {
LL | | extern "Rust" {
LL | | fn main();
LL | | }
LL | | unsafe { main(); }
LL | | }
| |_^
|
= help: did you use `#[no_mangle]` on `fn main`? Use `#[start]` instead

error: aborting due to previous error

0 comments on commit a796af7

Please sign in to comment.