Skip to content

Commit

Permalink
Merge pull request #548 from xxdavid/fix_shortcurcuit_inference
Browse files Browse the repository at this point in the history
Fix type inference for `andalso` & `orelse`
  • Loading branch information
erszcz authored Aug 30, 2023
2 parents 9d27389 + adc9de3 commit cb6c1cf
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/typechecker.erl
Original file line number Diff line number Diff line change
Expand Up @@ -2257,8 +2257,8 @@ type_check_logic_op(Env, Op, Arg1, Arg2) ->
{true, Cs4} ->
Inferred =
case Op of
'andalso' -> type(union, [Ty1, {atom, erl_anno:new(0), false}]);
'orelse' -> type(union, [Ty1, {atom, erl_anno:new(0), true}]);
'andalso' -> type(union, [Ty2, {atom, erl_anno:new(0), false}]);
'orelse' -> type(union, [Ty2, {atom, erl_anno:new(0), true}]);
_ -> type(boolean)
end,
{normalize(Inferred, Env)
Expand Down
20 changes: 20 additions & 0 deletions test/should_pass/shortcut_ops_pass.erl
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,26 @@ orelse_infer2(B, N) -> B orelse N.
-spec orelse_infer3(boolean(), boolean()) -> _.
orelse_infer3(B, X) -> B orelse X.

-spec andalso_infer_and_check1(boolean(), number()) -> false | number().
andalso_infer_and_check1(B, N) ->
X = B andalso N,
X.

-spec andalso_infer_and_check2(boolean(), false | number()) -> false | number().
andalso_infer_and_check2(B, N) ->
X = B andalso N,
X.

-spec orelse_infer_and_check1(boolean(), number()) -> true | number().
orelse_infer_and_check1(B, N) ->
X = B orelse N,
X.

-spec orelse_infer_and_check2(boolean(), true | number()) -> true | number().
orelse_infer_and_check2(B, N) ->
X = B orelse N,
X.

-spec is_false_number(false | number()) -> ok.
is_false_number(_) -> ok.

Expand Down

0 comments on commit cb6c1cf

Please sign in to comment.