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

Removes duplicate error in block expression parsing.
Removes cascading error in match arm parsing.
Adds proper error detection for unterminated expression.

Fixes Rust-GCC/gccrs#1210

gcc/rust/ChangeLog:

        * parse/rust-parse-impl-expr.hxx: Removes duplicate errors.
        * parse/rust-parse-impl.hxx: Detects when an expression without
        block is not properly terminated

Signed-off-by: lenny.chiadmi-delage <[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/eef385b63caf0da49981ed9e3d440b123a1673a1

The commit has been mentioned in the following pull-request(s):
 - https://github.com/Rust-GCC/gccrs/pull/4363

 gcc/rust/parse/rust-parse-impl-expr.hxx | 14 ++++----------
 gcc/rust/parse/rust-parse-impl.hxx      | 13 +++++++++++++
 2 files changed, 17 insertions(+), 10 deletions(-)

diff --git a/gcc/rust/parse/rust-parse-impl-expr.hxx 
b/gcc/rust/parse/rust-parse-impl-expr.hxx
index b870273e2..4de5599d6 100644
--- a/gcc/rust/parse/rust-parse-impl-expr.hxx
+++ b/gcc/rust/parse/rust-parse-impl-expr.hxx
@@ -77,11 +77,7 @@ Parser<ManagedTokenSource>::parse_block_expr (
 
   if (!skip_token (RIGHT_CURLY))
     {
-      Error error (t->get_locus (),
-                  "error may be from having an expression (as opposed to "
-                  "statement) in the body of the function but not last");
-      add_error (std::move (error));
-
+      // We don't need to throw an error as it already reported by skip_token
       skip_after_end_block ();
       return tl::unexpected<Parse::Error::Node> 
(Parse::Error::Node::MALFORMED);
     }
@@ -1289,11 +1285,9 @@ Parser<ManagedTokenSource>::parse_match_expr 
(AST::AttrVec outer_attrs,
 
       if (!expr)
        {
-         Error error (lexer.peek_token ()->get_locus (),
-                      "failed to parse expr in match arm in match expr");
-         add_error (std::move (error));
-
-         // skip somewhere?
+         /* We don't need to throw an error as it already reported by
+          * parse_expr
+          */
          return tl::unexpected<Parse::Error::Node> (
            Parse::Error::Node::CHILD_ERROR);
        }
diff --git a/gcc/rust/parse/rust-parse-impl.hxx 
b/gcc/rust/parse/rust-parse-impl.hxx
index 96d1753d6..9766cafa1 100644
--- a/gcc/rust/parse/rust-parse-impl.hxx
+++ b/gcc/rust/parse/rust-parse-impl.hxx
@@ -7198,6 +7198,19 @@ Parser<ManagedTokenSource>::parse_stmt_or_expr ()
        return ExprOrStmt (
          std::make_unique<AST::ExprStmt> (std::move (expr.value ()),
                                           t->get_locus (), false));
+
+      // Check if expr_without_block is properly terminated
+      if (expr.value ()->is_expr_without_block ()
+         && after_expr->get_id () != RIGHT_CURLY)
+       {
+         // expr_without_block must be followed by ';' or '}'
+         Error error (after_expr->get_locus (),
+                      "expected %<;%> or %<}%> after expression, found %qs",
+                      after_expr->get_token_description ());
+         add_error (std::move (error));
+         return tl::unexpected<Parse::Error::Node> (
+           Parse::Error::Node::MALFORMED);
+       }
     }
 
   // return expression

base-commit: a23b62111b4c6ec25d36dd2b179e6c3fc3c86aa9
-- 
2.52.0

Reply via email to