https://gcc.gnu.org/g:69e1835807dac79b426eb357e87191502d979657
commit 69e1835807dac79b426eb357e87191502d979657 Author: Pierre-Emmanuel Patry <pierre-emmanuel.pa...@embecosm.com> Date: Thu Apr 25 10:48:47 2024 +0200 Change return type for lookup_hir_item to optional 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.pa...@embecosm.com> Diff: --- gcc/rust/backend/rust-compile-resolve-path.cc | 8 +++----- gcc/rust/backend/rust-mangle-v0.cc | 9 ++++----- gcc/rust/checks/errors/rust-const-checker.cc | 5 +++-- gcc/rust/checks/errors/rust-unsafe-checker.cc | 16 ++++++++-------- gcc/rust/checks/lints/rust-lint-marklive.cc | 11 +++++------ gcc/rust/typecheck/rust-hir-trait-resolve.cc | 9 +++++---- gcc/rust/typecheck/rust-type-util.cc | 8 ++++---- gcc/rust/util/rust-hir-map.cc | 7 +++---- gcc/rust/util/rust-hir-map.h | 2 +- 9 files changed, 36 insertions(+), 39 deletions(-) diff --git a/gcc/rust/backend/rust-compile-resolve-path.cc b/gcc/rust/backend/rust-compile-resolve-path.cc index c9a27782bf89..baef4d67cf79 100644 --- a/gcc/rust/backend/rust-compile-resolve-path.cc +++ b/gcc/rust/backend/rust-compile-resolve-path.cc @@ -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) diff --git a/gcc/rust/backend/rust-mangle-v0.cc b/gcc/rust/backend/rust-mangle-v0.cc index bb2b0d40b170..f7a8191e59e0 100644 --- a/gcc/rust/backend/rust-mangle-v0.cc +++ b/gcc/rust/backend/rust-mangle-v0.cc @@ -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; diff --git a/gcc/rust/checks/errors/rust-const-checker.cc b/gcc/rust/checks/errors/rust-const-checker.cc index 18864fdeb5a5..7e881abc9fcc 100644 --- a/gcc/rust/checks/errors/rust-const-checker.cc +++ b/gcc/rust/checks/errors/rust-const-checker.cc @@ -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; } diff --git a/gcc/rust/checks/errors/rust-unsafe-checker.cc b/gcc/rust/checks/errors/rust-unsafe-checker.cc index ee3e52224fb3..6b3b4a2ac113 100644 --- a/gcc/rust/checks/errors/rust-unsafe-checker.cc +++ b/gcc/rust/checks/errors/rust-unsafe-checker.cc @@ -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 diff --git a/gcc/rust/checks/lints/rust-lint-marklive.cc b/gcc/rust/checks/lints/rust-lint-marklive.cc index c5b536425faa..35e32b57371d 100644 --- a/gcc/rust/checks/lints/rust-lint-marklive.cc +++ b/gcc/rust/checks/lints/rust-lint-marklive.cc @@ -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 { diff --git a/gcc/rust/typecheck/rust-hir-trait-resolve.cc b/gcc/rust/typecheck/rust-hir-trait-resolve.cc index c09cc9606e36..642bf2706fb6 100644 --- a/gcc/rust/typecheck/rust-hir-trait-resolve.cc +++ b/gcc/rust/typecheck/rust-hir-trait-resolve.cc @@ -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; } diff --git a/gcc/rust/typecheck/rust-type-util.cc b/gcc/rust/typecheck/rust-type-util.cc index 05bf7aeb5c7d..e96f5ed9359b 100644 --- a/gcc/rust/typecheck/rust-type-util.cc +++ b/gcc/rust/typecheck/rust-type-util.cc @@ -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; } diff --git a/gcc/rust/util/rust-hir-map.cc b/gcc/rust/util/rust-hir-map.cc index 6d9777136c9a..da2ca1dcb411 100644 --- a/gcc/rust/util/rust-hir-map.cc +++ b/gcc/rust/util/rust-hir-map.cc @@ -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; } diff --git a/gcc/rust/util/rust-hir-map.h b/gcc/rust/util/rust-hir-map.h index 039ace51eb08..3589197fa6f7 100644 --- a/gcc/rust/util/rust-hir-map.h +++ b/gcc/rust/util/rust-hir-map.h @@ -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);