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
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
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
$Nsuffix 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 withExt_ident.no_escape, now exported, the characters name mangling keeps. An escape in an identifier, as in a\u0069_finishcall, 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.tnor the.cmjformat 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 = 1with%raw("x")stays as it was. Only a second binding of the name, printedx$1, moves tox$2when raw code spellsx$1, as above. Raw code that spellsx$1relies 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.