Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Print inferred argument types on call_intersect error #491

Merged
merged 4 commits into from
Jan 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 6 additions & 23 deletions src/gradualizer_fmt.erl
Original file line number Diff line number Diff line change
Expand Up @@ -164,33 +164,16 @@ format_type_error({type_error, call_arity, Anno, Fun, TyArity, CallArity}, Opts)
TyArity,
["s" || TyArity /= 1],
CallArity]);
format_type_error({type_error, call_intersect, Anno, FunTy, Name}, Opts) ->
format_type_error({type_error, call_intersect, Anno, Name, FunTy, ArgTys}, Opts) ->
io_lib:format(
"~sThe type of the function ~s, called~s doesn't match "
"the surrounding calling context.~n"
"It has the following type~n~s~n",
"~s~s/~p call arguments~s don't match the function type:~n~s~n"
"Inferred argument types:~n~s~n",
[format_location(Anno, brief, Opts),
pp_expr(Name, Opts),
length(ArgTys),
format_location(Anno, verbose, Opts),
pp_intersection_type(FunTy, Opts)]);
format_type_error({type_error, expected_fun_type, Anno, Func, FunTy}, Opts) ->
Name = pp_expr(Func, Opts),
io_lib:format(
"~sExpected function ~s~s to have a function type,~n"
"but it has the following type:~n~s~n",
[format_location(Anno, brief, Opts),
Name,
format_location(Anno, verbose, Opts),
pp_type(FunTy, Opts)]);
format_type_error({type_error, no_type_match_intersection, Anno, Func, FunTy}, Opts) ->
Name = pp_expr(Func, Opts),
io_lib:format(
"~sNone of the types of the function ~s~s matches the "
"call site. Here's the types of the function:~n~s~n",
[format_location(Anno, brief, Opts),
Name,
format_location(Anno, verbose, Opts),
pp_intersection_type(FunTy, Opts)]);
pp_intersection_type(FunTy, Opts),
string:join([ pp_type(ATy, Opts) || ATy <- ArgTys ], ", ")]);
format_type_error({type_error, relop, RelOp, Anno, Ty1, Ty2}, Opts) ->
io_lib:format(
"~sThe operator ~p~s requires arguments of "
Expand Down
21 changes: 15 additions & 6 deletions src/typechecker.erl
Original file line number Diff line number Diff line change
Expand Up @@ -132,8 +132,8 @@

%% TODO: Some of these don't seem to be thrown at all, e.g. expected_fun_type
-type type_error() :: arith_error | badkey | call_arity | call_intersect | check_clauses | cons_pat
| cyclic_type_vars | expected_fun_type | int_error | list | mismatch
| no_type_match_intersection | non_number_argument_to_minus
| cyclic_type_vars | int_error | list | mismatch
| non_number_argument_to_minus
| non_number_argument_to_plus | op_type_too_precise | operator_pattern | pattern
| receive_after | record_pattern | rel_error | relop | unary_error
| unreachable_clauses.
Expand All @@ -147,6 +147,7 @@
| {type_error, type_error(), anno(), atom() | pattern(), type()}
| {type_error, type_error(), unary_op() | binary_op(), anno(), type()}
| {type_error, type_error(), binary_op(), anno(), type(), type()}
| {type_error, call_intersect, anno(), expr(), type(), [type()]}
| {type_error, call_arity, anno(), atom(), arity(), arity()}
| {undef, undef(), anno(), {atom(), atom() | non_neg_integer()} | mfa() | expr()}
| {undef, undef(), expr()}
Expand Down Expand Up @@ -2205,8 +2206,8 @@ type_check_call_ty(_Env, {type_error, _}, _Args, {Name, _P, FunTy}) ->
throw(type_error(Name, FunTy, type('fun'))).

-spec type_check_call_ty_intersect(env(), _, _, _) -> {type(), env(), constraints:t()}.
type_check_call_ty_intersect(_Env, [], _Args, {Name, P, FunTy}) ->
throw(type_error(call_intersect, P, FunTy, Name));
type_check_call_ty_intersect(Env, [], Args, {Name, P, FunTy}) ->
throw(type_error(call_intersect, P, Name, FunTy, infer_arg_types(Args, Env)));
type_check_call_ty_intersect(Env, [Ty | Tys], Args, E) ->
try
type_check_call_ty(Env, Ty, Args, E)
Expand All @@ -2215,6 +2216,13 @@ type_check_call_ty_intersect(Env, [Ty | Tys], Args, E) ->
type_check_call_ty_intersect(Env, Tys, Args, E)
end.

-spec infer_arg_types([expr()], env()) -> [type()].
infer_arg_types(Args, Env) ->
lists:map(fun (Arg) ->
{ArgTy, _VB, _Cs} = type_check_expr(Env#env{infer = true}, Arg),
ArgTy
end, Args).

-spec type_check_call_ty_union(env(), _, _, _) -> {type(), env(), constraints:t()}.
type_check_call_ty_union(Env, Tys, Args, E) ->
{ResTys, VBs, Css} =
Expand Down Expand Up @@ -3333,8 +3341,8 @@ type_check_call_intersection(Env, ResTy, OrigExpr, Tys, Args, E) ->
type_check_call_intersection_(Env, ResTy, OrigExpr, Tys, Args, E).

-spec type_check_call_intersection_(env(), type(), _, _, _, _) -> {env(), constraints:t()}.
type_check_call_intersection_(_Env, _ResTy, _, [], _Args, {P, Name, Ty}) ->
throw(type_error(no_type_match_intersection, P, Name, Ty));
type_check_call_intersection_(Env, _ResTy, _, [], Args, {P, Name, FunTy}) ->
throw(type_error(call_intersect, P, Name, FunTy, infer_arg_types(Args, Env)));
type_check_call_intersection_(Env, ResTy, OrigExpr, [Ty | Tys], Args, E) ->
try
type_check_call(Env, ResTy, OrigExpr, Ty, Args, E)
Expand Down Expand Up @@ -5620,6 +5628,7 @@ type_error(Kind, P, Info, Ty) ->
{type_error, Kind, P, Info, Ty}.

-spec type_error(call_arity, anno(), atom(), arity(), arity()) -> error();
(call_intersect, anno(), expr(), type(), [type()]) -> error();
(type_error(), binary_op(), anno(), type(), type()) -> error().
type_error(Kind, Op, P, Ty1, Ty2) ->
{type_error, Kind, Op, P, Ty1, Ty2}.
Expand Down