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

gcc/rust/ChangeLog:

        * ast/rust-ast-collector.cc (TokenCollector::visit): Remove error kind
        and change function call.
        * ast/rust-ast-visitor.cc (DefaultASTVisitor::visit): Change call name.
        * ast/rust-path.cc (ConstGenericParam::as_string): Likewise.
        * ast/rust-path.h: Remove error kind.
        * hir/rust-ast-lower-type.cc (ASTLowerGenericParam::visit): Change call
        name.
        * parse/rust-parse-impl.h (Parser::parse_generic_param): Use optional
        on parsing failure.
        (Parser::parse_generic_arg): Likewise.
        (Parser::parse_path_generic_args): Likewise.
        * parse/rust-parse.h: Likewise.
        * resolve/rust-ast-resolve-type.h: Change call name.

Signed-off-by: Pierre-Emmanuel Patry <pierre-emmanuel.pa...@embecosm.com>
---
 gcc/rust/ast/rust-ast-collector.cc       |  4 +--
 gcc/rust/ast/rust-ast-visitor.cc         |  2 +-
 gcc/rust/ast/rust-path.cc                |  2 +-
 gcc/rust/ast/rust-path.h                 | 28 ++++++------------
 gcc/rust/hir/rust-ast-lower-type.cc      |  2 +-
 gcc/rust/parse/rust-parse-impl.h         | 36 +++++++++++++-----------
 gcc/rust/parse/rust-parse.h              |  2 +-
 gcc/rust/resolve/rust-ast-resolve-type.h |  4 +--
 8 files changed, 35 insertions(+), 45 deletions(-)

diff --git a/gcc/rust/ast/rust-ast-collector.cc 
b/gcc/rust/ast/rust-ast-collector.cc
index 5a104e32b9a..c850e965cf4 100644
--- a/gcc/rust/ast/rust-ast-collector.cc
+++ b/gcc/rust/ast/rust-ast-collector.cc
@@ -491,7 +491,7 @@ TokenCollector::visit (ConstGenericParam &param)
   if (param.has_default_value ())
     {
       push (Rust::Token::make (EQUAL, UNDEF_LOCATION));
-      visit (param.get_default_value ());
+      visit (param.get_default_value_unchecked ());
     }
 }
 
@@ -639,8 +639,6 @@ TokenCollector::visit (GenericArg &arg)
        push (Rust::Token::make_identifier (UNDEF_LOCATION, std::move (path)));
       }
       break;
-    case GenericArg::Kind::Error:
-      rust_unreachable ();
     }
 }
 
diff --git a/gcc/rust/ast/rust-ast-visitor.cc b/gcc/rust/ast/rust-ast-visitor.cc
index 61a3b8b4bf7..6862024354f 100644
--- a/gcc/rust/ast/rust-ast-visitor.cc
+++ b/gcc/rust/ast/rust-ast-visitor.cc
@@ -82,7 +82,7 @@ DefaultASTVisitor::visit (AST::ConstGenericParam &const_param)
   if (const_param.has_type ())
     visit (const_param.get_type ());
   if (const_param.has_default_value ())
-    visit (const_param.get_default_value ());
+    visit (const_param.get_default_value_unchecked ());
 }
 
 void
diff --git a/gcc/rust/ast/rust-path.cc b/gcc/rust/ast/rust-path.cc
index 69627be81af..8e43ddf648a 100644
--- a/gcc/rust/ast/rust-path.cc
+++ b/gcc/rust/ast/rust-path.cc
@@ -119,7 +119,7 @@ ConstGenericParam::as_string () const
   str += "const " + name.as_string () + ": " + type->as_string ();
 
   if (has_default_value ())
-    str += " = " + get_default_value ().as_string ();
+    str += " = " + get_default_value_unchecked ().as_string ();
 
   return str;
 }
diff --git a/gcc/rust/ast/rust-path.h b/gcc/rust/ast/rust-path.h
index 805be8e91f7..a4ba93b718e 100644
--- a/gcc/rust/ast/rust-path.h
+++ b/gcc/rust/ast/rust-path.h
@@ -167,17 +167,11 @@ public:
    */
   enum class Kind
   {
-    Error,
     Const,  // A const value
     Type,   // A type argument (not discernable during parsing)
     Either, // Either a type or a const value, cleared up during resolving
   };
 
-  static GenericArg create_error ()
-  {
-    return GenericArg (nullptr, nullptr, {""}, Kind::Error, UNDEF_LOCATION);
-  }
-
   static GenericArg create_const (std::unique_ptr<Expr> expression)
   {
     auto locus = expression->get_locus ();
@@ -222,8 +216,6 @@ public:
   GenericArg (GenericArg &&other) = default;
   GenericArg &operator= (GenericArg &&other) = default;
 
-  bool is_error () const { return kind == Kind::Error; }
-
   Kind get_kind () const { return kind; }
   location_t get_locus () const { return locus; }
 
@@ -239,8 +231,6 @@ public:
        break;
       case Kind::Either:
        break;
-      case Kind::Error:
-       rust_unreachable ();
       }
   }
 
@@ -283,8 +273,6 @@ public:
   {
     switch (get_kind ())
       {
-      case Kind::Error:
-       rust_unreachable ();
       case Kind::Either:
        return "Ambiguous: " + path.as_string ();
       case Kind::Const:
@@ -355,15 +343,15 @@ class ConstGenericParam : public GenericParam
   /**
    * Default value for the const generic parameter
    */
-  GenericArg default_value;
+  tl::optional<GenericArg> default_value;
 
   AST::AttrVec outer_attrs;
   location_t locus;
 
 public:
   ConstGenericParam (Identifier name, std::unique_ptr<AST::Type> type,
-                    GenericArg default_value, AST::AttrVec outer_attrs,
-                    location_t locus)
+                    tl::optional<GenericArg> default_value,
+                    AST::AttrVec outer_attrs, location_t locus)
     : name (name), type (std::move (type)),
       default_value (std::move (default_value)), outer_attrs (outer_attrs),
       locus (locus)
@@ -376,7 +364,7 @@ public:
   {}
 
   bool has_type () const { return type != nullptr; }
-  bool has_default_value () const { return !default_value.is_error (); }
+  bool has_default_value () const { return default_value.has_value (); }
 
   const Identifier &get_name () const { return name; }
 
@@ -389,18 +377,18 @@ public:
     return *type;
   }
 
-  GenericArg &get_default_value ()
+  GenericArg &get_default_value_unchecked ()
   {
     rust_assert (has_default_value ());
 
-    return default_value;
+    return default_value.value ();
   }
 
-  const GenericArg &get_default_value () const
+  const GenericArg &get_default_value_unchecked () const
   {
     rust_assert (has_default_value ());
 
-    return default_value;
+    return default_value.value ();
   }
 
   std::string as_string () const override;
diff --git a/gcc/rust/hir/rust-ast-lower-type.cc 
b/gcc/rust/hir/rust-ast-lower-type.cc
index d3e528dc925..a678f189ac2 100644
--- a/gcc/rust/hir/rust-ast-lower-type.cc
+++ b/gcc/rust/hir/rust-ast-lower-type.cc
@@ -557,7 +557,7 @@ ASTLowerGenericParam::visit (AST::ConstGenericParam &param)
   HIR::Expr *default_expr = nullptr;
   if (param.has_default_value ())
     default_expr = ASTLoweringExpr::translate (
-      param.get_default_value ().get_expression ());
+      param.get_default_value_unchecked ().get_expression ());
 
   translated = new HIR::ConstGenericParam (param.get_name ().as_string (),
                                           std::unique_ptr<Type> (type),
diff --git a/gcc/rust/parse/rust-parse-impl.h b/gcc/rust/parse/rust-parse-impl.h
index 3bb758ef1d2..9dda231ba8f 100644
--- a/gcc/rust/parse/rust-parse-impl.h
+++ b/gcc/rust/parse/rust-parse-impl.h
@@ -3174,24 +3174,28 @@ Parser<ManagedTokenSource>::parse_generic_param 
(EndTokenPred is_end_token)
          return nullptr;
 
        // optional default value
-       auto default_expr = AST::GenericArg::create_error ();
+       tl::optional<AST::GenericArg> default_expr = tl::nullopt;
        if (lexer.peek_token ()->get_id () == EQUAL)
          {
            lexer.skip_token ();
            auto tok = lexer.peek_token ();
            default_expr = parse_generic_arg ();
 
-           if (default_expr.is_error ())
-             rust_error_at (tok->get_locus (),
-                            "invalid token for start of default value for "
-                            "const generic parameter: expected %<block%>, "
-                            "%<identifier%> or %<literal%>, got %qs",
-                            token_id_to_str (tok->get_id ()));
+           if (!default_expr)
+             {
+               rust_error_at (tok->get_locus (),
+                              "invalid token for start of default value for "
+                              "const generic parameter: expected %<block%>, "
+                              "%<identifier%> or %<literal%>, got %qs",
+                              token_id_to_str (tok->get_id ()));
+               return nullptr;
+             }
 
            // At this point, we *know* that we are parsing a const
            // expression
-           if (default_expr.get_kind () == AST::GenericArg::Kind::Either)
-             default_expr = default_expr.disambiguate_to_const ();
+           if (default_expr.value ().get_kind ()
+               == AST::GenericArg::Kind::Either)
+             default_expr = default_expr.value ().disambiguate_to_const ();
          }
 
        param = std::unique_ptr<AST::ConstGenericParam> (
@@ -6249,7 +6253,7 @@ Parser<ManagedTokenSource>::parse_type_path ()
 }
 
 template <typename ManagedTokenSource>
-AST::GenericArg
+tl::optional<AST::GenericArg>
 Parser<ManagedTokenSource>::parse_generic_arg ()
 {
   auto tok = lexer.peek_token ();
@@ -6270,7 +6274,7 @@ Parser<ManagedTokenSource>::parse_generic_arg ()
            if (type)
              return AST::GenericArg::create_type (std::move (type));
            else
-             return AST::GenericArg::create_error ();
+             return tl::nullopt;
          }
        else if (next_tok->get_id () == COLON)
          {
@@ -6287,7 +6291,7 @@ Parser<ManagedTokenSource>::parse_generic_arg ()
            if (type)
              return AST::GenericArg::create_type (std::move (type));
            else
-             return AST::GenericArg::create_error ();
+             return tl::nullopt;
          }
        lexer.skip_token ();
        return AST::GenericArg::create_ambiguous (tok->get_str (),
@@ -6313,12 +6317,12 @@ Parser<ManagedTokenSource>::parse_generic_arg ()
        if (type)
          return AST::GenericArg::create_type (std::move (type));
        else
-         return AST::GenericArg::create_error ();
+         return tl::nullopt;
       }
     }
 
   if (!expr)
-    return AST::GenericArg::create_error ();
+    return tl::nullopt;
 
   return AST::GenericArg::create_const (std::move (expr));
 }
@@ -6383,9 +6387,9 @@ Parser<ManagedTokenSource>::parse_path_generic_args ()
        break;
 
       auto arg = parse_generic_arg ();
-      if (!arg.is_error ())
+      if (arg)
        {
-         generic_args.emplace_back (std::move (arg));
+         generic_args.emplace_back (std::move (arg.value ()));
        }
 
       // FIXME: Do we need to break if we encounter an error?
diff --git a/gcc/rust/parse/rust-parse.h b/gcc/rust/parse/rust-parse.h
index ff7987930fb..827d91d6cbb 100644
--- a/gcc/rust/parse/rust-parse.h
+++ b/gcc/rust/parse/rust-parse.h
@@ -226,7 +226,7 @@ private:
   AST::TypePath parse_type_path ();
   std::unique_ptr<AST::TypePathSegment> parse_type_path_segment ();
   AST::PathIdentSegment parse_path_ident_segment ();
-  AST::GenericArg parse_generic_arg ();
+  tl::optional<AST::GenericArg> parse_generic_arg ();
   AST::GenericArgs parse_path_generic_args ();
   AST::GenericArgsBinding parse_generic_args_binding ();
   AST::TypePathFunction parse_type_path_function (location_t locus);
diff --git a/gcc/rust/resolve/rust-ast-resolve-type.h 
b/gcc/rust/resolve/rust-ast-resolve-type.h
index 8379d0e9f84..f1481fcc65e 100644
--- a/gcc/rust/resolve/rust-ast-resolve-type.h
+++ b/gcc/rust/resolve/rust-ast-resolve-type.h
@@ -141,8 +141,8 @@ public:
     if (first_pass)
       ResolveType::go (param.get_type ());
     else if (param.has_default_value ())
-      ResolveExpr::go (param.get_default_value ().get_expression (), prefix,
-                      canonical_prefix);
+      ResolveExpr::go (param.get_default_value_unchecked ().get_expression (),
+                      prefix, canonical_prefix);
   }
 
   void visit (AST::TypeParam &param) override
-- 
2.49.0

Reply via email to