]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
gccrs: util/attributes: handle #[export_name] on static items
authorJayant Chauhan <0001jayant@gmail.com>
Thu, 15 Jan 2026 17:59:10 +0000 (23:29 +0530)
committerArthur Cohen <arthur.cohen@embecosm.com>
Fri, 27 Feb 2026 14:57:06 +0000 (15:57 +0100)
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 <0001jayant@gmail.com>
gcc/rust/util/rust-attributes.cc
gcc/testsuite/rust/compile/issue-4388.rs [new file with mode: 0644]

index d15d40c075ff62497c4731f37dff8a1521974f62..4a0422d3fba1503e197fe8797c67615a5197e9ef 100644 (file)
@@ -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 (file)
index 0000000..f5976cc
--- /dev/null
@@ -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