From: Jayant Chauhan <[email protected]>

Emit a diagnostic when #[link_name] is used without arguments,
matching rustc behavior. This prevents silent acceptance of empty
attributes and provides a helpful diagnostic that shows the expected form.

Fixes Rust-GCC#4228

gcc/rust/ChangeLog:

        * util/rust-attributes.cc: Emit diagnostic.

gcc/testsuite/ChangeLog:

        * rust/compile/link_name-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/c4d2abe33d8ef1a74ae44011f322a1721faaadb9

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

 gcc/rust/util/rust-attributes.cc                  | 11 +++++++++++
 gcc/testsuite/rust/compile/link_name-malformed.rs |  5 +++++
 2 files changed, 16 insertions(+)
 create mode 100644 gcc/testsuite/rust/compile/link_name-malformed.rs

diff --git a/gcc/rust/util/rust-attributes.cc b/gcc/rust/util/rust-attributes.cc
index 0ecdb92c7..c001ab52d 100644
--- a/gcc/rust/util/rust-attributes.cc
+++ b/gcc/rust/util/rust-attributes.cc
@@ -866,6 +866,17 @@ AttributeChecker::visit (AST::Function &fun)
        }
       else if (result.name == "no_mangle")
        check_no_mangle_function (attribute, fun);
+
+      else if (result.name == Attrs::LINK_NAME)
+       {
+         if (!attribute.has_attr_input ())
+           {
+             rust_error_at (attribute.get_locus (),
+                            "malformed %<link_name%> attribute input");
+             rust_inform (attribute.get_locus (),
+                          "must be of the form: %<#[link_name = \"name\"]%>");
+           }
+       }
     }
   if (fun.has_body ())
     fun.get_definition ().value ()->accept_vis (*this);
diff --git a/gcc/testsuite/rust/compile/link_name-malformed.rs 
b/gcc/testsuite/rust/compile/link_name-malformed.rs
new file mode 100644
index 000000000..e09ec8b4b
--- /dev/null
+++ b/gcc/testsuite/rust/compile/link_name-malformed.rs
@@ -0,0 +1,5 @@
+// { dg-options "-w" }
+#[link_name] // { dg-error "malformed .link_name. attribute input" }
+fn foo() {}
+
+// { dg-note "must be of the form" "" { target *-*-* } .-3 }

base-commit: 5103d5d21671f50947d77c09f3a942237d6f7cbd
-- 
2.52.0

Reply via email to