public inbox for gcc-cvs@sourceware.org
help / color / mirror / Atom feed
* [gcc/devel/rust/master] Add support for generics within operator overloads
@ 2022-06-08 11:54 Thomas Schwinge
  0 siblings, 0 replies; only message in thread
From: Thomas Schwinge @ 2022-06-08 11:54 UTC (permalink / raw)
  To: gcc-cvs

https://gcc.gnu.org/g:ec23757d36928b9e59e3236790378cf824e7ab49

commit ec23757d36928b9e59e3236790378cf824e7ab49
Author: Philip Herron <philip.herron@embecosm.com>
Date:   Thu Nov 25 14:02:25 2021 +0000

    Add support for generics within operator overloads
    
    This ports over some code from the method-call expr to try and infer the
    arguments in the function definition when required.
    
    Addresses #809

Diff:
---
 gcc/rust/typecheck/rust-hir-type-check-expr.h | 22 +++++++++++++++++-----
 1 file changed, 17 insertions(+), 5 deletions(-)

diff --git a/gcc/rust/typecheck/rust-hir-type-check-expr.h b/gcc/rust/typecheck/rust-hir-type-check-expr.h
index 492342647e5..f8b9f051fa6 100644
--- a/gcc/rust/typecheck/rust-hir-type-check-expr.h
+++ b/gcc/rust/typecheck/rust-hir-type-check-expr.h
@@ -1293,6 +1293,8 @@ protected:
       return false;
 
     // mark the required tree addressable
+    Adjuster adj (lhs);
+    TyTy::BaseType *receiver_adjusted_self_ty = adj.adjust_type (adjustments);
     if (Adjuster::needs_address (adjustments))
       AddressTakenResolver::SetAddressTaken (*expr.get_expr ().get ());
 
@@ -1324,14 +1326,12 @@ protected:
 		TyTy::FnType *fntype = static_cast<TyTy::FnType *> (lookup);
 		rust_assert (fntype->is_method ());
 
-		Adjuster adj (lhs);
-		TyTy::BaseType *adjusted = adj.adjust_type (adjustments);
-
 		bool is_lang_item_impl
 		  = trait_reference->get_mappings ().get_defid ()
 		    == respective_lang_item_id;
 		bool self_is_lang_item_self
-		  = fntype->get_self_type ()->is_equal (*adjusted);
+		  = fntype->get_self_type ()->is_equal (
+		    *receiver_adjusted_self_ty);
 		bool recursive_operator_overload
 		  = is_lang_item_impl && self_is_lang_item_self;
 
@@ -1415,8 +1415,20 @@ protected:
 	  }
       }
 
+    // handle generics
+    if (!receiver_is_type_param)
+      {
+	if (lookup->needs_generic_substitutions ())
+	  {
+	    lookup = SubstMapper::InferSubst (lookup, expr.get_locus ());
+	  }
+      }
+
     // type check the arguments if required
     TyTy::FnType *type = static_cast<TyTy::FnType *> (lookup);
+    rust_assert (type->num_params () > 0);
+    auto fnparam = type->param_at (0);
+    fnparam.second->unify (receiver_adjusted_self_ty); // typecheck the self
     if (rhs == nullptr)
       {
 	rust_assert (type->num_params () == 1);
@@ -1429,7 +1441,7 @@ protected:
       }
 
     // get the return type
-    TyTy::BaseType *function_ret_tyty = fn->get_return_type ()->clone ();
+    TyTy::BaseType *function_ret_tyty = type->get_return_type ()->clone ();
 
     // store the expected fntype
     context->insert_operator_overload (expr.get_mappings ().get_hirid (), type);


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

only message in thread, other threads:[~2022-06-08 11:54 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-06-08 11:54 [gcc/devel/rust/master] Add support for generics within operator overloads 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).