Skip to content

Commit a796af7

Browse files
committed
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.
1 parent 0753459 commit a796af7

File tree

5 files changed

+31
-4
lines changed

5 files changed

+31
-4
lines changed

src/librustc_codegen_ssa/base.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -437,10 +437,10 @@ pub fn maybe_create_entry_wrapper<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>>(
437437
// listing.
438438
let main_ret_ty = cx.tcx().erase_regions(&main_ret_ty.no_bound_vars().unwrap());
439439

440-
if cx.get_defined_value("main").is_some() {
440+
if cx.get_declared_value("main").is_some() {
441441
// FIXME: We should be smart and show a better diagnostic here.
442442
cx.sess()
443-
.struct_span_err(sp, "entry symbol `main` defined multiple times")
443+
.struct_span_err(sp, "entry symbol `main` declared multiple times")
444444
.help("did you use `#[no_mangle]` on `fn main`? Use `#[start]` instead")
445445
.emit();
446446
cx.sess().abort_if_errors();

src/test/ui/duplicate/dupe-symbols-7.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// build-fail
22

33
//
4-
// error-pattern: entry symbol `main` defined multiple times
4+
// error-pattern: entry symbol `main` declared multiple times
55

66
// FIXME https://github.com/rust-lang/rust/issues/59774
77
// normalize-stderr-test "thread.*panicked.*Metadata module not compiled.*\n" -> ""

src/test/ui/duplicate/dupe-symbols-7.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
error: entry symbol `main` defined multiple times
1+
error: entry symbol `main` declared multiple times
22
--> $DIR/dupe-symbols-7.rs:12:1
33
|
44
LL | fn main(){}
+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// build-fail
2+
// error-pattern: entry symbol `main` declared multiple times
3+
//
4+
// See #67946.
5+
6+
#![allow(warnings)]
7+
fn main() {
8+
extern "Rust" {
9+
fn main();
10+
}
11+
unsafe { main(); }
12+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
error: entry symbol `main` declared multiple times
2+
--> $DIR/dupe-symbols-8.rs:7:1
3+
|
4+
LL | / fn main() {
5+
LL | | extern "Rust" {
6+
LL | | fn main();
7+
LL | | }
8+
LL | | unsafe { main(); }
9+
LL | | }
10+
| |_^
11+
|
12+
= help: did you use `#[no_mangle]` on `fn main`? Use `#[start]` instead
13+
14+
error: aborting due to previous error
15+

0 commit comments

Comments
 (0)