]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
gccrs: Change return type for lookup_hir_item to optional
authorPierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
Thu, 25 Apr 2024 08:48:47 +0000 (10:48 +0200)
committerArthur Cohen <arthur.cohen@embecosm.com>
Mon, 17 Mar 2025 15:35:23 +0000 (16:35 +0100)
gcc/rust/ChangeLog:

* backend/rust-compile-resolve-path.cc (HIRCompileBase::query_compile):
Adapt function call to new return type.
* backend/rust-mangle-v0.cc (v0_path): Likewise.
* checks/errors/rust-const-checker.cc (ConstChecker::check_function_call):
Likewise.
* checks/errors/rust-unsafe-checker.cc (UnsafeChecker::check_use_of_static):
Likewise.
(UnsafeChecker::check_function_call): Likewise.
(UnsafeChecker::check_function_attr): Likewise.
* checks/lints/rust-lint-marklive.cc (MarkLive::go): Likewise.
(MarkLive::visit): Likewise.
* typecheck/rust-hir-trait-resolve.cc (TraitResolver::resolve_path_to_trait):
Likewise.
* typecheck/rust-type-util.cc (query_type): Likewise.
* util/rust-hir-map.cc (Mappings::insert_hir_item): Likewise.
(Mappings::lookup_hir_item): Change function return type to use
optional.
* util/rust-hir-map.h: Update function prototype.

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

index 48c8d4ed5b64f2c4f9e7167a25a863b855a4f86f..1f7dc7d83c110496c48efa68cdd8b74507fbdb2c 100644 (file)
@@ -201,20 +201,18 @@ HIRCompileBase::query_compile (HirId ref, TyTy::BaseType *lookup,
                               const Analysis::NodeMapping &mappings,
                               location_t expr_locus, bool is_qualified_path)
 {
-  HIR::Item *resolved_item = ctx->get_mappings ().lookup_hir_item (ref);
   HirId parent_block;
   HIR::ExternalItem *resolved_extern_item
     = ctx->get_mappings ().lookup_hir_extern_item (ref, &parent_block);
-  bool is_hir_item = resolved_item != nullptr;
   bool is_hir_extern_item = resolved_extern_item != nullptr;
   bool is_fn = lookup->get_kind () == TyTy::TypeKind::FNDEF;
-  if (is_hir_item)
+  if (auto resolved_item = ctx->get_mappings ().lookup_hir_item (ref))
     {
       if (!lookup->has_substitutions_defined ())
-       return CompileItem::compile (resolved_item, ctx, nullptr, true,
+       return CompileItem::compile (*resolved_item, ctx, nullptr, true,
                                     expr_locus);
       else
-       return CompileItem::compile (resolved_item, ctx, lookup, true,
+       return CompileItem::compile (*resolved_item, ctx, lookup, true,
                                     expr_locus);
     }
   else if (is_hir_extern_item)
index b2f11b19fcd3bf22e15d712ef03c825c94de5dc0..9be7b2d2bf4ae12a252963f841dad52404f340ef 100644 (file)
@@ -387,7 +387,6 @@ v0_path (Rust::Compile::Context *ctx, const TyTy::BaseType *ty,
     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::Item *item = mappings.lookup_hir_item (hir_id);
     HIR::Expr *expr = mappings.lookup_hir_expr (hir_id);
 
     if (impl_item != nullptr)
@@ -428,11 +427,11 @@ v0_path (Rust::Compile::Context *ctx, const TyTy::BaseType *ty,
            break;
          }
       }
-    else if (item != nullptr)
-      switch (item->get_item_kind ())
+    else if (auto item = mappings.lookup_hir_item (hir_id))
+      switch (item.value ()->get_item_kind ())
        {
          case HIR::Item::ItemKind::Function: {
-           HIR::Function *fn = static_cast<HIR::Function *> (item);
+           HIR::Function *fn = static_cast<HIR::Function *> (*item);
            v0path = v0_function_path (v0path, ctx, ty, fn,
                                       v0_identifier (seg.get ()));
          }
@@ -453,7 +452,7 @@ v0_path (Rust::Compile::Context *ctx, const TyTy::BaseType *ty,
        case HIR::Item::ItemKind::Impl:
          // Trait impl or inherent impl.
          {
-           HIR::ImplBlock *impl_block = static_cast<HIR::ImplBlock *> (item);
+           HIR::ImplBlock *impl_block = static_cast<HIR::ImplBlock *> (*item);
            v0path = v0_inherent_or_trait_impl_path (ctx, impl_block);
          }
          break;
index 0a9923975245bb0e15d5faa2ac0b145715bce64d..1bb2c37d7eb281a4e0cece4081bb3db4dccfbe5c 100644 (file)
@@ -308,7 +308,8 @@ ConstChecker::check_function_call (HirId fn_id, location_t locus)
     return;
 
   auto maybe_fn = mappings.lookup_hir_item (fn_id);
-  if (maybe_fn && maybe_fn->get_item_kind () != Item::ItemKind::Function)
+  if (maybe_fn
+      && maybe_fn.value ()->get_item_kind () != Item::ItemKind::Function)
     return;
 
   // There are const extern functions (intrinsics)
@@ -325,7 +326,7 @@ ConstChecker::check_function_call (HirId fn_id, location_t locus)
   auto is_error = false;
   if (maybe_fn)
     {
-      auto fn = static_cast<Function *> (maybe_fn);
+      auto fn = static_cast<Function *> (*maybe_fn);
       if (!fn->get_qualifiers ().is_const ())
        is_error = true;
     }
index 92cf4769024689cd43729f2a3c37a8bc57777fb9..18e79e3ce6594a673bf0aacb48a6ea19571a4011 100644 (file)
@@ -70,14 +70,12 @@ UnsafeChecker::check_use_of_static (HirId node_id, location_t locus)
   if (unsafe_context.is_in_context ())
     return;
 
-  auto maybe_static_mut = mappings.lookup_hir_item (node_id);
-
   HirId extern_block;
   auto maybe_extern_static
     = mappings.lookup_hir_extern_item (node_id, &extern_block);
 
-  if (maybe_static_mut)
-    check_static_mut (maybe_static_mut, locus);
+  if (auto maybe_static_mut = mappings.lookup_hir_item (node_id))
+    check_static_mut (*maybe_static_mut, locus);
 
   if (maybe_extern_static)
     check_extern_static (static_cast<ExternalItem *> (maybe_extern_static),
@@ -173,8 +171,9 @@ UnsafeChecker::check_function_call (HirId node_id, location_t locus)
   auto maybe_extern
     = mappings.lookup_hir_extern_item (node_id, &parent_extern_block);
 
-  if (maybe_fn && maybe_fn->get_item_kind () == Item::ItemKind::Function)
-    check_unsafe_call (static_cast<Function *> (maybe_fn), locus, "function");
+  if (maybe_fn
+      && maybe_fn.value ()->get_item_kind () == Item::ItemKind::Function)
+    check_unsafe_call (static_cast<Function *> (*maybe_fn), locus, "function");
 
   if (maybe_extern)
     check_extern_call (static_cast<ExternalItem *> (maybe_extern),
@@ -204,8 +203,9 @@ UnsafeChecker::check_function_attr (HirId node_id, location_t locus)
 
   auto maybe_fn = mappings.lookup_hir_item (node_id);
 
-  if (maybe_fn && maybe_fn->get_item_kind () == Item::ItemKind::Function)
-    check_target_attr (static_cast<Function *> (maybe_fn), locus);
+  if (maybe_fn
+      && maybe_fn.value ()->get_item_kind () == Item::ItemKind::Function)
+    check_target_attr (static_cast<Function *> (*maybe_fn), locus);
 }
 
 void
index 815fcf595b33c9493337210ee66c2a30885c2634..f64e8b59462efb412c16486ecc3b0052451b8522 100644 (file)
@@ -86,11 +86,10 @@ MarkLive::go (HIR::Crate &)
       HirId hirId = worklist.back ();
       worklist.pop_back ();
       scannedSymbols.emplace (hirId);
-      HIR::Item *item = mappings.lookup_hir_item (hirId);
       liveSymbols.emplace (hirId);
-      if (item != nullptr)
+      if (auto item = mappings.lookup_hir_item (hirId))
        {
-         item->accept_vis (*this);
+         item.value ()->accept_vis (*this);
        }
       else
        { // the item maybe inside a trait impl
@@ -124,10 +123,10 @@ MarkLive::visit (HIR::PathInExpression &expr)
   auto ref = hid.value ();
 
   // it must resolve to some kind of HIR::Item or HIR::InheritImplItem
-  HIR::Item *resolved_item = mappings.lookup_hir_item (ref);
-  if (resolved_item != nullptr)
+  tl::optional<HIR::Item *> resolved_item = mappings.lookup_hir_item (ref);
+  if (resolved_item)
     {
-      mark_hir_id (resolved_item->get_mappings ().get_hirid ());
+      mark_hir_id (resolved_item.value ()->get_mappings ().get_hirid ());
     }
   else
     {
index b8075ee9c505182c3fb9b986497f7219f7d46445..51a64174ea4d9f2f1efc942103fe94b7c7134be2 100644 (file)
@@ -119,11 +119,12 @@ TraitResolver::resolve_path_to_trait (const HIR::TypePath &path,
 
   if (auto hid = mappings.lookup_node_to_hir (ref))
     {
-      HIR::Item *resolved_item = mappings.lookup_hir_item (hid.value ());
-      rust_assert (resolved_item != nullptr);
-      rust_assert (resolved_item->get_item_kind ()
+      tl::optional<HIR::Item *> resolved_item
+       = mappings.lookup_hir_item (hid.value ());
+      rust_assert (resolved_item.has_value ());
+      rust_assert (resolved_item.value ()->get_item_kind ()
                   == HIR::Item::ItemKind::Trait);
-      *resolved = static_cast<HIR::Trait *> (resolved_item);
+      *resolved = static_cast<HIR::Trait *> (*resolved_item);
 
       return true;
     }
index 914e40e92b8a79fff96fe89261218df96c9adc5a..f6f8c8e9e148334857f2bf41028097ec51ba536f 100644 (file)
@@ -61,11 +61,11 @@ query_type (HirId reference, TyTy::BaseType **result)
       return true;
     }
 
-  HIR::Item *item = mappings.lookup_hir_item (reference);
-  if (item != nullptr)
+  if (auto item = mappings.lookup_hir_item (reference))
     {
-      rust_debug_loc (item->get_locus (), "resolved item {%u} to", reference);
-      *result = TypeCheckItem::Resolve (*item);
+      rust_debug_loc (item.value ()->get_locus (), "resolved item {%u} to",
+                     reference);
+      *result = TypeCheckItem::Resolve (*item.value ());
       context->query_completed (reference);
       return true;
     }
index 57a766758d9a53f04f5d7b9edd558c7c6ba43d0c..535353bfc55831c3e59ac02dc583ff90078a8bcc 100644 (file)
@@ -366,19 +366,18 @@ void
 Mappings::insert_hir_item (HIR::Item *item)
 {
   auto id = item->get_mappings ().get_hirid ();
-  rust_assert (lookup_hir_item (id) == nullptr);
+  rust_assert (!lookup_hir_item (id).has_value ());
 
   hirItemMappings[id] = item;
   insert_node_to_hir (item->get_mappings ().get_nodeid (), id);
 }
 
-HIR::Item *
+tl::optional<HIR::Item *>
 Mappings::lookup_hir_item (HirId id)
 {
   auto it = hirItemMappings.find (id);
   if (it == hirItemMappings.end ())
-    return nullptr;
-
+    return tl::nullopt;
   return it->second;
 }
 
index 6ed3beee8fc45984cf6d5ccdb2e9fe7c0bbe6124..e6df05bd1a330e20c37d66de6c30ee0a69290aad 100644 (file)
@@ -115,7 +115,7 @@ public:
   HIR::Item *lookup_local_defid (CrateNum crateNum, LocalDefId id);
 
   void insert_hir_item (HIR::Item *item);
-  HIR::Item *lookup_hir_item (HirId id);
+  tl::optional<HIR::Item *> lookup_hir_item (HirId id);
 
   void insert_hir_enumitem (HIR::Enum *parent, HIR::EnumItem *item);
   std::pair<HIR::Enum *, HIR::EnumItem *> lookup_hir_enumitem (HirId id);