fix: array-literal pattern binders read 1-based Lua indices#69
Merged
Conversation
There was a problem hiding this comment.
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.ArrayIndexby+ 1during Lua codegen so pattern binder readsv[1]..v[n]instead ofv[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.
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.
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
niland[a, b] -> a + bblew up withattempt to perform arithmetic on a nil value.The fix shifts
ArrayIndexby one in the Lua backend, sov[0]/v[1]becomesv[1]/v[2]. This matches the rest of the codegen, which is already 1-based: array literals build{ [1] = ..., [2] = ... }, and the arrays FFIindexImplreadsxs[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[], andlastOfThree [7,8,9](matches[_, _, c]). It runs the generated Lua and checks the output is30 / -1 / -1 / 9. No golden previously exercised array-literal patterns, which is why this went unnoticed.