public inbox for gcc-cvs@sourceware.org
help / color / mirror / Atom feed
* [gcc/devel/rust/master] gccrs: Remove solve_missing_mappings_from_this to handle covariants
@ 2023-03-20  7:23 Thomas Schwinge
  0 siblings, 0 replies; only message in thread
From: Thomas Schwinge @ 2023-03-20  7:23 UTC (permalink / raw)
  To: gcc-cvs

https://gcc.gnu.org/g:9ace79ff7ec52b15708469a04b28070bfeed5eb3

commit 9ace79ff7ec52b15708469a04b28070bfeed5eb3
Author: Philip Herron <herron.philip@googlemail.com>
Date:   Mon Mar 13 16:49:36 2023 +0000

    gccrs: Remove solve_missing_mappings_from_this to handle covariants
    
    change how we monomorphize dyn-items when we need to compute the generics
    
    We might have a trait item such as:
    
      impl<'a, T> FnLike<&'a T, &'a T> for Identity {
          fn call(&self, arg: &'a T) -> &'a T { ... }
      }
    
    Which ended up monomorphized badly to:
    
      const isize & const &
        <example::Identity as example::FnLike::<& T, & T>>::call<& isize>
        (const struct example::Identity & const self,
         const isize & const & const arg)
    
    This is wrong because it turned into a double reference type becasuse this
    bug was consistent bugs were not picked up but this is not correct. We now
    reuse our type inference infrastructure to solve the parameters instead.
    
    Fixes #1984
    
    gcc/rust/ChangeLog:
    
            * backend/rust-compile.cc: use unify_and instead
            * typecheck/rust-tyty-subst.cc (SubstitutionRef::solve_missing_mappings_from_this): remove
            * typecheck/rust-tyty-subst.h: update header
    
    Signed-off-by: Philip Herron <herron.philip@googlemail.com>

Diff:
---
 gcc/rust/backend/rust-compile.cc      | 19 ++++++-------
 gcc/rust/typecheck/rust-tyty-subst.cc | 51 -----------------------------------
 gcc/rust/typecheck/rust-tyty-subst.h  | 11 ++++----
 3 files changed, 16 insertions(+), 65 deletions(-)

diff --git a/gcc/rust/backend/rust-compile.cc b/gcc/rust/backend/rust-compile.cc
index a01662c6da5..2c4699264f0 100644
--- a/gcc/rust/backend/rust-compile.cc
+++ b/gcc/rust/backend/rust-compile.cc
@@ -21,6 +21,8 @@
 #include "rust-compile-implitem.h"
 #include "rust-hir-type-bounds.h"
 #include "rust-compile-type.h"
+#include "rust-substitution-mapper.h"
+#include "rust-type-util.h"
 
 namespace Rust {
 namespace Compile {
@@ -299,11 +301,6 @@ HIRCompileBase::compute_address_for_trait_item (
     = self_bound->lookup_associated_item (ref->get_identifier ());
   rust_assert (!associated_self_item.is_error ());
 
-  TyTy::BaseType *mono1 = associated_self_item.get_tyty_for_receiver (self);
-  rust_assert (mono1 != nullptr);
-  rust_assert (mono1->get_kind () == TyTy::TypeKind::FNDEF);
-  TyTy::FnType *assocated_item_ty1 = static_cast<TyTy::FnType *> (mono1);
-
   // Lookup the impl-block for the associated impl_item if it exists
   HIR::Function *associated_function = nullptr;
   for (auto &impl_item : associated_impl_block->get_impl_items ())
@@ -333,10 +330,14 @@ HIRCompileBase::compute_address_for_trait_item (
 
       if (lookup_fntype->needs_substitution ())
 	{
-	  TyTy::SubstitutionArgumentMappings mappings
-	    = assocated_item_ty1->solve_missing_mappings_from_this (
-	      *trait_item_fntype, *lookup_fntype);
-	  lookup_fntype = lookup_fntype->handle_substitions (mappings);
+	  TyTy::BaseType *infer
+	    = Resolver::SubstMapper::InferSubst (lookup_fntype, Location ());
+	  infer
+	    = Resolver::unify_site (infer->get_ref (),
+				    TyTy::TyWithLocation (trait_item_fntype),
+				    TyTy::TyWithLocation (infer), Location ());
+	  rust_assert (infer->get_kind () == TyTy::TypeKind::FNDEF);
+	  lookup_fntype = static_cast<TyTy::FnType *> (infer);
 	}
 
       return CompileInherentImplItem::Compile (associated_function, ctx,
diff --git a/gcc/rust/typecheck/rust-tyty-subst.cc b/gcc/rust/typecheck/rust-tyty-subst.cc
index 6b77a9ee398..db31ff9323f 100644
--- a/gcc/rust/typecheck/rust-tyty-subst.cc
+++ b/gcc/rust/typecheck/rust-tyty-subst.cc
@@ -821,57 +821,6 @@ SubstitutionRef::solve_mappings_from_receiver_for_self (
 				       mappings.get_locus ());
 }
 
-SubstitutionArgumentMappings
-SubstitutionRef::solve_missing_mappings_from_this (SubstitutionRef &ref,
-						   SubstitutionRef &to)
-{
-  rust_assert (!ref.needs_substitution ());
-  rust_assert (needs_substitution ());
-  rust_assert (get_num_substitutions () == ref.get_num_substitutions ());
-
-  Location locus = used_arguments.get_locus ();
-  std::vector<SubstitutionArg> resolved_mappings;
-
-  std::map<HirId, std::pair<ParamType *, BaseType *>> substs;
-  for (size_t i = 0; i < get_num_substitutions (); i++)
-    {
-      SubstitutionParamMapping &a = substitutions.at (i);
-      SubstitutionParamMapping &b = ref.substitutions.at (i);
-
-      if (a.need_substitution ())
-	{
-	  const BaseType *root = a.get_param_ty ()->resolve ()->get_root ();
-	  rust_assert (root->get_kind () == TyTy::TypeKind::PARAM);
-	  const ParamType *p = static_cast<const TyTy::ParamType *> (root);
-
-	  substs[p->get_ty_ref ()] = {static_cast<ParamType *> (p->clone ()),
-				      b.get_param_ty ()->resolve ()};
-	}
-    }
-
-  for (auto it = substs.begin (); it != substs.end (); it++)
-    {
-      HirId param_id = it->first;
-      BaseType *arg = it->second.second;
-
-      const SubstitutionParamMapping *associate_param = nullptr;
-      for (SubstitutionParamMapping &p : to.substitutions)
-	{
-	  if (p.get_param_ty ()->get_ty_ref () == param_id)
-	    {
-	      associate_param = &p;
-	      break;
-	    }
-	}
-
-      rust_assert (associate_param != nullptr);
-      SubstitutionArg argument (associate_param, arg);
-      resolved_mappings.push_back (std::move (argument));
-    }
-
-  return SubstitutionArgumentMappings (resolved_mappings, {}, locus);
-}
-
 Resolver::AssociatedImplTrait *
 SubstitutionRef::lookup_associated_impl (const SubstitutionParamMapping &subst,
 					 const TypeBoundPredicate &bound,
diff --git a/gcc/rust/typecheck/rust-tyty-subst.h b/gcc/rust/typecheck/rust-tyty-subst.h
index 365fdb6c042..b5a41819fd4 100644
--- a/gcc/rust/typecheck/rust-tyty-subst.h
+++ b/gcc/rust/typecheck/rust-tyty-subst.h
@@ -291,11 +291,12 @@ public:
   SubstitutionArgumentMappings solve_mappings_from_receiver_for_self (
     SubstitutionArgumentMappings &mappings) const;
 
-  // TODO comment
-  SubstitutionArgumentMappings
-  solve_missing_mappings_from_this (SubstitutionRef &ref, SubstitutionRef &to);
-
-  // TODO comment
+  // Given a type such as:
+  //
+  //   fn<X,Y>(a:&X, b:Y) -> (...)
+  //
+  // This function will inject implicit inference variables for the type
+  // parameters X and Y
   BaseType *infer_substitions (Location locus);
 
   // this clears any possible projections from higher ranked trait bounds which

^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2023-03-20  7:23 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-03-20  7:23 [gcc/devel/rust/master] gccrs: Remove solve_missing_mappings_from_this to handle covariants Thomas Schwinge

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).