Repository navigation
fix(runtime): call operators as erlang functions - #110
Merged
Merged
Conversation
The 29 operator functions of the `erlang` module, such as `erlang:'+'(1, 2)`, `'=:='/2`, `'++'/2` and `'!'/2`, build natively and return what the operator returns. `apply(erlang, '+', Args)`, a computed `M:F(...)` call and `fun erlang:'+'/2` reach the same operator instead of raising `undef`. Arithmetic, boolean and comparison operator functions are accepted in guards, as erlc allows; `'++'`, `'--'` and `'!'` stay illegal there. A fun reference to a quoted name, such as `fun lists:'reverse'/1`, names the function without its quotes, and `nera fmt` keeps it intact. Editor completion after `erlang:` offers the operator functions and inserts their names quoted, such as `'div'`.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Why
Erlang code may call an operator by its function name, such as `erlang:'+'(1, 2)` or `lists:foldl(fun erlang:'+'/2, 0, Xs)`, and BEAM OTP 29 returns the operator's value, but Nera rejected or failed these calls.
People who write such calls will notice that their code now builds and runs natively; other code behaves as before.
What changed
Before and after
Before: `erlang:'+'(1, 2)` failed to build with "unresolved function `erlang:+/2`", a guard using it was rejected, `fun erlang:'+'/2` did not build and `apply(erlang, '+', [1, 2])` raised `undef`.
After: All 29 operator functions give the same 133 probe lines natively as on BEAM OTP 29, and the new `erlang_operator_functions` test passes natively and on BEAM.
This changes what the compiler and runtime accept and return, which shows in program output rather than on a screen.
Checked
How it was checked
cargo clippy --workspace --all-targets --locked -- -D warnings
cargo test --workspace --locked --no-fail-fast
python3 scripts/test-allocator-config.py
./target/release/nera test -j 1 --timeout 30
python3 scripts/test-stacktrace-order.py
erlc and erl on tests/erlang_operator_functions.erl
Risk
Risk above low: Blast radius, medium — Touches the built-in table, runtime apply, guard check, parser and editor completion
Full risk assessment
Not done
When an operator function raises, the top stacktrace frame is the caller, as for the operator itself, while BEAM shows `erlang:'+'/2`; FEATURES.md notes this.