https://gcc.gnu.org/g:86c14f5228547e9210c63323dc04702d422fbe43

commit r16-2890-g86c14f5228547e9210c63323dc04702d422fbe43
Author: Philip Herron <herron.phi...@googlemail.com>
Date:   Sat Jun 21 16:08:28 2025 +0100

    gccrs: Add unify rules for fnptr and closures
    
    Its valid to unify a closure to an fnptr as we are working on the
    fn traits. There are still other issues but this is part of the patch set.
    
    gcc/rust/ChangeLog:
    
            * typecheck/rust-unify.cc (UnifyRules::expect_fnptr): add unify 
rules
    
    Signed-off-by: Philip Herron <herron.phi...@googlemail.com>

Diff:
---
 gcc/rust/typecheck/rust-unify.cc | 38 +++++++++++++++++++++++++++++++++++++-
 1 file changed, 37 insertions(+), 1 deletion(-)

diff --git a/gcc/rust/typecheck/rust-unify.cc b/gcc/rust/typecheck/rust-unify.cc
index 9144f2eafba4..2a981acaf3a7 100644
--- a/gcc/rust/typecheck/rust-unify.cc
+++ b/gcc/rust/typecheck/rust-unify.cc
@@ -1098,6 +1098,43 @@ UnifyRules::expect_fnptr (TyTy::FnPtr *ltype, 
TyTy::BaseType *rtype)
       }
       break;
 
+    case TyTy::CLOSURE:
+      {
+       TyTy::ClosureType &type = *static_cast<TyTy::ClosureType *> (rtype);
+       auto this_ret_type = ltype->get_return_type ();
+       auto other_ret_type = type.get_return_type ();
+
+       auto unified_result
+         = resolve_subtype (TyTy::TyWithLocation (this_ret_type),
+                            TyTy::TyWithLocation (other_ret_type));
+       if (unified_result->get_kind () == TyTy::TypeKind::ERROR)
+         {
+           return new TyTy::ErrorType (0);
+         }
+
+       if (ltype->num_params () != type.get_num_params ())
+         {
+           return new TyTy::ErrorType (0);
+         }
+
+       for (size_t i = 0; i < ltype->num_params (); i++)
+         {
+           auto this_param = ltype->get_param_type_at (i);
+           auto other_param = type.get_param_type_at (i);
+
+           auto unified_param
+             = resolve_subtype (TyTy::TyWithLocation (this_param),
+                                TyTy::TyWithLocation (other_param));
+           if (unified_param->get_kind () == TyTy::TypeKind::ERROR)
+             {
+               return new TyTy::ErrorType (0);
+             }
+         }
+
+       return ltype->clone ();
+      }
+      break;
+
     case TyTy::TUPLE:
     case TyTy::BOOL:
     case TyTy::CHAR:
@@ -1117,7 +1154,6 @@ UnifyRules::expect_fnptr (TyTy::FnPtr *ltype, 
TyTy::BaseType *rtype)
     case TyTy::PLACEHOLDER:
     case TyTy::PROJECTION:
     case TyTy::DYNAMIC:
-    case TyTy::CLOSURE:
     case TyTy::OPAQUE:
     case TyTy::ERROR:
       return new TyTy::ErrorType (0);

Reply via email to