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
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
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
iin its header.