https://gcc.gnu.org/g:bf834bda455959a36323f98edefc59bfdfe1499e

commit r16-6842-gbf834bda455959a36323f98edefc59bfdfe1499e
Author: Jayant Chauhan <[email protected]>
Date:   Tue Jan 6 23:42:58 2026 +0530

    gccrs: util/attributes: error on malformed #[no_mangle] input
    
    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]>

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

diff --git a/gcc/rust/util/rust-attributes.cc b/gcc/rust/util/rust-attributes.cc
index ec14744fff49..57fad8c2a582 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 000000000000..5e73d5778fe2
--- /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 }

Reply via email to