Skip to content

feat(phase-17): 64-bit integer overflow wrapping and FuncDecl local assignment#142

Open
davydog187 wants to merge 3 commits intophase-17-vararg-scopefrom
phase-17-integer-overflow
Open

feat(phase-17): 64-bit integer overflow wrapping and FuncDecl local assignment#142
davydog187 wants to merge 3 commits intophase-17-vararg-scopefrom
phase-17-integer-overflow

Conversation

@davydog187
Copy link
Contributor

Summary

Critical fixes for Lua 5.3 integer semantics and function declaration behavior.

64-bit Integer Overflow Wrapping ⭐

All integer operations now wrap to signed 64-bit per Lua 5.3 spec:

  • Bitwise operations: <<, >>, &, |, ~, ^ wrap to signed 64-bit
  • Arithmetic operations: +, -, *, //, %, unary - wrap for integer operands
  • Added to_signed_int64/1 helper for proper IEEE 754 compliance
  • maxint + 1 now correctly equals minint
  • 1 << 63 now correctly equals minint (not overflow to big integer)

FuncDecl Local Assignment ⭐

Fixed function f() syntax to properly update local variables:

  • function f() now correctly updates local f when one exists in scope
  • Scope resolution marks assignment target in var_map at declaration time
  • Prevents future locals from incorrectly affecting earlier code during codegen
  • Fixes common Lua pattern where local f = function()... is followed by function f()

Example fixed pattern:

local f = function(x) return "first" end
-- Later redefine f
function f(x) return "second" end
-- f now correctly refers to "second"

Implementation Details

Integer Wrapping:

  • to_signed_int64/1 masks to 64 bits then converts to signed range
  • Applied to all bitwise ops: band, bor, bxor, bnot, bsl, bsr
  • Applied to integer arithmetic: safe_add, safe_subtract, safe_multiply, safe_negate, lua_idiv, safe_modulo
  • Float operations remain IEEE 754 (no wrapping)

FuncDecl Fix:

  • Scope resolution checks if local exists at FuncDecl position
  • Stores target as {:func_decl_target, decl} in var_map
  • Codegen uses this marker instead of checking ctx.scope.locals (which contains all function-scope locals)

Test Status

  • Unit tests: 1,273 passing, 0 failures ✅
  • No regressions: All existing tests continue to pass
  • Enables progress: constructs.lua now progresses past line 160 (was failing at line 20)

Dependencies

Depends on #141 (vararg and scope fixes).

🤖 Generated with Claude Code

Dave Lucia and others added 3 commits February 14, 2026 06:30
Fixes:
- Add 64-bit signed integer overflow wrapping for all arithmetic and bitwise operations
- Bitwise operations (<<, >>, &, |, ~, ^) now wrap to signed 64-bit
- Arithmetic operations (+, -, *, //, %, unary -) wrap for integer operands
- FuncDecl now correctly assigns to local variables when they exist in scope
  - Scope resolution records if a FuncDecl should target a local
  - Prevents treating future locals as current scope during codegen

These fixes improve Lua 5.3 compliance for integer arithmetic and function definitions.

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
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.

1 participant