public inbox for gcc-cvs@sourceware.org
help / color / mirror / Atom feed
* [gcc r14-7914] gccrs: remove horrible hack in solving complex generics on impl blocks
@ 2024-01-16 18:06 Arthur Cohen
  0 siblings, 0 replies; only message in thread
From: Arthur Cohen @ 2024-01-16 18:06 UTC (permalink / raw)
  To: gcc-cvs

https://gcc.gnu.org/g:237564fc08e409f5a0fbd36b54cb8ba32f0efb9d

commit r14-7914-g237564fc08e409f5a0fbd36b54cb8ba32f0efb9d
Author: Philip Herron <herron.philip@googlemail.com>
Date:   Sat Aug 12 17:06:37 2023 +0100

    gccrs: remove horrible hack in solving complex generics on impl blocks
    
    We hit an assertion with range based iterators here. This code was used
    to solve complex generics such as:
    
      struct Foo<X,Y>(X,Y);
      impl<T> Foo<T, i32> {
        fn test<Y>(self, a: Y) { }
      }
    
    The impl item will have the signiture of:
    
      fn test<T,Y> (Foo<T, i32> self, a:Y)
    
    So in the case where we have:
    
      let a = Foo(123f32, 456);
      a.test<bool>(true);
    
    We need to solve the generic argument T from the impl block by infering the
    arguments there and applying them so that when we apply the generic
    argument bool we dont end up in the case of missing number of generics.
    
    Addresses #1895
    
    gcc/rust/ChangeLog:
    
            * typecheck/rust-hir-type-check-expr.cc (TypeCheckExpr::visit): remove hack
    
    Signed-off-by: Philip Herron <herron.philip@googlemail.com>

Diff:
---
 gcc/rust/typecheck/rust-hir-type-check-expr.cc | 147 +++++++------------------
 1 file changed, 42 insertions(+), 105 deletions(-)

diff --git a/gcc/rust/typecheck/rust-hir-type-check-expr.cc b/gcc/rust/typecheck/rust-hir-type-check-expr.cc
index c7b124864c9..e8fbe34afd0 100644
--- a/gcc/rust/typecheck/rust-hir-type-check-expr.cc
+++ b/gcc/rust/typecheck/rust-hir-type-check-expr.cc
@@ -25,6 +25,7 @@
 #include "rust-hir-type-check-pattern.h"
 #include "rust-hir-type-check-expr.h"
 #include "rust-hir-type-check-stmt.h"
+#include "rust-hir-type-check-item.h"
 #include "rust-type-util.h"
 
 namespace Rust {
@@ -1122,58 +1123,31 @@ TypeCheckExpr::visit (HIR::MethodCallExpr &expr)
     }
 
   fn->prepare_higher_ranked_bounds ();
-  auto root = receiver_tyty->get_root ();
-  if (root->get_kind () == TyTy::TypeKind::ADT)
+  rust_debug_loc (expr.get_locus (), "resolved method call to: {%u} {%s}",
+		  candidate.candidate.ty->get_ref (),
+		  candidate.candidate.ty->debug_str ().c_str ());
+
+  if (resolved_candidate.is_impl_candidate ())
     {
-      const TyTy::ADTType *adt = static_cast<const TyTy::ADTType *> (root);
-      if (adt->has_substitutions () && fn->needs_substitution ())
+      auto infer_arguments = TyTy::SubstitutionArgumentMappings::error ();
+      HIR::ImplBlock &impl = *resolved_candidate.item.impl.parent;
+      TyTy::BaseType *impl_self_infer
+	= TypeCheckItem::ResolveImplBlockSelfWithInference (impl,
+							    expr.get_locus (),
+							    &infer_arguments);
+      if (impl_self_infer->get_kind () == TyTy::TypeKind::ERROR)
 	{
-	  // consider the case where we have:
-	  //
-	  // struct Foo<X,Y>(X,Y);
-	  //
-	  // impl<T> Foo<T, i32> {
-	  //   fn test<X>(self, a:X) -> (T,X) { (self.0, a) }
-	  // }
-	  //
-	  // In this case we end up with an fn type of:
-	  //
-	  // fn <T,X> test(self:Foo<T,i32>, a:X) -> (T,X)
-	  //
-	  // This means the instance or self we are calling this method for
-	  // will be substituted such that we can get the inherited type
-	  // arguments but then need to use the turbo fish if available or
-	  // infer the remaining arguments. Luckily rust does not allow for
-	  // default types GenericParams on impl blocks since these must
-	  // always be at the end of the list
-
-	  auto s = fn->get_self_type ()->get_root ();
-	  rust_assert (s->can_eq (adt, false));
-	  rust_assert (s->get_kind () == TyTy::TypeKind::ADT);
-	  const TyTy::ADTType *self_adt
-	    = static_cast<const TyTy::ADTType *> (s);
-
-	  // we need to grab the Self substitutions as the inherit type
-	  // parameters for this
-	  if (self_adt->needs_substitution ())
-	    {
-	      rust_assert (adt->was_substituted ());
-
-	      TyTy::SubstitutionArgumentMappings used_args_in_prev_segment
-		= GetUsedSubstArgs::From (adt);
-
-	      TyTy::SubstitutionArgumentMappings inherit_type_args
-		= self_adt->solve_mappings_from_receiver_for_self (
-		  used_args_in_prev_segment);
-
-	      // there may or may not be inherited type arguments
-	      if (!inherit_type_args.is_error ())
-		{
-		  // need to apply the inherited type arguments to the
-		  // function
-		  lookup = fn->handle_substitions (inherit_type_args);
-		}
-	    }
+	  rich_location r (line_table, expr.get_locus ());
+	  r.add_range (impl.get_type ()->get_locus ());
+	  rust_error_at (
+	    r, "failed to resolve impl type for method call resolution");
+	  return;
+	}
+
+      if (!infer_arguments.is_empty ())
+	{
+	  lookup = SubstMapperInternal::Resolve (lookup, infer_arguments);
+	  lookup->debug ();
 	}
     }
 
@@ -1687,6 +1661,24 @@ TypeCheckExpr::resolve_operator_overload (
   rust_debug ("is_impl_item_candidate: %s",
 	      resolved_candidate.is_impl_candidate () ? "true" : "false");
 
+  if (resolved_candidate.is_impl_candidate ())
+    {
+      auto infer_arguments = TyTy::SubstitutionArgumentMappings::error ();
+      HIR::ImplBlock &impl = *resolved_candidate.item.impl.parent;
+      TyTy::BaseType *impl_self_infer
+	= TypeCheckItem::ResolveImplBlockSelfWithInference (impl,
+							    expr.get_locus (),
+							    &infer_arguments);
+      if (impl_self_infer->get_kind () == TyTy::TypeKind::ERROR)
+	{
+	  return false;
+	}
+      if (!infer_arguments.is_empty ())
+	{
+	  lookup = SubstMapperInternal::Resolve (lookup, infer_arguments);
+	}
+    }
+
   // in the case where we resolve to a trait bound we have to be careful we are
   // able to do so there is a case where we are currently resolving the deref
   // operator overload function which is generic and this might resolve to the
@@ -1735,61 +1727,6 @@ TypeCheckExpr::resolve_operator_overload (
 		  candidate.candidate.ty->get_ref (),
 		  candidate.candidate.ty->debug_str ().c_str ());
 
-  auto root = lhs->get_root ();
-  if (root->get_kind () == TyTy::TypeKind::ADT)
-    {
-      const TyTy::ADTType *adt = static_cast<const TyTy::ADTType *> (root);
-      if (adt->has_substitutions () && fn->needs_substitution ())
-	{
-	  // consider the case where we have:
-	  //
-	  // struct Foo<X,Y>(X,Y);
-	  //
-	  // impl<T> Foo<T, i32> {
-	  //   fn test<X>(self, a:X) -> (T,X) { (self.0, a) }
-	  // }
-	  //
-	  // In this case we end up with an fn type of:
-	  //
-	  // fn <T,X> test(self:Foo<T,i32>, a:X) -> (T,X)
-	  //
-	  // This means the instance or self we are calling this method for
-	  // will be substituted such that we can get the inherited type
-	  // arguments but then need to use the turbo fish if available or
-	  // infer the remaining arguments. Luckily rust does not allow for
-	  // default types GenericParams on impl blocks since these must
-	  // always be at the end of the list
-
-	  auto s = fn->get_self_type ()->get_root ();
-	  rust_assert (s->can_eq (adt, false));
-	  rust_assert (s->get_kind () == TyTy::TypeKind::ADT);
-	  const TyTy::ADTType *self_adt
-	    = static_cast<const TyTy::ADTType *> (s);
-
-	  // we need to grab the Self substitutions as the inherit type
-	  // parameters for this
-	  if (self_adt->needs_substitution ())
-	    {
-	      rust_assert (adt->was_substituted ());
-
-	      TyTy::SubstitutionArgumentMappings used_args_in_prev_segment
-		= GetUsedSubstArgs::From (adt);
-
-	      TyTy::SubstitutionArgumentMappings inherit_type_args
-		= self_adt->solve_mappings_from_receiver_for_self (
-		  used_args_in_prev_segment);
-
-	      // there may or may not be inherited type arguments
-	      if (!inherit_type_args.is_error ())
-		{
-		  // need to apply the inherited type arguments to the
-		  // function
-		  lookup = fn->handle_substitions (inherit_type_args);
-		}
-	    }
-	}
-    }
-
   // handle generics
   if (lookup->needs_generic_substitutions ())
     lookup = SubstMapper::InferSubst (lookup, expr.get_locus ());

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

only message in thread, other threads:[~2024-01-16 18:06 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-01-16 18:06 [gcc r14-7914] gccrs: remove horrible hack in solving complex generics on impl blocks Arthur Cohen

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).