From: Jayant Chauhan <[email protected]>

Emit a diagnostic when #[no_mangle] is used with arguments,
matching rustc behavior. The no_mangle attribute is a word
attribute and should not accept any input values.

Fixes Rust-GCC#4230

gcc/rust/ChangeLog:

        * util/rust-attributes.cc (AttributeChecker::visit): Emit diagnostic.

gcc/testsuite/ChangeLog:

        * rust/compile/no_mangle-malformed.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/53e34519b36613d9e59bd2d8ffa0a768b638ab36

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

 gcc/rust/util/rust-attributes.cc                  | 14 +++++++++++---
 gcc/testsuite/rust/compile/no_mangle-malformed.rs |  4 ++++
 2 files changed, 15 insertions(+), 3 deletions(-)
 create mode 100644 gcc/testsuite/rust/compile/no_mangle-malformed.rs

diff --git a/gcc/rust/util/rust-attributes.cc b/gcc/rust/util/rust-attributes.cc
index ec14744ff..57fad8c2a 100644
--- a/gcc/rust/util/rust-attributes.cc
+++ b/gcc/rust/util/rust-attributes.cc
@@ -464,7 +464,6 @@ AttributeChecker::check_attribute (const AST::Attribute 
&attribute)
   else if (result.name == Attrs::DEPRECATED)
     check_deprecated_attribute (attribute);
 }
-
 void
 AttributeChecker::check_attributes (const AST::AttrVec &attributes)
 {
@@ -876,8 +875,17 @@ AttributeChecker::visit (AST::Function &fun)
            }
        }
       else if (result.name == "no_mangle")
-       check_no_mangle_function (attribute, fun);
-
+       {
+         if (attribute.has_attr_input ())
+           {
+             rust_error_at (attribute.get_locus (),
+                            "malformed %<no_mangle%> attribute input");
+             rust_inform (attribute.get_locus (),
+                          "must be of the form: %<#[no_mangle]%>");
+           }
+         else
+           check_no_mangle_function (attribute, fun);
+       }
       else if (result.name == Attrs::LINK_NAME)
        {
          if (!attribute.has_attr_input ())
diff --git a/gcc/testsuite/rust/compile/no_mangle-malformed.rs 
b/gcc/testsuite/rust/compile/no_mangle-malformed.rs
new file mode 100644
index 000000000..5e73d5778
--- /dev/null
+++ b/gcc/testsuite/rust/compile/no_mangle-malformed.rs
@@ -0,0 +1,4 @@
+// { dg-options "-w" }
+#[no_mangle(foo)] // { dg-error "malformed .no_mangle. attribute input" }
+fn foo() {}
+// { dg-note "must be of the form" "" { target *-*-* } .-2 }

base-commit: d63a4c39dc925e88fa66a5283cfa0212c933131d
-- 
2.52.0

Reply via email to