]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
gccrs: Add unify rules for fnptr and closures
authorPhilip Herron <herron.philip@googlemail.com>
Sat, 21 Jun 2025 15:08:28 +0000 (16:08 +0100)
committerArthur Cohen <arthur.cohen@embecosm.com>
Tue, 5 Aug 2025 14:36:50 +0000 (16:36 +0200)
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.philip@googlemail.com>
gcc/rust/typecheck/rust-unify.cc

index 9144f2eafba4af2e2e5949b91298ca8ab8a810dc..2a981acaf3a7b35a4155ed53371f2d688a8323f8 100644 (file)
@@ -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);