Repository navigation
[12.x] Evaluate self tail call arguments left to right - #9461
Open
illusionalsagacity wants to merge 2 commits into
Open
illusionalsagacity wants to merge 2 commits into
illusionalsagacity wants to merge 2 commits into
Conversation
A self tail call is compiled to a loop that reassigns the parameters, and the assignments were emitted in reverse parameter order, so the arguments were evaluated right to left. When a later argument mutated state an earlier one read, the earlier one saw the mutated value and the function returned a wrong result. Emit the assignments in argument order. An argument that compiles to statements still runs before the other arguments, as in any other call on 12.x. A self tail call whose argument count differs from the parameter count, possible through a cast such as `Obj.magic` that is resolved only once a function is inlined, made the compiler crash. Compile it as an ordinary call. A self tail call of a `()` function, which has no JS parameters, dropped its argument together with the argument's side effects, so a loop could run forever. Emit the side effects before `continue`. `reduce` of Belt's `HashMap`, `HashSet` and `MutableSet` read the rest of the container after its callback only because of the reversed order. Bind the callback's result first, so that they still skip entries the callback removes, as `forEach` does. Signed-off-by: Rob <illusionalsagacity@users.noreply.github.com>
Bring the backport in line with the version of rescript-lang#9419 merged on master (3b9f159). Its change to Lam_compile is the same here, and the Belt reduce functions bind the callback's result first as on master. The tests differ from master's. On 12.x a call through a cast such as `(Obj.magic(f): int => int)(x)` is compiled as a partial or an over-application, so it never reaches a self tail call with a different argument count; the tests cast the callee inside an inlined function instead, which crashed the compiler without the fix. 12.x still runs an argument compiled to statements before the other arguments, so master's statement-argument test is left out. Master's second commit updates unary_minus_test's output, which 12.x doesn't have yet. Belt's test is in tests/tests, as 12.x has no tests/belt_tests. 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 #9419 to 12.x.
Problem
A self tail call is compiled to a loop that reassigns the parameters. The assignments were emitted in reverse order, so the arguments were evaluated right to left. If a later argument mutates state that an earlier one reads, the result is wrong.
Before:
After:
A self tail call whose argument count differs from the parameter count, which a cast resolved once a function is inlined can produce, crashed the compiler:
Before:
After, the recursive call is
return inlinedCastCall(a - 1 | 0);.A self tail call of a
()function, which has no JS parameters, dropped its argument together with the argument's side effects, so a loop could run forever.Fix
As on master:
()function's argument are emitted beforecontinue.reduceonHashMap,HashSetandMutableSetread the next entry after the callback only because of the old order, so it skipped entries the callback removed. They now bind the callback's result first, so they still do, asforEachdoes.Differences from master:
Ext_list.same_length,Ext_list.filter_mapandExt_list.fold_left2where master uses theListfunctions. The Belt sources are inpackages/@rescript/runtime, and the Belt test is intests/tests, since 12.x has notests/belt_tests.try, or a block assigning a local ref) still runs before the other arguments, in self tail calls as in any other call on 12.x. Master holds the earlier arguments in temporaries, which isn't on 12.x, so master's statement-argument test is left out, and the test that reads a ref after an argument updates it does the update in an@inline(never)function.(Obj.magic(f): int => int)(x)is compiled as a partial or over-application, so it never reaches a self tail call with a different argument count. The tests cast the callee inside an inlinedapply/apply2instead, which crashes the compiler without the fix.unary_minus_test.mjs, which comes from Negate floats as -0 - x and parenthesize ** under prefix operators #9422. That test isn't on 12.x yet (its backport is [12.x] Negate floats as -0 - x and parenthesize ** under prefix operators #9452), so it isn't part of this PR; whichever of the two lands second needs that output regenerated.Tests
tail_call_argument_order_test.resandbelt_reduce_removal_test.res. Many checked-in outputs change: 64 files inpackages/@rescript/runtime/lib(mostly Belt), the rewatch test repo'sArray.mjsand 25 test outputs. In each, only the same assignments are reordered, apart from the three Belt modules above.make testandmake checkformatpass locally.Investigated by Claude.