Guard reduceTableDefinitionAccessor against ambiguous table literals - #152
Merged
Conversation
reduceTableDefinitionAccessor rewrote `{ foo = 1 }.foo` to `1` by scanning
name-value rows and taking the first match, falling back to Nil. Two shapes
broke that: a string-keyed row (`["foo"] = 1`) for the accessed field is
invisible to the name lookup, so the access folded to Nil and dropped the
real value; and on a duplicate field name Lua keeps the last assignment
while the first-match lookup returns the earliest. Neither is emitted by the
current codegen, so this is a latent miscompile, not a live one.
The rule now fires only when the constructor is unambiguous (every row is a
name-value row and no field name repeats) and otherwise leaves the
expression untouched. The rule had no test before; three cases pin it now,
two of them red against the old rule (#140).
Claude-Session: https://claude.ai/code/session_01Bfeu6sp2zVhvuQTnEzYpNG
Contributor
There was a problem hiding this comment.
Pull request overview
This PR hardens the Lua AST optimizer by making reduceTableDefinitionAccessor refuse to fold table-literal field accesses when the table constructor could be ambiguous, preventing latent miscompiles if such shapes ever appear in generated Lua.
Changes:
- Guard
reduceTableDefinitionAccessorso it only fires when all rows areTableRowNVand there are no duplicate field names. - Add an Hspec test group covering: (1) successful fold on an unambiguous literal, (2) decline when a
TableRowKVrow is present, and (3) decline on duplicate names. - Add a changelog fragment documenting the fix and the two hazard cases (KV rows and duplicates).
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
| lib/Language/PureScript/Backend/Lua/Optimizer.hs | Adds explicit guards and Haddock rationale so the fold only happens for unambiguous name-value-only constructors without duplicate keys. |
| test/Language/PureScript/Backend/Lua/Optimizer/Spec.hs | Introduces focused tests that lock in the intended “fold vs decline” behavior for the rule. |
| changelog.d/20260704_120000_unisay_table_accessor_guard.md | Records the behavior change and why it matters (avoids folding to nil or the wrong value). |
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 #140.
Summary
The Lua optimizer rule
reduceTableDefinitionAccessorfolds a field access into a table literal, rewriting{ foo = 1, bar = 2 }.footo1. It searched only name-value rows (TableRowNV) and took the first match, falling back toNil.Two shapes broke that:
["foo"] = 1, aTableRowKV) for the accessed field is invisible to the name lookup, so the rule folded the access toNiland dropped the real value.Neither shape is produced by the current codegen, so this is a latent miscompile rather than a live one. The rule now fires only when the constructor is unambiguous: every row is a name-value row and no field name repeats. Otherwise it leaves the expression untouched.
Changes
lib/Language/PureScript/Backend/Lua/Optimizer.hs: guard the rewrite with an all-name-value check and a duplicate-name check; the Haddock explains both hazards.test/Language/PureScript/Backend/Lua/Optimizer/Spec.hs: the rule had no test before. Three cases now pin it: the fold still fires on an unambiguous literal, and it declines on a key-value row and on duplicate names (both were red against the old rule).Test plan
cabal test all --test-show-details=directpassesfourmoluandhlintclean on the touched files, warning-free build