]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
gccrs: Fix ICE with duplicate root item main function
authorPhilip Herron <herron.philip@googlemail.com>
Fri, 18 Jul 2025 15:22:44 +0000 (16:22 +0100)
committerArthur Cohen <arthur.cohen@embecosm.com>
Tue, 5 Aug 2025 14:36:56 +0000 (16:36 +0200)
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 Rust-GCC#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 <herron.philip@googlemail.com>
gcc/rust/backend/rust-compile-base.cc
gcc/testsuite/rust/compile/issue-3978.rs [new file with mode: 0644]

index b2913ad3c3815f6c76ff647f3406166a8c89bab5..8994520f16d5bdd9277b88223d267996f70bcac6 100644 (file)
@@ -697,7 +697,8 @@ HIRCompileBase::compile_function (
     = canonical_path.get () + fntype->subst_as_string ();
 
   // we don't mangle the main fn since we haven't implemented the main shim
-  bool is_main_fn = fn_name.compare ("main") == 0 && is_root_item;
+  bool is_main_fn = fn_name.compare ("main") == 0 && is_root_item
+                   && canonical_path.size () <= 2;
   if (is_main_fn)
     {
       rust_assert (!main_identifier_node);
diff --git a/gcc/testsuite/rust/compile/issue-3978.rs b/gcc/testsuite/rust/compile/issue-3978.rs
new file mode 100644 (file)
index 0000000..4f17d3d
--- /dev/null
@@ -0,0 +1,8 @@
+type Dimension = usize;
+
+pub fn main() {}
+
+mod m2 {
+    fn main() {}
+    // { dg-warning "function is never used" "" { target *-*-* } .-1 }
+}