fix: parenthesise table literals before bracket indexing#62
Merged
Conversation
In Lua, table constructor literals cannot be directly indexed without
parentheses. The expression `{ ["key"] = val }["key"]` is a syntax error,
while `({ ["key"] = val })["key"]` is valid.
This fix adds the same `wrapPrec PrecAtom` wrapping to `VarIndex` that
was already applied to `VarField`, ensuring table literals are properly
parenthesized when used with bracket indexing.
Fixes pattern matching on ADT constructors that would generate invalid
Lua like:
if "Mod∷Type.Ctor" == { ["$ctor"] = "Mod∷Type.Ctor" }["$ctor"] then
Now correctly generates:
if "Mod∷Type.Ctor" == ({ ["$ctor"] = "Mod∷Type.Ctor" })["$ctor"] then
Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
Mirror the existing VarField spec case for VarIndex: a table constructor
must be parenthesised before bracket indexing, since
`{ ["foo"] = 1 }["foo"]` is a Lua syntax error while
`({ ["foo"] = 1 })["foo"]` is valid. Without the preceding fix this
assertion fails with a clear diff.
Regenerate the CharLiterals and StringCodePoints goldens, where the same
wrapPrec PrecAtom now parenthesises function-call results before
["$ctor"] indexing, matching VarField. Purely syntactic: IR and eval
output are unchanged and both files pass luacheck.
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.
Carries @afcondon's fix from #40 (rebased onto current main) and adds the test it was missing.
The bug
In Lua a table constructor cannot be indexed directly:
{ ["k"] = v }["k"]is a syntax error, while({ ["k"] = v })["k"]is valid. The Lua printer wrappedVarFieldtargets inwrapPrec PrecAtombut notVarIndex, so a table literal in index position emitted invalid Lua.The fix
VarIndexnow wraps its target withwrapPrec PrecAtom, exactly likeVarFieldalready did. That commit is @afcondon's, preserved as-is.Tests
Printerspec case mirroring the existingVarFieldone:({ ["foo"] = 1 })["foo"]. Without the fix it renders{ ["foo"] = 1 }["foo"]and the assertion fails with a clear diff, which is the regression guard the original PR was asked for.CharLiteralsandStringCodePointsgoldens are regenerated. The same wrapping now also parenthesises function-call results before["$ctor"]indexing (e.g.f(x)(y)["$ctor"]becomes(f(x)(y))["$ctor"]). That change is purely syntactic: the IR and eval output are unchanged and both files pass luacheck.Supersedes #40, whose branch predates the
StringCodePointsgolden, so the work has to sit on current main.