Skip to content

Fix shadowing of names declared in %%raw - #9458

Open
cknitt wants to merge 1 commit into
masterfrom
fix-raw-declared-shadow
Open

cknitt wants to merge 1 commit into
masterfrom
fix-raw-declared-shadow

Conversation

@cknitt

@cknitt cknitt commented Nov 28, 2026

Copy link
Copy Markdown
Member

Fixes #9456.

Problem

When a binding has the same name as a function or variable declared in %%raw, the compiler reads that name as globalThis.<name>. A declaration in a module is not a property of globalThis, so the read throws.

%%raw(`function helper() { return 1 }`)
@val external helper: unit => int = "helper"

let f = () => {
  let helper = helper() + 1
  helper * helper
}

Before:

function f() {
  let helper = globalThis.helper() + 1 | 0;
  return helper * helper | 0;
}

After:

function f() {
  let helper$1 = helper() + 1 | 0;
  return helper$1 * helper$1 | 0;
}

Fix

  • When a %%raw program is classified, the compiler now records the names it declares at its top level: functions, classes, variables (including destructuring), imports, and exported declarations.
  • No binding takes one of these names. It gets a $N suffix instead, and the name itself is read as written.
  • A binding that shadows a real global (e.g. document) still reads the global through globalThis, as before.
  • A toplevel binding named like a raw declaration used to give a second declaration of the same name. That is a SyntaxError in an ES module, and in CommonJS the call through globalThis throws. It is now renamed and still exported under its ReScript name (exports.helper = helper$1).

The printer registers a closure's free JS globals like bindings. Those globals keep their own name, so a renamed binding becomes helper$1, not helper$2.

Tests

  • ounit_flow_parser_tests.ml: the names collected from a raw program.
  • raw_declared_shadow_test.res: a local binding, a parameter and a loop variable named like a raw declaration.

Performance

Compile time shows no measurable change (+0.7% CPU over Belt, the large test files and two stress files; noise is ±2% per file). Allocations grow by 0.06% on code without %%raw, from a few extra read-only passes over the generated JS.

🤖 Generated with Claude Code

https://claude.ai/code/session_01Kbf3gGpCUgi7ZeTiVDi2QL

Why:
    A binding named like a function or variable that `%%raw` declares made
    the compiler read that name as `globalThis.<name>`. A declaration in a
    module is no property of `globalThis`, so the read threw. Fixes #9456.

The names a `%%raw` program declares at its toplevel (functions, classes,
variables, imports, also when exported) are now recorded when it is
classified. No binding takes such a name; it gets a `$N` suffix, and the
name is read as it is. A real global shadowed by a binding still goes
through `globalThis`.

JS modified (shadowed %%raw declaration):
Example:
    %%raw(`function helper() { return 1 }`)
    @Val external helper: unit => int = "helper"

    let f = () => {
      let helper = helper() + 1
      helper * helper
    }
Before (js):
    function helper() { return 1 }
    ;

    function f() {
      let helper = globalThis.helper() + 1 | 0;
      return helper * helper | 0;
    }
After (js):
    function helper() { return 1 }
    ;

    function f() {
      let helper$1 = helper() + 1 | 0;
      return helper$1 * helper$1 | 0;
    }

JS modified (toplevel binding named like a %%raw declaration):
Example:
    %%raw(`function helper() { return 1 }`)
    module G = { @Val external helper: unit => int = "helper" }
    let helper = () => G.helper() + 1
Before (js): a second `helper` declaration, a SyntaxError in an ES module; in CommonJS it replaces the raw one and the read throws
    function helper() { return 1 }
    ;

    function helper() {
      return globalThis.helper() + 1 | 0;
    }

    exports.helper = helper;
After (js):
    function helper() { return 1 }
    ;

    function helper$1() {
      return helper() + 1 | 0;
    }

    exports.helper = helper$1;

Review: Why are the names collected for the whole program, not per scope?
    A `%%raw` in a local module declares its names only in that block.
    Keeping every binding off those names everywhere is simpler and still
    right: a plain read then never meets a binding of that name.

Review: Why does `Ext_pp_scope.merge` need `~js`?
    `merge` registers a closure's free JS globals like bindings. Without
    the flag, the global `helper` itself avoided its own name and took a
    phantom `helper$1`, so the renamed binding became `helper$2`.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Kbf3gGpCUgi7ZeTiVDi2QL
Signed-off-by: Christoph Knittel <ck@cca.io>
@github-actions

Copy link
Copy Markdown

Changelog

🐛 Bug fix

  • Fix a local binding named like a function or variable that %%raw declares in the module: the name was read as globalThis.<name>, which throws.

Semantic Diff

1 commit · Full page

Fix shadowing of names declared in %%raw

d24cba6 · cknitt + Claude · Nov 28, 2026, 08:28 UTC

Why

A binding named like a function or variable that %%raw declares made the compiler read that name as globalThis.<name>. A declaration in a module is no property of globalThis, so the read threw. Fixes #9456.

The names a %%raw program declares at its toplevel (functions, classes, variables, imports, also when exported) are now recorded when it is classified. No binding takes such a name; it gets a $N suffix, and the name is read as it is. A real global shadowed by a binding still goes through globalThis.

JS modified (shadowed %%raw declaration)

%%raw(`function helper() { return 1 }`)
@val external helper: unit => int = "helper"

let f = () => {
  let helper = helper() + 1
  helper * helper
}

js

 function helper() { return 1 }
 ;
 
 function f() {
-  let helper = globalThis.helper() + 1 | 0;
-  return helper * helper | 0;
+  let helper$1 = helper() + 1 | 0;
+  return helper$1 * helper$1 | 0;
 }

JS modified (toplevel binding named like a %%raw declaration)

%%raw(`function helper() { return 1 }`)
module G = { @val external helper: unit => int = "helper" }
let helper = () => G.helper() + 1

js · Before: a second helper declaration, a SyntaxError in an ES module; in CommonJS it replaces the raw one and the read throws

 function helper() { return 1 }
 ;
 
-function helper() {
-  return globalThis.helper() + 1 | 0;
+function helper$1() {
+  return helper() + 1 | 0;
 }
 
-exports.helper = helper;
+exports.helper = helper$1;

Review

  • Why are the names collected for the whole program, not per scope? A %%raw in a local module declares its names only in that block. Keeping every binding off those names everywhere is simpler and still right: a plain read then never meets a binding of that name.
  • Why does Ext_pp_scope.merge need ~js? merge registers a closure's free JS globals like bindings. Without the flag, the global helper itself avoided its own name and took a phantom helper$1, so the renamed binding became helper$2.

@codecov

codecov Bot commented Nov 28, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 89.28571% with 6 lines in your changes missing coverage. Please review.
✅ Project coverage is 82.34%. Comparing base (83cf1de) to head (d24cba6).

Files with missing lines Patch % Lines
compiler/typing/classify_function.ml 86.66% 4 Missing ⚠️
compiler/backend/ext_pp_scope.ml 75.00% 1 Missing ⚠️
tests/ounit_tests/ounit_flow_parser_tests.ml 90.00% 1 Missing ⚠️
Additional details and impacted files
@@           Coverage Diff           @@
##           master    #9458   +/-   ##
=======================================
  Coverage   82.34%   82.34%           
=======================================
  Files         462      462           
  Lines       65802    65851   +49     
=======================================
+ Hits        54183    54226   +43     
- Misses      11619    11625    +6     
Files with missing lines Coverage Δ
compiler/backend/js_dump_program.ml 98.48% <100.00%> (+0.02%) ⬆️
compiler/backend/js_pass_external_shadow.ml 98.46% <100.00%> (+0.24%) ⬆️
compiler/backend/ext_pp_scope.ml 97.36% <75.00%> (-2.64%) ⬇️
tests/ounit_tests/ounit_flow_parser_tests.ml 92.85% <90.00%> (-1.09%) ⬇️
compiler/typing/classify_function.ml 83.56% <86.66%> (+1.74%) ⬆️
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@github-actions

Copy link
Copy Markdown

✅ Compiler benchmarks

Allocated words

In total: 655,717,540 → 655,884,830 (+0.03%).

Generated projects: 629,293,217 → 629,391,266 (+0.02%).

None of the 265 benchmarks changed by 0.1% or more.

Instructions

In total: 9,224,547,180 → 9,235,307,342 (+0.12%).

Benchmark Base Head Change
stress/InlineChain.res 138,983,413 139,901,224 +0.66%
stress/ListLiteral.res 131,209,197 132,026,377 +0.62%
packages/@rescript/belt/src/Belt_List.res 118,282,232 118,933,758 +0.55%
packages/@rescript/belt/src/Belt_internalAVLtree.res 136,550,271 137,171,133 +0.45%
stress/LetRec.res 1,378,732,119 1,383,938,093 +0.38%
stress/RefsInLoop.res 392,464,868 393,092,792 +0.16%

Output size

In total: 26,662,166 → 26,663,516 (+0.01%).

None of the 13 benchmarks changed by 0.1% or more.

Pull request d24cba6 compared with base 83cf1de in this run.

@pkg-pr-new

pkg-pr-new Bot commented Nov 28, 2026

Copy link
Copy Markdown

Open in StackBlitz

rescript

npm i https://pkg.pr.new/rescript@9458

@rescript/belt

npm i https://pkg.pr.new/@rescript/belt@9458

@rescript/darwin-arm64

npm i https://pkg.pr.new/@rescript/darwin-arm64@9458

@rescript/linux-arm64

npm i https://pkg.pr.new/@rescript/linux-arm64@9458

@rescript/linux-x64

npm i https://pkg.pr.new/@rescript/linux-x64@9458

@rescript/runtime

npm i https://pkg.pr.new/@rescript/runtime@9458

@rescript/win32-x64

npm i https://pkg.pr.new/@rescript/win32-x64@9458

commit: d24cba6

@github-actions

Copy link
Copy Markdown

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.

A local binding named like a %%raw function reads it as globalThis.<name>, which throws

1 participant