]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
gccrs: Fix ICE in get_function_expr when cfg'd return type inside macro
authorHarishankar <harishankarpp7@gmail.com>
Fri, 17 Apr 2026 09:38:48 +0000 (15:08 +0530)
committerArthur Cohen <arthur.cohen@embecosm.com>
Mon, 1 Jun 2026 13:24:50 +0000 (15:24 +0200)
the problem is cfg-strip emits an error for unstrippable expressions but
doesn't mark the parent for strip, leaving a broken subtree for later
passes to ICE on.

gcc/rust/ChangeLog:

* expand/rust-cfg-strip.cc (CfgStrip::visit): mark CallExpr for
strip when function expression fails stripping.
(CfgStrip::visit): mark ArrayIndexExpr for strip when array or
index expression fails stripping.

gcc/testsuite/ChangeLog:

* rust/compile/issue-4167.rs: New test.

Signed-off-by: Harishankar <harishankarpp7@gmail.com>
gcc/rust/expand/rust-cfg-strip.cc
gcc/testsuite/rust/compile/issue-4167.rs [new file with mode: 0644]

index 2e6971e63f32e667d7b34817e9399fb414394170..67baebec158d48f10681689ffc8f3f3ad89941d0 100644 (file)
@@ -828,9 +828,13 @@ CfgStrip::visit (AST::ArrayIndexExpr &expr)
 
   const auto &array_expr = expr.get_array_expr ();
   if (array_expr.is_marked_for_strip ())
-    rust_error_at (array_expr.get_locus (),
-                  "cannot strip expression in this position - outer "
-                  "attributes not allowed");
+    {
+      rust_error_at (array_expr.get_locus (),
+                    "cannot strip expression in this position - outer "
+                    "attributes not allowed");
+      expr.mark_for_strip ();
+      return;
+    }
 
   const auto &index_expr = expr.get_index_expr ();
   if (index_expr.is_marked_for_strip ())
@@ -1044,9 +1048,13 @@ CfgStrip::visit (AST::CallExpr &expr)
 
   auto &function = expr.get_function_expr ();
   if (function.is_marked_for_strip ())
-    rust_error_at (function.get_locus (),
-                  "cannot strip expression in this position - outer "
-                  "attributes not allowed");
+    {
+      rust_error_at (function.get_locus (),
+                    "cannot strip expression in this position - outer "
+                    "attributes not allowed");
+      expr.mark_for_strip ();
+      return;
+    }
 
   /* spec says outer attributes are specifically allowed for elements
    * of call expressions, so full stripping possible */
diff --git a/gcc/testsuite/rust/compile/issue-4167.rs b/gcc/testsuite/rust/compile/issue-4167.rs
new file mode 100644 (file)
index 0000000..47f4e88
--- /dev/null
@@ -0,0 +1,15 @@
+#![feature(no_core)]
+#![no_core]
+macro_rules! the_macro {
+    ( $foo:stmt ; $bar:stmt ; ) => {
+        #[cfg(foo)]
+        $foo
+
+        $foo[cfg(bar)]
+        $bar
+    };
+}
+
+fn the_function() {
+    the_macro!( (); (); ); // { dg-error "cannot strip expression in this position" }
+}