Skip to content

Commit a968963

Browse files
committed
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.philip@googlemail.com>
1 parent eb33ed9 commit a968963

1 file changed

Lines changed: 37 additions & 1 deletion

File tree

gcc/rust/typecheck/rust-unify.cc

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1098,6 +1098,43 @@ UnifyRules::expect_fnptr (TyTy::FnPtr *ltype, TyTy::BaseType *rtype)
10981098
}
10991099
break;
11001100

1101+
case TyTy::CLOSURE:
1102+
{
1103+
TyTy::ClosureType &type = *static_cast<TyTy::ClosureType *> (rtype);
1104+
auto this_ret_type = ltype->get_return_type ();
1105+
auto other_ret_type = type.get_return_type ();
1106+
1107+
auto unified_result
1108+
= resolve_subtype (TyTy::TyWithLocation (this_ret_type),
1109+
TyTy::TyWithLocation (other_ret_type));
1110+
if (unified_result->get_kind () == TyTy::TypeKind::ERROR)
1111+
{
1112+
return new TyTy::ErrorType (0);
1113+
}
1114+
1115+
if (ltype->num_params () != type.get_num_params ())
1116+
{
1117+
return new TyTy::ErrorType (0);
1118+
}
1119+
1120+
for (size_t i = 0; i < ltype->num_params (); i++)
1121+
{
1122+
auto this_param = ltype->get_param_type_at (i);
1123+
auto other_param = type.get_param_type_at (i);
1124+
1125+
auto unified_param
1126+
= resolve_subtype (TyTy::TyWithLocation (this_param),
1127+
TyTy::TyWithLocation (other_param));
1128+
if (unified_param->get_kind () == TyTy::TypeKind::ERROR)
1129+
{
1130+
return new TyTy::ErrorType (0);
1131+
}
1132+
}
1133+
1134+
return ltype->clone ();
1135+
}
1136+
break;
1137+
11011138
case TyTy::TUPLE:
11021139
case TyTy::BOOL:
11031140
case TyTy::CHAR:
@@ -1117,7 +1154,6 @@ UnifyRules::expect_fnptr (TyTy::FnPtr *ltype, TyTy::BaseType *rtype)
11171154
case TyTy::PLACEHOLDER:
11181155
case TyTy::PROJECTION:
11191156
case TyTy::DYNAMIC:
1120-
case TyTy::CLOSURE:
11211157
case TyTy::OPAQUE:
11221158
case TyTy::ERROR:
11231159
return new TyTy::ErrorType (0);

0 commit comments

Comments
 (0)