From df7f3413385b5e4b90b1da5058b9fae4b891224c Mon Sep 17 00:00:00 2001 From: Philip Herron Date: Thu, 29 Jun 2023 10:13:29 +0100 Subject: [PATCH] gccrs: Track associated type generics This fixes an issue with our qualified type-paths if we point to a generic associated type this needs to either use the placeholder type on the trait reference or use the impl-type but apply the generics. gcc/rust/ChangeLog: * typecheck/rust-hir-trait-reference.h: update prototype * typecheck/rust-hir-trait-resolve.cc: add generic args as an out param * typecheck/rust-hir-type-check-type.cc (TypeCheckType::visit): use the generics Signed-off-by: Philip Herron --- gcc/rust/typecheck/rust-hir-trait-reference.h | 3 ++- gcc/rust/typecheck/rust-hir-trait-resolve.cc | 20 +++++++++++++------ .../typecheck/rust-hir-type-check-type.cc | 11 +++++++++- 3 files changed, 26 insertions(+), 8 deletions(-) diff --git a/gcc/rust/typecheck/rust-hir-trait-reference.h b/gcc/rust/typecheck/rust-hir-trait-reference.h index c0a6f561ada2..440a232a50e2 100644 --- a/gcc/rust/typecheck/rust-hir-trait-reference.h +++ b/gcc/rust/typecheck/rust-hir-trait-reference.h @@ -253,7 +253,8 @@ public: TyTy::BaseType * setup_associated_types (const TyTy::BaseType *self, - const TyTy::TypeBoundPredicate &bound); + const TyTy::TypeBoundPredicate &bound, + TyTy::SubstitutionArgumentMappings *args = nullptr); void reset_associated_types (); diff --git a/gcc/rust/typecheck/rust-hir-trait-resolve.cc b/gcc/rust/typecheck/rust-hir-trait-resolve.cc index a8a026a7f9ae..f72c6fc225a2 100644 --- a/gcc/rust/typecheck/rust-hir-trait-resolve.cc +++ b/gcc/rust/typecheck/rust-hir-trait-resolve.cc @@ -434,7 +434,8 @@ AssociatedImplTrait::setup_raw_associated_types () TyTy::BaseType * AssociatedImplTrait::setup_associated_types ( - const TyTy::BaseType *self, const TyTy::TypeBoundPredicate &bound) + const TyTy::BaseType *self, const TyTy::TypeBoundPredicate &bound, + TyTy::SubstitutionArgumentMappings *args) { // compute the constrained impl block generic arguments based on self and the // higher ranked trait bound @@ -491,25 +492,27 @@ AssociatedImplTrait::setup_associated_types ( // generate inference variables for these bound arguments so we can compute // their values Location locus; - std::vector args; + std::vector subst_args; for (auto &p : substitutions) { if (p.needs_substitution ()) { TyTy::TyVar infer_var = TyTy::TyVar::get_implicit_infer_var (locus); - args.push_back (TyTy::SubstitutionArg (&p, infer_var.get_tyty ())); + subst_args.push_back ( + TyTy::SubstitutionArg (&p, infer_var.get_tyty ())); } else { TyTy::ParamType *param = p.get_param_ty (); TyTy::BaseType *resolved = param->destructure (); - args.push_back (TyTy::SubstitutionArg (&p, resolved)); + subst_args.push_back (TyTy::SubstitutionArg (&p, resolved)); param_mappings[param->get_symbol ()] = resolved->get_ref (); } } - TyTy::SubstitutionArgumentMappings infer_arguments (std::move (args), {}, - locus, param_subst_cb); + TyTy::SubstitutionArgumentMappings infer_arguments (std::move (subst_args), + {}, locus, + param_subst_cb); TyTy::BaseType *impl_self_infer = (!associated_self->is_concrete ()) ? SubstMapperInternal::Resolve (associated_self, infer_arguments) @@ -632,6 +635,11 @@ AssociatedImplTrait::setup_associated_types ( resolved_trait_item->associated_type_set (substituted); } + if (args != nullptr) + { + *args = associated_type_args; + } + return self_result; } diff --git a/gcc/rust/typecheck/rust-hir-type-check-type.cc b/gcc/rust/typecheck/rust-hir-type-check-type.cc index d309101d107a..c476edfb7a4d 100644 --- a/gcc/rust/typecheck/rust-hir-type-check-type.cc +++ b/gcc/rust/typecheck/rust-hir-type-check-type.cc @@ -214,6 +214,8 @@ TypeCheckType::visit (HIR::QualifiedPathInType &path) } // we try to look for the real impl item if possible + TyTy::SubstitutionArgumentMappings args + = TyTy::SubstitutionArgumentMappings::error (); HIR::ImplItem *impl_item = nullptr; if (root->is_concrete ()) { @@ -223,7 +225,8 @@ TypeCheckType::visit (HIR::QualifiedPathInType &path) = lookup_associated_impl_block (specified_bound, root); if (associated_impl_trait != nullptr) { - associated_impl_trait->setup_associated_types (root, specified_bound); + associated_impl_trait->setup_associated_types (root, specified_bound, + &args); for (auto &i : associated_impl_trait->get_impl_block ()->get_impl_items ()) @@ -262,6 +265,12 @@ TypeCheckType::visit (HIR::QualifiedPathInType &path) return; } + if (!args.is_error ()) + { + // apply the args + translated = SubstMapperInternal::Resolve (translated, args); + } + root_resolved_node_id = impl_item->get_impl_mappings ().get_nodeid (); } -- 2.47.2