|
| 1 | +%% EXPECT: fun_catch_class_ok |
| 2 | +%% Bound catch classes are reads, even when no guard or body reads the name. |
| 3 | +%% Run the same assertions natively and on BEAM. |
| 4 | +-module(fun_catch_class). |
| 5 | +-export([test/0]). |
| 6 | + |
| 7 | +test() -> |
| 8 | + right = requested(), |
| 9 | + lists:foreach(fun({Class, Raised, Expected}) -> |
| 10 | + F = capture(Class, Raised), |
| 11 | + Expected = F(), |
| 12 | + Outer = nested(Class, Raised), |
| 13 | + Inner = Outer(), |
| 14 | + Expected = Inner(), |
| 15 | + Recursive = named(Class, Raised), |
| 16 | + Expected = Recursive(2) |
| 17 | + end, [ |
| 18 | + {error, throw, other}, {throw, throw, matched}, |
| 19 | + {error, error, matched}, {throw, error, other}, |
| 20 | + {exit, exit, matched}, {error, exit, other}, |
| 21 | + {not_a_class, throw, other} |
| 22 | + ]), |
| 23 | + Comprehension = comprehension(id(error)), |
| 24 | + [other, matched, other] = Comprehension(), |
| 25 | + {error, matched, other} = shadowed_head(), |
| 26 | + Fresh = fun() -> try throw(x) catch Class:_ -> Class end end, |
| 27 | + throw = Fresh(), |
| 28 | + io:format("fun_catch_class_ok~n"), |
| 29 | + ok. |
| 30 | + |
| 31 | +requested() -> |
| 32 | + C = id(error), |
| 33 | + F = fun() -> try throw(x) catch C:_ -> wrong; _:_ -> right end end, |
| 34 | + F(). |
| 35 | + |
| 36 | +capture(C, Raised) -> |
| 37 | + fun() -> try raise(Raised) catch C:_ -> matched; _:_ -> other end end. |
| 38 | + |
| 39 | +nested(C, Raised) -> |
| 40 | + fun() -> |
| 41 | + fun() -> try raise(Raised) catch C:_ -> matched; _:_ -> other end end |
| 42 | + end. |
| 43 | + |
| 44 | +named(C, Raised) -> |
| 45 | + fun Loop(0) -> try raise(Raised) catch C:_ -> matched; _:_ -> other end; |
| 46 | + Loop(N) -> Loop(N - 1) |
| 47 | + end. |
| 48 | + |
| 49 | +comprehension(C) -> |
| 50 | + fun() -> |
| 51 | + [try raise(Raised) catch C:_ -> matched; _:_ -> other end |
| 52 | + || Raised <- id([throw, error, exit])] |
| 53 | + end. |
| 54 | + |
| 55 | +shadowed_head() -> |
| 56 | + C = id(error), |
| 57 | + F = fun(C) -> try throw(x) catch C:_ -> matched; _:_ -> other end end, |
| 58 | + {C, F(throw), F(error)}. |
| 59 | + |
| 60 | +raise(throw) -> throw(x); |
| 61 | +raise(error) -> erlang:error(x); |
| 62 | +raise(exit) -> exit(x). |
| 63 | + |
| 64 | +id(X) -> X. |
0 commit comments