From b5d74da17466d8f3c79bc2d79292629ed27f40df Mon Sep 17 00:00:00 2001 From: Pierre-Emmanuel Patry Date: Fri, 26 Apr 2024 12:02:53 +0200 Subject: [PATCH] Change return type of lookup_defid 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 (PrivacyReporter::check_for_privacy_violation): 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 --- gcc/rust/backend/rust-compile-base.cc | 7 +++---- .../errors/privacy/rust-privacy-reporter.cc | 3 +-- gcc/rust/typecheck/rust-hir-type-check-base.cc | 3 +-- gcc/rust/typecheck/rust-hir-type-check-expr.cc | 17 ++++++----------- gcc/rust/typecheck/rust-tyty-bounds.cc | 5 +++-- gcc/rust/typecheck/rust-tyty.cc | 2 +- gcc/rust/util/rust-hir-map.cc | 8 ++++---- gcc/rust/util/rust-hir-map.h | 2 +- 8 files changed, 20 insertions(+), 27 deletions(-) diff --git a/gcc/rust/backend/rust-compile-base.cc b/gcc/rust/backend/rust-compile-base.cc index dfd5f9f7348e..06c06479cb05 100644 --- a/gcc/rust/backend/rust-compile-base.cc +++ b/gcc/rust/backend/rust-compile-base.cc @@ -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 diff --git a/gcc/rust/checks/errors/privacy/rust-privacy-reporter.cc b/gcc/rust/checks/errors/privacy/rust-privacy-reporter.cc index 1ce47fda1a9c..c1e217d59987 100644 --- a/gcc/rust/checks/errors/privacy/rust-privacy-reporter.cc +++ b/gcc/rust/checks/errors/privacy/rust-privacy-reporter.cc @@ -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 (); diff --git a/gcc/rust/typecheck/rust-hir-type-check-base.cc b/gcc/rust/typecheck/rust-hir-type-check-base.cc index 4d92f0c2b4fe..82782c68f418 100644 --- a/gcc/rust/typecheck/rust-hir-type-check-base.cc +++ b/gcc/rust/typecheck/rust-hir-type-check-base.cc @@ -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 (item); diff --git a/gcc/rust/typecheck/rust-hir-type-check-expr.cc b/gcc/rust/typecheck/rust-hir-type-check-expr.cc index 57739e560259..2565a57b09c2 100644 --- a/gcc/rust/typecheck/rust-hir-type-check-expr.cc +++ b/gcc/rust/typecheck/rust-hir-type-check-expr.cc @@ -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 (item); diff --git a/gcc/rust/typecheck/rust-tyty-bounds.cc b/gcc/rust/typecheck/rust-tyty-bounds.cc index f1abfa9f5710..03ba622704e4 100644 --- a/gcc/rust/typecheck/rust-tyty-bounds.cc +++ b/gcc/rust/typecheck/rust-tyty-bounds.cc @@ -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 (item); diff --git a/gcc/rust/typecheck/rust-tyty.cc b/gcc/rust/typecheck/rust-tyty.cc index 8d3d3779c0a7..dc4fac956d1c 100644 --- a/gcc/rust/typecheck/rust-tyty.cc +++ b/gcc/rust/typecheck/rust-tyty.cc @@ -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 (item); diff --git a/gcc/rust/util/rust-hir-map.cc b/gcc/rust/util/rust-hir-map.cc index 24c8d618fdfe..64f2f5c2bfdb 100644 --- a/gcc/rust/util/rust-hir-map.cc +++ b/gcc/rust/util/rust-hir-map.cc @@ -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 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); diff --git a/gcc/rust/util/rust-hir-map.h b/gcc/rust/util/rust-hir-map.h index 8da6ea032bd8..6647e5773521 100644 --- a/gcc/rust/util/rust-hir-map.h +++ b/gcc/rust/util/rust-hir-map.h @@ -106,7 +106,7 @@ class Mappings bool is_local_hirid_crate (HirId crateNum); void insert_defid_mapping (DefId id, HIR::Item *item); - HIR::Item *lookup_defid (DefId id); + tl::optional lookup_defid (DefId id); void insert_defid_mapping (DefId id, HIR::TraitItem *item); HIR::TraitItem *lookup_trait_item_defid (DefId id);