Skip to content

Commit cc163e9

Browse files
philbertydkm
authored andcommitted
gccrs: Fix ICE with duplicate root item main function
Rust seems to allow duplicate HIR::Item 'main' functions but it needs to be a root item to be the true main entry point. This means we can use the canonical path to determine if this is a root one where its CrateName::main or CrateName::Module::main. Fixes #3978 gcc/rust/ChangeLog: * backend/rust-compile-base.cc: check the canonical path gcc/testsuite/ChangeLog: * rust/compile/issue-3978.rs: New test. Signed-off-by: Philip Herron <[email protected]>
1 parent b80b8f1 commit cc163e9

File tree

2 files changed

+10
-1
lines changed

2 files changed

+10
-1
lines changed

gcc/rust/backend/rust-compile-base.cc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -697,7 +697,8 @@ HIRCompileBase::compile_function (
697697
= canonical_path.get () + fntype->subst_as_string ();
698698

699699
// we don't mangle the main fn since we haven't implemented the main shim
700-
bool is_main_fn = fn_name.compare ("main") == 0 && is_root_item;
700+
bool is_main_fn = fn_name.compare ("main") == 0 && is_root_item
701+
&& canonical_path.size () <= 2;
701702
if (is_main_fn)
702703
{
703704
rust_assert (!main_identifier_node);
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
type Dimension = usize;
2+
3+
pub fn main() {}
4+
5+
mod m2 {
6+
fn main() {}
7+
// { dg-warning "function is never used" "" { target *-*-* } .-1 }
8+
}

0 commit comments

Comments
 (0)