Fold a field projection through a foreign-header scope IIFE - #161
Merged
Conversation
Copilot
AI
changed the title
[WIP] Fix field projection through foreign-header scope IIFE
Fold a field projection through a foreign-header scope IIFE
Jul 4, 2026
Unisay
marked this pull request as ready for review
July 5, 2026 20:47
# Conflicts: # test/ps/output/Golden.CharLiterals.Test/golden.lua
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.
A
ForeignImportwith a header lowers to a chunk[ForeignSourceStat fh, return exportsTable]. Projecting a field off it lowers to(function() ...; return exports end)().field— a shape neither the IR-levelreduceObjectPropnor the Lua-levelreduceTableDefinitionAccessorsees through, since the projected value is a function call, not a table constructor, at either level.Summary
Added
foldFieldProjectionThroughScopeCall, a Lua-AST rewrite rule that projects the field inside a no-arg immediately-invoked function whose last statement is areturn, rather than after the call:(function() …; return e end)().foo→(function() …; return e.foo end)(). Sound because the projection happens within the same activation regardless of whether it's before or after the call returns, so no side effect crosses the boundary.The rewritten projection is immediately re-optimized, so it composes with
reduceTableDefinitionAccessorin the same pass. For theData.Eq.eqCharcase from the issue, this fully collapses:down to:
Wired into
rewriteRulesInOrderalongside the existing rules.Added unit tests covering full reduction, partial fold (when the returned value isn't an unambiguous table constructor), and decline (callee not a no-arg IIFE).
Checklist
changelog.d/fragment for any user-facing change (scriv createin the dev shell), or this change ships nothing releasable (CI, docs, or an
internal refactor).
nix develop),fourmolu -i lib/ exe/ test/andhlint lib/ exe/ test/are clean.cabal test allpasses; structural goldens werere-accepted on purpose if codegen moved (
PSLUA_GOLDEN_ACCEPT=1), andeval/golden.txtstill holds.