]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
gccrs: Reject empty cfg_attr
authorPierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
Wed, 13 Aug 2025 15:20:40 +0000 (17:20 +0200)
committerArthur Cohen <arthur.cohen@embecosm.com>
Thu, 30 Oct 2025 19:58:39 +0000 (20:58 +0100)
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>
gcc/rust/ast/rust-ast.cc
gcc/testsuite/rust/compile/issue-3966.rs [new file with mode: 0644]

index 7feb7a688f7df3fff7f404db381819c18b6347d6..8072ce96b4f0c24639f26618021d171d21b5046c 100644 (file)
@@ -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 (file)
index 0000000..20d3031
--- /dev/null
@@ -0,0 +1,5 @@
+struct S {
+    #[cfg_attr()]
+    field: u8,
+    // { dg-error "malformed .cfg_attr. attribute input" "" { target *-*-* } .-2 }
+}