Skip to content

[12.x] Fix shadowed JS names colliding with $ identifiers - #9463

Open
illusionalsagacity wants to merge 1 commit into
rescript-lang:maintenance/12.xfrom
illusionalsagacity:fix-shadow-dollar-12.x
Open

illusionalsagacity wants to merge 1 commit into
rescript-lang:maintenance/12.xfrom
illusionalsagacity:fix-shadow-dollar-12.x

Conversation

@illusionalsagacity

Copy link
Copy Markdown
Contributor

Backport of #8778 to 12.x. It is a prerequisite for the backport of #9417 (branch fix-for-loop-closure-12.x), whose later commits build on the raw JS names that Ext_pp_scope reserves here.

Problem

A shadowed variable is printed with a $N suffix, but identifiers may themselves contain $, so a suffixed name like x$1 could equal another identifier's name. A suffixed local could also capture a raw JS name the module refers to, such as a @val global named y$1.

let paramCollision = (x, \"x$1") => {
  let x = \"x$1"(x)
  let y = \"x$1"(2)
  x + y
}

@val external y1: int = "y$1"
let y = base * 2
let topShadow = {
  let y = y + y1
  y + y
}

Before (a syntax error: x$1 is declared twice; y$1 reads the local, not the global):

function paramCollision(x, x$1) {
  let x$1 = x$1(x);
  let y = x$1(2);
  return x$1 + y | 0;
}

let y$1 = y + y$1 | 0;

After:

function paramCollision(x, x$1) {
  let x$2 = x$1(x);
  let y = x$1(2);
  return x$2 + y | 0;
}

let y$2 = y + y$1 | 0;

Fix

As on master:

  • Ext_pp_scope tracks every printed name in scope and picks the first $N suffix that is not in use.
  • Js_dump_program reserves the raw JS names a program refers to, so that no suffixed name equals one of them.

Differences from master:

Tests

tests/tests/src/shadow_dollar_test.res is master's: parameters and bindings named x$1 next to shadowed x, in nested functions, and raw JS names y$1, z$2 and w$1 next to shadowed locals at the toplevel and in a function. Its .mjs is the same as on master. Built with 12.x before this change, the module is a syntax error. No other module in tests/tests changes output.

No later change on master fixes a regression from #8778. The later commits of #9417 build on it, and are backported in fix-for-loop-closure-12.x.

Investigated by Claude.

Backport of rescript-lang#8778 to 12.x.

Track printed names in Ext_pp_scope so a suffix like x$1 is never reused
for an identifier that already contains $, and reserve the raw JS names a
program refers to, so a renamed local like x$2 can't capture an external
with that name.

Differs from master only in using 12.x's Map_string, Map_int and
Set_string (data-first, where master uses the stdlib Map and Set), and in
keeping Ext_pp_scope.print, which 12.x still has, adapted to the new scope
type.

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