]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
Change lookup_hir_implitem return type
authorPierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
Fri, 3 May 2024 14:30:06 +0000 (16:30 +0200)
committerP-E-P <32375388+P-E-P@users.noreply.github.com>
Fri, 17 May 2024 15:28:30 +0000 (15:28 +0000)
Wrap the return type within an optional. Now return the parent id within
a pair instead of taking an out reference.

gcc/rust/ChangeLog:

* backend/rust-compile-item.cc (CompileItem::visit): Change call site
to accept new return type.
* backend/rust-compile-resolve-path.cc (HIRCompileBase::query_compile):
Likewise.
* backend/rust-mangle-v0.cc (v0_path): Likewise.
* checks/errors/rust-unsafe-checker.cc (UnsafeChecker::visit):
Likewise.
* checks/lints/rust-lint-marklive.cc (MarkLive::go): Likewise.
(MarkLive::visit): Likewise.
* typecheck/rust-type-util.cc (query_type): Likewise.
* util/rust-hir-map.cc (Mappings::insert_hir_implitem): Likewise.
(Mappings::lookup_hir_implitem): Change return type.
* util/rust-hir-map.h: Update the function's prototype.

Signed-off-by: Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
gcc/rust/backend/rust-compile-item.cc
gcc/rust/backend/rust-compile-resolve-path.cc
gcc/rust/backend/rust-mangle-v0.cc
gcc/rust/checks/errors/rust-unsafe-checker.cc
gcc/rust/checks/lints/rust-lint-marklive.cc
gcc/rust/typecheck/rust-type-util.cc
gcc/rust/util/rust-hir-map.cc
gcc/rust/util/rust-hir-map.h

index 7f96694b759fbceffd64418730ed00b4a26cbffd..8380d8a857facbc09cb92f593ba9746969d80562 100644 (file)
@@ -152,15 +152,12 @@ CompileItem::visit (HIR::Function &function)
     {
       // if this is part of a trait impl block which is not generic we need to
       // ensure associated types are setup
-      HirId parent_impl_block = UNKNOWN_HIRID;
       HirId id = function.get_mappings ().get_hirid ();
-      HIR::ImplItem *impl_item
-       = ctx->get_mappings ().lookup_hir_implitem (id, &parent_impl_block);
-      if (impl_item != nullptr)
+      if (auto impl_item = ctx->get_mappings ().lookup_hir_implitem (id))
        {
          Resolver::AssociatedImplTrait *impl = nullptr;
          bool found = ctx->get_tyctx ()->lookup_associated_trait_impl (
-           parent_impl_block, &impl);
+           impl_item->second, &impl);
          if (found)
            impl->setup_raw_associated_types ();
        }
index a1188e978443ba0bf63b9cdf40eba8dc51176578..58d9e8e3b100b1692a567dac3de038b9da744047 100644 (file)
@@ -238,18 +238,14 @@ HIRCompileBase::query_compile (HirId ref, TyTy::BaseType *lookup,
            }
        }
 
-      HirId parent_impl_id = UNKNOWN_HIRID;
-      HIR::ImplItem *resolved_item
-       = ctx->get_mappings ().lookup_hir_implitem (ref, &parent_impl_id);
-      bool is_impl_item = resolved_item != nullptr;
-      if (is_impl_item)
+      if (auto resolved_item = ctx->get_mappings ().lookup_hir_implitem (ref))
        {
          if (!lookup->has_substitutions_defined ())
-           return CompileInherentImplItem::Compile (resolved_item, ctx,
+           return CompileInherentImplItem::Compile (resolved_item->first, ctx,
                                                     nullptr, true, expr_locus);
          else
-           return CompileInherentImplItem::Compile (resolved_item, ctx, lookup,
-                                                    true, expr_locus);
+           return CompileInherentImplItem::Compile (resolved_item->first, ctx,
+                                                    lookup, true, expr_locus);
        }
       else
        {
index 4c0319afbb60c53c7bd8fcce12f701fdc3fa80ca..6d9a59ab7931e043ff57064a207147d1fb4eee7c 100644 (file)
@@ -384,17 +384,15 @@ v0_path (Rust::Compile::Context *ctx, const TyTy::BaseType *ty,
 
     auto hir_id = hid.value ();
 
-    HirId parent_impl_id = UNKNOWN_HIRID;
-    HIR::ImplItem *impl_item
-      = mappings.lookup_hir_implitem (hir_id, &parent_impl_id);
     HIR::Expr *expr = mappings.lookup_hir_expr (hir_id);
 
-    if (impl_item != nullptr)
+    if (auto impl_item = mappings.lookup_hir_implitem (hir_id))
       {
-       switch (impl_item->get_impl_item_type ())
+       switch (impl_item->first->get_impl_item_type ())
          {
            case HIR::ImplItem::FUNCTION: {
-             HIR::Function *fn = static_cast<HIR::Function *> (impl_item);
+             HIR::Function *fn
+               = static_cast<HIR::Function *> (impl_item->first);
              v0path
                = v0_function_path (v0path, ctx, ty, fn->get_generic_params (),
                                    v0_identifier (seg.get ()));
index 9e2ffa9819987980662a872545adf0aff8f699cf..56ac2666752966de293012ddfd026a9c7efe1c11 100644 (file)
@@ -449,11 +449,11 @@ UnsafeChecker::visit (MethodCallExpr &expr)
                       &method_type);
 
   auto fn = *static_cast<TyTy::FnType *> (method_type);
-  auto method = mappings.lookup_hir_implitem (fn.get_ref (), nullptr);
 
+  auto method = mappings.lookup_hir_implitem (fn.get_ref ());
   if (!unsafe_context.is_in_context () && method)
-    check_unsafe_call (static_cast<Function *> (method), expr.get_locus (),
-                      "method");
+    check_unsafe_call (static_cast<Function *> (method->first),
+                      expr.get_locus (), "method");
 
   expr.get_receiver ()->accept_vis (*this);
 
index 35e32b57371daa5b8c99c0779eea1b05c2f3a85f..47f5540c02959b0be135f814979127952d4ea480 100644 (file)
@@ -88,17 +88,9 @@ MarkLive::go (HIR::Crate &)
       scannedSymbols.emplace (hirId);
       liveSymbols.emplace (hirId);
       if (auto item = mappings.lookup_hir_item (hirId))
-       {
-         item.value ()->accept_vis (*this);
-       }
-      else
-       { // the item maybe inside a trait impl
-         HirId parent_impl_id = UNKNOWN_HIRID;
-         HIR::ImplItem *implItem
-           = mappings.lookup_hir_implitem (hirId, &parent_impl_id);
-         if (implItem != nullptr)
-           implItem->accept_vis (*this);
-       }
+       item.value ()->accept_vis (*this);
+      else if (auto implItem = mappings.lookup_hir_implitem (hirId))
+       implItem->first->accept_vis (*this);
     }
 }
 
@@ -123,21 +115,10 @@ MarkLive::visit (HIR::PathInExpression &expr)
   auto ref = hid.value ();
 
   // it must resolve to some kind of HIR::Item or HIR::InheritImplItem
-  tl::optional<HIR::Item *> resolved_item = mappings.lookup_hir_item (ref);
-  if (resolved_item)
-    {
-      mark_hir_id (resolved_item.value ()->get_mappings ().get_hirid ());
-    }
-  else
-    {
-      HirId parent_impl_id = UNKNOWN_HIRID;
-      HIR::ImplItem *resolved_item
-       = mappings.lookup_hir_implitem (ref, &parent_impl_id);
-      if (resolved_item != nullptr)
-       {
-         mark_hir_id (resolved_item->get_impl_mappings ().get_hirid ());
-       }
-    }
+  if (auto resolved_item = mappings.lookup_hir_item (ref))
+    mark_hir_id (resolved_item.value ()->get_mappings ().get_hirid ());
+  else if (auto resolved_item = mappings.lookup_hir_implitem (ref))
+    mark_hir_id (resolved_item->first->get_impl_mappings ().get_hirid ());
 }
 
 void
index 8c258041fe1c34a5a12ed96b522bebc3bd4ffcbd..079bd956d3131743962de909cbb5c7cc27465836 100644 (file)
@@ -70,20 +70,16 @@ query_type (HirId reference, TyTy::BaseType **result)
       return true;
     }
 
-  HirId parent_impl_id = UNKNOWN_HIRID;
-  HIR::ImplItem *impl_item
-    = mappings.lookup_hir_implitem (reference, &parent_impl_id);
-  if (impl_item != nullptr)
+  if (auto impl_item = mappings.lookup_hir_implitem (reference))
     {
-      auto impl_block = mappings.lookup_hir_impl_block (parent_impl_id);
-      rust_assert (impl_block);
+      auto impl_block
+       = mappings.lookup_hir_impl_block (impl_item->second).value ();
 
       // found an impl item
-      rust_debug_loc (impl_item->get_locus (), "resolved impl-item {%u} to",
-                     reference);
+      rust_debug_loc (impl_item->first->get_locus (),
+                     "resolved impl-item {%u} to", reference);
 
-      *result
-       = TypeCheckItem::ResolveImplItem (*impl_block.value (), *impl_item);
+      *result = TypeCheckItem::ResolveImplItem (*impl_block, *impl_item->first);
       context->query_completed (reference);
       return true;
     }
index 8bc72e5aac7cf0928d7854eb321bf25e23175553..597c5745ed6c2ce21ebc5812cdf7431e147fa432 100644 (file)
@@ -509,25 +509,21 @@ void
 Mappings::insert_hir_implitem (HirId parent_impl_id, HIR::ImplItem *item)
 {
   auto id = item->get_impl_mappings ().get_hirid ();
-  rust_assert (lookup_hir_implitem (id, nullptr) == nullptr);
+  rust_assert (!lookup_hir_implitem (id));
 
   hirImplItemMappings[id]
     = std::pair<HirId, HIR::ImplItem *> (parent_impl_id, item);
   insert_node_to_hir (item->get_impl_mappings ().get_nodeid (), id);
 }
 
-HIR::ImplItem *
-Mappings::lookup_hir_implitem (HirId id, HirId *parent_impl_id)
+tl::optional<std::pair<HIR::ImplItem *, HirId>>
+Mappings::lookup_hir_implitem (HirId id)
 {
   auto it = hirImplItemMappings.find (id);
   if (it == hirImplItemMappings.end ())
-    return nullptr;
-
-  std::pair<HirId, HIR::ImplItem *> &ref = it->second;
-  if (parent_impl_id != nullptr)
-    *parent_impl_id = ref.first;
+    return tl::nullopt;
 
-  return ref.second;
+  return std::make_pair (it->second.second, it->second.first);
 }
 
 void
index 7f73f7a5a729e76baf5242d2a2a8e35bfaffaf11..ec299767ebb11b5e6e0e24f53bb12684265d729c 100644 (file)
@@ -141,7 +141,9 @@ public:
   tl::optional<HIR::Module *> lookup_module (HirId id);
 
   void insert_hir_implitem (HirId parent_impl_id, HIR::ImplItem *item);
-  HIR::ImplItem *lookup_hir_implitem (HirId id, HirId *parent_impl_id);
+  // Optional<ImpItem, ParentImpl Hir id>
+  tl::optional<std::pair<HIR::ImplItem *, HirId>>
+  lookup_hir_implitem (HirId id);
 
   void insert_hir_expr (HIR::Expr *expr);
   HIR::Expr *lookup_hir_expr (HirId id);
@@ -209,11 +211,7 @@ public:
   void iterate_trait_items (
     std::function<bool (HIR::TraitItem *item, HIR::Trait *)> cb);
 
-  bool is_impl_item (HirId id)
-  {
-    HirId parent_impl_block_id = UNKNOWN_HIRID;
-    return lookup_hir_implitem (id, &parent_impl_block_id) != nullptr;
-  }
+  bool is_impl_item (HirId id) { return lookup_hir_implitem (id).has_value (); }
 
   void insert_trait_item_mapping (HirId trait_item_id, HIR::Trait *trait)
   {