Semantic Diff

#9417 Give each for loop iteration its own variable when the start must run before the end

4 commits

3172330c7d3f1c7df8b28e3085cadc638f7d4872..pr-9417

fb5c6fb17 · illusionalsagacity · Nov 23, 2026, 20:27 UTC

Give each for loop iteration its own variable when the start must run before the end

Why

When a for loop's start is not safe to delay (a call, or a read of a mutable variable or field) and its end needs statements (a try or a switch), the start must be evaluated before those statements. The compiler did that by declaring the loop variable itself before the loop and emitting for (; i <= tmp; ++i), so every iteration shared one variable and closures created in the body all saw its final value. The start is now held in a temporary named after the loop variable (i_start, through evaluate_now) and the loop declares i itself. No loop is built without a start any more, so J.ForRange and S.for_ now take the start expression rather than an option. Both temporaries of a loop, i_start and i_finish, now drop the quotes of an uppercase exotic loop variable rather than mangle them.

modifiedJS for loop with a start evaluated before its end

@inline(never) let id = x => xlet fs = []for i in id(0) to try { id(1) } catch { | _ => 0 } {  fs->Array.push(() => i)}
js
Before
After
let i = id(0);let i_start = id(0); let tmp;let tmp; try {try { tmp = id(1); tmp = id(1);} catch (exn) {} catch (exn) { tmp = 0; tmp = 0;}} for (; i <= tmp; ++i) {for (let i = i_start; i <= tmp; ++i) { fs.push(() => i); fs.push(() => i);}}

modifiedJS temporaries of an uppercase exotic loop variable

@inline(never) let id = x => xlet fs = []for \"End" in 3 to id(4) {  fs->Array.push(() => \"End")}
js
Before
After
for (let End = 3, $unknown$unknownEnd$unknown_finish = id(4); End <= $unknown$unknownEnd$unknown_finish; ++End) {for (let End = 3, End_finish = id(4); End <= End_finish; ++End) { fs.push(() => End); fs.push(() => End);}}

Review

  • Are loops with a constant or immutable start affected? No: such a start is delay safe and stays in the loop header as before.
  • Is the end still re-read each iteration when it is a variable? Yes, unchanged here; #9356 tracks it.
  • Which checked-in outputs change? argument_evaluation_order_test.mjs, whose loop now declares i in its header.
247bd56b7 · illusionalsagacity · Nov 26, 2026, 22:09 UTC

Keep a for loop's end temporary from capturing a JS global of its name

Why

The printer binds a for loop's end in the loop header to a temporary named after the loop variable (i_finish). A JS global the end refers to may have that name, and the binding then captures it: let i_finish = i_finish() reads the temporary before it is initialized and throws a ReferenceError. Js_pass_external_shadow rewrites such references to globalThis.x for the bindings it sees, but the printer creates this one after that pass, and Ext_pp_scope let any binding take a reserved raw name unsuffixed. Ext_pp_scope.printer_ident now names a binding the printer introduces so that it is none of the reserved raw names. This answers Codex's review finding on #9417: the previous commit made the temporary of an uppercase exotic loop variable readable (End_finish), so it could now equal a global's name; a lowercase loop variable had the same bug before.

modifiedJS end of a for loop calling a global named like its temporary

@val external iFinish: unit => int = "i_finish"let fs = []for i in 2 to iFinish() {  fs->Array.push(() => i)}
js
Before
After
for (let i = 2, i_finish = i_finish(); i <= i_finish; ++i) {for (let i = 2, i_finish$1 = i_finish(); i <= i_finish$1; ++i) { fs.push(() => i); fs.push(() => i);}}

Review

  • Does the start's temporary need the same? No: lam_compile creates i_start before Js_pass_external_shadow runs, which rewrites a reference to a global i_start in its scope to globalThis.i_start.
  • Which checked-in outputs change? None besides the new for_loop_finish_global_test.mjs.
53c3c639f · illusionalsagacity · Nov 27, 2026, 21:32 UTC

Keep compiler temporaries clear of the raw JS names a module uses

Why

A temporary the compiler creates, such as the i_start the previous commits hold a loop's start in, could take the name of a raw JS name the module refers to. Js_pass_external_shadow then rewrote the references to that name in its scope to globalThis.x, which is wrong when a %%raw declaration in the module binds the name: such a function is no property of globalThis, so the call threw a TypeError. Ext_ident.create_tmp now flags the identifier as synthesized. Ext_pp_scope names a synthesized identifier so that it is none of the reserved raw names, and Js_pass_external_shadow does not count it as shadowing anything. The printer's i_finish is created with create_tmp too, which replaces Ext_pp_scope.printer_ident. This answers Codex's review finding on #9417.

modifiedJS start of a for loop calling a raw function named like its temporary

%%raw(`function i_start() { return 1 }`)@val external start: unit => int = "i_start"let f = g => {  let fs = []  for i in start() to {    try g() catch { | _ => 0 }  } {    fs->Array.push(() => i)  }  fs->Array.map(h => h())}
js
Before
After
let i_start = globalThis.i_start();let i_start$1 = i_start();

Review

  • Which other outputs can change? Any temporary from create_tmp, such as tmp, named like a raw JS name in its scope now gets a $N suffix instead of the reference going through globalThis. No checked-in output besides for_loop_finish_global_test.mjs changes.
509986aff · illusionalsagacity · Nov 27, 2026, 21:41 UTC

Test a for loop's end temporary that uses in

The first commit prints the end of a loop whose start must run before the end as the end temporary in the loop header. An end that binds through a try and then tests Dict.has, from Codex's review of #9417, and a conditional end using Dict.has print an in there, which JavaScript reads as a for...in unless it is in parentheses. #9423's handling of the loop header gives them parentheses; these tests check that both loops run.