Skip to content

fix: wrap table literals in parentheses before indexing#40

Closed
afcondon wants to merge 1 commit into
purescript-lua:mainfrom
afcondon:fix/table-literal-indexing-parens
Closed

fix: wrap table literals in parentheses before indexing#40
afcondon wants to merge 1 commit into
purescript-lua:mainfrom
afcondon:fix/table-literal-indexing-parens

Conversation

@afcondon

Copy link
Copy Markdown
Contributor

Hi there, i (Andrew) am submitting this PR but, full disclosure, the bug diagnosis, change and PR text were all prepared by Claude, an LLM. I watched it run the test suite and the logic seems sound so i'm pushing it upstream for your consideration. I very much hope this is helpful and not a burden.

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

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>
@afcondon

Copy link
Copy Markdown
Contributor Author

btw i have another larger PR that migrates pslua to the new spago, if you want it. Also LLM generated but working for me.

@afcondon

Copy link
Copy Markdown
Contributor Author

I see this failed CI but it was not a test that failed, the build ran out of space "copyFile: resource exhausted (No space left on device)"

@Unisay

Unisay commented Jan 19, 2026

Copy link
Copy Markdown
Collaborator

Hi Andrew,

I am glad you're interested in using and contributing to this project!

I see this PR delivers a fix without unit-test coverage. Please add a unit test (golden test) to demonstrate the problem being fixed (the idea is that without this fix the test should fail and in a way that is understandable)

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

This PR fixes a Lua syntax error when table constructor literals are directly indexed with bracket notation. In Lua, expressions like {key = val}[key] are invalid syntax, requiring parentheses: ({key = val})[key].

Changes:

  • Modified printVar function in the Lua printer to wrap table constructors in parentheses before bracket indexing, making VarIndex consistent with the existing VarField behavior

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

@Unisay

Unisay commented Jun 14, 2026

Copy link
Copy Markdown
Collaborator

Landed in #62, which carries your commit (rebased onto current main) plus the regression test the fix needed: a Printer spec case that renders { ["foo"] = 1 }["foo"] without the fix and so fails with a clear diff. Your branch predated the StringCodePoints golden that is now on main, so I redid it on top of current main rather than merging here. Closing this in favour of #62. Thanks again, Andrew!

@Unisay Unisay closed this Jun 14, 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.

3 participants