Skip to content

Commit

Permalink
Merge pull request #571 from erszcz/skip-maybe
Browse files Browse the repository at this point in the history
Skip functions using maybe expr, don't crash on maybe ... else
  • Loading branch information
erszcz authored Oct 11, 2024
2 parents c44e393 + b3445fb commit fd564a4
Show file tree
Hide file tree
Showing 5 changed files with 112 additions and 23 deletions.
19 changes: 16 additions & 3 deletions src/typechecker.erl
Original file line number Diff line number Diff line change
Expand Up @@ -2215,8 +2215,12 @@ do_type_check_expr(Env, {'try', _, Block, CaseCs, CatchCs, AfterBlock}) ->

%% Maybe - value-based error handling expression
%% See https://www.erlang.org/eeps/eep-0049
do_type_check_expr(_Env, {'maybe', Anno, [{maybe_match, _, _LHS, _RHS}]} = MaybeExpr) ->
erlang:throw({unsupported_expression, Anno, MaybeExpr}).
do_type_check_expr(Env, {'maybe', _, _}) ->
%% TODO: handle maybe expr properly
{type(any), Env};
do_type_check_expr(Env, {'maybe', _, _, {'else', _, _}}) ->
%% TODO: handle maybe expr properly
{type(any), Env}.

%% Helper for type_check_expr for funs
-spec type_check_fun(env(), _) -> {type(), env()}.
Expand Down Expand Up @@ -3073,7 +3077,16 @@ do_type_check_expr_in(Env, ResTy, {'try', _, Block, CaseCs, CatchCs, AfterBlock}
end,
%% no variable bindings are propagated from a try expression
%% as that would be "unsafe"
Env.
Env;

%% Maybe - value-based error handling expression
%% See https://www.erlang.org/eeps/eep-0049
do_type_check_expr_in(_Env, _ResTy, {'maybe', _, _}) ->
%% TODO: handle maybe expr properly
erlang:throw({skip, maybe_expr_not_supported});
do_type_check_expr_in(_Env, _ResTy, {'maybe', _, _, {'else', _, _}}) ->
%% TODO: handle maybe expr properly
erlang:throw({skip, maybe_expr_not_supported}).

-spec type_check_arith_op_in(env(), type(), _, _, _, _) -> env().
type_check_arith_op_in(Env, ResTy, Op, P, Arg1, Arg2) ->
Expand Down
46 changes: 46 additions & 0 deletions test/known_problems/should_fail/maybe_expr_should_fail.erl
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
-module(maybe_expr_should_fail).

-ifdef(OTP_RELEASE).
-if(?OTP_RELEASE >= 25).
-if(?FEATURE_AVAILABLE(maybe_expr)).

-feature(maybe_expr, enable).

-export([check1/0, check2/0]).
-export([infer1/0, infer2/1]).

-spec check1() -> integer().
check1() ->
maybe
ok ?= ok,
"one"
end.

-spec check2() -> integer().
check2() ->
maybe
ok ?= not_ok,
one
else
_ -> "two"
end.

-spec infer1() -> integer().
infer1() ->
R = maybe
ok ?= ok
end,
R.

-spec infer2(string()) -> integer().
infer2(Val) ->
R = maybe
ok ?= Val
else
_ -> ok
end,
R.

-endif. %% FEATURE_AVAILABLE
-endif. %% OTP >= 25
-endif. %% OTP_RELEASE
18 changes: 0 additions & 18 deletions test/known_problems/should_pass/maybe_expr.erl

This file was deleted.

4 changes: 2 additions & 2 deletions test/should_pass/binary_literal_pattern.erl
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
-export([f/1]).

-type t1() :: binary().
-type maybe(T) :: undefined | T.
-type option(T) :: undefined | T.

-spec f(maybe(t1())) -> ok | error | other.
-spec f(option(t1())) -> ok | error | other.
f(T) ->
case T of
undefined -> error;
Expand Down
48 changes: 48 additions & 0 deletions test/should_pass/maybe_expr_pass.erl
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
-module(maybe_expr_pass).

-ifdef(OTP_RELEASE).
-if(?OTP_RELEASE >= 25).
-if(?FEATURE_AVAILABLE(maybe_expr)).

-feature(maybe_expr, enable).

-export([check1/0, check2/0]).
-export([infer1/0, infer2/1]).

-spec check1() -> integer().
check1() ->
maybe
ok ?= ok,
1
end.

-spec check2() -> integer().
check2() ->
maybe
ok ?= not_ok,
1
else
_ -> 2
end.

-spec infer1() -> integer().
infer1() ->
R = maybe
ok ?= ok,
1
end,
R.

-spec infer2(string()) -> {ok, string()} | error.
infer2(Val) ->
R = maybe
"ok" ?= Val,
{ok, Val}
else
_ -> error
end,
R.

-endif. %% FEATURE_AVAILABLE
-endif. %% OTP >= 25
-endif. %% OTP_RELEASE

0 comments on commit fd564a4

Please sign in to comment.