fix: emit bxor, not bnot, for binary '~' with --gen-target 5.1 - #1168
Merged
hishamhm merged 1 commit intoSep 4, 2026
Merged
Conversation
The '~' token is both unary bitwise NOT and binary bitwise XOR, and the
5.1 compat pass matched on the operator token alone. Binary '~' was
rewritten to bit32.bnot(a), dropping the right operand and changing the
operation; a __bxor metamethod call was dispatched to __bnot. Check the
arity so binary '~' falls through to the existing bit_operators branch.
spec/util.lua built its line arrays with gmatch("([^\n]*)\n"), which
drops the final line, so a util.gen mismatch confined to the last line
of generated output was never reported. That hid the metamethod half of
this bug from the new test and from the existing __bnot metamethod test.
hishamhm
approved these changes
Sep 4, 2026
hishamhm
left a comment
Member
There was a problem hiding this comment.
LGTM! Thank you, good catch!
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.
~is both unary bitwise NOT and binary bitwise XOR, but the 5.1 compat pass inteal/gen/lua_compat.tlmatches on the operator token alone, so the unary branch swallows the binary one.tl gen --gen-target 5.1before / after:The right operand is dropped and the operation changes, silently: the checker accepts it as
integerand the output is still valid Lua.&,|,<<and>>are all correct;~is the only one of the five with a same-token unary form. The metamethod path splits the same way, sendinga ~ bon a record with__bxorto__bnotwith one argument.The fix is an arity check, so binary
~falls through to the existingbit_operatorsbranch, which already maps~tobxorand__bxor.Also
spec/util.lua:gen()built its line arrays withgmatch("([^\n]*)\n"), which drops the final line, so autil.genmismatch confined to the last line of the output was never reported. That is why the metamethod half stayed invisible, and why the existing__bnotmetamethod test cannot catch a regression either. With\n?the suite is unchanged at 1941 passing, but both halves of this bug now fail it.Regenerated the
.luaartifacts withmake.Drafted with AI assistance (Claude Opus 5).