]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
gccrs: Change return type of lookup_defid
authorPierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
Fri, 26 Apr 2024 10:02:53 +0000 (12:02 +0200)
committerArthur Cohen <arthur.cohen@embecosm.com>
Mon, 17 Mar 2025 15:35:24 +0000 (16:35 +0100)
Change the return type to an optional.

gcc/rust/ChangeLog:

* backend/rust-compile-base.cc (HIRCompileBase::resolve_method_address):
Change calling code to accomodate new return type.
* checks/errors/privacy/rust-privacy-reporter.cc:
Likewise.
* typecheck/rust-hir-type-check-base.cc (TypeCheckBase::get_marker_predicate):
Likewise.
* typecheck/rust-hir-type-check-expr.cc (TypeCheckExpr::visit):
Likewise.
* typecheck/rust-tyty-bounds.cc (TypeBoundsProbe::assemble_builtin_candidate):
Likewise.
* typecheck/rust-tyty.cc (ClosureType::setup_fn_once_output): Likewise.
* util/rust-hir-map.cc (Mappings::lookup_defid): Change function's
return type.
* util/rust-hir-map.h: Update function's prototype.

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

index 64ac121a28f91b7d33e32c59eb291224b5f350ca..c7031edd30bd334856398eaa0fbb160bbddbef10 100644 (file)
@@ -877,13 +877,12 @@ HIRCompileBase::resolve_method_address (TyTy::FnType *fntype,
   // Now we can try and resolve the address since this might be a forward
   // declared function, generic function which has not be compiled yet or
   // its an not yet trait bound function
-  HIR::Item *resolved_item = ctx->get_mappings ().lookup_defid (id);
-  if (resolved_item != nullptr)
+  if (auto resolved_item = ctx->get_mappings ().lookup_defid (id))
     {
       if (!fntype->has_substitutions_defined ())
-       return CompileItem::compile (resolved_item, ctx);
+       return CompileItem::compile (*resolved_item, ctx);
 
-      return CompileItem::compile (resolved_item, ctx, fntype);
+      return CompileItem::compile (*resolved_item, ctx, fntype);
     }
 
   // it might be resolved to a trait item
index 3a304e881d5e939063eec00d846c5baf901a9622..f8df9f795efaaac40a2b356439639891e408e991 100644 (file)
@@ -146,8 +146,7 @@ PrivacyReporter::check_for_privacy_violation (const NodeId &use_id,
        if (!current_module.has_value ())
          return;
 
-       auto module = mappings.lookup_defid (vis.get_module_id ());
-       rust_assert (module != nullptr);
+       auto module = mappings.lookup_defid (vis.get_module_id ()).value ();
 
        auto mod_node_id = module->get_mappings ().get_nodeid ();
 
index 7e57698a068bc96e65cb0939771eeebce48b55dd..7e34cef26c5ead25cf1e126cc9f77fa36707cda7 100644 (file)
@@ -414,8 +414,7 @@ TyTy::TypeBoundPredicate
 TypeCheckBase::get_marker_predicate (LangItem::Kind item_type, location_t locus)
 {
   DefId item_id = mappings.get_lang_item (item_type, locus);
-  HIR::Item *item = mappings.lookup_defid (item_id);
-  rust_assert (item != nullptr);
+  HIR::Item *item = mappings.lookup_defid (item_id).value ();
   rust_assert (item->get_item_kind () == HIR::Item::ItemKind::Trait);
 
   HIR::Trait &trait = *static_cast<HIR::Trait *> (item);
index 25eb628c7b7531c8181a8b277b59cedd6c1bc643..95e421d87200daedd70b135cac738b101a086f4f 100644 (file)
@@ -642,8 +642,7 @@ TypeCheckExpr::visit (HIR::RangeFromToExpr &expr)
     }
 
   // look it up and it _must_ be a struct definition
-  HIR::Item *item = mappings.lookup_defid (respective_lang_item_id);
-  rust_assert (item != nullptr);
+  HIR::Item *item = mappings.lookup_defid (respective_lang_item_id).value ();
 
   TyTy::BaseType *item_type = nullptr;
   bool ok
@@ -697,8 +696,7 @@ TypeCheckExpr::visit (HIR::RangeFromExpr &expr)
     }
 
   // look it up and it _must_ be a struct definition
-  HIR::Item *item = mappings.lookup_defid (respective_lang_item_id);
-  rust_assert (item != nullptr);
+  HIR::Item *item = mappings.lookup_defid (respective_lang_item_id).value ();
 
   TyTy::BaseType *item_type = nullptr;
   bool ok
@@ -745,8 +743,7 @@ TypeCheckExpr::visit (HIR::RangeToExpr &expr)
     }
 
   // look it up and it _must_ be a struct definition
-  HIR::Item *item = mappings.lookup_defid (respective_lang_item_id);
-  rust_assert (item != nullptr);
+  HIR::Item *item = mappings.lookup_defid (respective_lang_item_id).value ();
 
   TyTy::BaseType *item_type = nullptr;
   bool ok
@@ -792,8 +789,7 @@ TypeCheckExpr::visit (HIR::RangeFullExpr &expr)
     }
 
   // look it up and it _must_ be a struct definition
-  HIR::Item *item = mappings.lookup_defid (respective_lang_item_id);
-  rust_assert (item != nullptr);
+  HIR::Item *item = mappings.lookup_defid (respective_lang_item_id).value ();
 
   TyTy::BaseType *item_type = nullptr;
   bool ok
@@ -823,8 +819,7 @@ TypeCheckExpr::visit (HIR::RangeFromToInclExpr &expr)
     }
 
   // look it up and it _must_ be a struct definition
-  HIR::Item *item = mappings.lookup_defid (respective_lang_item_id);
-  rust_assert (item != nullptr);
+  HIR::Item *item = mappings.lookup_defid (respective_lang_item_id).value ();
 
   TyTy::BaseType *item_type = nullptr;
   bool ok
@@ -1599,7 +1594,7 @@ TypeCheckExpr::visit (HIR::ClosureExpr &expr)
   rust_assert (lang_item_defined);
 
   // these lang items are always traits
-  HIR::Item *item = mappings.lookup_defid (respective_lang_item_id);
+  HIR::Item *item = mappings.lookup_defid (respective_lang_item_id).value ();
   rust_assert (item->get_item_kind () == HIR::Item::ItemKind::Trait);
   HIR::Trait *trait_item = static_cast<HIR::Trait *> (item);
 
index 6ef8299a974698d9724d88f9b1092a9b1727e916..a18a0e40ddf16bdf9a8b917ee18c29f34e27ba32 100644 (file)
@@ -157,9 +157,10 @@ TypeBoundsProbe::assemble_builtin_candidate (LangItem::Kind lang_item)
   if (!found_lang_item)
     return;
 
-  HIR::Item *item = mappings.lookup_defid (id);
-  if (item == nullptr)
+  auto defid = mappings.lookup_defid (id);
+  if (!defid)
     return;
+  auto item = defid.value ();
 
   rust_assert (item->get_item_kind () == HIR::Item::ItemKind::Trait);
   HIR::Trait *trait = static_cast<HIR::Trait *> (item);
index c7a6e2200539fe1c003814fb3d91e2c6954dd2c3..2e9a551e4c7e69f37a373a8c663cc6f305fc1df0 100644 (file)
@@ -2229,7 +2229,7 @@ ClosureType::setup_fn_once_output () const
   rust_assert (trait_item_lang_item_defined);
 
   // resolve to the trait
-  HIR::Item *item = mappings.lookup_defid (trait_id);
+  HIR::Item *item = mappings.lookup_defid (trait_id).value ();
   rust_assert (item->get_item_kind () == HIR::Item::ItemKind::Trait);
   HIR::Trait *trait = static_cast<HIR::Trait *> (item);
 
index cb4e95ae8f0f2d07d65396dc37035265384a1b1c..899f71be49d8441baebdb12fe0ec126654b2300c 100644 (file)
@@ -312,7 +312,7 @@ Mappings::insert_defid_mapping (DefId id, HIR::Item *item)
   CrateNum crate_num = id.crateNum;
   LocalDefId local_def_id = id.localDefId;
 
-  rust_assert (lookup_defid (id) == nullptr);
+  rust_assert (!lookup_defid (id));
   rust_assert (lookup_local_defid (crate_num, local_def_id) == nullptr);
   rust_assert (lookup_trait_item_defid (id) == nullptr);
 
@@ -320,12 +320,12 @@ Mappings::insert_defid_mapping (DefId id, HIR::Item *item)
   insert_local_defid_mapping (crate_num, local_def_id, item);
 }
 
-HIR::Item *
+tl::optional<HIR::Item *>
 Mappings::lookup_defid (DefId id)
 {
   auto it = defIdMappings.find (id);
   if (it == defIdMappings.end ())
-    return nullptr;
+    return tl::nullopt;
 
   return it->second;
 }
@@ -336,7 +336,7 @@ Mappings::insert_defid_mapping (DefId id, HIR::TraitItem *item)
   CrateNum crate_num = id.crateNum;
   LocalDefId local_def_id = id.localDefId;
 
-  rust_assert (lookup_defid (id) == nullptr);
+  rust_assert (!lookup_defid (id));
   rust_assert (lookup_local_defid (crate_num, local_def_id) == nullptr);
   rust_assert (lookup_trait_item_defid (id) == nullptr);
 
index 4ac719ffea76e5aa9f89c7ef1f4e5d0923a72c26..8a927c9ad4faaa0faad7e501687c29b71f2dc863 100644 (file)
@@ -106,7 +106,7 @@ public:
   bool is_local_hirid_crate (HirId crateNum);
 
   void insert_defid_mapping (DefId id, HIR::Item *item);
-  HIR::Item *lookup_defid (DefId id);
+  tl::optional<HIR::Item *> lookup_defid (DefId id);
   void insert_defid_mapping (DefId id, HIR::TraitItem *item);
   HIR::TraitItem *lookup_trait_item_defid (DefId id);