]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
gccrs: Fix rogue macro error during lowering on expansion failure
authorHarishankar <harishankarpp7@gmail.com>
Thu, 1 Jan 2026 05:17:23 +0000 (10:47 +0530)
committerArthur Cohen <arthur.cohen@embecosm.com>
Fri, 16 Jan 2026 16:32:05 +0000 (17:32 +0100)
When a macro expansion fails (e.g. due to a parsing error like invalid
syntax in the macro body), the expander previously returned an error
fragment but did not update the AST. This left the original macro
invocation in place, which subsequently caused an ICE (rogue macro
detected) during the lowering phase.

This patch updates `expand_invoc` to replace the macro invocation with
an empty fragment if expansion fails, ensuring the compiler can proceed
(or exit gracefully) without crashing.

Fixes Rust-GCC/gccrs#4213

gcc/rust/ChangeLog:

* expand/rust-macro-expand.cc (MacroExpander::expand_invoc): Handle
error fragments by replacing them with empty fragments.

gcc/testsuite/ChangeLog:

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

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

index cc044395e3e6b0c61d64a7d28bef2fd4ef7adff2..4d658fcd55ae3d6e858d3536ea3b15da38260514 100644 (file)
@@ -327,7 +327,14 @@ MacroExpander::expand_invoc (AST::MacroInvocation &invoc,
   else
     fragment
       = expand_decl_macro (invoc.get_locus (), invoc_data, *rdef, semicolon);
-
+  // fix: if the expansion is failing, we must replace the marco with an empty
+  // error or node
+  // makes sure that it doesn't panic on Rouge macro (it -> Lowering Phase)
+  // added the parsing errors in gcc/testsuite/rust/compile/issue-4213.rs
+  if (fragment.is_error ())
+    {
+      fragment = AST::Fragment::create_empty ();
+    }
   set_expanded_fragment (std::move (fragment));
 }
 
diff --git a/gcc/testsuite/rust/compile/issue-4213.rs b/gcc/testsuite/rust/compile/issue-4213.rs
new file mode 100644 (file)
index 0000000..762787b
--- /dev/null
@@ -0,0 +1,34 @@
+// Test for issue #4213 - rogue macro detected during Lowering.
+
+macro_rules! inner {
+    () => {
+        $crate::    // { dg-error "could not parse path expression segment" }
+    };
+}
+
+macro_rules! multi_arm {
+    (a) => { $crate:: };     // { dg-error "could not parse path expression segment" }
+    (b) => { () };
+}
+
+macro_rules! another_macro {
+    () => { $crate:: }      // { dg-error "could not parse path expression segment" }
+}
+macro_rules! generic_macro {
+    ($t:ty) => {
+        $crate::            // { dg-error "could not parse path expression segment" }
+    };
+}
+
+macro_rules! empty_case {
+    () => {};
+}
+
+pub fn main() {
+    let _ = inner!();
+    let _ = multi_arm!(a);
+    let _ = multi_arm!(b);
+    let _ = another_macro!();
+    let _ = generic_macro!(i32);
+    empty_case!();
+}
\ No newline at end of file