Fix @val shadowing (rewrite using globalThis)
#8098
Merged
+155
−1
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Fix
@valshadowing (fixes #8093)Summary
When a
@valexternal is assigned to a localletwith the same name (example: "process"), the JS output becomeslet process = process;and crashes at runtime. This PR rewrites the initializer toglobalThis.processonly when a local lexical binding would shadow that global, while leaving all other uses unchanged.Example:
Output after this change:
Why this approach
let.globalThiseverywhere would be noisier and change more output than necessary.Why existing name mangling cannot solve it
ReScript’s
$1/$2name mangling only applies to compiler-owned identifiers (Ident.t) that go throughExt_pp_scope.@valexternals are emitted as raw JS globals (E.js_global/Ext_ident.create_js) and do not participate in that scope renaming system. That means the compiler cannot “rename”processtoprocess$1in this case, because the global read is not anIdent.tthat the mangler controls. We must explicitly disambiguate the global read instead.Implementation notes
globalThis.<name>.delete) or unnecessary rewrites.Tests
tests/tests/src/ExternalShadow.rescovers the shadowing case and a non-shadowed alias.