[gcc r16-6827] gccrs: Fix rogue macro error during lowering on expansion failure
Arthur Cohen
cohenarthur@gcc.gnu.org
Fri Jan 16 16:29:16 GMT 2026
https://gcc.gnu.org/g:1cfa7d0771790c1b02f78b855f22e203cd38055a
commit r16-6827-g1cfa7d0771790c1b02f78b855f22e203cd38055a
Author: Harishankar <harishankarpp7@gmail.com>
Date: Thu Jan 1 10:47:23 2026 +0530
gccrs: Fix rogue macro error during lowering on expansion failure
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>
Diff:
---
gcc/rust/expand/rust-macro-expand.cc | 9 ++++++++-
gcc/testsuite/rust/compile/issue-4213.rs | 34 ++++++++++++++++++++++++++++++++
2 files changed, 42 insertions(+), 1 deletion(-)
diff --git a/gcc/rust/expand/rust-macro-expand.cc b/gcc/rust/expand/rust-macro-expand.cc
index cc044395e3e6..4d658fcd55ae 100644
--- a/gcc/rust/expand/rust-macro-expand.cc
+++ b/gcc/rust/expand/rust-macro-expand.cc
@@ -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
index 000000000000..762787b2193a
--- /dev/null
+++ b/gcc/testsuite/rust/compile/issue-4213.rs
@@ -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
More information about the Gcc-cvs
mailing list