[committed 14/88] gccrs: Do not crash on empty macros expand. Fixes #1712

arthur.cohen@embecosm.com arthur.cohen@embecosm.com
Wed Apr 5 14:02:58 GMT 2023


From: Lyra <teromene@teromene.fr>

This commit fixes a compiler crash when expanding an empty macro into an existing AST.

gcc/rust/ChangeLog:

	* expand/rust-macro-expand.cc (transcribe_expression): Fix ICE when expanding
	empty macros.

gcc/testsuite/ChangeLog:

	* rust/compile/macro45.rs: New test.

Signed-off-by: Lyra Karenai <teromene@teromene.fr>
---
 gcc/rust/expand/rust-macro-expand.cc  | 2 ++
 gcc/testsuite/rust/compile/macro45.rs | 7 +++++++
 2 files changed, 9 insertions(+)
 create mode 100644 gcc/testsuite/rust/compile/macro45.rs

diff --git a/gcc/rust/expand/rust-macro-expand.cc b/gcc/rust/expand/rust-macro-expand.cc
index 9c3523e0515..bf914ee19e3 100644
--- a/gcc/rust/expand/rust-macro-expand.cc
+++ b/gcc/rust/expand/rust-macro-expand.cc
@@ -839,6 +839,8 @@ static AST::Fragment
 transcribe_expression (Parser<MacroInvocLexer> &parser)
 {
   auto expr = parser.parse_expr ();
+  if (expr == nullptr)
+    return AST::Fragment::create_error ();
 
   return AST::Fragment::complete ({std::move (expr)});
 }
diff --git a/gcc/testsuite/rust/compile/macro45.rs b/gcc/testsuite/rust/compile/macro45.rs
new file mode 100644
index 00000000000..52dbcbb0016
--- /dev/null
+++ b/gcc/testsuite/rust/compile/macro45.rs
@@ -0,0 +1,7 @@
+macro_rules! empty {
+    () => {}; // { dg-error "found unexpected token '\}' in null denotation" }
+}
+
+fn main() {
+    let a = empty!();
+}
-- 
2.40.0



More information about the Gcc-rust mailing list