Semantic Diff

#9423 Parenthesise the in operator in a for loop's header

1 commit

ecb21d0a24d0f820a270369339613d6676a19b9c..pr-9423

96b9d9314 · illusionalsagacity · Nov 25, 2026, 19:31 UTC

Parenthesise the in operator in a for loop's header

Why

JavaScript doesn't allow a bare in operator in the initializer of a for header, where the printer puts the start of a ReScript for loop and an end that is neither a number nor a variable. A start or end using Dict.has was therefore printed as a syntax error. While Js_dump prints the initializer, Ext_pp now records that in is forbidden until the next brackets, and In is printed in parentheses there.

modifiedJS for loop whose end uses in

let f = (d, g) => {  for i in 0 to d->Dict.has("a") ? 5 : 4 {    g(i)  }}
js
Before a syntax error
After
for (let i = 0, i_finish = "a" in d ? 5 : 4; i <= i_finish; ++i) {for (let i = 0, i_finish = ("a" in d) ? 5 : 4; i <= i_finish; ++i) { g(i); g(i);}}

modifiedJS for loop whose start uses in

let f = (d, g) => {  for i in d->Dict.has("a") ? 1 : 2 to 3 {    g(i)  }}
js
Before a syntax error
After
for (let i = "a" in d ? 1 : 2; i <= 3; ++i) {for (let i = ("a" in d) ? 1 : 2; i <= 3; ++i) { g(i); g(i);}}

Review

  • Does an in already inside brackets, such as a call's argument, get parentheses? No: every bracket the printer opens through Ext_pp allows in again. Only brackets it writes as plain strings, such as a template's ${…}, don't, so an in there gets redundant but valid parentheses.
  • Which checked-in outputs change? None, besides the new test's.