Skip to content

[12.x] Parenthesise the in operator in a for loop's header - #9462

Open
illusionalsagacity wants to merge 2 commits into
rescript-lang:maintenance/12.xfrom
illusionalsagacity:fix-for-loop-in-operator-12.x
Open

illusionalsagacity wants to merge 2 commits into
rescript-lang:maintenance/12.xfrom
illusionalsagacity:fix-for-loop-in-operator-12.x

Conversation

@illusionalsagacity

Copy link
Copy Markdown
Contributor

Backport of #9423 to 12.x. Builds on #9455 (the backport of #9428), which changes the same In case in Js_dump; on master #9423 was likewise rebased on top of #9428.

Problem

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.

let inEnd = (d, g) =>
  for i in 0 to d->Dict.has("a") ? 5 : 4 {
    g(i)
  }
let inStart = (d, g) =>
  for i in d->Dict.has("a") ? 1 : 2 to 3 {
    g(i)
  }

Before (syntax errors):

for (let i = 0, i_finish = "a" in d ? 5 : 4; i <= i_finish; ++i) {
  g(i);
}

for (let i = "a" in d ? 1 : 2; i <= 3; ++i) {
  g(i);
}

After:

for (let i = 0, i_finish = ("a" in d) ? 5 : 4; i <= i_finish; ++i) {
  g(i);
}

for (let i = ("a" in d) ? 1 : 2; i <= 3; ++i) {
  g(i);
}

Fix

As on master:

  • While Js_dump prints the initializer of a for header, Ext_pp records that in is forbidden until the next brackets, and In is printed in parentheses there.
  • Every bracket the printer opens through Ext_pp allows in again, so an in inside a call's arguments or an array gets no extra parentheses.

Differences from master:

  • Ext_pp is in compiler/ext on 12.x and has no source map position tracking, so only the new no_in field is added to its record.

Tests

tests/tests/src/for_loop_in_operator_test.res is master's: an in in the end, in the start, in both, in nested conditionals, inside brackets and in the loop's body. Its .mjs is the same as on master. Built with 12.x before this change (with #9455), the module is a syntax error. No other module in tests/tests changes output.

Investigated by Claude.

Js_dump printed both operands of the `in` operator, which Dict.has
compiles to, at level 0, so that a conditional, a sequence or a
comparison got no parentheses and JavaScript took only part of it as the
operand. It also printed an `in` without parentheses at levels up to
12, so that an `in` on the right of a comparison, or an operand of `+`,
was split the same way. `In` now takes its levels from `instanceof`,
which is in the same class of operators.

The start of a `for` loop whose end is a number or a variable was
printed at level 0 as well. JavaScript reads a comma in the start as the
next declaration, so a sequence was a syntax error, or, in a start ending
in a variable, a second loop variable. It is now printed at level 1.

12.x has no `for...of` or `for await...of`, so the master change to
their iterables and the tests of them don't apply here.

Signed-off-by: Rob <illusionalsagacity@users.noreply.github.com>
(cherry picked from commit ecb21d0)
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.

On 12.x, `Ext_pp` lives in compiler/ext and has no source map position
tracking, so only the new field is added to its record.

(cherry picked from commit 3172330)
Signed-off-by: Rob <illusionalsagacity@users.noreply.github.com>

This branch has not been deployed

No deployments
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