Repository navigation
compile: x = A and B into an existing local leaves x unchanged when A is falsy - #569
Open
itsnotyoutoday wants to merge 1 commit into
Open
itsnotyoutoday wants to merge 1 commit into
itsnotyoutoday wants to merge 1 commit into
Conversation
…ends the expression x = f() and 1, with x an existing local and f() falsy, left x unchanged: the jump that ends the expression used OP_TEST, so the falsy operand never reached x. The operand is the expression's value whenever its jump goes to the end, so that jump now uses OP_TESTSET (for and as for or). A jump to the next operand carries no value and now only tests, where it used to write the target early (x = (f() or g()) and x read the new x).
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.
Branch: itsnotyoutoday/gopher-lua
pr-and-or-assign(one commit on upstream master, module path unchanged)Problem
When an
and/orexpression is assigned to an existing local and an operand that is not aplain local or a comparison (a call, a field, ...) decides the result, the compiler emits
OP_TESTfor the jump that ends the expression. The operand is the expression's value in thatcase, but OP_TEST never copies it into the target register, so the local keeps its old value.
orhappened to work because its path always usedOP_TESTSET, but that path also wrote thetarget on jumps that go to the next operand, before the expression was finished:
Fix
In
compileLogicalOpExprAux, the general operand case now chooses by where the jump goes:OP_TESTSET target, operandwhen the jump ends the expression (and the target is anotherregister),
OP_TEST operandotherwise. This is what PUC Lua's code generator does (TESTSET onlyfor jumps that produce the value).
Tests
_glua-tests/issues.luagains a case coveringand/orwith calls,false,nilfields,chains (
f() and 1 or 2) and the early-write case. The whole suite passes; the new case failswithout the change (issues.lua: "x = f() and 1 with f() nil").
Found running Mudlet's Glu library (ThresholdUI, Highlighter, Chatter packages) on gopher-lua.