From: Lucas Ly Ba <[email protected]>

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]>
---
This change was merged into the gccrs repository and is posted here for
upstream visibility and potential drive-by review, as requested by GCC
release managers.
Each commit email contains a link to its details on github from where you can
find the Pull-Request and associated discussions.


Commit on github: 
https://github.com/Rust-GCC/gccrs/commit/ab11f462825159529a085117f88c2214fc30e191

The commit has NOT been mentioned in any issue.

The commit has been mentioned in the following pull-request(s):
 - https://github.com/Rust-GCC/gccrs/pull/4645

 .../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 2c521634d..553a2f71e 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 000000000..5fa05ac2b
--- /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 }

base-commit: 414c389f8094a888a30966485489e3680149cbb7
-- 
2.54.0

Reply via email to