Skip to content

fix: array-literal pattern binders read 1-based Lua indices#69

Merged
Unisay merged 1 commit into
mainfrom
issue-49/array-pattern-index
Jun 14, 2026
Merged

fix: array-literal pattern binders read 1-based Lua indices#69
Unisay merged 1 commit into
mainfrom
issue-49/array-pattern-index

Conversation

@Unisay

@Unisay Unisay commented Jun 14, 2026

Copy link
Copy Markdown
Collaborator

Closes #49.

Matching an array-literal pattern crashed at runtime. The binders were addressed with the IR's 0-based index (de Bruijn-style, like the source language) emitted straight into a 1-based Lua table, so the first element came back nil and [a, b] -> a + b blew up with attempt to perform arithmetic on a nil value.

The fix shifts ArrayIndex by one in the Lua backend, so v[0]/v[1] becomes v[1]/v[2]. This matches the rest of the codegen, which is already 1-based: array literals build { [1] = ..., [2] = ... }, and the arrays FFI indexImpl reads xs[i + 1].

Test

Adds an eval golden, Golden.ArrayPatternMatch, over the repro: firstTwo [10, 20] (matches [a, b]), the non-matching lengths [1,2,3] and [], and lastOfThree [7,8,9] (matches [_, _, c]). It runs the generated Lua and checks the output is 30 / -1 / -1 / 9. No golden previously exercised array-literal patterns, which is why this went unnoticed.

@Unisay Unisay requested a review from Copilot June 14, 2026 13:47
@Unisay Unisay self-assigned this Jun 14, 2026

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Pull request overview

Fixes a runtime crash in the Lua backend when matching array-literal patterns by aligning IR’s 0-based ArrayIndex with Lua’s 1-based table indexing, and adds a new golden+eval test to prevent regressions.

Changes:

  • Shift IR.ArrayIndex by + 1 during Lua codegen so pattern binder reads v[1]..v[n] instead of v[0]...
  • Add a new PureScript golden module covering array-literal pattern matches (matching + non-matching cases).
  • Check in corresponding generated artifacts (CoreFn/IR/Lua) and expected eval output for the new golden.

Reviewed changes

Copilot reviewed 7 out of 7 changed files in this pull request and generated no comments.

Show a summary per file
File Description
lib/Language/PureScript/Backend/Lua.hs Shifts emitted Lua table indices for IR.ArrayIndex to be 1-based.
test/ps/golden/Golden/ArrayPatternMatch/Test.purs Adds a repro-based golden module exercising array-literal pattern matching.
test/ps/output/Golden.ArrayPatternMatch.Test/golden.lua New expected Lua output showing 1-based indexing in pattern binder reads.
test/ps/output/Golden.ArrayPatternMatch.Test/golden.ir New expected IR output containing ArrayIndex nodes (still 0-based at IR level).
test/ps/output/Golden.ArrayPatternMatch.Test/corefn.json New expected CoreFn JSON for the golden module.
test/ps/output/Golden.ArrayPatternMatch.Test/eval/golden.txt New expected runtime output for eval test coverage.
test/ps/output/Golden.ArrayPatternMatch.Test/eval/.gitignore Ignores eval’s generated actual.txt output.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@Unisay Unisay merged commit 0293ed2 into main Jun 14, 2026
2 checks passed
@Unisay Unisay deleted the issue-49/array-pattern-index branch June 14, 2026 13:51
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.

Array-literal pattern matching reads 0-based indices from 1-based tables

2 participants