Conversation
There was a problem hiding this comment.
Pull request overview
This PR improves Lua code generation by avoiding unnecessary IIFE wrapping when lowering IR.Abs bodies that naturally lower to statement chunks (e.g. Let, IfThenElse). This reduces generated Lua noise and removes a now-redundant optimizer rewrite that existed primarily to undo those IIFEs.
Changes:
- Update
IR.Abslowering to use the chunk-awaregopath and emit the chunk directly as the Lua function body when available. - Remove the dead optimizer rule
removeScopeWhenInsideEmptyFunction(and its now-unneededReturnimport). - Remove the corresponding optimizer spec that asserted the deleted rewrite behavior.
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.hs | Avoids wrapping chunk-lowered lambda bodies via asExpression by emitting chunks directly in IR.Abs lowering. |
| lib/Language/PureScript/Backend/Lua/Optimizer.hs | Removes the now-dead removeScopeWhenInsideEmptyFunction rewrite rule and its unused import. |
| test/Language/PureScript/Backend/Lua/Optimizer/Spec.hs | Deletes the unit test for the removed optimizer rewrite rule. |
Unisay
left a comment
There was a problem hiding this comment.
Thanks, this is the right fix and I verified it locally: full suite green, zero diff against any golden.lua/golden.ir/eval/golden.txt, fourmolu/hlint clean.
Two things before this is ready to merge:
1. Add a regression test for the fix itself. Right now nothing in the test suite specifically pins down the behavior this PR changes; the golden suite happens to pass because the old wrap-then-unwrap produced byte-identical output to the new direct construction, not because any test targets this code path. Please add a focused test that would fail if IR.Abs lowering regressed back to wrapping a chunk body in a scope IIFE. Cover both chunk-producing shapes, since they build the chunk differently: an Abs over a Let body, and an Abs over an IfThenElse body. There's no existing precedent for direct fromIR/fromUberModule unit tests in this codebase (golden tests are the primary integration mechanism here), so either approach works: a small direct unit test calling fromIR/fromUberModule on a hand-built IR fixture and asserting the printed Lua has no intermediate (function() ... end)() wrapper, or a new minimal golden fixture under test/ps/src/Golden/ built specifically around this shape (mirroring the before/after example from the PR description) so the regression is visible on its own instead of folded into a large pre-existing golden.
2. Link the issue. The PR body doesn't reference #158 anywhere, so merging won't auto-close it. Please add "Closes #158" to the description.
Once both are in, I'll approve the CI run.
Addressed in commit
|
# Conflicts: # lib/Language/PureScript/Backend/Lua/Optimizer.hs # test/Language/PureScript/Backend/Lua/Optimizer/Spec.hs
|
Superseded by #170: same commits, reopened as a maintainer-authored PR so the required workflow runs are created. Runs on Copilot-authored PRs require manual approval and were never created for the follow-up pushes. |
IR.Abslowering was routing every body throughasExpression, so aLetorIfThenElsebody became(function() chunk end)()inside areturn.removeScopeWhenInsideEmptyFunctionthen had to undo that on every such lambda.Changes:
lib/Language/PureScript/Backend/Lua.hs—IR.Absnow uses the chunk-awaregopath. When the body lowers toLeft chunk, it emitsLua.functionDef luaParams chunkdirectly; onlyRight exprfalls back to[Lua.return e].lib/Language/PureScript/Backend/Lua/Optimizer.hs— removed the now-deadremoveScopeWhenInsideEmptyFunctionrule and itsReturnimport.test/Language/PureScript/Backend/Lua/Optimizer/Spec.hs— removed the test for the deleted rule.Before:
After:
Summary
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.