Semantic Diff

#9458 Fix shadowing of names declared in %%raw

1 commit

83cf1de1984f1b00d45c63202a0fe032c079ce4f..pr-9458

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

Fix shadowing of names declared in %%raw

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.

modifiedJS shadowed %%raw declaration

%%raw(`function helper() { return 1 }`)@val external helper: unit => int = "helper" let f = () => {  let helper = helper() + 1  helper * helper}
js
Before
After
function helper() { return 1 }function helper() { return 1 };; function f() {function f() { let helper = globalThis.helper() + 1 | 0; return helper * helper | 0; let helper$1 = helper() + 1 | 0; return helper$1 * helper$1 | 0;}}

modifiedJS 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
After
function helper() { return 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

  • 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.