[COMMITTED 52/77] gccrs: add no mangle generic items lint

arthur.cohen@embecosm.com arthur.cohen@embecosm.com
Fri Aug 7 12:39:43 GMT 2026


From: Lucas Ly Ba <lucas.ly-ba@outlook.com>

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 <lucas.ly-ba@outlook.com>
---
 .../checks/lints/unused/rust-unused-checker.cc   | 16 ++++++++++++++++
 .../rust/compile/no-mangle-generic-items_0.rs    | 10 ++++++++++
 2 files changed, 26 insertions(+)
 create mode 100644 gcc/testsuite/rust/compile/no-mangle-generic-items_0.rs

diff --git a/gcc/rust/checks/lints/unused/rust-unused-checker.cc b/gcc/rust/checks/lints/unused/rust-unused-checker.cc
index 2c521634d8b..553a2f71ed8 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 00000000000..5fa05ac2bc7
--- /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 }
-- 
2.50.1



More information about the Gcc-rust mailing list