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]>
---
.../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
lmpx.com only provides a reader for public news (NNTP) servers. It is not
affiliated with the servers or forums shown here and is not responsible for
the content of articles, which is written by their respective authors.