Semantic Diff

#9419 Evaluate self tail call arguments left to right

2 commits

64cdd3c68bf1bc9602acd146b7e72339e2e56d65..pr-9419

ecb011902 · illusionalsagacity · Nov 23, 2026, 20:40 UTC

Evaluate self tail call arguments left to right

Why

A self tail call is compiled to a loop that reassigns the parameters, and the assignments were emitted in reverse parameter order, so the arguments were evaluated right to left, unlike any other call. When a later argument mutated state an earlier one read, the earlier one saw the mutated value and the function returned a wrong result. The assignments are now emitted in argument order.

A self tail call whose argument count differs from the parameter count, possible through a cast such as Obj.magic, made the compiler crash; it is now compiled as an ordinary call. A self tail call of a () function, which has no JS parameters, dropped its argument together with the argument's side effects, so a loop could run forever; the side effects are now kept. Such a function still loops whatever a cast passes to it.

modifiedJS self tail call loops

let rec f = (a, b, n) =>  n == 0 ? a + b : f(Console.log("a") == () ? a : 0, Console.log("b") == () ? b : 0, n - 1)
js
Before
After
_n = n - 1 | 0;_b = (console.log("b"), undefined) === undefined ? b : 0; _a = (console.log("a"), undefined) === undefined ? a : 0;_a = (console.log("a"), undefined) === undefined ? a : 0; _b = (console.log("b"), undefined) === undefined ? b : 0;_n = n - 1 | 0;continue;continue;

modifiedDiagnostic self tail call with a different argument count

let rec f = (a, b) => a == 0 ? b : (Obj.magic(f): int => int)(a - 1)
diagnostic
Before
Fatal error: exception Invalid_argument("List.fold_left2")
js
After
function f(a, b) {  if (a === 0) {    return b;  } else {    return f(a - 1 | 0);  }}

modifiedJS self tail call of a () function

let left = ref(3)@inline(never) let tick = () => left := left.contents - 1let rec drain = () => if left.contents > 0 { drain(tick()) } else { 0 }
js
Before
After
function drain() {function drain() { while (true) { while (true) { if (left.contents <= 0) { if (left.contents <= 0) { return 0; return 0; } } tick(); continue; continue; }; };}}

Review

  • Can reordering the assignments change which values the arguments read? No: each argument reads the loop's local copies of the parameters, never the reassigned ones, so only the order of side effects changes; arguments that compile to statements are held in temporaries before any assignment.
  • Does Belt rely on the old order? The reduce of HashMap, HashSet and MutableSet read the rest of the container after its callback only because of it. They now bind the callback's result first, so they still skip entries the callback removes, as forEach does. The other reordered loops of the runtime and Belt only read immutable data or values no callback can change.
  • Which checked-in outputs change? Stdlib_List, Primitive_list, the rewatch test repo's Array.mjs and several tests, each only in the order of those assignments.
d53d2b4f4 · cknitt + Claude · Nov 28, 2026, 06:17 UTC

Update unary_minus_test output for left-to-right tail call arguments

Why

unary_minus_test, added on master by #9422, has a self tail call. With this change its parameters are assigned in argument order, so the checked-in output changes.

modifiedJS

countdown(_start, _x): _start = 0; is now assigned before _x = x - 1;.