From: Jayant Chauhan <[email protected]>

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

Fixes Rust-GCC#4229

gcc/rust/ChangeLog:

        * util/rust-attributes.cc (check_link_section_attribute): New helper.
        (AttributeChecker::visit): Check link_section on functions and statics.

gcc/testsuite/ChangeLog:

        * rust/compile/link_section-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/edad96f60e57b6a2938b6c5f41e17b4da3590cd8

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

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

diff --git a/gcc/rust/util/rust-attributes.cc b/gcc/rust/util/rust-attributes.cc
index 57fad8c2a..0e5234687 100644
--- a/gcc/rust/util/rust-attributes.cc
+++ b/gcc/rust/util/rust-attributes.cc
@@ -433,6 +433,18 @@ AttributeChecker::check_inner_attributes (const 
AST::AttrVec &attributes)
     check_inner_attribute (attr);
 }
 
+static void
+check_link_section_attribute (const AST::Attribute &attribute)
+{
+  if (!attribute.has_attr_input ())
+    {
+      rust_error_at (attribute.get_locus (),
+                    "malformed %<link_section%> attribute input");
+      rust_inform (attribute.get_locus (),
+                  "must be of the form: %<#[link_section = \"name\"]%>");
+    }
+}
+
 void
 AttributeChecker::check_attribute (const AST::Attribute &attribute)
 {
@@ -896,7 +908,12 @@ AttributeChecker::visit (AST::Function &fun)
                           "must be of the form: %<#[link_name = \"name\"]%>");
            }
        }
+      else if (result.name == Attrs::LINK_SECTION)
+       {
+         check_link_section_attribute (attribute);
+       }
     }
+
   if (fun.has_body ())
     fun.get_definition ().value ()->accept_vis (*this);
 }
@@ -958,6 +975,15 @@ void
 AttributeChecker::visit (AST::StaticItem &item)
 {
   check_proc_macro_non_function (item.get_outer_attrs ());
+
+  BuiltinAttrDefinition result;
+  for (auto &attribute : item.get_outer_attrs ())
+    {
+      if (is_builtin (attribute, result) && result.name == Attrs::LINK_SECTION)
+       {
+         check_link_section_attribute (attribute);
+       }
+    }
 }
 
 void
diff --git a/gcc/testsuite/rust/compile/link_section-malformed.rs 
b/gcc/testsuite/rust/compile/link_section-malformed.rs
new file mode 100644
index 000000000..2841dc066
--- /dev/null
+++ b/gcc/testsuite/rust/compile/link_section-malformed.rs
@@ -0,0 +1,5 @@
+// { dg-options "-w" }
+#[link_section] // { dg-error "malformed .link_section. attribute input" }
+pub static VAR1: u32 = 1;
+
+// { dg-note "must be of the form" "" { target *-*-* } .-3 }

base-commit: a1ee38afbd0cf504e96ea2e8358963dbe0284b00
-- 
2.52.0

Reply via email to