https://gcc.gnu.org/g:dcd38375635ff023a11e695fb2d88bdfb9665feb

commit dcd38375635ff023a11e695fb2d88bdfb9665feb
Author: Pierre-Emmanuel Patry <pierre-emmanuel.pa...@embecosm.com>
Date:   Thu Apr 25 14:54:23 2024 +0200

    Change return type of lookup_hir_trait_item
    
    Change the return type to an optional instead of returning a null
    pointer. This allows easier tracking of rogue null pointers in the
    map. This commit also fixes a bug in trait associated function mangling,
    the function was using an already invalidated pointer.
    
    gcc/rust/ChangeLog:
    
            * backend/rust-compile-resolve-path.cc 
(HIRCompileBase::query_compile):
            Adapt return type to new optional.
            * backend/rust-mangle-v0.cc (v0_function_path): Change prototype to 
use
            the generic arguments vector instead of the whole HIR function.
            (v0_path): Fix a bug with a null pointer being used to create the
            trait function mangling.
            * util/rust-hir-map.cc (Mappings::insert_hir_trait_item): Adapt code
            to new return type.
            (Mappings::lookup_hir_trait_item): Change the return type of the
            function to an optional.
            * util/rust-hir-map.h: Update the function's prototype.
    
    Signed-off-by: Pierre-Emmanuel Patry <pierre-emmanuel.pa...@embecosm.com>

Diff:
---
 gcc/rust/backend/rust-compile-resolve-path.cc |  7 ++++---
 gcc/rust/backend/rust-mangle-v0.cc            | 30 +++++++++++++++------------
 gcc/rust/util/rust-hir-map.cc                 |  6 +++---
 gcc/rust/util/rust-hir-map.h                  |  2 +-
 4 files changed, 25 insertions(+), 20 deletions(-)

diff --git a/gcc/rust/backend/rust-compile-resolve-path.cc 
b/gcc/rust/backend/rust-compile-resolve-path.cc
index baef4d67cf79..6ca4d62972cd 100644
--- a/gcc/rust/backend/rust-compile-resolve-path.cc
+++ b/gcc/rust/backend/rust-compile-resolve-path.cc
@@ -256,10 +256,11 @@ HIRCompileBase::query_compile (HirId ref, TyTy::BaseType 
*lookup,
       else
        {
          // it might be resolved to a trait item
-         HIR::TraitItem *trait_item
+         tl::optional<HIR::TraitItem *> trait_item
            = ctx->get_mappings ().lookup_hir_trait_item (ref);
+
          HIR::Trait *trait = ctx->get_mappings ().lookup_trait_item_mapping (
-           trait_item->get_mappings ().get_hirid ());
+           trait_item.value ()->get_mappings ().get_hirid ());
 
          Resolver::TraitReference *trait_ref
            = &Resolver::TraitReference::error_node ();
@@ -285,7 +286,7 @@ HIRCompileBase::query_compile (HirId ref, TyTy::BaseType 
*lookup,
              // this means we are defaulting back to the trait_item if
              // possible
              Resolver::TraitItemReference *trait_item_ref = nullptr;
-             bool ok = trait_ref->lookup_hir_trait_item (*trait_item,
+             bool ok = trait_ref->lookup_hir_trait_item (*trait_item.value (),
                                                          &trait_item_ref);
              rust_assert (ok);                             // found
              rust_assert (trait_item_ref->is_optional ()); // has definition
diff --git a/gcc/rust/backend/rust-mangle-v0.cc 
b/gcc/rust/backend/rust-mangle-v0.cc
index f7a8191e59e0..4c0319afbb60 100644
--- a/gcc/rust/backend/rust-mangle-v0.cc
+++ b/gcc/rust/backend/rust-mangle-v0.cc
@@ -279,16 +279,17 @@ v0_type_path (V0Path path, std::string ident)
 }
 
 static V0Path
-v0_function_path (V0Path path, Rust::Compile::Context *ctx,
-                 const TyTy::BaseType *ty, HIR::Function *fn,
-                 std::string ident)
+v0_function_path (
+  V0Path path, Rust::Compile::Context *ctx, const TyTy::BaseType *ty,
+  const std::vector<std::unique_ptr<HIR::GenericParam>> &generic_params,
+  std::string ident)
 {
   V0Path v0path;
   v0path.prefix = "N";
   v0path.ns = "v";
   v0path.path = path.as_string ();
   v0path.ident = ident;
-  if (!fn->get_generic_params ().empty ())
+  if (!generic_params.empty ())
     {
       v0path.generic_prefix = "I";
       v0path.generic_postfix = v0_generic_args (ctx, ty) + "E";
@@ -386,7 +387,6 @@ v0_path (Rust::Compile::Context *ctx, const TyTy::BaseType 
*ty,
     HirId parent_impl_id = UNKNOWN_HIRID;
     HIR::ImplItem *impl_item
       = mappings.lookup_hir_implitem (hir_id, &parent_impl_id);
-    HIR::TraitItem *trait_item = mappings.lookup_hir_trait_item (hir_id);
     HIR::Expr *expr = mappings.lookup_hir_expr (hir_id);
 
     if (impl_item != nullptr)
@@ -395,8 +395,9 @@ v0_path (Rust::Compile::Context *ctx, const TyTy::BaseType 
*ty,
          {
            case HIR::ImplItem::FUNCTION: {
              HIR::Function *fn = static_cast<HIR::Function *> (impl_item);
-             v0path = v0_function_path (v0path, ctx, ty, fn,
-                                        v0_identifier (seg.get ()));
+             v0path
+               = v0_function_path (v0path, ctx, ty, fn->get_generic_params (),
+                                   v0_identifier (seg.get ()));
            }
            break;
          case HIR::ImplItem::CONSTANT:
@@ -408,13 +409,15 @@ v0_path (Rust::Compile::Context *ctx, const 
TyTy::BaseType *ty,
            break;
          }
       }
-    else if (trait_item != nullptr)
+    else if (auto trait_item = mappings.lookup_hir_trait_item (hir_id))
       {
-       switch (trait_item->get_item_kind ())
+       switch (trait_item.value ()->get_item_kind ())
          {
            case HIR::TraitItem::FUNC: {
-             HIR::Function *fn = static_cast<HIR::Function *> (impl_item);
-             v0path = v0_function_path (v0path, ctx, ty, fn,
+             auto fn = static_cast<HIR::TraitItemFunc *> (*trait_item);
+             rust_unreachable ();
+             v0path = v0_function_path (v0path, ctx, ty,
+                                        fn->get_decl ().get_generic_params (),
                                         v0_identifier (seg.get ()));
            }
            break;
@@ -432,8 +435,9 @@ v0_path (Rust::Compile::Context *ctx, const TyTy::BaseType 
*ty,
        {
          case HIR::Item::ItemKind::Function: {
            HIR::Function *fn = static_cast<HIR::Function *> (*item);
-           v0path = v0_function_path (v0path, ctx, ty, fn,
-                                      v0_identifier (seg.get ()));
+           v0path
+             = v0_function_path (v0path, ctx, ty, fn->get_generic_params (),
+                                 v0_identifier (seg.get ()));
          }
          break;
        case HIR::Item::ItemKind::Module:
diff --git a/gcc/rust/util/rust-hir-map.cc b/gcc/rust/util/rust-hir-map.cc
index da2ca1dcb411..1cc0dd7fb73b 100644
--- a/gcc/rust/util/rust-hir-map.cc
+++ b/gcc/rust/util/rust-hir-map.cc
@@ -406,18 +406,18 @@ void
 Mappings::insert_hir_trait_item (HIR::TraitItem *item)
 {
   auto id = item->get_mappings ().get_hirid ();
-  rust_assert (lookup_hir_trait_item (id) == nullptr);
+  rust_assert (!lookup_hir_trait_item (id).has_value ());
 
   hirTraitItemMappings[id] = item;
   insert_node_to_hir (item->get_mappings ().get_nodeid (), id);
 }
 
-HIR::TraitItem *
+tl::optional<HIR::TraitItem *>
 Mappings::lookup_hir_trait_item (HirId id)
 {
   auto it = hirTraitItemMappings.find (id);
   if (it == hirTraitItemMappings.end ())
-    return nullptr;
+    return tl::nullopt;
 
   return it->second;
 }
diff --git a/gcc/rust/util/rust-hir-map.h b/gcc/rust/util/rust-hir-map.h
index 3589197fa6f7..db404674b4a7 100644
--- a/gcc/rust/util/rust-hir-map.h
+++ b/gcc/rust/util/rust-hir-map.h
@@ -121,7 +121,7 @@ public:
   std::pair<HIR::Enum *, HIR::EnumItem *> lookup_hir_enumitem (HirId id);
 
   void insert_hir_trait_item (HIR::TraitItem *item);
-  HIR::TraitItem *lookup_hir_trait_item (HirId id);
+  tl::optional<HIR::TraitItem *> lookup_hir_trait_item (HirId id);
 
   void insert_hir_extern_block (HIR::ExternBlock *block);
   HIR::ExternBlock *lookup_hir_extern_block (HirId id);

Reply via email to