]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
gccrs: 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)
committerArthur Cohen <arthur.cohen@embecosm.com>
Mon, 17 Mar 2025 15:35:23 +0000 (16:35 +0100)
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 28b901e69de99992cf787ff1641b2172e2d38a98..e2fa6dda09e387d89f0db53c6114b0d35fc7c844 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 869f99c5b803102a74268de43c7ae2ed1e0821cc..3a304e881d5e939063eec00d846c5baf901a9622 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 78ce17db138ebfac6e4736bff85dd8d76ff92e1a..b8d393759d6a46333fe667008ca19e2985de6690 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 c2b3e27fd9fa08a26ebc16a8582a91985c8c604c..6fd0e7a52f4cd6681dd266768a5c143facf47ab7 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);