Skip to content

Commit

Permalink
Rollup merge of rust-lang#69379 - jumbatm:llvm-sigsegv, r=pnkfelix
Browse files Browse the repository at this point in the history
Fail on multiple declarations of `main`.

Closes rust-lang#67946.

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.

r? @pnkfelix
  • Loading branch information
Dylan-DPC committed Feb 27, 2020
2 parents 4524657 + a796af7 commit f7f57eb
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 f7f57eb

Please sign in to comment.