Skip to content

Avoid IIFE wrapping in IR.Abs lowering - #170

Merged
Unisay merged 5 commits into
mainfrom
issue-158/abs-chunk-lowering
Jul 6, 2026
Merged

Avoid IIFE wrapping in IR.Abs lowering#170
Unisay merged 5 commits into
mainfrom
issue-158/abs-chunk-lowering

Conversation

@Unisay

@Unisay Unisay commented Jul 6, 2026

Copy link
Copy Markdown
Collaborator

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.

Fixes #158.

Supersedes #160: same commits, reopened as a maintainer-authored PR so the required workflow runs are created (runs on PRs authored by the Copilot agent require manual approval and were never created for the follow-up pushes).

@Unisay Unisay mentioned this pull request Jul 6, 2026
3 tasks
@Unisay Unisay self-assigned this Jul 6, 2026
@Unisay
Unisay requested a review from Copilot July 6, 2026 06:49
@Unisay
Unisay merged commit 01dbe8d into main Jul 6, 2026
5 checks passed
@Unisay
Unisay deleted the issue-158/abs-chunk-lowering branch July 6, 2026 06:50

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 removes an avoidable (function() ... end)() scope-IIFE introduced during lowering of IR.Abs when its body lowers to a statement chunk (e.g., Let / IfThenElse). By making IR.Abs lowering chunk-aware, it prevents the redundant wrap-then-unwrap cycle and allows deleting the optimizer rule that previously stripped that pattern.

Changes:

  • Update IR.Abs lowering to emit Lua.functionDef directly when the body lowers to Left chunk, only using return for Right expr.
  • Delete the now-dead optimizer rewrite rule removeScopeWhenInsideEmptyFunction (and its unit test).
  • Add a targeted regression spec ensuring Lua.fromUberModule doesn’t emit the scope-IIFE for Abs bodies that are Let/IfThenElse.

Reviewed changes

Copilot reviewed 6 out of 6 changed files in this pull request and generated no comments.

Show a summary per file
File Description
lib/Language/PureScript/Backend/Lua.hs Makes IR.Abs lowering chunk-aware to avoid generating scope-IIFEs for chunk bodies.
lib/Language/PureScript/Backend/Lua/Optimizer.hs Removes the rewrite rule that stripped the now-avoided scope-IIFE pattern.
test/Language/PureScript/Backend/Lua/Optimizer/Spec.hs Deletes the unit test for the removed optimizer rule.
test/Language/PureScript/Backend/Lua/Spec.hs Adds regression tests asserting no (function() scope-IIFE appears for Abs over Let/IfThenElse.
test/Main.hs Wires the new Lua spec into the Hspec test runner.
pslua.cabal Registers the new test module in the spec test-suite build list.

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