From: Pierre-Emmanuel Patry <pierre-emmanuel.pa...@embecosm.com>

gcc/rust/ChangeLog:

        * parse/rust-parse-impl.h (Parser::parse_loop_label): Change function
        return type to expected.
        (Parser::parse_labelled_loop_expr): Adapt call location to new return
        type.
        * parse/rust-parse.h (enum class): Update function prototype.

Signed-off-by: Pierre-Emmanuel Patry <pierre-emmanuel.pa...@embecosm.com>
---
 gcc/rust/parse/rust-parse-impl.h | 18 ++++++++++++------
 gcc/rust/parse/rust-parse.h      |  8 +++++++-
 2 files changed, 19 insertions(+), 7 deletions(-)

diff --git a/gcc/rust/parse/rust-parse-impl.h b/gcc/rust/parse/rust-parse-impl.h
index 40ecb2ed441..3bb758ef1d2 100644
--- a/gcc/rust/parse/rust-parse-impl.h
+++ b/gcc/rust/parse/rust-parse-impl.h
@@ -7568,14 +7568,15 @@ Parser<ManagedTokenSource>::parse_continue_expr 
(AST::AttrVec outer_attrs,
 
 // Parses a loop label used in loop expressions.
 template <typename ManagedTokenSource>
-tl::optional<AST::LoopLabel>
+tl::expected<AST::LoopLabel, ParseLoopLabelError>
 Parser<ManagedTokenSource>::parse_loop_label (const_TokenPtr tok)
 {
   // parse lifetime - if doesn't exist, assume no label
   if (tok->get_id () != LIFETIME)
     {
       // not necessarily an error
-      return tl::nullopt;
+      return tl::unexpected<ParseLoopLabelError> (
+       ParseLoopLabelError::NOT_LOOP_LABEL);
     }
   /* FIXME: check for named lifetime requirement here? or check in semantic
    * analysis phase? */
@@ -7584,10 +7585,11 @@ Parser<ManagedTokenSource>::parse_loop_label 
(const_TokenPtr tok)
   if (!skip_token (COLON))
     {
       // skip somewhere?
-      return tl::nullopt;
+      return tl::unexpected<ParseLoopLabelError> (
+       ParseLoopLabelError::MISSING_COLON);
     }
 
-  return tl::optional<AST::LoopLabel> (
+  return tl::expected<AST::LoopLabel, ParseLoopLabelError> (
     AST::LoopLabel (std::move (label), tok->get_locus ()));
 }
 
@@ -8217,8 +8219,8 @@ Parser<ManagedTokenSource>::parse_labelled_loop_expr 
(const_TokenPtr tok,
 
   // parse loop label (required)
   // TODO: Convert this return type to tl::expected instead of tl::optional
-  tl::optional<AST::LoopLabel> label = parse_loop_label (tok);
-  if (!label)
+  auto parsed_label = parse_loop_label (tok);
+  if (!parsed_label)
     {
       Error error (lexer.peek_token ()->get_locus (),
                   "failed to parse loop label in labelled loop expr");
@@ -8228,6 +8230,10 @@ Parser<ManagedTokenSource>::parse_labelled_loop_expr 
(const_TokenPtr tok,
       return nullptr;
     }
 
+  auto label = parsed_label
+                ? tl::optional<AST::LoopLabel> (parsed_label.value ())
+                : tl::nullopt;
+
   // branch on next token
   const_TokenPtr t = lexer.peek_token ();
   switch (t->get_id ())
diff --git a/gcc/rust/parse/rust-parse.h b/gcc/rust/parse/rust-parse.h
index 5373106c189..ff7987930fb 100644
--- a/gcc/rust/parse/rust-parse.h
+++ b/gcc/rust/parse/rust-parse.h
@@ -33,6 +33,11 @@ class ParseLifetimeParamError
 class ParseLifetimeError
 {
 };
+enum class ParseLoopLabelError
+{
+  NOT_LOOP_LABEL,
+  MISSING_COLON,
+};
 enum ParseSelfError
 {
   SELF_PTR,
@@ -620,7 +625,8 @@ private:
   std::unique_ptr<AST::Expr> parse_labelled_loop_expr (const_TokenPtr tok,
                                                       AST::AttrVec outer_attrs
                                                       = AST::AttrVec ());
-  tl::optional<AST::LoopLabel> parse_loop_label (const_TokenPtr tok);
+  tl::expected<AST::LoopLabel, ParseLoopLabelError>
+  parse_loop_label (const_TokenPtr tok);
   std::unique_ptr<AST::AsyncBlockExpr>
   parse_async_block_expr (AST::AttrVec outer_attrs = AST::AttrVec ());
   std::unique_ptr<AST::GroupedExpr> parse_grouped_expr (AST::AttrVec 
outer_attrs
-- 
2.49.0

Reply via email to