From: "lenny.chiadmi-delage" <[email protected]>

Check if parser throw an error to avoid cloning nullptr

Fixes Rust-GCC#4140

gcc/rust/ChangeLog:

        * expand/rust-macro-expand.cc (transcribe_expression): Check if
        parser didn't fail.
        (transcribe_type): Likewise.
        (transcribe_pattern): Likewise.

Signed-off-by: lenny.chiadmi-delage <[email protected]>
---
 gcc/rust/expand/rust-macro-expand.cc | 15 +++++++++------
 1 file changed, 9 insertions(+), 6 deletions(-)

diff --git a/gcc/rust/expand/rust-macro-expand.cc 
b/gcc/rust/expand/rust-macro-expand.cc
index 52f8e2b10e3..b47e43afd76 100644
--- a/gcc/rust/expand/rust-macro-expand.cc
+++ b/gcc/rust/expand/rust-macro-expand.cc
@@ -962,12 +962,10 @@ transcribe_expression (Parser<MacroInvocLexer> &parser)
 
   auto attrs = parser.parse_outer_attributes ();
   auto expr = parser.parse_expr (std::move (attrs));
-  if (expr == nullptr)
-    {
-      for (auto error : parser.get_errors ())
-       error.emit ();
-      return AST::Fragment::create_error ();
-    }
+  for (auto error : parser.get_errors ())
+    error.emit ();
+  if (!expr)
+    return AST::Fragment::create_error ();
 
   // FIXME: make this an error for some edititons
   if (parser.peek_current_token ()->get_id () == SEMICOLON)
@@ -997,6 +995,8 @@ transcribe_type (Parser<MacroInvocLexer> &parser)
   auto type = parser.parse_type (true);
   for (auto err : parser.get_errors ())
     err.emit ();
+  if (!type)
+    return AST::Fragment::create_error ();
 
   auto end = lexer.get_offs ();
 
@@ -1018,6 +1018,9 @@ transcribe_pattern (Parser<MacroInvocLexer> &parser)
   for (auto err : parser.get_errors ())
     err.emit ();
 
+  if (!pattern)
+    return AST::Fragment::create_error ();
+
   auto end = lexer.get_offs ();
 
   return AST::Fragment ({std::move (pattern)},
-- 
2.50.1

Reply via email to