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]>
---
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/5b9706fc190e3f3c87f851e39ad5ef6de7ec4315

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

 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 b613f95ae..d15d40c07 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 000000000..7bb8a4a4d
--- /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

base-commit: 457a44e399c359e0bdc176422eb48bbcab393ca7
-- 
2.52.0

Reply via email to