From: Jayant Chauhan <[email protected]>

Emit a diagnostic when #[export_name] is used without arguments or
with invalid arguments (non-string literals). This prevents silent
failures or backend crashes when lowering the attribute to GIMPLE,
ensuring the attribute follows the expected form: #[export_name = name].

Fixes Rust-GCC#4387

gcc/rust/ChangeLog:

        * util/rust-attributes.cc (check_export_name_attribute): New helper.
        (AttributeChecker::visit): Check export_name on functions.

gcc/testsuite/ChangeLog:

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

Signed-off-by: Jayant Chauhan <[email protected]>
---
 gcc/rust/util/rust-attributes.cc         | 23 +++++++++++++++++++++++
 gcc/testsuite/rust/compile/issue-4387.rs | 11 +++++++++++
 2 files changed, 34 insertions(+)
 create mode 100644 gcc/testsuite/rust/compile/issue-4387.rs

diff --git a/gcc/rust/util/rust-attributes.cc b/gcc/rust/util/rust-attributes.cc
index b613f95aea0..d15d40c075f 100644
--- a/gcc/rust/util/rust-attributes.cc
+++ b/gcc/rust/util/rust-attributes.cc
@@ -446,6 +446,25 @@ check_link_section_attribute (const AST::Attribute 
&attribute)
     }
 }
 
+static void
+check_export_name_attribute (const AST::Attribute &attribute)
+{
+  if (!attribute.has_attr_input ())
+    {
+      rust_error_at (attribute.get_locus (),
+                    "malformed %<export_name%> attribute input");
+      rust_inform (attribute.get_locus (),
+                  "must be of the form: %<#[export_name = \"name\"]%>");
+      return;
+    }
+
+  if (!Attributes::extract_string_literal (attribute))
+    {
+      rust_error_at (attribute.get_locus (),
+                    "attribute must be a string literal");
+    }
+}
+
 void
 AttributeChecker::check_attribute (const AST::Attribute &attribute)
 {
@@ -906,6 +925,10 @@ AttributeChecker::visit (AST::Function &fun)
          else
            check_no_mangle_function (attribute, fun);
        }
+      else if (result.name == Attrs::EXPORT_NAME)
+       {
+         check_export_name_attribute (attribute);
+       }
       else if (result.name == Attrs::LINK_NAME)
        {
          if (!attribute.has_attr_input ())
diff --git a/gcc/testsuite/rust/compile/issue-4387.rs 
b/gcc/testsuite/rust/compile/issue-4387.rs
new file mode 100644
index 00000000000..7bb8a4a4dec
--- /dev/null
+++ b/gcc/testsuite/rust/compile/issue-4387.rs
@@ -0,0 +1,11 @@
+#[export_name] // { dg-error "malformed" }
+fn foo() {}
+
+#[export_name(123)] // { dg-error "attribute must be a string literal" }
+fn bar() {}
+
+#[export_name = 123] // { dg-error "attribute must be a string literal" }
+fn baz() {}
+
+#[export_name = "valid"]
+fn qux() {}
\ No newline at end of file
-- 
2.50.1

Reply via email to