Skip to content
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
14 changes: 14 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,20 @@

All notable milestones for the nera project are documented in this file.

## error/3 compiles and raises its reason

- `error(Reason, Args, Options)` and `erlang:error(Reason, Args, Options)`
now compile and raise `error:Reason`, as on BEAM OTP 29. Previously a
direct call failed to build as unresolved `erlang:error/3`, and dynamic
calls raised `error:undef`.
- This also works through tail calls, `apply/3`, fun references and computed
module or function names. As on OTP 29, arbitrary `Args` and `Options`
do not change the exception class or reason.
- Nera accepts the `error_info` option used by OTP libraries, but does not
retain that metadata or `Args` in its arity-only stacktrace frames.
- The `error_3` fixture checks these calls, argument evaluation and exception
control flow natively and on BEAM.

## Binary comprehensions reject bodies that are not bitstrings

- A binary comprehension whose body is not a bitstring now raises
Expand Down
1 change: 1 addition & 0 deletions FEATURES.md
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,7 @@ Maintainers with OTP 29.0.4 installed can verify the catalogue with
| get_keys/0,1 | ✅ | All keys or keys whose values match exactly (`=:=`); order unspecified |
| error/1 | ✅ | |
| error/2 | ✅ | Raises error:Reason through direct, apply and fun calls; Args is not kept, so the top stacktrace frame shows the arity, not the arguments |
| error/3 | 🔶 | Raises error:Reason through direct, apply and fun calls; Args and Options are accepted but not kept in the stacktrace, including error_info metadata |
| exit/1,2 | ✅ | |
| throw/1 | ✅ | |
| apply/3 | ✅ | Via nera_rt_apply |
Expand Down
1 change: 1 addition & 0 deletions crates/nera-ir/src/bif_table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,7 @@ pub const BIF_TABLE: &[BifEntry] = &[
BifEntry { module: "erlang", function: "throw", arity: 1, ffi_symbol: Some("nera_rt_throw"), param_names: None, spec: Some("-spec throw(Any) -> no_return().") },
BifEntry { module: "erlang", function: "error", arity: 1, ffi_symbol: Some("nera_rt_error"), param_names: None, spec: Some("-spec error(Reason) -> no_return().") },
BifEntry { module: "erlang", function: "error", arity: 2, ffi_symbol: Some("nera_rt_error_2"), param_names: Some(&["Reason", "Args"]), spec: Some("-spec error(Reason, Args) -> no_return().") },
BifEntry { module: "erlang", function: "error", arity: 3, ffi_symbol: Some("nera_rt_error_3"), param_names: Some(&["Reason", "Args", "Options"]), spec: Some("-spec error(Reason, Args, Options) -> no_return().") },
BifEntry { module: "erlang", function: "exit", arity: 1, ffi_symbol: Some("nera_rt_exit"), param_names: None, spec: Some("-spec exit(Reason) -> no_return().") },
BifEntry { module: "erlang", function: "raise", arity: 3, ffi_symbol: Some("nera_rt_raise"), param_names: Some(&["Class", "Reason", "Stacktrace"]), spec: Some("-spec raise(Class, Reason, Stacktrace) -> no_return() when Class :: error | exit | throw.") },
BifEntry { module: "erlang", function: "halt", arity: 0, ffi_symbol: Some("nera_rt_halt_0"), param_names: None, spec: Some("-spec halt() -> no_return().") },
Expand Down
1 change: 1 addition & 0 deletions crates/nera-runtime/src/ffi/apply.rs
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,7 @@ pub(super) unsafe fn apply_impl(
(Some("erlang"), Some("throw"), 1) => Some(nera_rt_throw(ctx, arg_terms[0])),
(Some("erlang"), Some("error"), 1) => Some(nera_rt_error(ctx, arg_terms[0])),
(Some("erlang"), Some("error"), 2) => Some(nera_rt_error_2(ctx, arg_terms[0], arg_terms[1])),
(Some("erlang"), Some("error"), 3) => Some(nera_rt_error_3(ctx, arg_terms[0], arg_terms[1], arg_terms[2])),
(Some("erlang"), Some("exit"), 1) => Some(nera_rt_exit(ctx, arg_terms[0])),
(Some("erlang"), Some("halt"), 0) => Some(nera_rt_halt_0(ctx)),
(Some("erlang"), Some("halt"), 1) => Some(nera_rt_halt_1(ctx, arg_terms[0])),
Expand Down
15 changes: 15 additions & 0 deletions crates/nera-runtime/src/ffi/exception.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,21 @@ pub unsafe extern "C" fn nera_rt_error_2(ctx: *mut ProcessContext, reason: u64,
unsafe { nera_rt_error(ctx, reason) }
}

/// `erlang:error/3`: raise an error exception with `reason`.
///
/// Like OTP 29, accept arbitrary `args` and `options` without changing the
/// class or reason. Nera's arity-only stack frames do not retain the arguments
/// or the `error_info` metadata supplied in options.
#[no_mangle]
pub unsafe extern "C" fn nera_rt_error_3(
ctx: *mut ProcessContext,
reason: u64,
_args: u64,
_options: u64,
) -> u64 {
unsafe { nera_rt_error(ctx, reason) }
}

/// Raise an exit exception.
#[no_mangle]
pub unsafe extern "C" fn nera_rt_exit(ctx: *mut ProcessContext, reason: u64) -> u64 {
Expand Down
1 change: 1 addition & 0 deletions tests/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,7 @@ These are automatically skipped by the runner and show up as SKIP in output.
| `try_after_reraise.erl` | `try ... after` as the only handler in its function: the after body runs once when the body, an `of` clause or a catch clause raises, then the same class, reason and stacktrace reach the caller; a normal return keeps the body's value; an exception from the after body replaces the held one; an after body that waits, times out and collects garbage; an uncaught exit; also run on BEAM | 28 |
| `catch_expr.erl` | Catch expression | ~4 |
| `error_2.erl` | `error/2` and `erlang:error/2` raise `error:Reason` through the auto-imported and qualified names, a tail call, `apply/3`, a fun and a computed module, whatever `Args` holds; the code after the call does not run; `catch` gives `{'EXIT', {Reason, Stack}}`; also run on BEAM | 16 |
| `error_3.erl` | `error/3` and `erlang:error/3` raise `error:Reason` with `error_info` options through direct, tail, apply, fun and computed calls; arbitrary Args/Options, argument evaluation, no continuation after raising, cleanup and catch; also run on BEAM | Table-driven |
| `try_operation_short_circuit.erl` | An operator, unary op, map update, send, spawn, comprehension or `throw` that raises in a `try` body goes to the handler at once: no later send, put, receive or call runs and the first reason is kept, including `catch`, nested `try`, `of`, catch and `after` clauses; a raising guard there still just fails; outside `try`, a failed send or spawn or a `throw` stops the process at once and a map update evaluates every value first; also run on BEAM | Table-driven |

### Pattern Matching
Expand Down
83 changes: 83 additions & 0 deletions tests/error_3.erl
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
%% EXPECT: error_3_ok
%% Intent: error(Reason, Args, Options) raises error:Reason as on OTP 29,
%% including error_info options used by OTP libraries. Run the same fixture
%% natively and on BEAM; stacktrace arguments and error_info are not compared.
-module(error_3).
-export([test/0]).

test() ->
Mod = id(erlang),
Name = id(error),
Fun = fun erlang:error/3,
Options = [{error_info, #{module => erl_stdlib_errors, cause => #{1 => bad_type}}}],
Calls = [
{auto_import, fun(R, A, O) -> error(R, A, O) end},
{qualified, fun(R, A, O) -> erlang:error(R, A, O) end},
{tail_call, fun raise_in_tail/3},
{apply, fun(R, A, O) -> apply(erlang, error, [R, A, O]) end},
{apply_computed, fun(R, A, O) -> apply(Mod, Name, [R, A, O]) end},
{remote_fun, Fun},
{apply_fun, fun(R, A, O) -> apply(Fun, [R, A, O]) end},
{computed_module, fun(R, A, O) -> Mod:error(R, A, O) end},
{computed_function, fun(R, A, O) -> erlang:Name(R, A, O) end}
],
%% Both a small reason and a heap-allocated reason survive every path.
Reasons = [badarg, {invalid_option, id([a, b])}],
[begin
{Call, Reason, {error, Reason}} =
{Call, Reason, outcome(fun() -> F(Reason, [a], Options) end)}
end || {Call, F} <- Calls, Reason <- Reasons],

%% OTP 29 accepts arbitrary Args and Options without changing the reason.
%% In particular, an unrecognized or malformed option does not raise badarg.
Inputs = [
{[], []},
{none, [{error_info, #{}}]},
{not_a_list, Options},
{[a], not_a_list},
{[a], [unknown_option]},
{[a], [{error_info, not_a_map}]},
{[a], [{error_info, #{}} | improper_tail]}
],
[begin
{Call, Args, Opts, {error, x}} =
{Call, Args, Opts, outcome(fun() -> F(x, Args, Opts) end)}
end || {Call, F} <- Calls, {Args, Opts} <- Inputs],

%% Literal auto-imported calls must compile too (not just variable operands).
{error, x} = outcome(fun() -> error(x, [a], [{error_info, #{}}]) end),

%% A non-tail call raises before the next expression; its after body runs.
{error, x} = outcome(fun() ->
try
erlang:error(x, [a], Options),
self() ! after_error
after
self() ! cleanup
end
end),
[cleanup] = drain(),

%% All arguments are evaluated before raising, in source order.
{error, x} = outcome(fun() ->
error(mark(reason, x), mark(args, [a]), mark(options, Options))
end),
[reason, args, options] = drain(),
{throw, option_failure} = outcome(fun() -> error(x, [a], throw(option_failure)) end),

%% Ordinary catch still returns the reason and a stacktrace list.
{'EXIT', {x, Stack}} = (catch error(x, [a], Options)),
true = is_list(Stack),
io:format("error_3_ok~n"),
ok.

raise_in_tail(Reason, Args, Options) -> erlang:error(Reason, Args, Options).

outcome(F) ->
try {ok, F()} catch Class:Reason -> {Class, Reason} end.

mark(Name, Value) -> self() ! Name, Value.

drain() -> receive M -> [M | drain()] after 0 -> [] end.

id(X) -> X.
Loading