diff options
author | Philip Herron <philip.herron@embecosm.com> | 2021-10-21 22:31:21 +0100 |
---|---|---|
committer | Philip Herron <philip.herron@embecosm.com> | 2021-10-22 11:48:42 +0100 |
commit | f3bd3f0dd8d145469598dcccc597ae69500369ae (patch) | |
tree | 545497533edcc1850207926f291d73b9937822bb | |
parent | b3b37e2c5cda1eace7acf9dad6eb6568a90a4ea4 (diff) | |
download | gcc-f3bd3f0dd8d145469598dcccc597ae69500369ae.zip gcc-f3bd3f0dd8d145469598dcccc597ae69500369ae.tar.gz gcc-f3bd3f0dd8d145469598dcccc597ae69500369ae.tar.bz2 |
Decouple the loop from processing a potential candidate in a path
Code cleanup to extract the body out of the loop to make the code more
testable and reuseable.
-rw-r--r-- | gcc/rust/typecheck/rust-hir-path-probe.h | 155 |
1 files changed, 75 insertions, 80 deletions
diff --git a/gcc/rust/typecheck/rust-hir-path-probe.h b/gcc/rust/typecheck/rust-hir-path-probe.h index 589b0d6..326adb4 100644 --- a/gcc/rust/typecheck/rust-hir-path-probe.h +++ b/gcc/rust/typecheck/rust-hir-path-probe.h @@ -146,8 +146,14 @@ public: std::vector<std::pair<const TraitReference *, HIR::ImplBlock *>> union_type_bounds = probe.union_bounds (probed_bounds, specified_bounds); - probe.process_traits_for_candidates (union_type_bounds, - ignore_mandatory_trait_items); + for (auto &candidate : union_type_bounds) + { + const TraitReference *trait_ref = candidate.first; + HIR::ImplBlock *impl = candidate.second; + probe.process_associated_trait_for_candidates ( + trait_ref, impl, ignore_mandatory_trait_items); + } + return probe.candidates; } @@ -234,96 +240,85 @@ protected: item->accept_vis (*this); } - void process_traits_for_candidates ( - const std::vector<std::pair<const TraitReference *, HIR::ImplBlock *>> - traits, - bool ignore_mandatory_trait_items) + void + process_associated_trait_for_candidates (const TraitReference *trait_ref, + HIR::ImplBlock *impl, + bool ignore_mandatory_trait_items) { - for (auto &ref : traits) - { - const TraitReference *trait_ref = ref.first; - HIR::ImplBlock *impl = ref.second; - - const TraitItemReference *trait_item_ref = nullptr; - if (!trait_ref->lookup_trait_item (search.as_string (), - &trait_item_ref)) - continue; - - bool trait_item_needs_implementation = !trait_item_ref->is_optional (); - if (ignore_mandatory_trait_items && trait_item_needs_implementation) - continue; + const TraitItemReference *trait_item_ref = nullptr; + if (!trait_ref->lookup_trait_item (search.as_string (), &trait_item_ref)) + return; - PathProbeCandidate::CandidateType candidate_type; - switch (trait_item_ref->get_trait_item_type ()) - { - case TraitItemReference::TraitItemType::FN: - candidate_type = PathProbeCandidate::CandidateType::TRAIT_FUNC; - break; - case TraitItemReference::TraitItemType::CONST: - candidate_type - = PathProbeCandidate::CandidateType::TRAIT_ITEM_CONST; - break; - case TraitItemReference::TraitItemType::TYPE: - candidate_type - = PathProbeCandidate::CandidateType::TRAIT_TYPE_ALIAS; - break; + bool trait_item_needs_implementation = !trait_item_ref->is_optional (); + if (ignore_mandatory_trait_items && trait_item_needs_implementation) + return; - case TraitItemReference::TraitItemType::ERROR: - default: - gcc_unreachable (); - break; - } + PathProbeCandidate::CandidateType candidate_type; + switch (trait_item_ref->get_trait_item_type ()) + { + case TraitItemReference::TraitItemType::FN: + candidate_type = PathProbeCandidate::CandidateType::TRAIT_FUNC; + break; + case TraitItemReference::TraitItemType::CONST: + candidate_type = PathProbeCandidate::CandidateType::TRAIT_ITEM_CONST; + break; + case TraitItemReference::TraitItemType::TYPE: + candidate_type = PathProbeCandidate::CandidateType::TRAIT_TYPE_ALIAS; + break; + + case TraitItemReference::TraitItemType::ERROR: + default: + gcc_unreachable (); + break; + } - TyTy::BaseType *trait_item_tyty = trait_item_ref->get_tyty (); - if (impl != nullptr && !is_reciever_generic ()) + TyTy::BaseType *trait_item_tyty = trait_item_ref->get_tyty (); + if (impl != nullptr && !is_reciever_generic ()) - { - HirId impl_block_id = impl->get_mappings ().get_hirid (); - AssociatedImplTrait *lookup_associated = nullptr; - bool found_impl_trait - = context->lookup_associated_trait_impl (impl_block_id, - &lookup_associated); - // see testsuite/rust/compile/torture/traits10.rs this can be false - if (found_impl_trait) - lookup_associated->setup_associated_types (); - } + { + HirId impl_block_id = impl->get_mappings ().get_hirid (); + AssociatedImplTrait *lookup_associated = nullptr; + bool found_impl_trait + = context->lookup_associated_trait_impl (impl_block_id, + &lookup_associated); + // see testsuite/rust/compile/torture/traits10.rs this can be false + if (found_impl_trait) + lookup_associated->setup_associated_types (); + } - // we can substitute the Self with the receiver here - if (trait_item_tyty->get_kind () == TyTy::TypeKind::FNDEF) + // we can substitute the Self with the receiver here + if (trait_item_tyty->get_kind () == TyTy::TypeKind::FNDEF) + { + TyTy::FnType *fn = static_cast<TyTy::FnType *> (trait_item_tyty); + TyTy::SubstitutionParamMapping *param = nullptr; + for (auto ¶m_mapping : fn->get_substs ()) { - TyTy::FnType *fn = static_cast<TyTy::FnType *> (trait_item_tyty); - TyTy::SubstitutionParamMapping *param = nullptr; - for (auto ¶m_mapping : fn->get_substs ()) + const HIR::TypeParam &type_param + = param_mapping.get_generic_param (); + if (type_param.get_type_representation ().compare ("Self") == 0) { - const HIR::TypeParam &type_param - = param_mapping.get_generic_param (); - if (type_param.get_type_representation ().compare ("Self") == 0) - { - param = ¶m_mapping; - break; - } + param = ¶m_mapping; + break; } - rust_assert (param != nullptr); - - std::vector<TyTy::SubstitutionArg> mappings; - mappings.push_back ( - TyTy::SubstitutionArg (param, receiver->clone ())); - - Location locus; // FIXME - TyTy::SubstitutionArgumentMappings args (std::move (mappings), - locus); - trait_item_tyty - = SubstMapperInternal::Resolve (trait_item_tyty, args); } + rust_assert (param != nullptr); - PathProbeCandidate::TraitItemCandidate trait_item_candidate{ - trait_ref, trait_item_ref, impl}; - PathProbeCandidate candidate{candidate_type, - trait_item_tyty, - trait_ref->get_locus (), - {trait_item_candidate}}; - candidates.push_back (std::move (candidate)); + std::vector<TyTy::SubstitutionArg> mappings; + mappings.push_back (TyTy::SubstitutionArg (param, receiver->clone ())); + + Location locus; // FIXME + TyTy::SubstitutionArgumentMappings args (std::move (mappings), locus); + trait_item_tyty = SubstMapperInternal::Resolve (trait_item_tyty, args); } + + PathProbeCandidate::TraitItemCandidate trait_item_candidate{trait_ref, + trait_item_ref, + impl}; + PathProbeCandidate candidate{candidate_type, + trait_item_tyty, + trait_ref->get_locus (), + {trait_item_candidate}}; + candidates.push_back (std::move (candidate)); } protected: |