From a796af7a7685a21e8c43f93aa5fb3007cd847253 Mon Sep 17 00:00:00 2001 From: jumbatm Date: Sun, 23 Feb 2020 01:43:47 +1000 Subject: [PATCH] Fail on multiple declarations of `main`. 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. --- src/librustc_codegen_ssa/base.rs | 4 ++-- src/test/ui/duplicate/dupe-symbols-7.rs | 2 +- src/test/ui/duplicate/dupe-symbols-7.stderr | 2 +- src/test/ui/duplicate/dupe-symbols-8.rs | 12 ++++++++++++ src/test/ui/duplicate/dupe-symbols-8.stderr | 15 +++++++++++++++ 5 files changed, 31 insertions(+), 4 deletions(-) create mode 100644 src/test/ui/duplicate/dupe-symbols-8.rs create mode 100644 src/test/ui/duplicate/dupe-symbols-8.stderr diff --git a/src/librustc_codegen_ssa/base.rs b/src/librustc_codegen_ssa/base.rs index e9431d94863ef..d6e1ab8909c59 100644 --- a/src/librustc_codegen_ssa/base.rs +++ b/src/librustc_codegen_ssa/base.rs @@ -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(); diff --git a/src/test/ui/duplicate/dupe-symbols-7.rs b/src/test/ui/duplicate/dupe-symbols-7.rs index 89a32c61620fd..633ca4c318952 100644 --- a/src/test/ui/duplicate/dupe-symbols-7.rs +++ b/src/test/ui/duplicate/dupe-symbols-7.rs @@ -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" -> "" diff --git a/src/test/ui/duplicate/dupe-symbols-7.stderr b/src/test/ui/duplicate/dupe-symbols-7.stderr index 608ae27110aa0..2ea5521e09533 100644 --- a/src/test/ui/duplicate/dupe-symbols-7.stderr +++ b/src/test/ui/duplicate/dupe-symbols-7.stderr @@ -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(){} diff --git a/src/test/ui/duplicate/dupe-symbols-8.rs b/src/test/ui/duplicate/dupe-symbols-8.rs new file mode 100644 index 0000000000000..ce7fa24a9fe6b --- /dev/null +++ b/src/test/ui/duplicate/dupe-symbols-8.rs @@ -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(); } +} diff --git a/src/test/ui/duplicate/dupe-symbols-8.stderr b/src/test/ui/duplicate/dupe-symbols-8.stderr new file mode 100644 index 0000000000000..f001201b8d0cf --- /dev/null +++ b/src/test/ui/duplicate/dupe-symbols-8.stderr @@ -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 +