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

Fix var binds in guard sequences #533

Merged
merged 4 commits into from
May 2, 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
4 changes: 2 additions & 2 deletions src/gradualizer_fmt.erl
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ format_type_error({type_error, op_type_too_precise, Op, Anno, Ty}, Opts) ->
pp_type(Ty, Opts)]);
format_type_error({type_error, arith_error, ArithOp, Anno, Ty1, Ty2}, Opts) ->
io_lib:format(
"~sThe operator ~p~s is requires numeric arguments, but "
"~sThe operator ~p~s requires numeric arguments, but "
"has arguments of type ~s and ~s~n",
[format_location(Anno, brief, Opts),
ArithOp,
Expand All @@ -211,7 +211,7 @@ format_type_error({type_error, arith_error, ArithOp, Anno, Ty1, Ty2}, Opts) ->
pp_type(Ty2, Opts)]);
format_type_error({type_error, int_error, ArithOp, Anno, Ty1, Ty2}, Opts) ->
io_lib:format(
"~sThe operator ~p~s is requires integer arguments, but "
"~sThe operator ~p~s requires integer arguments, but "
" has arguments of type ~s and ~s~n",
[format_location(Anno, brief, Opts),
ArithOp,
Expand Down
12 changes: 9 additions & 3 deletions src/typechecker.erl
Original file line number Diff line number Diff line change
Expand Up @@ -4593,7 +4593,7 @@ mirror_comp_op(Comm) -> Comm.
-spec check_guard_expression(env(), term()) -> env().
check_guard_expression(Env, {call, _, {atom, _, Fun}, Vars}) ->
Env#env{venv = check_guard_call(Fun, Vars)};
check_guard_expression(Env, {call, _, {remote,_, {atom, _, erlang},{atom, _, Fun}}, Vars}) ->
check_guard_expression(Env, {call, _, {remote,_, {atom, _, erlang}, {atom, _, Fun}}, Vars}) ->
Env#env{venv = check_guard_call(Fun, Vars)};
check_guard_expression(Env, {op, _, Op, {var, _, Var}, {Tag, _, Value}})
when ?is_comp_op(Op), Tag =:= integer orelse Tag =:= atom ->
Expand All @@ -4617,8 +4617,14 @@ check_guard_expression(Env, Guard) ->
-spec check_guard(env(), list()) -> env().
check_guard(Env, GuardSeq) ->
RefTys = lists:foldl(fun(Guard, E) ->
Ne = check_guard_expression(E, Guard),
union_var_binds([Ne], E)
NEnv = check_guard_expression(E, Guard),
%% This is somewhat tricky.
%% check_guard_expression/2 will return an env with just
%% the types of vars used in the guard expression - we want these to
%% refine any types previously in the env.
%% We still want to keep types of vars not used in the guard expression,
%% though, so we pass both NEnv and E to union_var_binds/2.
union_var_binds([NEnv, E], E)
end, Env, GuardSeq),
Env#env{venv = maps:merge(Env#env.venv, RefTys#env.venv)}.

Expand Down
18 changes: 18 additions & 0 deletions test/should_pass/guard_sequences_pass.erl
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
-module(guard_sequences_pass).

-export([foo/2]).

-spec foo(List1, List2) -> List2 when
List1 :: [Int],
List2 :: [Int|{Int,Int}],
Int :: integer().
foo([], Acc) ->
Acc;
foo([H | T], []) ->
foo(T, [H]);
foo([H | T], [HA | TA]) when is_integer(HA), H == HA + 1 ->
foo(T, [{HA, H} | TA]);
foo([H | T], [{HAB, HAE} | TA]) when H == HAE + 1 ->
foo(T, [{HAB, H} | TA]);
foo([H | T], Acc) ->
foo(T, [H | Acc]).