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>
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));
}
--- /dev/null
+// 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