Fix @val shadowing (rewrite using globalThis)#8098
Merged
cknitt merged 3 commits intorescript-lang:masterfrom Dec 21, 2025
Merged
Fix @val shadowing (rewrite using globalThis)#8098cknitt merged 3 commits intorescript-lang:masterfrom
@val shadowing (rewrite using globalThis)#8098cknitt merged 3 commits intorescript-lang:masterfrom
Conversation
rescript
@rescript/darwin-arm64
@rescript/darwin-x64
@rescript/linux-arm64
@rescript/linux-x64
@rescript/runtime
@rescript/win32-x64
commit: |
@val shadowing (rewrite using globalThis)
Member
Author
cristianoc
approved these changes
Dec 21, 2025
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.