Skip to content

fix(runtime): call operators as erlang functions - #110

Merged
mremond merged 1 commit into
mainfrom
infinitary-batch/55d0bc89
Dec 4, 2026
Merged

mremond merged 1 commit into
mainfrom
infinitary-batch/55d0bc89

Conversation

@mremond

@mremond mremond commented Dec 4, 2026

Copy link
Copy Markdown
Member

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

  • 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'`.

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

  • Probe calling all 29 operator functions directly, through apply/3 and through a computed module and function, natively and on BEAM OTP 29: 133 of 133 output lines identical
  • New erlang_operator_functions test with the main branch compiler: fails to build at the first guard
  • Same test with the runtime apply entries removed: fails with error:undef
  • New parser, guard and completion unit tests against the code before the fix: all three fail
  • erlang_operator_functions repeated 10 times natively, 10 with one scheduler and 10 on BEAM; each new unit test 10 times: every run passed
  • Full local gate on macOS: release build, clippy with warnings as errors, workspace tests, allocator check, end-to-end suite, stacktrace order: 2306 unit tests and 562 end-to-end tests passed, none failed
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
Dimension Level Why
Blast radius medium Touches the built-in table, runtime apply, guard check, parser and editor completion
Behavior low Calls that failed or were rejected now work; existing code takes the same paths
Undo low Revert the two commits
Evidence low Native output matches BEAM for every operator and the full suite passes
Sensitivity low No saved data, sign-ins or release steps
Novelty low Table entries and dispatch arms that reuse the operators' existing runtime routines
Dependencies low No new packages
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.

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'`.
@mremond
mremond merged commit e82331b into main Dec 4, 2026
21 checks passed
@mremond
mremond deleted the infinitary-batch/55d0bc89 branch December 4, 2026 12:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant