Repository navigation
[12.x] Give each for loop iteration its own variable when the start must run before the end - #9465
Open
illusionalsagacity wants to merge 7 commits into
Conversation
Js_dump printed both operands of the `in` operator, which Dict.has compiles to, at level 0, so that a conditional, a sequence or a comparison got no parentheses and JavaScript took only part of it as the operand. It also printed an `in` without parentheses at levels up to 12, so that an `in` on the right of a comparison, or an operand of `+`, was split the same way. `In` now takes its levels from `instanceof`, which is in the same class of operators. The start of a `for` loop whose end is a number or a variable was printed at level 0 as well. JavaScript reads a comma in the start as the next declaration, so a sequence was a syntax error, or, in a start ending in a variable, a second loop variable. It is now printed at level 1. 12.x has no `for...of` or `for await...of`, so the master change to their iterables and the tests of them don't apply here. Signed-off-by: Rob <illusionalsagacity@users.noreply.github.com> (cherry picked from commit ecb21d0)
JavaScript doesn't allow a bare `in` operator in the initializer of a `for` header, where the printer puts the start of a ReScript `for` loop and an end that is neither a number nor a variable. A start or end using `Dict.has` was therefore printed as a syntax error. While `Js_dump` prints the initializer, `Ext_pp` now records that `in` is forbidden until the next brackets, and `In` is printed in parentheses there. On 12.x, `Ext_pp` lives in compiler/ext and has no source map position tracking, so only the new field is added to its record. (cherry picked from commit 3172330) Signed-off-by: Rob <illusionalsagacity@users.noreply.github.com>
Backport of rescript-lang#8778 to 12.x. Track printed names in Ext_pp_scope so a suffix like x$1 is never reused for an identifier that already contains $, and reserve the raw JS names a program refers to, so a renamed local like x$2 can't capture an external with that name. Differs from master only in using 12.x's Map_string, Map_int and Set_string (data-first, where master uses the stdlib Map and Set), and in keeping Ext_pp_scope.print, which 12.x still has, adapted to the new scope type. Signed-off-by: Rob <illusionalsagacity@users.noreply.github.com>
…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>
… before the end When a for loop's start has side effects and its end needs statements (a try or a switch), the start must be evaluated before those statements. The compiler did that by declaring the loop variable itself before the loop and emitting `for (; i <= tmp; ++i)`, so every iteration shared one variable and closures created in the body all saw its final value. Hold the start in a temporary named after the loop variable (`i_start`) instead, and let the loop declare its own variable. No loop is built without a start any more, so `J.ForRange` and `S.for_` now take the start expression rather than an option. A start without side effects was evaluated after the end's statements, also when it read a mutable variable, field or array element that they change. Hold such a start too. Only a number or a local variable that the end does not assign stays in the loop header. (On master, the delay_safe check from The temporaries of a loop, i_start and i_finish, now drop the quotes of an uppercase exotic loop variable rather than mangle them. Also tests an end temporary that uses `in`, which the rescript-lang#9423 backport parenthesises in the loop header. (cherry picked from commit 3b2d03e) Signed-off-by: Rob <illusionalsagacity@users.noreply.github.com>
Backport of the second commit of rescript-lang#9417 to 12.x. The printer binds a `for` loop's end in the loop header to a temporary named after the loop variable (`i_finish`). A JS global the end refers to may have that name, and the binding then captures it: `let i_finish = i_finish()` reads the temporary before it is initialized and throws a ReferenceError. Js_pass_external_shadow rewrites such references to `globalThis.x` for the bindings it sees, but the printer creates this one after that pass, and Ext_pp_scope let any binding take a reserved raw name unsuffixed. Ext_pp_scope.printer_ident now names a binding the printer introduces so that it is none of the reserved raw names. Differences from master: - Ext_pp_scope uses 12.x's data-first Map_string, Map_int and Set_string. - The test adds loops at the toplevel, before the functions. On 12.x a loop inside a function, or after a function referring to the global, already got a suffixed temporary (the globals a function refers to are added to the printer's scope), so master's cases pass here without the fix; the toplevel loops threw a ReferenceError when the module loaded. - 12.x has CHANGELOG.md in place of master's changes/ fragment. Signed-off-by: Rob <illusionalsagacity@users.noreply.github.com>
Backport of the third commit of rescript-lang#9417 to 12.x. A temporary the compiler creates, such as the `i_start` the previous commits hold a loop's start in, could take the name of a raw JS name the module refers to. Js_pass_external_shadow then rewrote the references to that name in its scope to `globalThis.x`, which is wrong when a `%%raw` declaration in the module binds the name: such a function is no property of `globalThis`, so the call threw a TypeError. Ext_ident.create_tmp now flags the identifier as synthesized. Ext_pp_scope names a synthesized identifier so that it is none of the reserved raw names, and Js_pass_external_shadow does not count it as shadowing anything. This replaces Ext_pp_scope.printer_ident: the printer's `i_finish` is created with create_tmp. Differences from master: - Ext_ident is in compiler/ext on 12.x (compiler/typing on master). - The printer's `i_finish` already came from create_tmp on 12.x, so only the call to printer_ident changes back to Ext_pp_scope.ident in js_dump.ml. - 12.x has CHANGELOG.md in place of master's changes/ fragment. 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 #9417 to 12.x. Builds on the backports of #9423 (
fix-for-loop-in-operator-12.x, #9462, which builds on #9455,fix-printer-operand-levels-12.x), #8778 (fix-shadow-dollar-12.x, #9463) and #8785 (fix-external-shadow-scope-12.x, #9464), in that order. One of the tests has an end temporary usingin, which needs #9423's parentheses in the loop header; on master #9417 likewise builds on #9423. The second and third commits build on the raw JS names thatExt_pp_scopereserves since #8778 and on #8785'sglobalThisrewriting, which were on master before #9417.Problem
When a
forloop's start is not safe to delay (a call, or a read of a mutable variable, field or array element) and its end needs statements (atryor aswitch), the start must be evaluated before those statements. The compiler did that by declaring the loop variable itself before the loop and emittingfor (; i <= tmp; ++i), so every iteration shared one variable and closures created in the body all saw its final value.Before (every closure returns 2):
After:
A loop's temporaries could also capture a JS name the module refers to. The printer binds the end to
i_finish, which could take the name of a global the end calls, solet i_finish = i_finish()threw aReferenceError. And thei_starttemporary could take the name of a function declared in%%raw;Js_pass_external_shadowthen read it throughglobalThis, where a module's function isn't, so the call threw aTypeError.Before (with the first commit):
After:
Fix
As on master:
i_start) and the loop declaresiitself.J.ForRangeandS.for_take the start expression rather than an option.i_startandi_finishdrop the quotes of an uppercase exotic loop variable rather than mangle them (End_finishinstead of$unknown$unknownEnd$unknown_finish).Ext_ident.create_tmpflags the identifiers it creates as synthesized, and the printer'si_finishcomes from it.Ext_pp_scopenames a synthesized identifier so that it is none of the reserved raw JS names, andJs_pass_external_shadowdoesn't count it as shadowing anything. The second commit'sExt_pp_scope.printer_identis replaced by this in the third, as on master.Differences from master:
delay_safe/evaluate_now(from Bind elements and inlined arguments read from assigned variables #9284), and kept a start without side effects in the loop header after the end's statements even when it read a mutable variable, field or array element that they change. A localmay_changecheck inlam_compile.mldecides instead: only a number, or a local variable the end doesn't assign, stays in the loop header. The tests for mutable starts are 12.x-only for that reason.js_fold.ml, whoseForRangecase is updated like the record folds.Ext_pp_scopeuses 12.x's data-firstMap_string,Map_intandSet_string, andExt_identis incompiler/ext(compiler/typingon master).for_loop_finish_global_testhas two 12.x-only cases, loops at the toplevel before any function. On 12.x the globals a function refers to are added to the printer's scope, so a loop inside such a function, or after it, already goti_finish$N, and master's twoi_finishcases pass on 12.x without the fix. The toplevel loops threw aReferenceErrorwhen the module loaded. The temporaries inside the functions are numbered differently from master (i_finish$3instead ofi_finish$1) for the same reason.changes/fragment.Tests
tests/tests/src/for_loop_closure_test.reshas master's cases (closures withtoanddownto, an empty range, start before end, uppercase exotic loop variables, and an end usingin, alone and after a start that runs first), plus 12.x cases for a start in the header before an end without statements, mutable variable, field and array element starts, and an immutable start. The.mjsof master's cases is the same as on master.tests/tests/src/for_loop_finish_global_test.reshas master's cases (an end calling a global named like its temporary, for an uppercase exotic and a lowercase loop variable, and a start calling a%%rawfunction named like its temporary) and the two toplevel loops. Without the second and third commits, the module throws aReferenceErrorwhen it loads, and the%%rawcase throws aTypeError.No other module in
tests/testschanges output (12.x has noargument_evaluation_order_test, whose output changes on master).Investigated by Claude.