Skip to content

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

Description

@Unisay

IR.Abs lowering (Lua.hs, the IR.Abs case) computes the body via goExp, which is asExpression <<$>> go. When the body lowers to a chunk rather than a bare expression, for example a Let or an IfThenElse, asExpression wraps it through Lua.chunkToExpression into a niladic IIFE: (function() chunk end)(). Abs then wraps that single expression in Lua.return, producing:

function(params)
  return (function()
    chunk
  end)()
end

removeScopeWhenInsideEmptyFunction in Lua/Optimizer.hs exists specifically to undo this: it matches Function outerArgs [Return (FunctionCall (Function [] body) [])] and rewrites it back to Function outerArgs body. Every lambda whose body is a Let or IfThenElse pays for this wrapping and then immediately pays again to unwrap it.

Proposal: give IR.Abs lowering a chunk-aware path. When the body lowers to Left chunk, emit Lua.functionDef luaParams chunk directly instead of routing it through asExpression/Lua.return. Only fall back to the wrap-and-return form when the body lowers to Right expr. Once nothing produces the wrapped shape, removeScopeWhenInsideEmptyFunction and its spec become dead and can be deleted.

Noticed while implementing #153: same pattern as #146 (a rule that's a symptom of an avoidable encoding introduced during lowering, rather than something that belongs at the AST level).

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions