From: Philip Herron Date: Sat, 21 Jun 2025 15:08:28 +0000 (+0100) Subject: gccrs: Add unify rules for fnptr and closures X-Git-Url: http://git.ipfire.org/gitweb/gitweb.cgi?a=commitdiff_plain;h=86c14f5228547e9210c63323dc04702d422fbe43;p=thirdparty%2Fgcc.git 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 --- diff --git a/gcc/rust/typecheck/rust-unify.cc b/gcc/rust/typecheck/rust-unify.cc index 9144f2eafba..2a981acaf3a 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 (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);