https://gcc.gnu.org/g:173ba18c5742f815fbb821eb2e4d6747afacbc3a
commit r16-4787-g173ba18c5742f815fbb821eb2e4d6747afacbc3a Author: Pierre-Emmanuel Patry <[email protected]> Date: Wed Aug 13 17:20:40 2025 +0200 gccrs: Reject empty cfg_attr gcc/rust/ChangeLog: * ast/rust-ast.cc (Attribute::check_cfg_predicate): Emit an error with empty cfg_attr input. gcc/testsuite/ChangeLog: * rust/compile/issue-3966.rs: New test. Signed-off-by: Pierre-Emmanuel Patry <[email protected]> Diff: --- gcc/rust/ast/rust-ast.cc | 12 ++++++++++-- gcc/testsuite/rust/compile/issue-3966.rs | 5 +++++ 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/gcc/rust/ast/rust-ast.cc b/gcc/rust/ast/rust-ast.cc index 7feb7a688f7d..8072ce96b4f0 100644 --- a/gcc/rust/ast/rust-ast.cc +++ b/gcc/rust/ast/rust-ast.cc @@ -4165,11 +4165,12 @@ AttrInputMetaItemContainer::separate_cfg_attrs () const bool Attribute::check_cfg_predicate (const Session &session) const { + auto string_path = path.as_string (); /* assume that cfg predicate actually can exist, i.e. attribute has cfg or * cfg_attr path */ if (!has_attr_input () - || (path.as_string () != Values::Attributes::CFG - && path.as_string () != Values::Attributes::CFG_ATTR)) + || (string_path != Values::Attributes::CFG + && string_path != Values::Attributes::CFG_ATTR)) { // DEBUG message rust_debug ( @@ -4185,6 +4186,13 @@ Attribute::check_cfg_predicate (const Session &session) const return false; auto &meta_item = static_cast<AttrInputMetaItemContainer &> (*attr_input); + if (meta_item.get_items ().empty () + && string_path == Values::Attributes::CFG_ATTR) + { + rust_error_at (path.get_locus (), + "malformed %<cfg_attr%> attribute input"); + return false; + } return meta_item.get_items ().front ()->check_cfg_predicate (session); } diff --git a/gcc/testsuite/rust/compile/issue-3966.rs b/gcc/testsuite/rust/compile/issue-3966.rs new file mode 100644 index 000000000000..20d3031efa4f --- /dev/null +++ b/gcc/testsuite/rust/compile/issue-3966.rs @@ -0,0 +1,5 @@ +struct S { + #[cfg_attr()] + field: u8, + // { dg-error "malformed .cfg_attr. attribute input" "" { target *-*-* } .-2 } +}
