https://gcc.gnu.org/g:4980b14064649b7d44f5ea12eb34790f0c8d06d2

commit r17-3111-g4980b14064649b7d44f5ea12eb34790f0c8d06d2
Author: Lucas Ly Ba <[email protected]>
Date:   Sat Jun 27 23:46:24 2026 +0200

    gccrs: add no mangle generic items lint
    
    Warn when `#[no_mangle]` or `#[export_name]` is applied to a generic
    function, since generic items must be mangled and the attribute has no
    effect.
    
    gcc/rust/ChangeLog:
    
            * checks/lints/unused/rust-unused-checker.cc (UnusedChecker::visit):
            Warn on a no-mangle generic function.
    
    gcc/testsuite/ChangeLog:
    
            * rust/compile/no-mangle-generic-items_0.rs: New test.
    
    Signed-off-by: Lucas Ly Ba <[email protected]>

Diff:
---
 gcc/rust/checks/lints/unused/rust-unused-checker.cc     | 16 ++++++++++++++++
 gcc/testsuite/rust/compile/no-mangle-generic-items_0.rs | 10 ++++++++++
 2 files changed, 26 insertions(+)

diff --git a/gcc/rust/checks/lints/unused/rust-unused-checker.cc 
b/gcc/rust/checks/lints/unused/rust-unused-checker.cc
index 2c521634d8b7..553a2f71ed80 100644
--- a/gcc/rust/checks/lints/unused/rust-unused-checker.cc
+++ b/gcc/rust/checks/lints/unused/rust-unused-checker.cc
@@ -152,6 +152,22 @@ UnusedChecker::visit (HIR::Function &fct)
     rust_warning_at (fct.get_locus (), OPT_Wunused_variable,
                     "function %qs should have a snake case name",
                     fct.get_function_name ().as_string ().c_str ());
+
+  // The no_mangle_generic_items lint: a generic function cannot be exported
+  // with a fixed symbol, so `#[no_mangle]`/`#[export_name]` has no effect.
+  if (fct.has_generics ())
+    for (auto &attr : fct.get_outer_attrs ())
+      {
+       auto name = attr.get_path ().as_string ();
+       if (name == "no_mangle" || name == "export_name")
+         {
+           rust_warning_at (fct.get_locus (), OPT_Wattributes,
+                            "generic functions must be mangled, %qs has no "
+                            "effect",
+                            name.c_str ());
+           break;
+         }
+      }
   walk (fct);
 }
 
diff --git a/gcc/testsuite/rust/compile/no-mangle-generic-items_0.rs 
b/gcc/testsuite/rust/compile/no-mangle-generic-items_0.rs
new file mode 100644
index 000000000000..5fa05ac2bc76
--- /dev/null
+++ b/gcc/testsuite/rust/compile/no-mangle-generic-items_0.rs
@@ -0,0 +1,10 @@
+// { dg-additional-options "-frust-unused-check-2.0" }
+#![feature(no_core, lang_items)]
+#![no_core]
+
+#[lang = "sized"]
+pub trait Sized {}
+
+#[no_mangle]
+pub fn f<T>(_x: T) {}
+// { dg-warning "generic functions must be mangled" "" { target *-*-* } .-1 }

Reply via email to