Skip to content

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

Open
davydog187 wants to merge 5 commits intomainfrom
phase-17-integer-overflow
Open

feat(phase-17): 64-bit integer overflow wrapping and FuncDecl local assignment#142
davydog187 wants to merge 5 commits intomainfrom
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

Base automatically changed from phase-17-vararg-scope to main February 17, 2026 15:29
Dave Lucia and others added 4 commits February 17, 2026 11:58
Implement multi-return expansion across call arguments, table constructors,
and return statements using state.multi_return_count tracking. Fix floor
division/modulo to use Lua 5.3 floor semantics. Add _ENV support, fix
assert to return all args, support hex float literals, fix local function
redefinition scoping, and fix table.unpack nil handling.

Suite tests passing: 3/29 (simple_test.lua, api.lua, code.lua)
Tests: 1257 passing, 0 failures, 33 skipped

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
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>
@davydog187 davydog187 force-pushed the phase-17-integer-overflow branch from 2090fc9 to f041e44 Compare February 17, 2026 18:42
Add 24 new tests covering:

**64-bit Integer Overflow Wrapping (13 tests)**:
- maxint + 1 wraps to minint
- minint - 1 wraps to maxint
- Bitwise operations (<<, >>, &, |, ~, ^) wrap to signed 64-bit
- Arithmetic operations (+, -, *, //, %, unary -) wrap for integers
- Float operations preserve IEEE 754 semantics (no wrapping)
- Edge cases: shifts >= 64 bits, mixed integer/float arithmetic

**FuncDecl Local Assignment (11 tests)**:
- function f() updates local f when it exists in scope
- function f() creates global when no local exists
- Nested scopes handle local updates correctly
- Works with upvalue capture
- Multiple redefinitions in same scope
- Forward reference patterns
- Dotted names (t.f) always use table assignment
- Method syntax (:) works correctly
- Loop iterations create independent locals

All tests verify Lua 5.3 spec compliance and pass with 0 failures.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
davydog187 added a commit that referenced this pull request Feb 18, 2026
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

Comments