https://gcc.gnu.org/g:610b30ce3567b5bccd9f3a4263d101c21df58f40
commit r16-7766-g610b30ce3567b5bccd9f3a4263d101c21df58f40 Author: Jayant Chauhan <[email protected]> Date: Sat Jan 10 05:17:33 2026 +0530 gccrs: util/attributes: error on malformed #[link_section] input 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]> Diff: --- gcc/rust/util/rust-attributes.cc | 26 ++++++++++++++++++++++ .../rust/compile/link_section-malformed.rs | 5 +++++ 2 files changed, 31 insertions(+) diff --git a/gcc/rust/util/rust-attributes.cc b/gcc/rust/util/rust-attributes.cc index 57fad8c2a582..0e5234687602 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 000000000000..2841dc066020 --- /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 }
