From: Pierre-Emmanuel Patry <[email protected]>

A parser error header with Parse::Error namespace has recently been
introduced. Move some old parser error classes to this namespace.

gcc/rust/ChangeLog:

        * parse/rust-parse.h (class ParseLifetimeParamError): Move error from
        here ...
        (class ParseLifetimeError): Likewise.
        (enum class): Likewise.
        * parse/rust-parse-error.h (class LifetimeParam): ... to here.
        here.
        (class Lifetime): Likewise.
        (enum class): Likewise.
        (struct LoopLabel): Likewise and make it a full struct with ctors.
        (struct Self): Likewise.
        * parse/rust-parse-impl-expr.hxx: Make error point to new namespace.
        * parse/rust-parse-impl.hxx: Likewise.

Signed-off-by: Pierre-Emmanuel Patry <[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/d38dd3142a18e0b009851a6d62fab019e3c61b37

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

 gcc/rust/parse/rust-parse-error.h       | 66 +++++++++++++++++++++++++
 gcc/rust/parse/rust-parse-impl-expr.hxx | 49 +++++++++---------
 gcc/rust/parse/rust-parse-impl.hxx      | 38 +++++++-------
 gcc/rust/parse/rust-parse.h             | 37 +++-----------
 4 files changed, 115 insertions(+), 75 deletions(-)

diff --git a/gcc/rust/parse/rust-parse-error.h 
b/gcc/rust/parse/rust-parse-error.h
index c480ad761..06497a770 100644
--- a/gcc/rust/parse/rust-parse-error.h
+++ b/gcc/rust/parse/rust-parse-error.h
@@ -319,6 +319,72 @@ private:
   Visibility (Kind kind) : kind (kind) {}
 };
 
+class LifetimeParam
+{
+};
+
+class Lifetime
+{
+};
+
+enum class AnonConst
+{
+  InvalidSizeExpr,
+};
+
+struct LoopLabel
+{
+  static tl::expected<AST::LoopLabel, LoopLabel> make_not_loop_label ()
+  {
+    return tl::unexpected<LoopLabel> (LoopLabel (Kind::NOT_LOOP_LABEL));
+  }
+
+  static tl::expected<AST::LoopLabel, LoopLabel> make_missing_colon ()
+  {
+    return tl::unexpected<LoopLabel> (LoopLabel (Kind::MISSING_COLON));
+  }
+
+  enum class Kind
+  {
+    // Not an hard error
+    NOT_LOOP_LABEL,
+    // Hard error
+    MISSING_COLON,
+  } kind;
+
+private:
+  LoopLabel (Kind kind) : kind (kind) {}
+};
+
+struct Self
+{
+  static tl::expected<std::unique_ptr<AST::Param>, Self>
+  make_self_raw_pointer ()
+  {
+    return tl::unexpected<Self> (Self (Kind::SELF_RAW_PTR));
+  }
+
+  static tl::expected<std::unique_ptr<AST::Param>, Self> make_not_self ()
+  {
+    return tl::unexpected<Self> (Self (Kind::NOT_SELF));
+  }
+
+  static tl::expected<std::unique_ptr<AST::Param>, Self> make_parsing_error ()
+  {
+    return tl::unexpected<Self> (Self (Kind::PARSING));
+  }
+
+  enum class Kind
+  {
+    SELF_RAW_PTR,
+    PARSING,
+    NOT_SELF,
+  } kind;
+
+private:
+  Self (Kind kind) : kind (kind) {}
+};
+
 } // namespace Error
 } // namespace Parse
 } // namespace Rust
diff --git a/gcc/rust/parse/rust-parse-impl-expr.hxx 
b/gcc/rust/parse/rust-parse-impl-expr.hxx
index 43124b725..958b303ab 100644
--- a/gcc/rust/parse/rust-parse-impl-expr.hxx
+++ b/gcc/rust/parse/rust-parse-impl-expr.hxx
@@ -98,7 +98,7 @@ Parser<ManagedTokenSource>::parse_block_expr (
 /* Parse an anonymous const expression. This can be a regular const expression
  * or an underscore for deferred const inference */
 template <typename ManagedTokenSource>
-tl::expected<AST::AnonConst, AnonConstError>
+tl::expected<AST::AnonConst, Parse::Error::AnonConst>
 Parser<ManagedTokenSource>::parse_anon_const ()
 {
   auto current = lexer.peek_token ();
@@ -111,7 +111,7 @@ Parser<ManagedTokenSource>::parse_anon_const ()
   auto expr = parse_expr ();
 
   if (!expr)
-    return tl::make_unexpected (AnonConstError::InvalidSizeExpr);
+    return tl::make_unexpected (Parse::Error::AnonConst::InvalidSizeExpr);
 
   return AST::AnonConst (std::move (expr), locus);
 }
@@ -1106,33 +1106,34 @@ std::unique_ptr<AST::Expr>
 Parser<ManagedTokenSource>::parse_labelled_loop_expr (const_TokenPtr tok,
                                                      AST::AttrVec outer_attrs)
 {
-  /* TODO: decide whether it should not work if there is no label, or parse it
-   * with no label at the moment, I will make it not work with no label
-   * because that's the implication. */
-
-  if (tok->get_id () != LIFETIME)
-    {
-      Error error (tok->get_locus (),
-                  "expected lifetime in labelled loop expr (to parse loop "
-                  "label) - found %qs",
-                  tok->get_token_description ());
-      add_error (std::move (error));
-
-      // skip?
-      return nullptr;
-    }
-
   // parse loop label (required)
-  // TODO: Convert this return type to tl::expected instead of tl::optional
   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");
-      add_error (std::move (error));
+      /* TODO: decide whether it should not work if there is no label, or parse
+       * it with no label at the moment, I will make it not work with no label
+       * because that's the implication. */
 
-      // skip?
-      return nullptr;
+      if (parsed_label.error ().kind
+         == Parse::Error::LoopLabel::Kind::NOT_LOOP_LABEL)
+       {
+         Error error (tok->get_locus (),
+                      "expected lifetime in labelled loop expr (to parse loop "
+                      "label) - found %qs",
+                      tok->get_token_description ());
+         add_error (std::move (error));
+         return nullptr;
+       }
+
+      else
+       {
+         Error error (lexer.peek_token ()->get_locus (),
+                      "failed to parse loop label in labelled loop expr");
+         add_error (std::move (error));
+
+         // skip?
+         return nullptr;
+       }
     }
 
   auto label = parsed_label
diff --git a/gcc/rust/parse/rust-parse-impl.hxx 
b/gcc/rust/parse/rust-parse-impl.hxx
index 01091c9f3..9f307717b 100644
--- a/gcc/rust/parse/rust-parse-impl.hxx
+++ b/gcc/rust/parse/rust-parse-impl.hxx
@@ -1588,7 +1588,7 @@ Parser<ManagedTokenSource>::parse_function 
(AST::Visibility vis,
   auto initial_param = parse_self_param ();
 
   if (!initial_param.has_value ()
-      && initial_param.error () != ParseSelfError::NOT_SELF)
+      && initial_param.error ().kind != Parse::Error::Self::Kind::NOT_SELF)
     return nullptr;
 
   if (initial_param.has_value () && lexer.peek_token ()->get_id () == COMMA)
@@ -2138,7 +2138,7 @@ Parser<ManagedTokenSource>::parse_non_ptr_sequence (
 
 /* Parses a single lifetime generic parameter (not including comma). */
 template <typename ManagedTokenSource>
-tl::expected<AST::LifetimeParam, ParseLifetimeParamError>
+tl::expected<AST::LifetimeParam, Parse::Error::LifetimeParam>
 Parser<ManagedTokenSource>::parse_lifetime_param ()
 {
   // parse outer attributes, which are optional and may not exist
@@ -2149,7 +2149,7 @@ Parser<ManagedTokenSource>::parse_lifetime_param ()
   if (lifetime_tok->get_id () != LIFETIME)
     {
       // if lifetime is missing, must not be a lifetime param, so return error
-      return tl::make_unexpected<ParseLifetimeParamError> ({});
+      return tl::make_unexpected<Parse::Error::LifetimeParam> ({});
     }
   lexer.skip_token ();
   AST::Lifetime lifetime (AST::Lifetime::NAMED, lifetime_tok->get_str (),
@@ -2827,7 +2827,7 @@ Parser<ManagedTokenSource>::parse_lifetime_bounds 
(EndTokenPred is_end_token)
 /* Parses a lifetime token (named, 'static, or '_). Also handles lifetime not
  * existing. */
 template <typename ManagedTokenSource>
-tl::expected<AST::Lifetime, ParseLifetimeError>
+tl::expected<AST::Lifetime, Parse::Error::Lifetime>
 Parser<ManagedTokenSource>::parse_lifetime (bool allow_elided)
 {
   const_TokenPtr lifetime_tok = lexer.peek_token ();
@@ -2839,7 +2839,7 @@ Parser<ManagedTokenSource>::parse_lifetime (bool 
allow_elided)
        }
       else
        {
-         return tl::make_unexpected<ParseLifetimeError> ({});
+         return tl::make_unexpected<Parse::Error::Lifetime> ({});
        }
     }
   lexer.skip_token ();
@@ -4286,7 +4286,7 @@ 
Parser<ManagedTokenSource>::parse_inherent_impl_function_or_method (
   auto initial_param = parse_self_param ();
 
   if (!initial_param.has_value ()
-      && initial_param.error () != ParseSelfError::NOT_SELF)
+      && initial_param.error ().kind != Parse::Error::Self::Kind::NOT_SELF)
     return nullptr;
 
   /* FIXME: ensure that self param doesn't accidently consume tokens for a
@@ -4487,7 +4487,7 @@ 
Parser<ManagedTokenSource>::parse_trait_impl_function_or_method (
   auto initial_param = parse_self_param ();
 
   if (!initial_param.has_value ()
-      && initial_param.error () != ParseSelfError::NOT_SELF)
+      && initial_param.error ().kind != Parse::Error::Self::Kind::NOT_SELF)
     return nullptr;
 
   // FIXME: ensure that self param doesn't accidently consume tokens for a
@@ -5132,7 +5132,7 @@ Parser<ManagedTokenSource>::parse_generic_args_binding ()
 
 // Parses a self param. Also handles self param not existing.
 template <typename ManagedTokenSource>
-tl::expected<std::unique_ptr<AST::Param>, ParseSelfError>
+tl::expected<std::unique_ptr<AST::Param>, Parse::Error::Self>
 Parser<ManagedTokenSource>::parse_self_param ()
 {
   bool has_reference = false;
@@ -5156,7 +5156,7 @@ Parser<ManagedTokenSource>::parse_self_param ()
        {
          rust_error_at (lexer.peek_token ()->get_locus (),
                         "cannot pass %<self%> by raw pointer");
-         return tl::make_unexpected (ParseSelfError::SELF_PTR);
+         return Parse::Error::Self::make_self_raw_pointer ();
        }
     }
 
@@ -5177,7 +5177,7 @@ Parser<ManagedTokenSource>::parse_self_param ()
       is_self = true;
 
   if (!is_self)
-    return tl::make_unexpected (ParseSelfError::NOT_SELF);
+    return Parse::Error::Self::make_not_self ();
 
   // test if self is a reference parameter
   if (lexer.peek_token ()->get_id () == AMP)
@@ -5200,7 +5200,7 @@ Parser<ManagedTokenSource>::parse_self_param ()
              add_error (std::move (error));
 
              // skip after somewhere?
-             return tl::make_unexpected (ParseSelfError::PARSING);
+             return Parse::Error::Self::make_parsing_error ();
            }
        }
     }
@@ -5218,7 +5218,7 @@ Parser<ManagedTokenSource>::parse_self_param ()
   if (self_tok->get_id () != SELF)
     {
       // skip after somewhere?
-      return tl::make_unexpected (ParseSelfError::NOT_SELF);
+      return Parse::Error::Self::make_not_self ();
     }
   lexer.skip_token ();
 
@@ -5237,7 +5237,7 @@ Parser<ManagedTokenSource>::parse_self_param ()
          add_error (std::move (error));
 
          // skip after somewhere?
-         return tl::make_unexpected (ParseSelfError::PARSING);
+         return Parse::Error::Self::make_parsing_error ();
        }
     }
 
@@ -5250,7 +5250,7 @@ Parser<ManagedTokenSource>::parse_self_param ()
       add_error (std::move (error));
 
       // skip after somewhere?
-      return tl::make_unexpected (ParseSelfError::PARSING);
+      return Parse::Error::Self::make_parsing_error ();
     }
 
   if (has_reference)
@@ -5379,15 +5379,14 @@ Parser<ManagedTokenSource>::parse_expr_stmt 
(AST::AttrVec outer_attrs,
 
 // Parses a loop label used in loop expressions.
 template <typename ManagedTokenSource>
-tl::expected<AST::LoopLabel, ParseLoopLabelError>
+tl::expected<AST::LoopLabel, Parse::Error::LoopLabel>
 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::unexpected<ParseLoopLabelError> (
-       ParseLoopLabelError::NOT_LOOP_LABEL);
+      return Parse::Error::LoopLabel::make_not_loop_label ();
     }
   /* FIXME: check for named lifetime requirement here? or check in semantic
    * analysis phase? */
@@ -5396,11 +5395,10 @@ Parser<ManagedTokenSource>::parse_loop_label 
(const_TokenPtr tok)
   if (!skip_token (COLON))
     {
       // skip somewhere?
-      return tl::unexpected<ParseLoopLabelError> (
-       ParseLoopLabelError::MISSING_COLON);
+      Parse::Error::LoopLabel::make_missing_colon ();
     }
 
-  return tl::expected<AST::LoopLabel, ParseLoopLabelError> (
+  return tl::expected<AST::LoopLabel, Parse::Error::LoopLabel> (
     AST::LoopLabel (std::move (label), tok->get_locus ()));
 }
 
diff --git a/gcc/rust/parse/rust-parse.h b/gcc/rust/parse/rust-parse.h
index 9e6d4bc21..95745a60c 100644
--- a/gcc/rust/parse/rust-parse.h
+++ b/gcc/rust/parse/rust-parse.h
@@ -29,32 +29,6 @@ along with GCC; see the file COPYING3.  If not see
 
 namespace Rust {
 
-class ParseLifetimeParamError
-{
-};
-
-class ParseLifetimeError
-{
-};
-
-enum class AnonConstError
-{
-  InvalidSizeExpr,
-};
-
-enum class ParseLoopLabelError
-{
-  NOT_LOOP_LABEL,
-  MISSING_COLON,
-};
-
-enum class ParseSelfError
-{
-  SELF_PTR,
-  PARSING,
-  NOT_SELF,
-};
-
 // Left binding powers of operations.
 enum binding_powers
 {
@@ -275,7 +249,7 @@ public:
                    tl::optional<AST::LoopLabel> = tl::nullopt,
                    location_t pratt_parsed_loc = UNKNOWN_LOCATION);
 
-  tl::expected<AST::AnonConst, AnonConstError> parse_anon_const ();
+  tl::expected<AST::AnonConst, Parse::Error::AnonConst> parse_anon_const ();
 
   std::unique_ptr<AST::ConstBlock>
   parse_const_block_expr (AST::AttrVec outer_attrs = AST::AttrVec (),
@@ -420,7 +394,7 @@ private:
     ParseFunction parsing_function, EndTokenPred is_end_token,
     std::string error_msg = "failed to parse generic param in generic params")
     -> std::vector<decltype (parsing_function ())>;
-  tl::expected<AST::LifetimeParam, ParseLifetimeParamError>
+  tl::expected<AST::LifetimeParam, Parse::Error::LifetimeParam>
   parse_lifetime_param ();
   std::vector<std::unique_ptr<AST::TypeParam>> parse_type_params ();
   template <typename EndTokenPred>
@@ -450,7 +424,7 @@ private:
   std::vector<AST::Lifetime> parse_lifetime_bounds ();
   template <typename EndTokenPred>
   std::vector<AST::Lifetime> parse_lifetime_bounds (EndTokenPred is_end_token);
-  tl::expected<AST::Lifetime, ParseLifetimeError>
+  tl::expected<AST::Lifetime, Parse::Error::Lifetime>
   parse_lifetime (bool allow_elided);
   AST::Lifetime lifetime_from_token (const_TokenPtr tok);
   std::unique_ptr<AST::ExternalTypeItem>
@@ -486,7 +460,8 @@ private:
   std::unique_ptr<AST::ConstantItem>
   parse_trait_const (AST::AttrVec outer_attrs);
 
-  tl::expected<std::unique_ptr<AST::Param>, ParseSelfError> parse_self_param 
();
+  tl::expected<std::unique_ptr<AST::Param>, Parse::Error::Self>
+  parse_self_param ();
 
   std::unique_ptr<AST::Impl> parse_impl (AST::Visibility vis,
                                         AST::AttrVec outer_attrs);
@@ -752,7 +727,7 @@ private:
   std::unique_ptr<AST::Expr> parse_labelled_loop_expr (const_TokenPtr tok,
                                                       AST::AttrVec outer_attrs
                                                       = AST::AttrVec ());
-  tl::expected<AST::LoopLabel, ParseLoopLabelError>
+  tl::expected<AST::LoopLabel, Parse::Error::LoopLabel>
   parse_loop_label (const_TokenPtr tok);
   std::unique_ptr<AST::AsyncBlockExpr>
   parse_async_block_expr (AST::AttrVec outer_attrs = AST::AttrVec ());

base-commit: 712b273bdc0ae4279a1798e066ddeb630742e6ef
-- 
2.52.0

Reply via email to