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

Check exhaustiveness argument-wise #391

Merged
merged 2 commits into from
Mar 31, 2022
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
27 changes: 17 additions & 10 deletions src/typechecker.erl
Original file line number Diff line number Diff line change
Expand Up @@ -3398,21 +3398,28 @@ check_clauses(Env, ArgsTy, ResTy, Clauses, Caps) ->
end,
{[], [], ArgsTy, Env#env.venv},
Clauses),
% Checking for exhaustive pattern matching
check_exhaustiveness(Env, ArgsTy, Clauses, RefinedArgsTy, VarBindsList, Css).

check_exhaustiveness(Env, ArgsTy, Clauses, RefinedArgsTy, VarBindsList, Css) ->
case {Env#env.exhaust,
ArgsTy =/= any andalso lists:all(fun (Ty) -> refinable(Ty, Env) end, ArgsTy),
lists:all(fun no_guards/1, Clauses),
is_list(RefinedArgsTy) andalso lists:any(fun (T) -> T =/= type(none) end, RefinedArgsTy)} of
%% Checking for exhaustive pattern matching argument-wise,
%% i.e. separately for each argument.
%% This allows to report non-exhaustiveness warnings even if some arguments are not subject
%% to exhaustiveness checking, e.g. 'any'.
lists:foreach(fun ({ArgTy, RefinedArgTy}) ->
check_arg_exhaustiveness(Env, [ArgTy], Clauses, [RefinedArgTy])
end, lists:zip(ArgsTy, RefinedArgsTy)),
{union_var_binds(VarBindsList, Env), constraints:combine(Css)}.

check_arg_exhaustiveness(Env, ArgsTy, Clauses, RefinedArgsTy) ->
Conditions =
{Env#env.exhaust,
ArgsTy =/= any andalso lists:all(fun (Ty) -> refinable(Ty, Env) end, ArgsTy),
lists:all(fun no_guards/1, Clauses),
is_list(RefinedArgsTy) andalso lists:any(fun (T) -> T =/= type(none) end, RefinedArgsTy)},
case Conditions of
{true, true, true, true} ->
[{clause, P, _, _, _}|_] = Clauses,
throw({nonexhaustive, P, gradualizer_lib:pick_value(RefinedArgsTy, Env)});
_ ->
ok
end,
{union_var_binds(VarBindsList, Env), constraints:combine(Css)}.
end.

%% This function checks clauses.
%% * If clauses have 0 arguments;
Expand Down
8 changes: 8 additions & 0 deletions test/should_fail/exhaustive_argumentwise.erl
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
-module(exhaustive_argumentwise).

-export([f/2]).

-type t() :: ala | ola.

-spec f(t(), any()) -> ok.
f(ala, _) -> ok.