From: Pierre-Emmanuel Patry <pierre-emmanuel.pa...@embecosm.com> Wrap the function's return type with an optional.
gcc/rust/ChangeLog: * typecheck/rust-autoderef.cc: Adapt calling code to new return type. * typecheck/rust-hir-type-check-expr.cc (TypeCheckExpr::visit): Likewise. (TypeCheckExpr::resolve_operator_overload): 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::get_lang_item): Change return type. * util/rust-hir-map.h: Update the function's prototype. Signed-off-by: Pierre-Emmanuel Patry <pierre-emmanuel.pa...@embecosm.com> --- gcc/rust/typecheck/rust-autoderef.cc | 5 +-- .../typecheck/rust-hir-type-check-expr.cc | 42 +++++++------------ gcc/rust/typecheck/rust-tyty-bounds.cc | 6 +-- gcc/rust/typecheck/rust-tyty.cc | 12 ++---- gcc/rust/util/rust-hir-map.cc | 10 ++--- gcc/rust/util/rust-hir-map.h | 7 ++-- 6 files changed, 30 insertions(+), 52 deletions(-) diff --git a/gcc/rust/typecheck/rust-autoderef.cc b/gcc/rust/typecheck/rust-autoderef.cc index 233d5d459d1..7e80b8efddf 100644 --- a/gcc/rust/typecheck/rust-autoderef.cc +++ b/gcc/rust/typecheck/rust-autoderef.cc @@ -129,12 +129,11 @@ resolve_operator_overload_fn ( // look up lang item for arithmetic type std::string associated_item_name = LangItem::ToString (lang_item_type); - DefId respective_lang_item_id = UNKNOWN_DEFID; - bool lang_item_defined - = mappings.lookup_lang_item (lang_item_type, &respective_lang_item_id); + auto lang_item_defined = mappings.lookup_lang_item (lang_item_type); if (!lang_item_defined) return false; + DefId &respective_lang_item_id = lang_item_defined.value (); // we might be in a static or const context and unknown is fine TypeCheckContextItem current_context = TypeCheckContextItem::get_error (); diff --git a/gcc/rust/typecheck/rust-hir-type-check-expr.cc b/gcc/rust/typecheck/rust-hir-type-check-expr.cc index 95e421d8720..7cbcdedbe4d 100644 --- a/gcc/rust/typecheck/rust-hir-type-check-expr.cc +++ b/gcc/rust/typecheck/rust-hir-type-check-expr.cc @@ -628,10 +628,7 @@ TypeCheckExpr::visit (HIR::RangeFromToExpr &expr) { auto lang_item_type = LangItem::Kind::RANGE; - DefId respective_lang_item_id = UNKNOWN_DEFID; - bool lang_item_defined - = mappings.lookup_lang_item (lang_item_type, &respective_lang_item_id); - + auto lang_item_defined = mappings.lookup_lang_item (lang_item_type); // we need to have it maybe if (!lang_item_defined) { @@ -640,6 +637,7 @@ TypeCheckExpr::visit (HIR::RangeFromToExpr &expr) LangItem::ToString (lang_item_type).c_str ()); return; } + DefId respective_lang_item_id = lang_item_defined.value (); // look it up and it _must_ be a struct definition HIR::Item *item = mappings.lookup_defid (respective_lang_item_id).value (); @@ -682,10 +680,7 @@ TypeCheckExpr::visit (HIR::RangeFromExpr &expr) { auto lang_item_type = LangItem::Kind::RANGE_FROM; - DefId respective_lang_item_id = UNKNOWN_DEFID; - bool lang_item_defined - = mappings.lookup_lang_item (lang_item_type, &respective_lang_item_id); - + auto lang_item_defined = mappings.lookup_lang_item (lang_item_type); // we need to have it maybe if (!lang_item_defined) { @@ -694,6 +689,7 @@ TypeCheckExpr::visit (HIR::RangeFromExpr &expr) LangItem::ToString (lang_item_type).c_str ()); return; } + DefId &respective_lang_item_id = lang_item_defined.value (); // look it up and it _must_ be a struct definition HIR::Item *item = mappings.lookup_defid (respective_lang_item_id).value (); @@ -729,10 +725,7 @@ TypeCheckExpr::visit (HIR::RangeToExpr &expr) { auto lang_item_type = LangItem::Kind::RANGE_TO; - DefId respective_lang_item_id = UNKNOWN_DEFID; - bool lang_item_defined - = mappings.lookup_lang_item (lang_item_type, &respective_lang_item_id); - + auto lang_item_defined = mappings.lookup_lang_item (lang_item_type); // we need to have it maybe if (!lang_item_defined) { @@ -742,6 +735,7 @@ TypeCheckExpr::visit (HIR::RangeToExpr &expr) return; } + DefId &respective_lang_item_id = lang_item_defined.value (); // look it up and it _must_ be a struct definition HIR::Item *item = mappings.lookup_defid (respective_lang_item_id).value (); @@ -775,10 +769,7 @@ TypeCheckExpr::visit (HIR::RangeFullExpr &expr) { auto lang_item_type = LangItem::Kind::RANGE_FULL; - DefId respective_lang_item_id = UNKNOWN_DEFID; - bool lang_item_defined - = mappings.lookup_lang_item (lang_item_type, &respective_lang_item_id); - + auto lang_item_defined = mappings.lookup_lang_item (lang_item_type); // we need to have it maybe if (!lang_item_defined) { @@ -787,6 +778,7 @@ TypeCheckExpr::visit (HIR::RangeFullExpr &expr) LangItem::ToString (lang_item_type).c_str ()); return; } + DefId &respective_lang_item_id = lang_item_defined.value (); // look it up and it _must_ be a struct definition HIR::Item *item = mappings.lookup_defid (respective_lang_item_id).value (); @@ -805,10 +797,7 @@ TypeCheckExpr::visit (HIR::RangeFromToInclExpr &expr) { auto lang_item_type = LangItem::Kind::RANGE_INCLUSIVE; - DefId respective_lang_item_id = UNKNOWN_DEFID; - bool lang_item_defined - = mappings.lookup_lang_item (lang_item_type, &respective_lang_item_id); - + auto lang_item_defined = mappings.lookup_lang_item (lang_item_type); // we need to have it maybe if (!lang_item_defined) { @@ -817,6 +806,7 @@ TypeCheckExpr::visit (HIR::RangeFromToInclExpr &expr) LangItem::ToString (lang_item_type).c_str ()); return; } + DefId respective_lang_item_id = lang_item_defined.value (); // look it up and it _must_ be a struct definition HIR::Item *item = mappings.lookup_defid (respective_lang_item_id).value (); @@ -1580,9 +1570,8 @@ TypeCheckExpr::visit (HIR::ClosureExpr &expr) // closure LangItem::Kind lang_item_type = LangItem::Kind::FN_ONCE; - DefId respective_lang_item_id = UNKNOWN_DEFID; - bool lang_item_defined - = mappings.lookup_lang_item (lang_item_type, &respective_lang_item_id); + + auto lang_item_defined = mappings.lookup_lang_item (lang_item_type); if (!lang_item_defined) { // FIXME @@ -1591,7 +1580,7 @@ TypeCheckExpr::visit (HIR::ClosureExpr &expr) rust_fatal_error (expr.get_locus (), "unable to find lang item: %qs", LangItem::ToString (lang_item_type).c_str ()); } - rust_assert (lang_item_defined); + DefId &respective_lang_item_id = lang_item_defined.value (); // these lang items are always traits HIR::Item *item = mappings.lookup_defid (respective_lang_item_id).value (); @@ -1635,13 +1624,12 @@ TypeCheckExpr::resolve_operator_overload (LangItem::Kind lang_item_type, { // look up lang item for arithmetic type std::string associated_item_name = LangItem::ToString (lang_item_type); - DefId respective_lang_item_id = UNKNOWN_DEFID; - bool lang_item_defined - = mappings.lookup_lang_item (lang_item_type, &respective_lang_item_id); + auto lang_item_defined = mappings.lookup_lang_item (lang_item_type); // probe for the lang-item if (!lang_item_defined) return false; + DefId &respective_lang_item_id = lang_item_defined.value (); // we might be in a static or const context and unknown is fine TypeCheckContextItem current_context = TypeCheckContextItem::get_error (); diff --git a/gcc/rust/typecheck/rust-tyty-bounds.cc b/gcc/rust/typecheck/rust-tyty-bounds.cc index 43404385cdd..b73e5929129 100644 --- a/gcc/rust/typecheck/rust-tyty-bounds.cc +++ b/gcc/rust/typecheck/rust-tyty-bounds.cc @@ -152,10 +152,10 @@ TypeBoundsProbe::assemble_sized_builtin () void TypeBoundsProbe::assemble_builtin_candidate (LangItem::Kind lang_item) { - DefId id; - bool found_lang_item = mappings.lookup_lang_item (lang_item, &id); - if (!found_lang_item) + auto lang_item_defined = mappings.lookup_lang_item (lang_item); + if (!lang_item_defined) return; + DefId &id = lang_item_defined.value (); auto defid = mappings.lookup_defid (id); if (!defid) diff --git a/gcc/rust/typecheck/rust-tyty.cc b/gcc/rust/typecheck/rust-tyty.cc index 565f2bc58aa..b877c0c740b 100644 --- a/gcc/rust/typecheck/rust-tyty.cc +++ b/gcc/rust/typecheck/rust-tyty.cc @@ -2218,15 +2218,9 @@ ClosureType::setup_fn_once_output () const auto fn_once_lang_item = LangItem::Kind::FN_ONCE; auto fn_once_output_lang_item = LangItem::Kind::FN_ONCE_OUTPUT; - DefId trait_id = UNKNOWN_DEFID; - bool trait_lang_item_defined - = mappings.lookup_lang_item (fn_once_lang_item, &trait_id); - rust_assert (trait_lang_item_defined); - - DefId trait_item_id = UNKNOWN_DEFID; - bool trait_item_lang_item_defined - = mappings.lookup_lang_item (fn_once_output_lang_item, &trait_item_id); - rust_assert (trait_item_lang_item_defined); + DefId &trait_id = mappings.lookup_lang_item (fn_once_lang_item).value (); + DefId &trait_item_id + = mappings.lookup_lang_item (fn_once_output_lang_item).value (); // resolve to the trait HIR::Item *item = mappings.lookup_defid (trait_id).value (); diff --git a/gcc/rust/util/rust-hir-map.cc b/gcc/rust/util/rust-hir-map.cc index c77ffea354e..4a78ae15525 100644 --- a/gcc/rust/util/rust-hir-map.cc +++ b/gcc/rust/util/rust-hir-map.cc @@ -1253,13 +1253,11 @@ Mappings::lookup_builtin_marker () DefId Mappings::get_lang_item (LangItem::Kind item_type, location_t locus) { - DefId item = UNKNOWN_DEFID; - bool ok = lookup_lang_item (item_type, &item); - if (!ok) - rust_fatal_error (locus, "failed to find lang item %s", - LangItem::ToString (item_type).c_str ()); + if (auto item = lookup_lang_item (item_type)) + return *item; - return item; + rust_fatal_error (locus, "failed to find lang item %s", + LangItem::ToString (item_type).c_str ()); } tl::optional<HIR::TraitItem *> diff --git a/gcc/rust/util/rust-hir-map.h b/gcc/rust/util/rust-hir-map.h index 753e06f7966..3ca980ea3a9 100644 --- a/gcc/rust/util/rust-hir-map.h +++ b/gcc/rust/util/rust-hir-map.h @@ -267,14 +267,13 @@ public: lang_item_mappings[item_type] = id; } - bool lookup_lang_item (LangItem::Kind item_type, DefId *id) + tl::optional<DefId &> lookup_lang_item (LangItem::Kind item_type) { auto it = lang_item_mappings.find (item_type); if (it == lang_item_mappings.end ()) - return false; + return tl::nullopt; - *id = it->second; - return true; + return it->second; } // This will fatal_error when this lang item does not exist -- 2.45.2