Polymorphism support #848

Merged
ghallak merged 53 commits from ghallak/307 into master 2022-06-17 18:09:07 +09:00
2 changed files with 20 additions and 2 deletions
Showing only changes of commit 136035b952 - Show all commits

View File

@ -1107,6 +1107,8 @@ check_typedef_sccs(Env, TypeMap, [{acyclic, Name} | SCCs], Acc) ->
type_error({empty_record_definition, Ann, Name}),
check_typedef_sccs(Env1, TypeMap, SCCs, Acc1);
{record_t, Fields} ->
ets_insert(type_vars_variance, {Env#env.namespace ++ qname(D),
infer_type_vars_variance(Xs, Fields)}),
%% check_type to get qualified name
RecTy = check_type(Env1, app_t(Ann, D, Xs)),
Env2 = check_fields(Env1, TypeMap, RecTy, Fields),
@ -1141,7 +1143,7 @@ check_typedef(Env, {variant_t, Cons}) ->
infer_type_vars_variance(TypeParams, Cons) ->
% args from all type constructors
FlatArgs = lists:flatten([Args || {constr_t, _, _, Args} <- Cons]),
FlatArgs = lists:flatten([Args || {constr_t, _, _, Args} <- Cons]) ++ [Type || {field_t, _, _, Type} <- Cons],
Vs = lists:flatten([element(1, infer_type_vars_variance(Arg)) || Arg <- FlatArgs]),
lists:map(fun({tvar, _, TVar}) ->
@ -1188,7 +1190,7 @@ infer_type_vars_variance({fun_t, _, [], [{tvar, _, TVar}], Res}) ->
1 -> {TVar, contravariant}
end,
{[Cur | TVVs], Depth + 1};
infer_type_vars_variance({fun_t, _, [], [_Arg], Res}) ->
infer_type_vars_variance({fun_t, _, [], _, Res}) ->
{X, Depth} = infer_type_vars_variance(Res),
{X, Depth + 1};
infer_type_vars_variance({tvar, _, TVar}) ->

View File

@ -990,6 +990,22 @@ failing_contracts() ->
"Cannot unify `Animal` and `Cat`\n"
"when checking the type of the expression `TK(f_a_to_c_to_u) : tk(Animal, Cat)` against the expected type `tk(Cat, Cat)`">>
])
, ?TYPE_ERROR(polymorphism_variance_switching_records,
[<<?Pos(26,13)
"Cannot unify `Animal` and `Cat`\n"
"when checking the type of the pattern `r03 : rec_a(Cat)` against the expected type `Main.rec_a(Animal)`">>,
<<?Pos(32,13)
"Cannot unify `Cat` and `Animal`\n"
"when checking the type of the pattern `r06 : rec_b(Animal)` against the expected type `Main.rec_b(Cat)`">>,
<<?Pos(40,13)
"Cannot unify `Animal` and `Cat`\n"
"when checking the type of the pattern `r11 : rec_c(Cat)` against the expected type `Main.rec_c(Animal)`">>,
<<?Pos(46,13)
"Cannot unify `Cat` and `Animal`\n"
"when checking the type of the pattern `r16 : rec_d(Animal)` against the expected type `Main.rec_d(Cat)`">>,
<<?Pos(47,13)
"Cannot unify `Animal` and `Cat`\n"
"when checking the type of the pattern `r17 : rec_d(Cat)` against the expected type `Main.rec_d(Animal)`">>])
].
-define(Path(File), "code_errors/" ??File).