From: Jayant Chauhan <0001jayant@gmail.com> Date: Thu, 15 Jan 2026 17:59:10 +0000 (+0530) Subject: gccrs: util/attributes: handle #[export_name] on static items X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=a92b620de79ad1dc28470037afe92deceedf3ccd;p=thirdparty%2Fgcc.git gccrs: util/attributes: handle #[export_name] on static items This patch enables validation for the #[export_name] attribute when used on static items. It reuses the validation logic introduced for functions to ensure that statics also receive compile-time checks for malformed inputs (e.g. non-string literals). Fixes Rust-GCC#4388 gcc/rust/ChangeLog: * util/rust-attributes.cc (AttributeChecker::visit): Add check for export_name on static items. gcc/testsuite/ChangeLog: * rust/compile/issue-4388.rs: New test. Signed-off-by: Jayant Chauhan <0001jayant@gmail.com> --- diff --git a/gcc/rust/util/rust-attributes.cc b/gcc/rust/util/rust-attributes.cc index d15d40c075f..4a0422d3fba 100644 --- a/gcc/rust/util/rust-attributes.cc +++ b/gcc/rust/util/rust-attributes.cc @@ -1016,9 +1016,16 @@ AttributeChecker::visit (AST::StaticItem &item) BuiltinAttrDefinition result; for (auto &attribute : item.get_outer_attrs ()) { - if (is_builtin (attribute, result) && result.name == Attrs::LINK_SECTION) + if (is_builtin (attribute, result)) { - check_link_section_attribute (attribute); + if (result.name == Attrs::LINK_SECTION) + { + check_link_section_attribute (attribute); + } + else if (result.name == Attrs::EXPORT_NAME) + { + check_export_name_attribute (attribute); + } } } } diff --git a/gcc/testsuite/rust/compile/issue-4388.rs b/gcc/testsuite/rust/compile/issue-4388.rs new file mode 100644 index 00000000000..f5976cc7418 --- /dev/null +++ b/gcc/testsuite/rust/compile/issue-4388.rs @@ -0,0 +1,13 @@ +#[export_name] // { dg-error "malformed" } +static A: i32 = 0; + +#[export_name(123)] // { dg-error "attribute must be a string literal" } +static B: i32 = 0; + +#[export_name = 123] // { dg-error "attribute must be a string literal" } +static C: i32 = 0; + +#[export_name = "valid_static"] +static D: i32 = 0; + +fn main() {} \ No newline at end of file