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

Message ID 20230405140411.3016563-15-arthur.cohen@embecosm.com
State Accepted
Headers
Series [committed,01/88] gccrs: fatal_error_flag: Fix typo in error message |

Checks

Context Check Description
snail/gcc-patch-check success Github commit url

Commit Message

Arthur Cohen April 5, 2023, 2:02 p.m. UTC
  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
  

Patch

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!();
+}