Repository navigation
[12.x] Read JS globals through globalThis wherever a same-named binding is in scope - #9464
Open
illusionalsagacity wants to merge 1 commit into
Conversation
…n scope Backport of rescript-lang#8785 to 12.x. A reference to a @Val global compiled to its bare name, which reads a binding with that name if one is in scope. Previously this was only handled inside the binding's own initializer. The cases of a switch share one scope, and the discriminant is read outside of it. Differences from master: - 12.x has no rescript-lang#8772, which replaced the per-binding rewrite of an initializer with a single walk; rescript-lang#8785 replaces that code as a whole, so the pass here is master's rescript-lang#8785 version without rescript-lang#8772 in between, and rescript-lang#8772's ExternalShadow tests are not included. - 12.x's J.ForRange has five fields and there is no ForOf or ForAwaitOf. - Js_call_info.na_full_call takes the transformed-JSX flag on 12.x. - ppx_apply_test.mjs: 12.x already printed the toplevel `xx` unsuffixed, so only the read of the global changes to `globalThis.xx` (on 12.x it read the local `xx = 6`). Signed-off-by: Rob <illusionalsagacity@users.noreply.github.com>
This branch has not been deployed
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.
Backport of #8785 to 12.x. It is a prerequisite for the backport of #9417 (branch
fix-for-loop-closure-12.x), whose later commits change which bindingsJs_pass_external_shadowcounts.Problem
A reference to a
@valglobal compiled to its bare name, which reads a binding with that name if one is in scope there.Js_pass_external_shadowonly rewrote the global toglobalThis.xinside the initializer of the binding itself, and a@scopeaccess through a toplevel binding's name.Before (
readAAfter()returns the functionshadowA;paramBreads its parameter):After:
Fix
As on master,
Js_pass_external_shadowtracks the names of the bindings in scope (declarations of a block, which are in scope in the whole block, function parameters, loop variables, the catch parameter in its handler, imported modules, and the declarations of all cases of a switch, which share one scope), and reads a global with such a name throughglobalThis. A switch's discriminant is read outside the scope of its cases.Differences from master:
ExternalShadowcases are not included.J.ForRangehas five fields, and there is nofor...oforfor await...of.Js_call_info.na_full_calltakes the transformed-JSX flag on 12.x, which the OUnit test passes.fix-shadow-dollar-12.x) and is based onmaintenance/12.x. On master Read JS globals through globalThis wherever a same-named binding is in scope #8785 was merged before Fix shadowed JS names colliding with $ identifiers #8778.Tests
tests/tests/src/external_shadow_test.resis master's: globals shadowed by a toplevel binding before and after their reads, a parameter, a local binding after a read in the same block, a binding in one case of a switch, a loop variable and an imported module. Its.mjsis the same as on master. Built with 12.x before this change, its test fails.tests/ounit_tests/ounit_external_shadow_tests.ml(master's) checks that a switch's discriminant reads the global directly and a read in a case goes throughglobalThis. It fails without this change.ExternalShadow.mjsandppx_apply_test.mjschange as on master. Inppx_apply_test.mjs12.x printed the toplevelxxwithout a suffix already, sohread the localxx = 6instead of the global; now it readsglobalThis.xx. No other module intests/testschanges output.Later changes on master to the pass don't fix regressions from #8785. #9284 also collects the declarations in a nested
Block, which it needs because it moves the pass afterJs_pass_tailcall_inlineandJs_pass_flatten_and_mark_dead; on 12.x the pass still runs before them, right afterJs_pass_flatten, so that isn't needed here. The later commits of #9417 change the pass, and are backported infix-for-loop-closure-12.x.Investigated by Claude.