From: Jayant Chauhan <[email protected]>

This patch enables validation for the #[export_name] attribute when used
on static items. It reuses the validation logic introduced for functions
to ensure that statics also receive compile-time checks for malformed
inputs (e.g. non-string literals).

Fixes Rust-GCC#4388

gcc/rust/ChangeLog:

        * util/rust-attributes.cc (AttributeChecker::visit): Add check for
        export_name on static items.

gcc/testsuite/ChangeLog:

        * rust/compile/issue-4388.rs: New test.

Signed-off-by: Jayant Chauhan <[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/23de527f9c7b608b9e242328c0b7fbcf4667b545

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

 gcc/rust/util/rust-attributes.cc         | 11 +++++++++--
 gcc/testsuite/rust/compile/issue-4388.rs | 13 +++++++++++++
 2 files changed, 22 insertions(+), 2 deletions(-)
 create mode 100644 gcc/testsuite/rust/compile/issue-4388.rs

diff --git a/gcc/rust/util/rust-attributes.cc b/gcc/rust/util/rust-attributes.cc
index d15d40c07..4a0422d3f 100644
--- a/gcc/rust/util/rust-attributes.cc
+++ b/gcc/rust/util/rust-attributes.cc
@@ -1016,9 +1016,16 @@ AttributeChecker::visit (AST::StaticItem &item)
   BuiltinAttrDefinition result;
   for (auto &attribute : item.get_outer_attrs ())
     {
-      if (is_builtin (attribute, result) && result.name == Attrs::LINK_SECTION)
+      if (is_builtin (attribute, result))
        {
-         check_link_section_attribute (attribute);
+         if (result.name == Attrs::LINK_SECTION)
+           {
+             check_link_section_attribute (attribute);
+           }
+         else if (result.name == Attrs::EXPORT_NAME)
+           {
+             check_export_name_attribute (attribute);
+           }
        }
     }
 }
diff --git a/gcc/testsuite/rust/compile/issue-4388.rs 
b/gcc/testsuite/rust/compile/issue-4388.rs
new file mode 100644
index 000000000..f5976cc74
--- /dev/null
+++ b/gcc/testsuite/rust/compile/issue-4388.rs
@@ -0,0 +1,13 @@
+#[export_name] // { dg-error "malformed" }
+static A: i32 = 0;
+
+#[export_name(123)] // { dg-error "attribute must be a string literal" }
+static B: i32 = 0;
+
+#[export_name = 123] // { dg-error "attribute must be a string literal" }
+static C: i32 = 0;
+
+#[export_name = "valid_static"]
+static D: i32 = 0;
+
+fn main() {}
\ No newline at end of file

base-commit: 5b9706fc190e3f3c87f851e39ad5ef6de7ec4315
-- 
2.52.0

Reply via email to