Skip to content

fix >> (signed right shift) on Lua 5.3+ (#1720)#1721

Merged
Perryvw merged 5 commits into
TypeScriptToLua:masterfrom
RealColdFry:fix-arithmetic-right-shift-lua53
May 3, 2026
Merged

fix >> (signed right shift) on Lua 5.3+ (#1720)#1721
Perryvw merged 5 commits into
TypeScriptToLua:masterfrom
RealColdFry:fix-arithmetic-right-shift-lua53

Conversation

@RealColdFry
Copy link
Copy Markdown
Contributor

@RealColdFry RealColdFry commented May 2, 2026

Fixes #1720

alternatively, can improve diagnostic and let users provide their own functions, which is potentially less controversial and stays truer to the "pure transpile" story.

Separately, not directly relevant:

  • any scope to allow users to provide overrides for lowering/transpiling at AST leaves that could be inlined? something more "granular" than visitor overrides)
  • scope to fix error codes (not auto-increment via factory), show error codes in diagnostics, combined with a link to an error code listing on TSTL website describing the error in more detail?

@RealColdFry RealColdFry changed the title fix >> (signed right shift) on Lua 5.3+ fix >> (signed right shift) on Lua 5.3+ (#1720) May 2, 2026
@RealColdFry RealColdFry marked this pull request as ready for review May 2, 2026 12:59

exports[`Bitop [5.5] ("a>>b") 1`] = `
"local ____exports = {}
____exports.__result = (((a & 4294967295) ~ 2147483648) - 2147483648) // (1 << (b & 31))
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure this amount of magic in the generated code is really explainable. I would be perfectly happy with fixing the wording on the diagnostic and telling people to use >>> (or their own function) instead - and mentioning it in the caveat docs.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

agreed, this was a demonstration of the "magic" approach. Reverting to option 2 with improved diagnostics

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Side note: a chunk of the apparent magic was the printer, not the values. In the source the constants were 0xFFFFFFFF (low 32 bits) and 0x80000000 (sign bit), readable in hex but opaque after lua.createNumericLiteral rendered them as decimal (4294967295, 2147483648).

Might be worth letting the emitter preserve the original radix for numeric literals. Not relevant for this PR, but in other cases where hex is the meaningful form it would keep generated code readable. Could file a separate issue for this?

@Perryvw Perryvw merged commit 9158ace into TypeScriptToLua:master May 3, 2026
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Right shift operator not supported for target 5.4

2 participants