Skip to content

Avoid IIFE wrapping in IR.Abs lowering - #160

Closed
Unisay with Copilot wants to merge 5 commits into
mainfrom
copilot/fix-ir-abs-lowering-scope-iife
Closed

Avoid IIFE wrapping in IR.Abs lowering#160
Unisay with Copilot wants to merge 5 commits into
mainfrom
copilot/fix-ir-abs-lowering-scope-iife

Conversation

Copilot AI commented Jul 4, 2026

Copy link
Copy Markdown
Contributor

IR.Abs lowering was routing every body through asExpression, so a Let or IfThenElse body became (function() chunk end)() inside a return. removeScopeWhenInsideEmptyFunction then had to undo that on every such lambda.

Changes:

  • lib/Language/PureScript/Backend/Lua.hsIR.Abs now uses the chunk-aware go path. When the body lowers to Left chunk, it emits Lua.functionDef luaParams chunk directly; only Right expr falls back to [Lua.return e].
  • lib/Language/PureScript/Backend/Lua/Optimizer.hs — removed the now-dead removeScopeWhenInsideEmptyFunction rule and its Return import.
  • test/Language/PureScript/Backend/Lua/Optimizer/Spec.hs — removed the test for the deleted rule.

Before:

function(x)
  return (function()
    local y = f(x)
    return y
  end)()
end

After:

function(x)
  local y = f(x)
  return y
end

Summary

Checklist

  • Added a changelog.d/ fragment for any user-facing change (scriv create
    in the dev shell), or this change ships nothing releasable (CI, docs, or an
    internal refactor).
  • In the dev shell (nix develop), fourmolu -i lib/ exe/ test/ and
    hlint lib/ exe/ test/ are clean.
  • In the dev shell, cabal test all passes; structural goldens were
    re-accepted on purpose if codegen moved (PSLUA_GOLDEN_ACCEPT=1), and
    eval/golden.txt still holds.

Copilot AI changed the title [WIP] Fix IR.Abs lowering to prevent scope IIFE wrapping Avoid IIFE wrapping in IR.Abs lowering Jul 4, 2026
Copilot finished work on behalf of Unisay July 4, 2026 20:48
Copilot AI requested a review from Unisay July 4, 2026 20:48
@Unisay
Unisay marked this pull request as ready for review July 5, 2026 08:25
@Unisay
Unisay requested a review from Copilot July 5, 2026 08:26

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

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 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.Abs lowering to use the chunk-aware go path and emit the chunk directly as the Lua function body when available.
  • Remove the dead optimizer rule removeScopeWhenInsideEmptyFunction (and its now-unneeded Return import).
  • 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 Unisay left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copilot AI commented Jul 5, 2026

Copy link
Copy Markdown
Contributor Author

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 m...

Addressed in commit e711449.

  • Added focused regression tests in test/Language/PureScript/Backend/Lua/Spec.hs covering both Abs over Let and Abs over IfThenElse, asserting no scope-IIFE wrapper is introduced.
  • Wired the new spec into test/Main.hs and pslua.cabal.
  • Updated the PR description to include Closes #158.

Copilot AI requested a review from Unisay July 5, 2026 08:36
Copilot finished work on behalf of Unisay July 5, 2026 08:36
Unisay added 2 commits July 6, 2026 08:36
# Conflicts:
#	lib/Language/PureScript/Backend/Lua/Optimizer.hs
#	test/Language/PureScript/Backend/Lua/Optimizer/Spec.hs
@Unisay Unisay mentioned this pull request Jul 6, 2026
3 tasks
@Unisay

Unisay commented Jul 6, 2026

Copy link
Copy Markdown
Collaborator

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.

@Unisay Unisay closed this Jul 6, 2026
@Unisay
Unisay deleted the copilot/fix-ir-abs-lowering-scope-iife branch July 6, 2026 06:48
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.

IR.Abs lowering wraps a chunk body in a scope IIFE that a separate rule then strips

3 participants