]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
Change lookup_hir_to_node return type to optional
authorPierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
Wed, 24 Apr 2024 20:32:54 +0000 (22:32 +0200)
committerP-E-P <32375388+P-E-P@users.noreply.github.com>
Fri, 17 May 2024 15:28:30 +0000 (15:28 +0000)
Optional are more convenient to use and avoid uninitialized data.

gcc/rust/ChangeLog:

* backend/rust-compile-expr.cc (CompileExpr::generate_closure_function):
Adapt code for new optional return type.
* checks/errors/privacy/rust-privacy-reporter.cc (PrivacyReporter::check_base_type_privacy):
Likewise.
* util/rust-hir-map.cc (Mappings::lookup_hir_to_node): Change return
type to an optional.
* util/rust-hir-map.h: Adapt function prototype with the new return
type.

Signed-off-by: Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
gcc/rust/backend/rust-compile-expr.cc
gcc/rust/checks/errors/privacy/rust-privacy-reporter.cc
gcc/rust/util/rust-hir-map.cc
gcc/rust/util/rust-hir-map.h

index 3cf37ded3db13046d316f44cdd2f04bfd0a7763a..557b9fe59c5837841c771ad7a95045c932079072 100644 (file)
@@ -2216,10 +2216,12 @@ CompileExpr::generate_closure_function (HIR::ClosureExpr &expr,
 
   const Resolver::CanonicalPath &parent_canonical_path
     = closure_tyty.get_ident ().path;
-  NodeId node_id;
-  bool ok = ctx->get_mappings ().lookup_hir_to_node (
-    expr.get_mappings ().get_hirid (), &node_id);
-  rust_assert (ok);
+
+  tl::optional<NodeId> nid = ctx->get_mappings ().lookup_hir_to_node (
+    expr.get_mappings ().get_hirid ());
+  rust_assert (nid.has_value ());
+  auto node_id = nid.value ();
+
   Resolver::CanonicalPath path = parent_canonical_path.append (
     Resolver::CanonicalPath::new_seg (node_id, "{{closure}}"));
 
index a4e18cc900204eeb4e73f364d358c6f6535d4dea..1ce47fda1a9c2c83814ab90ff8c13ee01e7a298c 100644 (file)
@@ -201,12 +201,9 @@ PrivacyReporter::check_base_type_privacy (Analysis::NodeMapping &node_mappings,
     case TyTy::ADT:
       case TyTy::STR: {
        auto ref_id = ty->get_ref ();
-       NodeId lookup_id;
-
-       bool ok = mappings.lookup_hir_to_node (ref_id, &lookup_id);
-       rust_assert (ok);
-
-       return check_for_privacy_violation (lookup_id, locus);
+       if (auto lookup_id = mappings.lookup_hir_to_node (ref_id))
+         return check_for_privacy_violation (*lookup_id, locus);
+       rust_unreachable ();
       }
     case TyTy::REF:
       return recursive_check (
index 29fd4cf6bfd80125d97ec61b94be90f56fd82f44..06f3e7ab7a0c58d3328c66ed6766009ef4a6a22c 100644 (file)
@@ -779,15 +779,14 @@ Mappings::lookup_node_to_hir (NodeId id)
   return {it->second};
 }
 
-bool
-Mappings::lookup_hir_to_node (HirId id, NodeId *ref)
+tl::optional<NodeId>
+Mappings::lookup_hir_to_node (HirId id)
 {
   auto it = hirIdToNodeMappings.find (id);
   if (it == hirIdToNodeMappings.end ())
-    return false;
+    return tl::nullopt;
 
-  *ref = it->second;
-  return true;
+  return {it->second};
 }
 
 void
index 85bcfdeeda1d24c08e397712b308560ddeaf616a..753b2e0eeaf1b7c07acf797453fc9b98b1db83aa 100644 (file)
@@ -171,7 +171,7 @@ public:
 
   void insert_node_to_hir (NodeId id, HirId ref);
   tl::optional<HirId> lookup_node_to_hir (NodeId id);
-  bool lookup_hir_to_node (HirId id, NodeId *ref);
+  tl::optional<NodeId> lookup_hir_to_node (HirId id);
 
   void insert_location (HirId id, location_t locus);
   location_t lookup_location (HirId id);