From: Harishankar <[email protected]>
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 <[email protected]>
---
This change was merged into the gccrs repository and is posted here for
upstream visibility and potential drive-by review, as requested by GCC
release managers.
Each commit email contains a link to its details on github from where you can
find the Pull-Request and associated discussions.
Commit on github:
https://github.com/Rust-GCC/gccrs/commit/712b273bdc0ae4279a1798e066ddeb630742e6ef
The commit has been mentioned in the following pull-request(s):
- https://github.com/Rust-GCC/gccrs/pull/4347
gcc/rust/expand/rust-macro-expand.cc | 9 ++++++-
gcc/testsuite/rust/compile/issue-4213.rs | 34 ++++++++++++++++++++++++
2 files changed, 42 insertions(+), 1 deletion(-)
create mode 100644 gcc/testsuite/rust/compile/issue-4213.rs
diff --git a/gcc/rust/expand/rust-macro-expand.cc
b/gcc/rust/expand/rust-macro-expand.cc
index cc044395e..4d658fcd5 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 000000000..762787b21
--- /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
base-commit: c35ffdb8ea1e8e9262c6e130d43fbb053094e1da
--
2.52.0