https://gcc.gnu.org/g:196890daf200cb05ae5fa9f3b439b15fb0f1edda
commit r16-4803-g196890daf200cb05ae5fa9f3b439b15fb0f1edda Author: Pierre-Emmanuel Patry <[email protected]> Date: Tue Aug 19 17:06:38 2025 +0200 gccrs: Convert lookup return type to optional Remove usage of error state (but not error state itelf) and use an optional to convey the missing value meaning instead. gcc/rust/ChangeLog: * backend/rust-compile-expr.cc (CompileExpr::generate_closure_fntype): Unwrap the optional. * backend/rust-compile.cc: Change return type container. Adapt code to new return type. * typecheck/rust-hir-dot-operator.cc: Likewise. * typecheck/rust-hir-path-probe.cc: Likewise. * typecheck/rust-hir-type-check-implitem.cc (TypeCheckImplItemWithTrait::visit): Likewise. * typecheck/rust-hir-type-check-type.cc (TypeCheckType::visit): Likewise. * typecheck/rust-tyty-bounds.cc (TypeBoundPredicate::contains_item): Likewise. (TypeBoundPredicate::lookup_associated_item): Likewise. (TypeBoundPredicateItem::get_parent): Likewise. (TypeBoundPredicate::lookup_associated_type): Likewise. * typecheck/rust-tyty.cc (BaseType::satisfies_bound): Likewise. * typecheck/rust-hir-type-check-path.cc (TypeCheckExpr::visit): Change return type. * typecheck/rust-tyty.h: Likewise. Signed-off-by: Pierre-Emmanuel Patry <[email protected]> Diff: --- gcc/rust/backend/rust-compile-expr.cc | 6 ++-- gcc/rust/backend/rust-compile.cc | 7 +++-- gcc/rust/typecheck/rust-hir-dot-operator.cc | 8 +++--- gcc/rust/typecheck/rust-hir-path-probe.cc | 12 ++++---- gcc/rust/typecheck/rust-hir-type-check-implitem.cc | 12 ++++---- gcc/rust/typecheck/rust-hir-type-check-path.cc | 8 +++--- gcc/rust/typecheck/rust-hir-type-check-type.cc | 6 ++-- gcc/rust/typecheck/rust-tyty-bounds.cc | 32 ++++++++++++---------- gcc/rust/typecheck/rust-tyty.cc | 6 ++-- gcc/rust/typecheck/rust-tyty.h | 4 +-- 10 files changed, 53 insertions(+), 48 deletions(-) diff --git a/gcc/rust/backend/rust-compile-expr.cc b/gcc/rust/backend/rust-compile-expr.cc index 64339236f89a..d059eade2caf 100644 --- a/gcc/rust/backend/rust-compile-expr.cc +++ b/gcc/rust/backend/rust-compile-expr.cc @@ -2617,15 +2617,15 @@ CompileExpr::generate_closure_fntype (HIR::ClosureExpr &expr, TyTy::TypeBoundPredicateItem item = TyTy::TypeBoundPredicateItem::error (); if (predicate.get_name ().compare ("FnOnce") == 0) { - item = predicate.lookup_associated_item ("call_once"); + item = predicate.lookup_associated_item ("call_once").value (); } else if (predicate.get_name ().compare ("FnMut") == 0) { - item = predicate.lookup_associated_item ("call_mut"); + item = predicate.lookup_associated_item ("call_mut").value (); } else if (predicate.get_name ().compare ("Fn") == 0) { - item = predicate.lookup_associated_item ("call"); + item = predicate.lookup_associated_item ("call").value (); } else { diff --git a/gcc/rust/backend/rust-compile.cc b/gcc/rust/backend/rust-compile.cc index dbd85158aef8..a3e047a2dab1 100644 --- a/gcc/rust/backend/rust-compile.cc +++ b/gcc/rust/backend/rust-compile.cc @@ -241,12 +241,13 @@ HIRCompileBase::compute_address_for_trait_item ( &receiver_bounds, const TyTy::BaseType *receiver, const TyTy::BaseType *root, location_t locus) { - TyTy::TypeBoundPredicateItem predicate_item + tl::optional<TyTy::TypeBoundPredicateItem> predicate_item = predicate->lookup_associated_item (ref->get_identifier ()); - rust_assert (!predicate_item.is_error ()); + rust_assert (predicate_item.has_value ()); // This is the expected end type - TyTy::BaseType *trait_item_type = predicate_item.get_tyty_for_receiver (root); + TyTy::BaseType *trait_item_type + = predicate_item->get_tyty_for_receiver (root); rust_assert (trait_item_type->get_kind () == TyTy::TypeKind::FNDEF); TyTy::FnType *trait_item_fntype = static_cast<TyTy::FnType *> (trait_item_type); diff --git a/gcc/rust/typecheck/rust-hir-dot-operator.cc b/gcc/rust/typecheck/rust-hir-dot-operator.cc index 29919aaaec80..296cc440b7f5 100644 --- a/gcc/rust/typecheck/rust-hir-dot-operator.cc +++ b/gcc/rust/typecheck/rust-hir-dot-operator.cc @@ -471,17 +471,17 @@ MethodResolver::get_predicate_items ( std::vector<predicate_candidate> predicate_items; for (auto &bound : specified_bounds) { - TyTy::TypeBoundPredicateItem lookup + tl::optional<TyTy::TypeBoundPredicateItem> lookup = bound.lookup_associated_item (segment_name.as_string ()); - if (lookup.is_error ()) + if (!lookup.has_value ()) continue; - TyTy::BaseType *ty = lookup.get_tyty_for_receiver (&receiver); + TyTy::BaseType *ty = lookup->get_tyty_for_receiver (&receiver); if (ty->get_kind () == TyTy::TypeKind::FNDEF) { TyTy::FnType *fnty = static_cast<TyTy::FnType *> (ty); if (fnty->is_method ()) - predicate_items.emplace_back (lookup, fnty); + predicate_items.emplace_back (lookup.value (), fnty); } } diff --git a/gcc/rust/typecheck/rust-hir-path-probe.cc b/gcc/rust/typecheck/rust-hir-path-probe.cc index 6ed6e25858f9..bd42e906b4d8 100644 --- a/gcc/rust/typecheck/rust-hir-path-probe.cc +++ b/gcc/rust/typecheck/rust-hir-path-probe.cc @@ -367,15 +367,15 @@ PathProbeType::process_predicate_for_candidates ( { const TraitReference *trait_ref = predicate.get (); - TyTy::TypeBoundPredicateItem item + tl::optional<TyTy::TypeBoundPredicateItem> item = predicate.lookup_associated_item (search.as_string ()); - if (item.is_error ()) + if (!item.has_value ()) return; - if (ignore_mandatory_trait_items && item.needs_implementation ()) + if (ignore_mandatory_trait_items && item->needs_implementation ()) return; - const TraitItemReference *trait_item_ref = item.get_raw_item (); + const TraitItemReference *trait_item_ref = item->get_raw_item (); PathProbeCandidate::CandidateType candidate_type; switch (trait_item_ref->get_trait_item_type ()) { @@ -395,9 +395,9 @@ PathProbeType::process_predicate_for_candidates ( break; } - TyTy::BaseType *trait_item_tyty = item.get_raw_item ()->get_tyty (); + TyTy::BaseType *trait_item_tyty = item->get_raw_item ()->get_tyty (); if (receiver->get_kind () != TyTy::DYNAMIC) - trait_item_tyty = item.get_tyty_for_receiver (receiver); + trait_item_tyty = item->get_tyty_for_receiver (receiver); PathProbeCandidate::TraitItemCandidate trait_item_candidate{trait_ref, trait_item_ref, diff --git a/gcc/rust/typecheck/rust-hir-type-check-implitem.cc b/gcc/rust/typecheck/rust-hir-type-check-implitem.cc index 077a228e52b1..e7f66327d02d 100644 --- a/gcc/rust/typecheck/rust-hir-type-check-implitem.cc +++ b/gcc/rust/typecheck/rust-hir-type-check-implitem.cc @@ -469,8 +469,8 @@ TypeCheckImplItemWithTrait::visit (HIR::ConstantItem &constant) } // get the item from the predicate - resolved_trait_item = trait_reference.lookup_associated_item (raw_trait_item); - rust_assert (!resolved_trait_item.is_error ()); + resolved_trait_item + = trait_reference.lookup_associated_item (raw_trait_item).value (); // merge the attributes const HIR::TraitItem *hir_trait_item @@ -519,8 +519,8 @@ TypeCheckImplItemWithTrait::visit (HIR::TypeAlias &type) } // get the item from the predicate - resolved_trait_item = trait_reference.lookup_associated_item (raw_trait_item); - rust_assert (!resolved_trait_item.is_error ()); + resolved_trait_item + = trait_reference.lookup_associated_item (raw_trait_item).value (); // merge the attributes const HIR::TraitItem *hir_trait_item @@ -578,8 +578,8 @@ TypeCheckImplItemWithTrait::visit (HIR::Function &function) } // get the item from the predicate - resolved_trait_item = trait_reference.lookup_associated_item (raw_trait_item); - rust_assert (!resolved_trait_item.is_error ()); + resolved_trait_item + = trait_reference.lookup_associated_item (raw_trait_item).value (); // merge the attributes const HIR::TraitItem *hir_trait_item diff --git a/gcc/rust/typecheck/rust-hir-type-check-path.cc b/gcc/rust/typecheck/rust-hir-type-check-path.cc index cc5c412f9868..3068f0c6e357 100644 --- a/gcc/rust/typecheck/rust-hir-type-check-path.cc +++ b/gcc/rust/typecheck/rust-hir-type-check-path.cc @@ -77,9 +77,9 @@ TypeCheckExpr::visit (HIR::QualifiedPathInExpression &expr) // lookup the associated item from the specified bound HIR::PathExprSegment &item_seg = expr.get_segments ().at (0); HIR::PathIdentSegment item_seg_identifier = item_seg.get_segment (); - TyTy::TypeBoundPredicateItem item + tl::optional<TyTy::TypeBoundPredicateItem> item = specified_bound.lookup_associated_item (item_seg_identifier.as_string ()); - if (item.is_error ()) + if (!item.has_value ()) { rust_error_at (item_seg.get_locus (), "unknown associated item"); return; @@ -116,9 +116,9 @@ TypeCheckExpr::visit (HIR::QualifiedPathInExpression &expr) // and we dont need to worry if the trait item is actually implemented or // not because this will have already been validated as part of the trait // impl block - infered = item.get_tyty_for_receiver (root); + infered = item->get_tyty_for_receiver (root); root_resolved_node_id - = item.get_raw_item ()->get_mappings ().get_nodeid (); + = item->get_raw_item ()->get_mappings ().get_nodeid (); } else { diff --git a/gcc/rust/typecheck/rust-hir-type-check-type.cc b/gcc/rust/typecheck/rust-hir-type-check-type.cc index 4a6c703d764b..39772b42a0db 100644 --- a/gcc/rust/typecheck/rust-hir-type-check-type.cc +++ b/gcc/rust/typecheck/rust-hir-type-check-type.cc @@ -211,9 +211,9 @@ TypeCheckType::visit (HIR::QualifiedPathInType &path) // lookup the associated item from the specified bound HIR::TypePathSegment &item_seg = path.get_associated_segment (); HIR::PathIdentSegment item_seg_identifier = item_seg.get_ident_segment (); - TyTy::TypeBoundPredicateItem item + tl::optional<TyTy::TypeBoundPredicateItem> item = specified_bound.lookup_associated_item (item_seg_identifier.as_string ()); - if (item.is_error ()) + if (!item.has_value ()) { std::string item_seg_ident_name, rich_msg; item_seg_ident_name = qual_path_type.get_trait ().as_string (); @@ -265,7 +265,7 @@ TypeCheckType::visit (HIR::QualifiedPathInType &path) // and we dont need to worry if the trait item is actually implemented or // not because this will have already been validated as part of the trait // impl block - translated = item.get_tyty_for_receiver (root); + translated = item->get_tyty_for_receiver (root); } else { diff --git a/gcc/rust/typecheck/rust-tyty-bounds.cc b/gcc/rust/typecheck/rust-tyty-bounds.cc index 91de35976b66..bb2e6d4d708f 100644 --- a/gcc/rust/typecheck/rust-tyty-bounds.cc +++ b/gcc/rust/typecheck/rust-tyty-bounds.cc @@ -575,10 +575,13 @@ TypeBoundPredicate::apply_argument_mappings ( std::string identifier = it.first; TyTy::BaseType *type = it.second; - TypeBoundPredicateItem item = lookup_associated_item (identifier); - rust_assert (!item.is_error ()); + tl::optional<TypeBoundPredicateItem> item + = lookup_associated_item (identifier); - const auto item_ref = item.get_raw_item (); + if (!item.has_value ()) + continue; + + const auto item_ref = item->get_raw_item (); item_ref->associated_type_set (type); } @@ -599,7 +602,7 @@ TypeBoundPredicate::contains_item (const std::string &search) const return trait_ref->lookup_trait_item (search, &trait_item_ref); } -TypeBoundPredicateItem +tl::optional<TypeBoundPredicateItem> TypeBoundPredicate::lookup_associated_item (const std::string &search) const { auto trait_ref = get (); @@ -611,11 +614,11 @@ TypeBoundPredicate::lookup_associated_item (const std::string &search) const for (auto &super_trait : super_traits) { auto lookup = super_trait.lookup_associated_item (search); - if (!lookup.is_error ()) + if (lookup.has_value ()) return lookup; } - return TypeBoundPredicateItem::error (); + return tl::nullopt; } TypeBoundPredicateItem::TypeBoundPredicateItem ( @@ -656,7 +659,7 @@ TypeBoundPredicateItem::get_parent () const return &parent; } -TypeBoundPredicateItem +tl::optional<TypeBoundPredicateItem> TypeBoundPredicate::lookup_associated_item ( const Resolver::TraitItemReference *ref) const { @@ -732,10 +735,11 @@ TypeBoundPredicate::handle_substitions ( std::string identifier = it.first; TyTy::BaseType *type = it.second; - TypeBoundPredicateItem item = lookup_associated_item (identifier); - if (!item.is_error ()) + tl::optional<TypeBoundPredicateItem> item + = lookup_associated_item (identifier); + if (item.has_value ()) { - const auto item_ref = item.get_raw_item (); + const auto item_ref = item->get_raw_item (); item_ref->associated_type_set (type); } } @@ -799,18 +803,18 @@ TypeBoundPredicate::get_trait_hierachy ( TypeBoundPredicateItem TypeBoundPredicate::lookup_associated_type (const std::string &search) { - TypeBoundPredicateItem item = lookup_associated_item (search); + tl::optional<TypeBoundPredicateItem> item = lookup_associated_item (search); // only need to check that it is infact an associated type because other // wise if it was not found it will just be an error node anyway - if (!item.is_error ()) + if (item.has_value ()) { - const auto raw = item.get_raw_item (); + const auto raw = item->get_raw_item (); if (raw->get_trait_item_type () != Resolver::TraitItemReference::TraitItemType::TYPE) return TypeBoundPredicateItem::error (); } - return item; + return item.value (); } std::vector<TypeBoundPredicateItem> diff --git a/gcc/rust/typecheck/rust-tyty.cc b/gcc/rust/typecheck/rust-tyty.cc index af5f9777fd41..95e9bc2c17f7 100644 --- a/gcc/rust/typecheck/rust-tyty.cc +++ b/gcc/rust/typecheck/rust-tyty.cc @@ -351,12 +351,12 @@ BaseType::satisfies_bound (const TypeBoundPredicate &predicate, return false; std::string item_name = item->get_impl_item_name (); - TypeBoundPredicateItem lookup + tl::optional<TypeBoundPredicateItem> lookup = predicate.lookup_associated_item (item_name); - if (lookup.is_error ()) + if (!lookup.has_value ()) return false; - const auto *item_ref = lookup.get_raw_item (); + const auto *item_ref = lookup->get_raw_item (); TyTy::BaseType *bound_ty = item_ref->get_tyty (); // compare the types diff --git a/gcc/rust/typecheck/rust-tyty.h b/gcc/rust/typecheck/rust-tyty.h index 49415ea0b31a..b90bc83e8bf3 100644 --- a/gcc/rust/typecheck/rust-tyty.h +++ b/gcc/rust/typecheck/rust-tyty.h @@ -617,10 +617,10 @@ public: bool contains_item (const std::string &search) const; - TypeBoundPredicateItem + tl::optional<TypeBoundPredicateItem> lookup_associated_item (const std::string &search) const; - TypeBoundPredicateItem + tl::optional<TypeBoundPredicateItem> lookup_associated_item (const Resolver::TraitItemReference *ref) const; // WARNING THIS WILL ALWAYS RETURN NULLPTR
