Skip to content

Commit e4ef66d

Browse files
committed
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 <pierre-emmanuel.patry@embecosm.com>
1 parent 5f72783 commit e4ef66d

2 files changed

Lines changed: 15 additions & 2 deletions

File tree

gcc/rust/ast/rust-ast.cc

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4149,11 +4149,12 @@ AttrInputMetaItemContainer::separate_cfg_attrs () const
41494149
bool
41504150
Attribute::check_cfg_predicate (const Session &session) const
41514151
{
4152+
auto string_path = path.as_string ();
41524153
/* assume that cfg predicate actually can exist, i.e. attribute has cfg or
41534154
* cfg_attr path */
41544155
if (!has_attr_input ()
4155-
|| (path.as_string () != Values::Attributes::CFG
4156-
&& path.as_string () != Values::Attributes::CFG_ATTR))
4156+
|| (string_path != Values::Attributes::CFG
4157+
&& string_path != Values::Attributes::CFG_ATTR))
41574158
{
41584159
// DEBUG message
41594160
rust_debug (
@@ -4169,6 +4170,13 @@ Attribute::check_cfg_predicate (const Session &session) const
41694170
return false;
41704171

41714172
auto &meta_item = static_cast<AttrInputMetaItemContainer &> (*attr_input);
4173+
if (meta_item.get_items ().empty ()
4174+
&& string_path == Values::Attributes::CFG_ATTR)
4175+
{
4176+
rust_error_at (path.get_locus (),
4177+
"malformed %<cfg_attr%> attribute input");
4178+
return false;
4179+
}
41724180
return meta_item.get_items ().front ()->check_cfg_predicate (session);
41734181
}
41744182

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
struct S {
2+
#[cfg_attr()]
3+
field: u8,
4+
// { dg-error "malformed .cfg_attr. attribute input" "" { target *-*-* } .-2 }
5+
}

0 commit comments

Comments
 (0)