Title: Lua optimizer: fold a field projection through a branching scope-call tail · Issue #335 · purescript-lua/purescript-lua · GitHub
Open Graph Title: Lua optimizer: fold a field projection through a branching scope-call tail · Issue #335 · purescript-lua/purescript-lua
X Title: Lua optimizer: fold a field projection through a branching scope-call tail · Issue #335 · purescript-lua/purescript-lua
Description: Problem The Lua-level optimizer has two sibling rewrites that fold an operation applied to an immediately-invoked scope call ((function() … end)(), the shape codegen emits for an if in expression position) into the callee's return sites....
Open Graph Description: Problem The Lua-level optimizer has two sibling rewrites that fold an operation applied to an immediately-invoked scope call ((function() … end)(), the shape codegen emits for an if in expression p...
X Description: Problem The Lua-level optimizer has two sibling rewrites that fold an operation applied to an immediately-invoked scope call ((function() … end)(), the shape codegen emits for an if in expression p...
Opengraph URL: https://github.com/purescript-lua/purescript-lua/issues/335
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Lua optimizer: fold a field projection through a branching scope-call tail","articleBody":"## Problem\n\nThe Lua-level optimizer has two sibling rewrites that fold an operation applied to an immediately-invoked scope call (`(function() … end)()`, the shape codegen emits for an `if` in expression position) into the callee's return sites. They cover their tail chains asymmetrically. `foldCallThroughScopeCall` — the rule folding `(function() … return e end)()(args)` into `return e(args)` — recurses through branching tails: its `pushIntoStatement` handles `Return`, `IfThenElse` (both blocks, gated on atomic arguments), and `Do`. Its projection sibling `foldFieldProjectionThroughScopeCall` — folding `((function() … return e end)()).f` into `return e.f` — only matches a body whose last statement is a single `Return` (`matchScopeCallProjection`), so a field read over a *branching* scope call keeps its IIFE:\n\n```lua\nreturn ((function() if b then return l else return r end end)()).name\n```\n\nstays as is, while the call-shaped analogue of the same body folds. Since #243 the IR optimizer distributes accessors into `IfThenElse` arms before codegen, so IR-originated shapes no longer reach this backstop — but the Lua optimizer also runs over shapes the IR never sees (FFI-adjacent rewriting, fixtures), and the asymmetry makes the backstop's coverage surprising.\n\n## Approach\n\nExtend `foldFieldProjectionThroughScopeCall` to traverse the same tail chain as `foldCallThroughScopeCall`: recurse the projection through `IfThenElse`/`Do` tails, declining on the same conditions (an early `Return` among the leading statements, a fall-off path, a multi-value tail `Return`). A field name has no evaluation cost, so no atomic-arguments analogue is needed. Alternatively, extract the shared tail-chain walker from `foldCallThroughScopeCall` and instantiate it for both consumers, so the two rules cannot drift again.\n\n## Prerequisites / Relations\n\nNone blocking. Relates to #243 (the IR-level distribution that now handles these shapes before codegen for IR-originated code) and to the scope-call fold family shipped for #172.\n\n## Verification / Measurement\n\nA unit test over the Lua optimizer: a projection over a branching scope call folds into both return sites, and a body with an early return or fall-off path is declined. Existing goldens should not move (the IR rule already strips these shapes from IR-originated output); if any do, the diff is the review artifact.\n","author":{"url":"https://github.com/Unisay","@type":"Person","name":"Unisay"},"datePublished":"2026-07-28T11:53:58Z","url":"https://github.com/purescript-lua/purescript-lua/issues/335"}
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322 |
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:7f3d2a23-8302-1f67-bf6f-f430432a3c54 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | C1A4:2978F1:7D62F8:A38E95:6B00D93F |
| html-safe-nonce | 2074a2c3d1010708fe2fcd1e7179e2d57536621ba0680fb53dec8aa2554861b4 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJDMUE0OjI5NzhGMTo3RDYyRjg6QTM4RTk1OjZCMDBEOTNGIiwidmlzaXRvcl9pZCI6IjI4OTIxMDU4MjM1NzQ2MTIyODciLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | 0c00761e9b8331665bd4c459af0b19715787be36cbf12900914c5fa1eb48b6cb |
| hovercard-subject-tag | issue:4998240836 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/purescript-lua/purescript-lua/335/issue_layout |
| twitter:image | https://opengraph.githubassets.com/5b660265ad42e9053643684af0ebb745e88969bbc48b597eb59219e616a8b795/purescript-lua/purescript-lua/issues/335 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/5b660265ad42e9053643684af0ebb745e88969bbc48b597eb59219e616a8b795/purescript-lua/purescript-lua/issues/335 |
| og:image:alt | Problem The Lua-level optimizer has two sibling rewrites that fold an operation applied to an immediately-invoked scope call ((function() … end)(), the shape codegen emits for an if in expression p... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | Unisay |
| hostname | github.com |
| expected-hostname | github.com |
| None | a481f62a1e04c9b9bdcb5628230682c66d59006f5b13b080f79cba84a176905b |
| turbo-cache-control | no-preview |
| go-import | github.com/purescript-lua/purescript-lua git https://github.com/purescript-lua/purescript-lua.git |
| octolytics-dimension-user_id | 131581790 |
| octolytics-dimension-user_login | purescript-lua |
| octolytics-dimension-repository_id | 587200487 |
| octolytics-dimension-repository_nwo | purescript-lua/purescript-lua |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 587200487 |
| octolytics-dimension-repository_network_root_nwo | purescript-lua/purescript-lua |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 84c8c5cc25e2fb7f2308428458538436baecc6d2 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width