Repository navigation
Conversation
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>
Changelog🐛 Bug fix
Semantic Diff1 commit · Full page Fix shadowing of names declared in %%rawd24cba6 · cknitt + Claude · Nov 28, 2026, 08:28 UTC
The names a JS modified (shadowed %%raw declaration) %%raw(`function helper() { return 1 }`)
@val external helper: unit => int = "helper"
let f = () => {
let helper = helper() + 1
helper * helper
}
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
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
|
Codecov Report❌ Patch coverage is 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
🚀 New features to boost your workflow:
|
✅ Compiler benchmarksAllocated wordsIn 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. InstructionsIn total: 9,224,547,180 → 9,235,307,342 (+0.12%).
Output sizeIn 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. |
rescript
@rescript/belt
@rescript/darwin-arm64
@rescript/linux-arm64
@rescript/linux-x64
@rescript/runtime
@rescript/win32-x64
commit: |
|
Developer playground preview: https://rescript-lang.github.io/rescript/dev-playground/?version=pr-9458 |
Fixes #9456.
Problem
When a binding has the same name as a function or variable declared in
%%raw, the compiler reads that name asglobalThis.<name>. A declaration in a module is not a property ofglobalThis, so the read throws.Before:
After:
Fix
%%rawprogram is classified, the compiler now records the names it declares at its top level: functions, classes, variables (including destructuring), imports, and exported declarations.$Nsuffix instead, and the name itself is read as written.document) still reads the global throughglobalThis, as before.globalThisthrows. 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, nothelper$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