Semantic Diff

#9472 Keep names that raw code spells from the compiler's temporaries

2 commits

e51ee460f8c8190e9c0edec81ae11284233d22b6..pr-9472

bec5f4fad · illusionalsagacity · Nov 28, 2026, 18:50 UTC

Keep names that raw code spells from the compiler's temporaries

Why

A name the compiler makes up, such as the temporary i_finish that holds a for loop's end, avoids the JS names of the externals the program refers to (Ext_pp_scope.reserve), but not the names that %raw and %%raw code refers to. A loop whose end or body calls a function of that name in raw code read the temporary instead, before it was initialized. Js_dump_program.initial_scope now also reserves every word of the program's raw code, every run of [A-Za-z0-9_$].

modifiedJS raw code calling a function named like a loop's temporary

%%raw(`function i_finish() { return 3 }`)let f = () => {  let s = ref(0)  for i in 0 to %raw(`i_finish()`) {    s := s.contents + i  }  s.contents}
js
Before
After
function i_finish() { return 3 }function i_finish() { return 3 };; function f() {function f() { let s = 0; let s = 0; for (let i = 0, i_finish = (i_finish()); i <= i_finish; ++i) { for (let i = 0, i_finish$1 = (i_finish()); i <= i_finish$1; ++i) { s = s + i | 0; s = s + i | 0; } } return s; return s;}}

modifiedJS raw code spelling a suffixed name

let f = x => {  let g = () => {    let x = x + 1    (%raw(`typeof x$1`): string) ++ Int.toString(x)  }  g()}
js
Before
After
function f(x) {function f(x) { let g = () => { let g = () => { let x$1 = x + 1 | 0; return (typeof x$1) + x$1.toString(); let x$2 = x + 1 | 0; return (typeof x$1) + x$2.toString(); }; }; return g(); return g();}}

Review

  • Why scan the text and not parse it? The scan needs no JS parser, so it does not depend on the vendored Flow parser, which is to be upgraded or replaced (#8567). A word in a string, a comment or a property name is reserved too, which costs at most a $N suffix on a name the compiler makes up. The scan is linear in the size of the raw code, and raw code is printed in the same pass anyway. It splits words with Ext_ident.no_escape, now exported, the characters name mangling keeps. An escape in an identifier, as in a \u0069_finish call, is not decoded.
  • Why at print time? The raw code's text is in the JS IR, so raw code inlined from another module is scanned as well, and neither Js_raw_info.t nor the .cmj format changes.
  • Does a source binding that raw code refers to change? No: a name from the source is printed unsuffixed whether it is reserved or not, so let x = 1 with %raw("x") stays as it was. Only a second binding of the name, printed x$1, moves to x$2 when raw code spells x$1, as above. Raw code that spells x$1 relies on a name the compiler makes up, and no longer sees the binding; the changelog fragment says so.
  • Which checked-in outputs change? Only for_loop_finish_global_test's.
0f988b1a5 · illusionalsagacity · Nov 29, 2026, 04:16 UTC

Collect JS global names and raw code words separately in initial_scope

Keep js_names (Ext_ident.is_js) and raw_words (Raw_js_code) in separate sets in Js_dump_program.initial_scope, passing their union to Ext_pp_scope.reserve. This makes rebasing with #9458 safe: #9458 intersects raw_declarations with only the JS globals read by the program (js_names), whereas merging raw words into the same set would pull every %%raw-declared name into ~declared and rename unsuffixed bindings that %raw snippets expect to read.