]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
gccrs: Remove Rust::Optional in favor of tl::optional
authorArthur Cohen <arthur.cohen@embecosm.com>
Mon, 26 Jun 2023 12:47:06 +0000 (14:47 +0200)
committerArthur Cohen <arthur.cohen@embecosm.com>
Tue, 16 Jan 2024 17:49:35 +0000 (18:49 +0100)
gcc/rust/ChangeLog:

* Make-lang.in: Remove rust-optional-test.cc's object file
* ast/rust-macro.h: Remove use of Rust::Optional
* backend/rust-compile-base.cc
(HIRCompileBase::resolve_method_address): Likewise.
* checks/errors/privacy/rust-privacy-reporter.cc
(PrivacyReporter::check_for_privacy_violation): Likewise.
(PrivacyReporter::visit): Likewise.
* checks/errors/privacy/rust-privacy-reporter.h: Likewise.
* checks/errors/rust-feature-gate.cc (FeatureGate::check): Likewise.
* checks/errors/rust-feature.cc (Feature::create): Likewise.
(Feature::as_name): Likewise.
* checks/errors/rust-feature.h: Likewise.
* expand/rust-macro-builtins.cc: Likewise.
* lex/rust-lex.cc (Lexer::Lexer): Likewise.
(Lexer::skip_token): Likewise.
(Lexer::dump_and_skip): Likewise.
* lex/rust-lex.h: Likewise.
* resolve/rust-ast-resolve-path.cc (ResolvePath::resolve_path): Likewise.
* resolve/rust-ast-resolve-type.cc (ResolveRelativeTypePath::go): Likewise.
* rust-lang.cc (run_rust_tests): Likewise.
* rust-session-manager.cc (Session::compile_crate): Likewise.
(TargetOptions::dump_target_options): Likewise.
* rust-session-manager.h (struct TargetOptions): Likewise.
* util/rust-hir-map.cc (Mappings::lookup_module_children): Likewise.
(Mappings::lookup_module_chidren_items): Likewise.
(Mappings::lookup_module_child): Likewise.
(Mappings::lookup_parent_module): Likewise.
* util/rust-hir-map.h (RUST_HIR_MAP_H): Likewise.
* util/rust-optional-test.cc: Removed.
* util/rust-optional.h: Removed.

20 files changed:
gcc/rust/Make-lang.in
gcc/rust/ast/rust-macro.h
gcc/rust/backend/rust-compile-base.cc
gcc/rust/checks/errors/privacy/rust-privacy-reporter.cc
gcc/rust/checks/errors/privacy/rust-privacy-reporter.h
gcc/rust/checks/errors/rust-feature-gate.cc
gcc/rust/checks/errors/rust-feature.cc
gcc/rust/checks/errors/rust-feature.h
gcc/rust/expand/rust-macro-builtins.cc
gcc/rust/lex/rust-lex.cc
gcc/rust/lex/rust-lex.h
gcc/rust/resolve/rust-ast-resolve-path.cc
gcc/rust/resolve/rust-ast-resolve-type.cc
gcc/rust/rust-lang.cc
gcc/rust/rust-session-manager.cc
gcc/rust/rust-session-manager.h
gcc/rust/util/rust-hir-map.cc
gcc/rust/util/rust-hir-map.h
gcc/rust/util/rust-optional-test.cc [deleted file]
gcc/rust/util/rust-optional.h [deleted file]

index 939fa6419b27265bd89e5a7ff052061ead696c4c..641d5798a485c8c60b502040df2170d61a834ea0 100644 (file)
@@ -161,7 +161,6 @@ GRS_OBJS = \
     rust/rust-compile-pattern.o \
     rust/rust-compile-fnparam.o \
     rust/rust-base62.o \
-    rust/rust-optional-test.o \
     rust/rust-compile-item.o \
     rust/rust-compile-implitem.o \
     rust/rust-compile-stmt.o \
index decc7bee21d80924434d20afd705b9d15403f156..1620ff6974b8bcc93ba9e0a5d04d997a830a3bef 100644 (file)
@@ -622,9 +622,8 @@ public:
           Location locus, bool is_semi_coloned = false)
   {
     return std::unique_ptr<MacroInvocation> (
-      new MacroInvocation (InvocKind::Regular, Optional<BuiltinMacro>::none (),
-                          invoc_data, outer_attrs, locus, is_semi_coloned,
-                          {}));
+      new MacroInvocation (InvocKind::Regular, tl::nullopt, invoc_data,
+                          outer_attrs, locus, is_semi_coloned, {}));
   }
 
   /**
@@ -640,9 +639,8 @@ public:
     bool is_semi_coloned = false)
   {
     return std::unique_ptr<MacroInvocation> (
-      new MacroInvocation (InvocKind::Builtin,
-                          Optional<BuiltinMacro>::some (kind), invoc_data,
-                          outer_attrs, locus, is_semi_coloned,
+      new MacroInvocation (InvocKind::Builtin, kind, invoc_data, outer_attrs,
+                          locus, is_semi_coloned,
                           std::move (pending_eager_invocations)));
   }
 
@@ -685,7 +683,7 @@ public:
   bool has_semicolon () const { return is_semi_coloned; }
 
   InvocKind get_kind () const { return kind; }
-  Optional<BuiltinMacro> get_builtin_kind () const { return builtin_kind; }
+  tl::optional<BuiltinMacro> get_builtin_kind () const { return builtin_kind; }
 
   /**
    * Turn the current MacroInvocation into a builtin macro invocation
@@ -693,7 +691,7 @@ public:
   void map_to_builtin (BuiltinMacro macro)
   {
     kind = InvocKind::Builtin;
-    builtin_kind = Optional<BuiltinMacro>::some (macro);
+    builtin_kind = macro;
   }
 
   /**
@@ -711,7 +709,7 @@ public:
 private:
   /* Full constructor */
   MacroInvocation (
-    InvocKind kind, Optional<BuiltinMacro> builtin_kind,
+    InvocKind kind, tl::optional<BuiltinMacro> builtin_kind,
     MacroInvocData invoc_data, std::vector<Attribute> outer_attrs,
     Location locus, bool is_semi_coloned,
     std::vector<std::unique_ptr<MacroInvocation>> &&pending_eager_invocs)
@@ -748,7 +746,7 @@ private:
   InvocKind kind;
 
   /* If it is a builtin macro, which one */
-  Optional<BuiltinMacro> builtin_kind = Optional<BuiltinMacro>::none ();
+  tl::optional<BuiltinMacro> builtin_kind = tl::nullopt;
 
   /**
    * Pending invocations within a builtin macro invocation. This vector is empty
index d2e1d62c1bb32d1a23b6012e54556e7ee8cba86c..522487eeb59d4e85f173cb50686c1ef32e70a493 100644 (file)
@@ -816,7 +816,7 @@ HIRCompileBase::resolve_method_address (TyTy::FnType *fntype,
       rust_assert (ok);                                    // found
       rust_assert (trait_item_ref->is_optional ()); // has definition
 
-      // FIXME Optional means it has a definition and an associated
+      // FIXME tl::optional means it has a definition and an associated
       // block which can be a default implementation, if it does not
       // contain an implementation we should actually return
       // error_mark_node
index ca03975a6a7df6ceb6a794f3d6ecb81aebcdd3b9..1f1551a7176e58116980574c5d93790c5b1744e2 100644 (file)
@@ -28,7 +28,7 @@ PrivacyReporter::PrivacyReporter (
   Analysis::Mappings &mappings, Resolver::Resolver &resolver,
   const Rust::Resolver::TypeCheckContext &ty_ctx)
   : mappings (mappings), resolver (resolver), ty_ctx (ty_ctx),
-    current_module (Optional<NodeId>::none ())
+    current_module (tl::nullopt)
 {}
 
 void
@@ -92,7 +92,7 @@ PrivacyReporter::check_for_privacy_violation (const NodeId &use_id,
       case ModuleVisibility::Restricted: {
        // If we are in the crate, everything is restricted correctly, but we
        // can't get a module for it
-       if (current_module.is_none ())
+       if (!current_module.has_value ())
          return;
 
        auto module = mappings.lookup_defid (vis.get_module_id ());
@@ -102,12 +102,12 @@ PrivacyReporter::check_for_privacy_violation (const NodeId &use_id,
 
        // We are in the module referenced by the pub(restricted) visibility.
        // This is valid
-       if (mod_node_id == current_module.get ())
+       if (mod_node_id == current_module.value ())
          break;
 
        // FIXME: This needs a LOT of TLC: hinting about the definition, a
        // string to say if it's a module, function, type, etc...
-       if (!is_child_module (mappings, mod_node_id, current_module.get ()))
+       if (!is_child_module (mappings, mod_node_id, current_module.value ()))
          valid = false;
       }
       break;
@@ -584,8 +584,7 @@ PrivacyReporter::visit (HIR::Module &module)
   // FIXME: We also need to think about module privacy
 
   auto old_module = current_module;
-  current_module
-    = Optional<NodeId>::some (module.get_mappings ().get_nodeid ());
+  current_module = module.get_mappings ().get_nodeid ();
 
   for (auto &item : module.get_items ())
     item->accept_vis (*this);
index e391642eee444385d58edaa65d6a1f5a5ecf734c..af35ccf332555988277a675cc2d43b2b7b821fc6 100644 (file)
@@ -158,7 +158,7 @@ types
   const Rust::Resolver::TypeCheckContext &ty_ctx;
 
   // `None` means we're in the root module - the crate
-  Optional<NodeId> current_module;
+  tl::optional<NodeId> current_module;
 };
 
 } // namespace Privacy
index 21b20a834e96e13eb656060338af2590cb401d31..2177a5b3a0c1dd9169e91b89abc5400d5379a4c4 100644 (file)
@@ -42,9 +42,9 @@ FeatureGate::check (AST::Crate &crate)
                {
                  const auto &name_str = item->as_string ();
                  auto tname = Feature::as_name (name_str);
-                 if (!tname.is_none ())
+                 if (tname.has_value ())
                    {
-                     auto name = tname.get ();
+                     auto name = tname.value ();
                      valid_features.insert (name);
                    }
 
index f7fd69865a63afe9050277de25cc8691a3d7980e..035a1d1ce27cc5c5bf5c998985cf9bc9e3f6286a 100644 (file)
@@ -29,24 +29,19 @@ Feature::create (Feature::Name name)
     case Feature::Name::ASSOCIATED_TYPE_BOUNDS:
       return Feature (Feature::Name::ASSOCIATED_TYPE_BOUNDS,
                      Feature::State::ACCEPTED, "associated_type_bounds",
-                     "1.34.0", 52662,
-                     Optional<CompileOptions::Edition>::none (), "");
+                     "1.34.0", 52662, tl::nullopt, "");
     case Feature::Name::INTRINSICS:
       return Feature (Feature::Name::INTRINSICS, Feature::State::ACCEPTED,
-                     "intrinsics", "1.0.0", 0,
-                     Optional<CompileOptions::Edition>::none (), "");
+                     "intrinsics", "1.0.0", 0, tl::nullopt, "");
     case Feature::Name::RUSTC_ATTRS:
       return Feature (Feature::Name::RUSTC_ATTRS, Feature::State::ACCEPTED,
-                     "rustc_attrs", "1.0.0", 0,
-                     Optional<CompileOptions::Edition>::none (), "");
+                     "rustc_attrs", "1.0.0", 0, tl::nullopt, "");
     case Feature::Name::DECL_MACRO:
       return Feature (Feature::Name::DECL_MACRO, Feature::State::ACCEPTED,
-                     "decl_macro", "1.0.0", 0,
-                     Optional<CompileOptions::Edition>::none (), "");
+                     "decl_macro", "1.0.0", 0, tl::nullopt, "");
     case Feature::Name::EXTERN_TYPES:
       return Feature (Feature::Name::EXTERN_TYPES, Feature::State::ACTIVE,
-                     "extern_types", "1.23.0", 43467,
-                     Optional<CompileOptions::Edition>::none (), "");
+                     "extern_types", "1.23.0", 43467, tl::nullopt, "");
     default:
       gcc_unreachable ();
     }
@@ -63,12 +58,13 @@ const std::map<std::string, Feature::Name> Feature::name_hash_map = {
   {"extern_types", Feature::Name::EXTERN_TYPES},
 }; // namespace Rust
 
-Optional<Feature::Name>
+tl::optional<Feature::Name>
 Feature::as_name (const std::string &name)
 {
   if (Feature::name_hash_map.count (name))
-    return Optional<Feature::Name>::some (Feature::name_hash_map.at (name));
-  return Optional<Feature::Name>::none ();
+    return Feature::name_hash_map.at (name);
+
+  return tl::nullopt;
 }
 
 } // namespace Rust
\ No newline at end of file
index 91fc150b223d76cb36b0cc5d067fe7d14fa70648..4ff059c6167586397670198dc9b16d707b3a7132 100644 (file)
@@ -20,7 +20,7 @@
 #define RUST_FEATURE_H
 
 #include "rust-session-manager.h"
-#include "rust-optional.h"
+#include "optional.h"
 
 namespace Rust {
 
@@ -51,13 +51,13 @@ public:
   State state () { return m_state; }
   unsigned issue () { return m_issue; }
 
-  static Optional<Name> as_name (const std::string &name);
+  static tl::optional<Name> as_name (const std::string &name);
   static Feature create (Name name);
 
 private:
   Feature (Name name, State state, const char *name_str,
           const char *rustc_since, unsigned issue_number,
-          const Optional<CompileOptions::Edition> &edition,
+          const tl::optional<CompileOptions::Edition> &edition,
           const char *description)
     : m_state (state), m_name (name), m_name_str (name_str),
       m_rustc_since (rustc_since), m_issue (issue_number), edition (edition),
@@ -69,7 +69,7 @@ private:
   std::string m_name_str;
   std::string m_rustc_since;
   unsigned m_issue;
-  Optional<CompileOptions::Edition> edition;
+  tl::optional<CompileOptions::Edition> edition;
   std::string m_description;
 
   static const std::map<std::string, Name> name_hash_map;
index 8d000383287c36c3703b1a3831753a1239573a25..7fe023a184cb7ea554b8127a3ad6fbb60853c0cd 100644 (file)
@@ -123,7 +123,7 @@ std::unordered_map<
     {"Hash", MacroBuiltin::proc_macro_builtin},
 };
 
-// FIXME: This should return an Optional
+// FIXME: This should return an tl::optional
 BuiltinMacro
 builtin_macro_from_string (const std::string &identifier)
 {
index 9f2fec6d57fef8188f3d62d2e705603f9e810d71..628fecc9982c3b1127296e97dabae0711a7a8103 100644 (file)
@@ -138,13 +138,13 @@ is_identifier_continue (uint32_t codepoint)
 
 Lexer::Lexer (const std::string &input)
   : input (RAIIFile::create_error ()), current_line (1), current_column (1),
-    line_map (nullptr), dump_lex_out (Optional<std::ofstream &>::none ()),
+    line_map (nullptr), dump_lex_out ({}),
     raw_input_source (new BufferInputSource (input, 0)),
     input_queue{*raw_input_source}, token_queue (TokenSource (this))
 {}
 
 Lexer::Lexer (const char *filename, RAIIFile file_input, Linemap *linemap,
-             Optional<std::ofstream &> dump_lex_opt)
+             tl::optional<std::ofstream &> dump_lex_opt)
   : input (std::move (file_input)), current_line (1), current_column (1),
     line_map (linemap), dump_lex_out (dump_lex_opt),
     raw_input_source (new FileInputSource (input.get_raw ())),
@@ -218,7 +218,7 @@ void
 Lexer::skip_token (int n)
 {
   // dump tokens if dump-lex option is enabled
-  if (dump_lex_out.is_some ())
+  if (dump_lex_out.has_value ())
     dump_and_skip (n);
   else
     token_queue.skip (n);
@@ -227,7 +227,7 @@ Lexer::skip_token (int n)
 void
 Lexer::dump_and_skip (int n)
 {
-  std::ofstream &out = dump_lex_out.get ();
+  std::ofstream &out = dump_lex_out.value ();
   bool found_eof = false;
   const_TokenPtr tok;
   for (int i = 0; i < n + 1; i++)
index 0c7b998feec67dd35ea70bae71dd38f94e30c606..1bb35ae64ca15ab3ee34ec7c24f7255f5e974e29 100644 (file)
@@ -22,7 +22,7 @@
 #include "rust-linemap.h"
 #include "rust-buffered-queue.h"
 #include "rust-token.h"
-#include "rust-optional.h"
+#include "optional.h"
 #include "selftest.h"
 
 namespace Rust {
@@ -158,8 +158,7 @@ private:
 public:
   // Construct lexer with input file and filename provided
   Lexer (const char *filename, RAIIFile input, Linemap *linemap,
-        Optional<std::ofstream &> dump_lex_opt
-        = Optional<std::ofstream &>::none ());
+        tl::optional<std::ofstream &> dump_lex_opt = tl::nullopt);
 
   // Lex the contents of a string instead of a file
   Lexer (const std::string &input);
@@ -397,7 +396,7 @@ private:
    * allocating new linemap */
   static const int max_column_hint = 80;
 
-  Optional<std::ofstream &> dump_lex_out;
+  tl::optional<std::ofstream &> dump_lex_out;
 
   // The input source for the lexer.
   // InputSource input_source;
index 67ff7d9bcd1fa8b15327f436076eda9fcb99cba0..918e0c67773383340e62bd0ef3b54821a1790992 100644 (file)
@@ -164,10 +164,10 @@ ResolvePath::resolve_path (AST::PathInExpression *expr)
       if (resolved_node_id == UNKNOWN_NODEID
          && previous_resolved_node_id == module_scope_id)
        {
-         Optional<CanonicalPath &> resolved_child
+         tl::optional<CanonicalPath &> resolved_child
            = mappings->lookup_module_child (module_scope_id,
                                             ident_seg.as_string ());
-         if (resolved_child.is_some ())
+         if (resolved_child.has_value ())
            {
              NodeId resolved_node = resolved_child->get_node_id ();
              if (resolver->get_name_scope ().decl_was_declared_here (
@@ -303,10 +303,10 @@ ResolvePath::resolve_path (AST::SimplePath *expr)
          continue;
        }
 
-      Optional<CanonicalPath &> resolved_child
+      tl::optional<CanonicalPath &> resolved_child
        = mappings->lookup_module_child (module_scope_id,
                                         segment.get_segment_name ());
-      if (resolved_child.is_some ())
+      if (resolved_child.has_value ())
        {
          NodeId resolved_node = resolved_child->get_node_id ();
          if (resolver->get_name_scope ().decl_was_declared_here (
index 58f677d79098949489ae7e67fc218dd8f1cd101f..2dacaee7abb137078b06803f128aaba687a59b2c 100644 (file)
@@ -198,10 +198,10 @@ ResolveRelativeTypePath::go (AST::TypePath &path, NodeId &resolved_node_id)
       if (resolved_node_id == UNKNOWN_NODEID
          && previous_resolved_node_id == module_scope_id)
        {
-         Optional<CanonicalPath &> resolved_child
+         tl::optional<CanonicalPath &> resolved_child
            = mappings->lookup_module_child (module_scope_id,
                                             ident_seg.as_string ());
-         if (resolved_child.is_some ())
+         if (resolved_child.has_value ())
            {
              NodeId resolved_node = resolved_child->get_node_id ();
              if (resolver->get_name_scope ().decl_was_declared_here (
index 2032cab87be362ce6f17bac1a1c6694d8ab29f85..631ecedb335c4b5b6b4da34d42c29ed2090b1e49 100644 (file)
@@ -35,8 +35,8 @@
 #include "rust-cfg-parser.h"
 #include "rust-privacy-ctx.h"
 #include "rust-ast-resolve-item.h"
-#include "rust-optional.h"
 #include "rust-lex.h"
+#include "optional.h"
 
 #include <mpfr.h>
 // note: header files must be in this order or else forward declarations don't
@@ -455,7 +455,6 @@ run_rust_tests ()
   rust_privacy_ctx_test ();
   rust_crate_name_validation_test ();
   rust_simple_path_resolve_test ();
-  rust_optional_test ();
 }
 } // namespace selftest
 
index 3b3781c2792c6e0d9b9c43dc08f0a0c779e17f6c..27ac1cd78865e1e328526ff7e55bde0fdab9d86f 100644 (file)
@@ -481,18 +481,16 @@ Session::compile_crate (const char *filename)
   // parse file here
   /* create lexer and parser - these are file-specific and so aren't instance
    * variables */
-  Optional<std::ofstream &> dump_lex_opt = Optional<std::ofstream &>::none ();
+  tl::optional<std::ofstream &> dump_lex_opt = tl::nullopt;
   std::ofstream dump_lex_stream;
   if (options.dump_option_enabled (CompileOptions::LEXER_DUMP))
     {
       dump_lex_stream.open (kLexDumpFile);
       if (dump_lex_stream.fail ())
-       {
-         rust_error_at (UNKNOWN_LOCATION, "cannot open %s:%m; ignored",
-                        kLexDumpFile);
-       }
-      auto stream = Optional<std::ofstream &>::some (dump_lex_stream);
-      dump_lex_opt = std::move (stream);
+       rust_error_at (UNKNOWN_LOCATION, "cannot open %s:%m; ignored",
+                      kLexDumpFile);
+
+      dump_lex_opt = dump_lex_stream;
     }
 
   Lexer lex (filename, std::move (file_wrap), linemap, dump_lex_opt);
@@ -1051,8 +1049,8 @@ TargetOptions::dump_target_options () const
     {
       for (const auto &value : pairs.second)
        {
-         if (value.is_some ())
-           out << pairs.first + ": \"" + value.get () + "\"\n";
+         if (value.has_value ())
+           out << pairs.first + ": \"" + value.value () + "\"\n";
          else
            out << pairs.first + "\n";
        }
index 882de1a0458073dd6ab79bb12f7d4a7cb3b81093..b2e77aced1aba732e9a2d250e3f0d3b0a6726016 100644 (file)
@@ -30,7 +30,7 @@
 #include "coretypes.h"
 #include "options.h"
 
-#include "rust-optional.h"
+#include "optional.h"
 
 namespace Rust {
 // parser forward decl
@@ -51,7 +51,7 @@ struct TargetOptions
 {
   /* TODO: maybe make private and access through helpers to allow changes to
    * impl */
-  std::unordered_map<std::string, std::unordered_set<Optional<std::string>>>
+  std::unordered_map<std::string, std::unordered_set<tl::optional<std::string>>>
     features;
 
 public:
@@ -60,8 +60,7 @@ public:
   {
     auto it = features.find (key);
     return it != features.end ()
-          && it->second.find (Optional<std::string>::none ())
-               != it->second.end ();
+          && it->second.find (tl::nullopt) != it->second.end ();
   }
 
   // Returns whether a key exists with the given value in the feature set.
@@ -71,7 +70,7 @@ public:
     if (it != features.end ())
       {
        auto set = it->second;
-       auto it2 = set.find (Optional<std::string>::some (value));
+       auto it2 = set.find (value);
        if (it2 != set.end ())
          return true;
       }
@@ -86,8 +85,8 @@ public:
     if (it != features.end ())
       {
        auto set = it->second;
-       if (set.size () == 1 && set.begin ()->is_some ())
-         return set.begin ()->get ();
+       if (set.size () == 1 && set.begin ()->has_value ())
+         return set.begin ()->value ();
       }
     return "";
   }
@@ -103,8 +102,8 @@ public:
       return {};
 
     for (auto &val : it->second)
-      if (val.is_some ())
-       ret.insert (val.get ());
+      if (val.has_value ())
+       ret.insert (val.value ());
 
     return ret;
   }
@@ -117,13 +116,14 @@ public:
     auto it = features.find (key);
 
     if (it == features.end ())
-      it = features
-            .insert (
-              std::make_pair (std::move (key),
-                              std::unordered_set<Optional<std::string>> ()))
-            .first;
-
-    return it->second.insert (Optional<std::string>::none ()).second;
+      it
+       = features
+           .insert (
+             std::make_pair (std::move (key),
+                             std::unordered_set<tl::optional<std::string>> ()))
+           .first;
+
+    return it->second.insert (tl::nullopt).second;
   }
 
   // Inserts a key-value pair into the feature set.
@@ -132,13 +132,14 @@ public:
     auto it = features.find (key);
 
     if (it == features.end ())
-      it = features
-            .insert (
-              std::make_pair (std::move (key),
-                              std::unordered_set<Optional<std::string>> ()))
-            .first;
-
-    it->second.insert (Optional<std::string>::some (std::move (value)));
+      it
+       = features
+           .insert (
+             std::make_pair (std::move (key),
+                             std::unordered_set<tl::optional<std::string>> ()))
+           .first;
+
+    it->second.insert (std::move (value));
   }
 
   // Dump all target options to stderr.
index d7b5c96205d1cbdc96a3b4709706571263a6092c..9510a5bcee79a01466121a9c52f66b5042b55f78 100644 (file)
@@ -1035,14 +1035,14 @@ Mappings::insert_module_child (NodeId module, NodeId child)
     it->second.emplace_back (child);
 }
 
-Optional<std::vector<NodeId> &>
+tl::optional<std::vector<NodeId> &>
 Mappings::lookup_module_children (NodeId module)
 {
   auto it = module_child_map.find (module);
   if (it == module_child_map.end ())
-    return Optional<std::vector<NodeId> &>::none ();
+    return tl::nullopt;
 
-  return Optional<std::vector<NodeId> &>::some (it->second);
+  return it->second;
 }
 
 void
@@ -1059,33 +1059,34 @@ Mappings::insert_module_child_item (NodeId module,
     it->second.emplace_back (child);
 }
 
-Optional<std::vector<Resolver::CanonicalPath> &>
+tl::optional<std::vector<Resolver::CanonicalPath> &>
 Mappings::lookup_module_chidren_items (NodeId module)
 {
   auto it = module_child_items.find (module);
   if (it == module_child_items.end ())
-    return Optional<std::vector<Resolver::CanonicalPath> &>::none ();
+    return tl::nullopt;
 
-  return Optional<std::vector<Resolver::CanonicalPath> &>::some (it->second);
+  return it->second;
 }
 
-Optional<Resolver::CanonicalPath &>
+tl::optional<Resolver::CanonicalPath &>
 Mappings::lookup_module_child (NodeId module, const std::string &item_name)
 {
-  Optional<std::vector<Resolver::CanonicalPath> &> children
+  tl::optional<std::vector<Resolver::CanonicalPath> &> children
     = lookup_module_chidren_items (module);
-  if (children.is_none ())
-    return Optional<Resolver::CanonicalPath &>::none ();
+  if (!children.has_value ())
+    return tl::nullopt;
 
   // lookup the children to match the name if we can
-  for (auto &child : children.get ())
+  for (auto &child : children.value ())
     {
       const std::string &raw_identifier = child.get ();
       bool found = raw_identifier.compare (item_name) == 0;
       if (found)
-       return Optional<Resolver::CanonicalPath &>::some (child);
+       return child;
     }
-  return Optional<Resolver::CanonicalPath &>::none ();
+
+  return tl::nullopt;
 }
 
 void
@@ -1095,14 +1096,14 @@ Mappings::insert_child_item_to_parent_module_mapping (NodeId child_item,
   child_to_parent_module_map.insert ({child_item, parent_module});
 }
 
-Optional<NodeId>
+tl::optional<NodeId>
 Mappings::lookup_parent_module (NodeId child_item)
 {
   auto it = child_to_parent_module_map.find (child_item);
   if (it == child_to_parent_module_map.end ())
-    return Optional<NodeId>::none ();
+    return tl::nullopt;
 
-  return Optional<NodeId>::some (it->second);
+  return it->second;
 }
 
 bool
index 5c962286218ea8cb0f738a4c342f535ef08ef663..5b8e09c36d032cec43d58c95f57a300c28094f07 100644 (file)
@@ -19,7 +19,7 @@
 #ifndef RUST_HIR_MAP_H
 #define RUST_HIR_MAP_H
 
-#include "rust-optional.h"
+#include "optional.h"
 #include "rust-system.h"
 #include "rust-location.h"
 #include "rust-mapping-common.h"
@@ -303,17 +303,17 @@ public:
   bool lookup_visibility (NodeId id, Privacy::ModuleVisibility &def);
 
   void insert_module_child (NodeId module, NodeId child);
-  Optional<std::vector<NodeId> &> lookup_module_children (NodeId module);
+  tl::optional<std::vector<NodeId> &> lookup_module_children (NodeId module);
 
   void insert_module_child_item (NodeId module, Resolver::CanonicalPath item);
-  Optional<std::vector<Resolver::CanonicalPath> &>
+  tl::optional<std::vector<Resolver::CanonicalPath> &>
   lookup_module_chidren_items (NodeId module);
-  Optional<Resolver::CanonicalPath &>
+  tl::optional<Resolver::CanonicalPath &>
   lookup_module_child (NodeId module, const std::string &item_name);
 
   void insert_child_item_to_parent_module_mapping (NodeId child_item,
                                                   NodeId parent_module);
-  Optional<NodeId> lookup_parent_module (NodeId child_item);
+  tl::optional<NodeId> lookup_parent_module (NodeId child_item);
   bool node_is_module (NodeId query);
 
   void insert_ast_item (AST::Item *item);
diff --git a/gcc/rust/util/rust-optional-test.cc b/gcc/rust/util/rust-optional-test.cc
deleted file mode 100644 (file)
index 844d2a6..0000000
+++ /dev/null
@@ -1,110 +0,0 @@
-// Copyright (C) 2020-2024 Free Software Foundation, Inc.
-
-// This file is part of GCC.
-
-// GCC is free software; you can redistribute it and/or modify it under
-// the terms of the GNU General Public License as published by the Free
-// Software Foundation; either version 3, or (at your option) any later
-// version.
-
-// GCC is distributed in the hope that it will be useful, but WITHOUT ANY
-// WARRANTY; without even the implied warranty of MERCHANTABILITY or
-// FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
-// for more details.
-
-// You should have received a copy of the GNU General Public License
-// along with GCC; see the file COPYING3.  If not see
-// <http://www.gnu.org/licenses/>.
-
-#include "rust-system.h"
-#include "rust-optional.h"
-#include "selftest.h"
-
-#if CHECKING_P
-
-static void
-rust_optional_create ()
-{
-  auto opt = Rust::Optional<int>::some (15);
-
-  ASSERT_TRUE (opt.is_some ());
-  ASSERT_EQ (opt.get (), 15);
-
-  Rust::Optional<int> const_opt = Rust::Optional<int>::some (15);
-  const int &value = const_opt.get ();
-
-  ASSERT_EQ (value, 15);
-}
-
-static void
-rust_optional_operators ()
-{
-  auto opt = Rust::Optional<int>::some (15);
-
-  // as bool
-  ASSERT_TRUE (opt);
-
-  // deref
-  ASSERT_EQ (*opt, 15);
-
-  class Methodable
-  {
-  public:
-    int method () { return 15; }
-  };
-
-  auto m_opt = Rust::Optional<Methodable>::some (Methodable ());
-  ASSERT_EQ (m_opt->method (), 15);
-}
-
-static void
-rust_optional_take ()
-{
-  auto opt = Rust::Optional<int>::some (15);
-  auto value = opt.take ();
-
-  ASSERT_EQ (value, 15);
-  ASSERT_TRUE (opt.is_none ());
-}
-
-static void
-rust_optional_map ()
-{
-  auto opt = Rust::Optional<int>::some (15);
-  auto twice = opt.map<int> ([] (int value) { return value * 2; });
-
-  ASSERT_FALSE (opt);
-  ASSERT_TRUE (twice);
-  ASSERT_EQ (*twice, 30);
-}
-
-static void
-rust_optional_reference ()
-{
-  auto value = std::vector<std::string> ();
-  value.emplace_back ("rust");
-  value.emplace_back ("+");
-  value.emplace_back ("gcc");
-  value.emplace_back ("=");
-  value.emplace_back ("<3");
-
-  auto opt = Rust::Optional<std::vector<std::string> &>::some (value);
-
-  ASSERT_EQ (opt->at (0), "rust");
-  ASSERT_EQ (opt->at (2), "gcc");
-}
-
-#endif /* #if CHECKING_P */
-
-void
-rust_optional_test ()
-{
-#if CHECKING_P
-  rust_optional_create ();
-  rust_optional_operators ();
-  rust_optional_take ();
-  rust_optional_map ();
-  rust_optional_reference ();
-
-#endif /* #if CHECKING_P */
-}
diff --git a/gcc/rust/util/rust-optional.h b/gcc/rust/util/rust-optional.h
deleted file mode 100644 (file)
index e545c15..0000000
+++ /dev/null
@@ -1,288 +0,0 @@
-// Copyright (C) 2020-2024 Free Software Foundation, Inc.
-
-// This file is part of GCC.
-
-// GCC is free software; you can redistribute it and/or modify it under
-// the terms of the GNU General Public License as published by the Free
-// Software Foundation; either version 3, or (at your option) any later
-// version.
-
-// GCC is distributed in the hope that it will be useful, but WITHOUT ANY
-// WARRANTY; without even the implied warranty of MERCHANTABILITY or
-// FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
-// for more details.
-
-// You should have received a copy of the GNU General Public License
-// along with GCC; see the file COPYING3.  If not see
-// <http://www.gnu.org/licenses/>.
-
-#ifndef RUST_OPTIONAL_H
-#define RUST_OPTIONAL_H
-
-#include "config.h"
-#include "rust-system.h"
-
-#include "selftest.h"
-
-namespace Rust {
-
-/**
- * Tagged union to try and simulate a sum type. This is safer and more ergonomic
- * than one of the two alternatives we're currently using in the compiler:
- *
- * 1. Storing a raw pointer, which can be `nullptr` or valid
- *
- * This is wildly unsafe, and usable in conjunction with local references, stack
- * variables, or pointers managed elsewhere, which can cause crashes, hard to
- * debug issues or undefined behavior. Likewise, if you do not check for the
- * pointer's validity, this will cause a crash.
- *
- * 2. Storing an extra boolean alongside the object
- *
- * This causes implementors to use a "dummy object": Either an empty version or
- * an error version. But what happens if what you really wanted to store was
- * the empty or error version? You can also easily incorporate logic bugs if you
- * forget to check for the associated boolean.
- *
- * The `Optional<T>` type has the same "ergonomic" cost: You need to check
- * whether your option is valid or not. However, the main advantage is that it
- * is more restrictive: You can only acess the member it contains "safely".
- * It is similar to storing a value + an associated boolean, but has the
- * advantage of making up only one member in your class.
- * You also benefit from some helper methods such as `map()`.
- *
- * You also get helper functions and operator overloading to "seamlessly"
- * replace raw pointer alternatives.
- *
- * ```c++
- * MyType *raw_pointer = something_that_can_fail();
- * if (raw_pointer)
- *     raw_pointer->method();
- *
- * // or
- *
- * Optional<MyType> opt = something_that_can_fail2();
- * if (opt)
- *     opt->method();
- *
- * // equivalent to
- *
- * if (opt.is_some())
- *     opt.get().method();
- * ```
- */
-template <typename T> class Optional
-{
-private:
-  struct tag_some
-  {
-  };
-  struct tag_none
-  {
-  };
-
-  bool field_is_some;
-
-  union
-  {
-    T value;
-    // prevents initialization warnings
-    // https://gcc.gnu.org/bugzilla/show_bug.cgi?id=80635#c53
-    // FIXME: remove
-    volatile char unused;
-  };
-
-  Optional (tag_some, const T &value) : field_is_some (true), value (value) {}
-  Optional (tag_some, T &&value) : field_is_some (true), value (value) {}
-
-  Optional (tag_none) : field_is_some (false) {}
-
-public:
-  Optional (const Optional &other)
-  {
-    if ((field_is_some = other.field_is_some))
-      new (&value) T (other.value);
-  }
-
-  Optional (Optional &&other)
-  {
-    if ((field_is_some = other.field_is_some))
-      new (&value) T (other.value);
-  }
-
-  Optional &operator= (const Optional &other)
-  {
-    if (is_some ())
-      value.~T ();
-    if ((field_is_some = other.field_is_some))
-      new (&value) T (other.value);
-    return *this;
-  }
-
-  Optional &operator= (Optional &&other)
-  {
-    if (is_some ())
-      value.~T ();
-    if ((field_is_some = other.field_is_some))
-      new (&value) T (other.value);
-    return *this;
-  }
-
-  ~Optional ()
-  {
-    if (is_some ())
-      value.~T ();
-  }
-
-  static Optional some (const T &value)
-  {
-    return Optional (tag_some (), value);
-  }
-
-  static Optional some (T &&value) { return Optional (tag_some (), value); }
-
-  static Optional none () { return Optional (tag_none ()); }
-
-  bool is_some () const { return field_is_some; }
-  bool is_none () const { return !is_some (); }
-
-  /**
-   * Enable boolean-like comparisons.
-   */
-  operator bool () { return is_some (); }
-
-  /**
-   * Enables dereferencing to access the contained value
-   */
-  T &operator* () { return get (); }
-  const T &operator* () const { return get (); }
-  T *operator-> () { return &get (); }
-  const T *operator-> () const { return &get (); }
-
-  const T &get () const
-  {
-    rust_assert (is_some ());
-
-    return value;
-  }
-
-  T &get ()
-  {
-    rust_assert (is_some ());
-
-    return value;
-  }
-
-  T take ()
-  {
-    rust_assert (is_some ());
-
-    T to_return = std::move (value);
-    value.~T ();
-
-    field_is_some = false;
-
-    return to_return;
-  }
-
-  template <typename U> Optional<U> map (std::function<U (T)> functor)
-  {
-    if (is_none ())
-      return Optional::none ();
-
-    auto value = functor (take ());
-
-    return Optional::some (value);
-  }
-};
-
-template <typename T> class Optional<T &>
-{
-private:
-  T *inner;
-
-  Optional (T *inner) : inner (inner) {}
-
-public:
-  static Optional<T &> some (T &value) { return Optional (&value); }
-
-  static Optional<T &> none () { return Optional (nullptr); }
-
-  bool is_some () const { return inner; }
-  bool is_none () const { return !is_some (); }
-
-  // FIXME: Can we factor this in a single class?
-
-  /**
-   * Enable boolean-like comparisons.
-   */
-  operator bool () { return is_some (); }
-
-  /**
-   * Enables dereferencing to access the contained value
-   */
-  T &operator* () { return get (); }
-  const T &operator* () const { return get (); }
-  T *operator-> () { return &get (); }
-  const T *operator-> () const { return &get (); }
-
-  T &get () const
-  {
-    rust_assert (is_some ());
-
-    return *inner;
-  }
-
-  T &take ()
-  {
-    rust_assert (is_some ());
-
-    T *to_return = inner;
-    inner = nullptr;
-
-    return *to_return;
-  }
-
-  template <typename U> Optional<U &> map (std::function<U &(T &)> functor)
-  {
-    if (is_none ())
-      return Optional::none ();
-
-    auto value = functor (take ());
-
-    return Optional::some (value);
-  }
-};
-
-template <typename T, typename U>
-bool
-operator== (const Optional<T> &t, const Optional<U> &u)
-{
-  if (t.is_some ())
-    return u.is_some () && t.get () == u.get ();
-  else
-    return u.is_none ();
-}
-
-} // namespace Rust
-
-namespace std {
-
-template <typename T> struct hash<Rust::Optional<T>>
-{
-  size_t operator() (const Rust::Optional<T> &op) const
-  {
-    return op.is_some () ? std::hash<T> () (op.get ()) : 0;
-  }
-};
-
-} // namespace std
-
-#ifdef CHECKING_P
-
-void
-rust_optional_test ();
-
-#endif // !CHECKING_P
-
-#endif // !RUST_OPTIONAL_H