Skip to content

fix(compiler): resolve FuncDecl head names at scope analysis - #274

Merged
davydog187 merged 5 commits into
mainfrom
fix/funcdecl-head-name
May 28, 2026
Merged

davydog187 merged 5 commits into
mainfrom
fix/funcdecl-head-name

Conversation

@davydog187

@davydog187 davydog187 commented May 28, 2026 •

Copy link
Copy Markdown
Contributor

Resolve FuncDecl head names at scope analysis (not post-block codegen)

Plan: .agents/plans/A41-funcdecl-head-name.md
Closes #255

Goal

Fix the multi-name FuncDecl codegen path (function a.b.c(...) /
function a:m(...)) so the head name a resolves against the live
scope at scope-analysis time, not against the function-scope snapshot
left over after the enclosing block has been restored. This is the
blocker keeping calls.lua deferred at :all in
test/lua53_skips.exs.

The single-name FuncDecl already does the right thing — it resolves
the target during scope analysis and stashes the result under
{:func_decl_target, decl}. This PR extends that same pattern to
multi-name FuncDecl heads.

Success criteria

  • Repro from FuncDecl target name resolves against post-block scope #255 passes — do / local a = {x=0} / function a:add(x) ... end / a:add(10).
  • Same fix covers function a.b.c.f(...) (dotted multi-name).
  • mix test passes with no regressions (1963 → 1970 passed, no failures).
  • Regression tests added in test/lua/vm/func_decl_test.exs covering six shape variants.
  • calls.lua no longer deferred at :all. The file now compiles and executes up to line 218; four narrow ranges inside that envelope (lines 24–36, 65–69, 135–137, 207–208, 217–218) are skipped for separately-tracked issues, and one trailing range (219–401) is bundled pending its own triage.

Changes

 .agents/plans/A41-funcdecl-head-name.md | 248 ++++++++++++++++++++++++
 lib/lua/compiler/codegen.ex             | 119 +++++-------
 lib/lua/compiler/scope.ex               |  58 +++++-
 test/lua/vm/func_decl_test.exs          | 121 ++++++++++++
 test/lua53_skips.exs                    |  36 +++-

Approach

  1. lib/lua/compiler/scope.ex — new Statement.FuncDecl clause for the multi-name case ([first | rest] with rest != []). Resolves the head name via a new resolve_func_decl_head/3 helper after resolve_function_scope/4 has run, so state.captured_locals correctly reflects whether the head was captured by the body. Mirrors the ordering used by the single-name FuncDecl clause.

  2. lib/lua/compiler/codegen.ex — multi-name arm of Statement.FuncDecl codegen now loads the head via a new gen_func_decl_head/2 helper that performs a var_map lookup (the same four cases as gen_expr on %Expr.Var{}). Deleted the now-dead gen_var_by_name/2, compute_env_var_ref_at_codegen/1, and current_function_env_upvalue_index/1 helpers — the var_map approach is strictly more general (it can resolve an upvalue head, which the old helper could not).

  3. test/lua/vm/func_decl_test.exs — new file with six regression tests pinning each shape: method sugar against block-local, dotted multi-name against block-local, three-deep dotted, head-as-upvalue, body captures head, and free-name head through _ENV.

  4. test/lua53_skips.exs — replaced the calls.lua :all skip with five narrowed ranges. Four point at known separate issues (Parenthesised call/vararg should adjust to a single value (§3.4) #254 parenthesized adjustment, parser ambiguity wart at line 135, stdlib extra-arg tolerance at line 217, a load()/string.dump cluster at lines 219–401). One — lines: 65..69 — is a new discovery: open-upvalue staleness across do blocks, filed as Open-upvalue cells leak across do/block boundaries when registers are reused #276 and linked from the skip entry.

Discoveries (full detail in plan file)

  1. Body-first ordering matters. First draft resolved the head before the body — wrong, because captured_locals isn't populated yet. Single-name FuncDecl already documents why body-first is required (scope.ex:261 comment); multi-name now matches.
  2. gen_var_by_name/2 had no other callers. With multi-name migrated to var_map, the entire helper trio became dead. --warnings-as-errors flagged it; removed.
  3. Open-upvalue staleness across do blocks — pre-existing, exposed by this fix at calls.lua:65–69. Filed as Open-upvalue cells leak across do/block boundaries when registers are reused #276; test/lua53_skips.exs:65..69 links to it.

Verification

mix format
mix compile --warnings-as-errors
mix test                                # 1970 passed, 0 failures, 24 skipped (was 1963/25)
mix test test/lua/vm/func_decl_test.exs # 6 passed
mix test --only lua53                   # calls.lua now runs, 23 lines skipped across 5 ranges

Out of scope (intentional)

`function a.b.c(...)` and `function a:m(...)` resolved the head name
`a` at codegen time against the function-scope snapshot, which was
already restored to the parent scope by the time codegen ran. A local
declared inside an enclosing `do`/`if`/`while`/`for` block missed the
lookup, and the assignment was emitted against `_ENV.a` (the global)
instead of the live local. This was the blocker keeping calls.lua
deferred at `:all` in test/lua53_skips.exs.

Mirror what single-name FuncDecl already does: resolve the head during
scope analysis (after the body, so captured_locals is populated) and
stash the result in `var_map` keyed on the FuncDecl node. Codegen reads
that entry to emit the appropriate load of `a` before walking the field
chain.

Closes #255
@davydog187
davydog187 merged commit 308bd8f into main May 28, 2026
5 checks passed
@davydog187
davydog187 deleted the fix/funcdecl-head-name branch May 28, 2026 12:55
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.

FuncDecl target name resolves against post-block scope

1 participant