From 93e8c2dd46c89e1095c7eee9633363f61a2f6b72 Mon Sep 17 00:00:00 2001 From: Dave Lucia Date: Thu, 28 May 2026 07:48:37 -0400 Subject: [PATCH 1/5] chore(A41): draft plan for FuncDecl head-name resolution --- .agents/plans/A41-funcdecl-head-name.md | 173 ++++++++++++++++++++++++ 1 file changed, 173 insertions(+) create mode 100644 .agents/plans/A41-funcdecl-head-name.md diff --git a/.agents/plans/A41-funcdecl-head-name.md b/.agents/plans/A41-funcdecl-head-name.md new file mode 100644 index 00000000..051eb65d --- /dev/null +++ b/.agents/plans/A41-funcdecl-head-name.md @@ -0,0 +1,173 @@ +--- +id: A41 +title: Resolve FuncDecl head names at scope analysis (not post-block codegen) +issue: 255 +pr: null +branch: fix/funcdecl-head-name +base: main +status: ready +direction: A +unlocks: + - calls.lua +--- + +## 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}` (`lib/lua/compiler/scope.ex:246–287`). This plan +extends that same pattern to multi-name FuncDecl heads. + +## Out of scope + +- Reworking `gen_var_by_name/2` for its other (chunk-level) hypothetical + callers — it has none today besides multi-name FuncDecl, but the helper + can stay in place until a follow-up cleanup removes it deliberately. +- Adding hints / better error messages for the multi-name path. +- Any other `calls.lua` follow-up failures that surface after this fix — + document them and decide in-flight whether they're in scope or split. + +## Success criteria + +- [ ] Repro from issue #255 passes (a `do` block with a `local a` shadowing + a global, then `function a:m(...)`, then `a:m(...)`). +- [ ] The same fix covers `function a.b.c.f(...)` (dotted multi-name). +- [ ] `mix test` passes with no regressions (baseline: 1963 passed, + 25 skipped on main at `e8b9c4d`). +- [ ] Regression test added in `test/lua/vm/` covering both the method + sugar (`function a:m`) and the dotted multi-name (`function a.b.c`) + against a block-local `a`. +- [ ] `calls.lua` either passes end-to-end (drop `:all` skip) or narrows + to a smaller, separately-tracked failure with an updated `reason:`. + +## Implementation notes + +### Scope analysis (`lib/lua/compiler/scope.ex`) + +Today the multi-name FuncDecl clause is a one-liner at lines 289–292 that +only calls `resolve_function_scope/4` for the body. Add a new clause that +runs *before* it (more specific match) for `[first | rest]` when +`length(rest) > 0`: + +1. Resolve the head name `first` exactly like `Expr.Var` is resolved + (`scope.ex:360–382`): check `state.locals`, then `find_upvalue/3`, + else `{:env_field, env_ref, first}`. +2. Stash the result under `var_map` key `{:func_decl_head, decl}`. +3. Then call `resolve_function_scope(decl, all_params, body, state)`. + +The head resolution must happen *before* `resolve_function_scope/4` so it +reads the live scope (the surrounding block's locals are still present). +Inside `resolve_function_scope/4` the current function changes and locals +are swapped, which would shadow the lookup. + +`all_params` accounts for `is_method`: prepend `"self"` when true. + +### Codegen (`lib/lua/compiler/codegen.ex`) + +At `codegen.ex:676–691`, the multi-name arm currently calls +`gen_var_by_name(first, ctx)` (line 678). Replace that with a `var_map` +lookup keyed on `{:func_decl_head, decl}`. The four cases mirror +`gen_expr(%Expr.Var{}, ctx)` (lines 979–1008): + +- `{:register, reg}` → `{[], reg, ctx}` (already loaded). +- `{:captured_local, reg}` → emit `get_open_upvalue` into a fresh reg. +- `{:upvalue, index}` → emit `get_upvalue` into a fresh reg. +- `{:env_field, env_ref, name}` → call `gen_env_field_get(env_ref, name, ctx)`. + +Everything downstream (the field-walk over `rest` and the trailing +`set_field`) stays unchanged. + +Leave `gen_var_by_name/2` in place if no other caller is removed — the +private helper costs nothing while it's unreachable. Annotate it +`# Deprecated: kept temporarily; sole caller migrated to var_map.` only +if it's obviously dead, otherwise leave it untouched. + +### Tests (`test/lua/vm/`) + +Add regression tests in `test/lua/vm/func_decl_test.exs` (new file — +existing function-declaration coverage is scattered, this gives the +plan a dedicated home). + +Required cases: + +1. **Method sugar against block-local** (the issue's repro): + ```lua + a = {i=10} -- global + local result + do + local a = {x=0} -- shadows + function a:add(x) + self.x = self.x + x + return self + end + result = a:add(10).x + end + return result -- expected 10 + ``` +2. **Dotted multi-name against block-local**: + ```lua + local result + do + local a = {} + function a.b() + return 42 + end + result = a.b() + end + return result -- expected 42 + ``` +3. **Upvalue head name** — outer fn declares `local a = {}`, inner fn + defines `function a.b()`. Verifies the upvalue branch of the new + var_map entry. +4. **Free-name head (global)** — `function a.b()` at chunk top level + with no local `a`. Verifies the `:env_field` branch still emits a + `_ENV` field read. + +### Suite skip (`test/lua53_skips.exs`) + +Drop or narrow the `calls.lua` `:all` entry (`test/lua53_skips.exs:42–51`). +After the fix, run `mix test --only lua53` against `calls.lua` and either: +- Remove the entry entirely if calls.lua passes. +- Narrow `lines:` to the residual failure range and update `reason:` + to describe the remaining cause (not the one fixed here). + +## Verification + +```bash +mix format +mix compile --warnings-as-errors +mix test +mix test test/lua/vm/func_decl_test.exs +mix test --only lua53 +``` + +Capture before/after status with `mix lua.suite --status` and include the +delta in the PR body. + +## Risks + +- Multi-name FuncDecl is rarer than single-name in unit tests, so a subtle + regression here may not show up immediately. Mitigated by adding all four + shape variants in the new test file. +- `find_upvalue/3` walks parent scopes and may create new upvalue + descriptors. For the head name of a multi-name FuncDecl, that's exactly + what we want (same as for an `Expr.Var` read of the same name). Verify + that `state.functions[...].upvalue_descriptors` ends up identical to + what the single-name path would produce for the same lookup. +- `gen_var_by_name/2` may still be on the critical path for some + edge case we haven't enumerated. Leave it in for now; only remove if + a deliberate cleanup PR audits all callers. + +## Discoveries + +(populated during implementation) + +## What changed + +(populated when PR opens) From 7871e8efb23608e21951dc979de52c5405259bde Mon Sep 17 00:00:00 2001 From: Dave Lucia Date: Thu, 28 May 2026 07:48:44 -0400 Subject: [PATCH 2/5] chore(A41): start plan --- .agents/plans/A41-funcdecl-head-name.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.agents/plans/A41-funcdecl-head-name.md b/.agents/plans/A41-funcdecl-head-name.md index 051eb65d..1ab941d3 100644 --- a/.agents/plans/A41-funcdecl-head-name.md +++ b/.agents/plans/A41-funcdecl-head-name.md @@ -5,7 +5,7 @@ issue: 255 pr: null branch: fix/funcdecl-head-name base: main -status: ready +status: in-progress direction: A unlocks: - calls.lua From 64b7d85b25a34e5b09bf11bc416f18e206acf2a5 Mon Sep 17 00:00:00 2001 From: Dave Lucia Date: Thu, 28 May 2026 08:06:10 -0400 Subject: [PATCH 3/5] fix(compiler): resolve FuncDecl head names at scope analysis `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 --- lib/lua/compiler/codegen.ex | 119 ++++++++--------------------- lib/lua/compiler/scope.ex | 58 ++++++++++++-- test/lua/vm/func_decl_test.exs | 135 +++++++++++++++++++++++++++++++++ test/lua53_skips.exs | 36 ++++++++- 4 files changed, 252 insertions(+), 96 deletions(-) create mode 100644 test/lua/vm/func_decl_test.exs diff --git a/lib/lua/compiler/codegen.ex b/lib/lua/compiler/codegen.ex index 6b6fe111..cf051bb3 100644 --- a/lib/lua/compiler/codegen.ex +++ b/lib/lua/compiler/codegen.ex @@ -673,9 +673,10 @@ defmodule Lua.Compiler.Codegen do {closure_instructions ++ store_instructions, ctx} - [first | rest] -> - # Dotted name: get the table chain, then set the final field - {get_instructions, table_reg, ctx} = gen_var_by_name(first, ctx) + [_first | rest] -> + # Dotted name: load the head value via the var_map entry resolved + # by scope analysis, then walk the field chain and set the leaf. + {get_instructions, table_reg, ctx} = gen_func_decl_head(decl, ctx) {final_instructions, final_table_reg, ctx} = Enum.reduce(Enum.slice(rest, 0..-2//1), {get_instructions, table_reg, ctx}, fn field, {instrs, reg, ctx} -> @@ -748,6 +749,34 @@ defmodule Lua.Compiler.Codegen do # Stub for other statements defp gen_statement(_stmt, ctx), do: {[], ctx} + # Emit instructions that load the head value of a multi-name FuncDecl + # (`function a.b.c(...)`) into a register, using the var_map entry + # populated by scope analysis. Mirrors the four cases of `gen_expr` on + # `%Expr.Var{}` — local register, captured-local cell, parent upvalue, + # or free name read through `_ENV`. + defp gen_func_decl_head(decl, ctx) do + case Map.get(ctx.scope.var_map, {:func_decl_head, decl}) do + {:register, reg} -> + {[], reg, ctx} + + {:captured_local, reg} -> + dest = ctx.next_reg + ctx = %{ctx | next_reg: dest + 1} + {[Instruction.get_open_upvalue(dest, reg)], dest, ctx} + + {:upvalue, index} -> + dest = ctx.next_reg + ctx = %{ctx | next_reg: dest + 1} + {[Instruction.get_upvalue(dest, index)], dest, ctx} + + {:env_field, env_ref, name} -> + gen_env_field_get(env_ref, name, ctx) + + nil -> + raise "codegen: missing var_map entry for FuncDecl head #{inspect(decl.name)}" + end + end + # Helpers for assignment target code generation defp gen_assign_target(%Expr.Var{} = target_var, value_reg, ctx) do case Map.get(ctx.scope.var_map, target_var) do @@ -1436,90 +1465,6 @@ defmodule Lua.Compiler.Codegen do {[Instruction.load_constant(reg, nil)], reg, ctx} end - # Look up a variable by name (not by AST node identity). - # Used when we need to resolve a variable name that doesn't have a corresponding - # scope-resolved Expr.Var node (e.g., FuncDecl table chain names). - # - # Note: this path operates on the chunk-level locals map. It only matches - # chunk-level locals (and chunk-level `_ENV` at register 0). This means for - # `function foo.bar()` where `foo` is an upvalue of an outer function, this - # path will fall through to `_ENV.foo`, which is the correct Lua 5.3 - # behaviour for a free name in a chunk. Pre-existing limitation: if `foo` - # is captured from a deeper enclosing scope, this won't walk the upvalue - # chain — `_ENV.foo` is used instead. Acceptable: all FuncDecl table-chain - # head names that aren't chunk-level locals are conventionally globals. - defp gen_var_by_name(name, ctx) do - case Map.get(ctx.scope.locals, name) do - nil -> - # Not a chunk-level local — treat as `_ENV.name`. Resolve `_ENV` - # itself via the same chain as scope.ex so we use the right cell - # (chunk register 0, captured local, or upvalue). - env_ref = compute_env_var_ref_at_codegen(ctx) - gen_env_field_get(env_ref, name, ctx) - - local_reg -> - if MapSet.member?(ctx.scope.captured_locals, name) do - # Captured local — read from open upvalue cell - dest = ctx.next_reg - ctx = %{ctx | next_reg: dest + 1} - {[Instruction.get_open_upvalue(dest, local_reg)], dest, ctx} - else - {[], local_reg, ctx} - end - end - end - - # Resolve `_ENV` as a `var_ref` at codegen time. This is used by - # `gen_var_by_name`, which doesn't go through the AST tag path; it - # operates on the *current function's* scope (`ctx.scope.locals`). - # - # `gen_var_by_name` is only invoked from FuncDecl statement codegen at - # the top level of a chunk (or nested fn body), so the lookup happens - # against the function whose locals are reflected in `ctx.scope`. The - # chunk's `_ENV` is at register 0; nested functions hold `_ENV` as an - # upvalue. - defp compute_env_var_ref_at_codegen(ctx) do - case Map.get(ctx.scope.locals, "_ENV") do - nil -> - # No local _ENV — must be an upvalue. Find its index in the current - # function's upvalue_descriptors. Note: the scope resolver should - # have ensured this upvalue exists (any FuncDecl in a non-chunk - # function will have triggered ensure_upvalue when free names were - # resolved). If somehow the function has no free names elsewhere, - # we still need _ENV — fall back to looking at the current - # function's upvalues for a `_ENV` entry. - case current_function_env_upvalue_index(ctx) do - {:ok, index} -> {:upvalue, index} - :not_found -> raise "codegen: _ENV unreachable in gen_var_by_name (Plan A16)" - end - - reg -> - if MapSet.member?(ctx.scope.captured_locals, "_ENV") do - {:captured_local, reg} - else - {:register, reg} - end - end - end - - defp current_function_env_upvalue_index(ctx) do - # The codegen ctx doesn't expose the current function key directly, but - # we can look it up via ctx.scope.functions for the function we're - # currently generating. The chunk's locals map should always have - # `_ENV`, so this path is only hit for nested functions, where we'd - # have a `_ENV` upvalue allocated by scope resolution. If not, that's - # a scope-resolution bug. - func_scope = ctx.scope.functions[ctx.current_function] - - case Enum.find_index(func_scope.upvalue_descriptors, fn - {:parent_local, _, n} -> n == "_ENV" - {:parent_upvalue, _, n} -> n == "_ENV" - end) do - nil -> :not_found - idx -> {:ok, idx} - end - end - # Emit a `_ENV` lookup followed by a `get_field` for `name`. defp gen_env_field_get(env_ref, name, ctx) do {load_instrs, env_reg, ctx} = gen_load_env(env_ref, ctx) diff --git a/lib/lua/compiler/scope.ex b/lib/lua/compiler/scope.ex index 0f3f8bcf..2641d5dc 100644 --- a/lib/lua/compiler/scope.ex +++ b/lib/lua/compiler/scope.ex @@ -286,6 +286,24 @@ defmodule Lua.Compiler.Scope do end end + defp resolve_statement( + %Statement.FuncDecl{name: [first | rest], params: params, body: body, is_method: is_method} = decl, + state + ) + when rest != [] do + # Multi-name FuncDecl (`function a.b.c(...)` / `function a:m(...)`). + # Process the body FIRST so `state.captured_locals` reflects whether + # the head name is captured by the function body (e.g. `a.y = ...` + # inside `function a:m`). `resolve_function_scope` restores the outer + # scope's `locals` and merges newly-captured names into + # `captured_locals`, so the post-call state matches what the head + # lookup needs. Mirrors the single-name FuncDecl ordering above. + all_params = if is_method, do: ["self" | params], else: params + state = resolve_function_scope(decl, all_params, body, state) + + resolve_func_decl_head(first, decl, state) + end + defp resolve_statement(%Statement.FuncDecl{params: params, body: body, is_method: is_method} = decl, state) do all_params = if is_method, do: ["self" | params], else: params resolve_function_scope(decl, all_params, body, state) @@ -431,6 +449,33 @@ defmodule Lua.Compiler.Scope do # For now, stub out other expression types defp resolve_expr(_expr, state), do: state + # Resolve the head name of a multi-name FuncDecl the same way an + # `Expr.Var` read is resolved. The result is stashed under + # `{:func_decl_head, decl}` so codegen can replay the lookup without + # re-reading the post-block locals snapshot. + defp resolve_func_decl_head(name, decl, state) do + key = {:func_decl_head, decl} + + case Map.get(state.locals, name) do + nil -> + case find_upvalue(name, state.parent_scopes, state) do + {:ok, upvalue_index, state} -> + %{state | var_map: Map.put(state.var_map, key, {:upvalue, upvalue_index})} + + :not_found -> + {env_ref, state} = resolve_env_ref(state) + %{state | var_map: Map.put(state.var_map, key, {:env_field, env_ref, name})} + end + + reg -> + if MapSet.member?(state.captured_locals, name) do + %{state | var_map: Map.put(state.var_map, key, {:captured_local, reg})} + else + %{state | var_map: Map.put(state.var_map, key, {:register, reg})} + end + end + end + # Walk up the scope chain to find a variable and create upvalue descriptors. # Delegates to ensure_upvalue which handles multi-level nesting correctly. defp find_upvalue(name, parent_scopes, state) do @@ -594,13 +639,12 @@ defmodule Lua.Compiler.Scope do state = %{state | functions: Map.put(state.functions, func_key, func_scope)} - # Plan A16: every nested function inherits `_ENV` as an upvalue. We - # eagerly resolve `_ENV` before the body is processed so codegen paths - # that need it (notably `gen_var_by_name` for FuncDecl table-chain - # heads) can rely on it being present, and so the upvalue index is - # known up-front. If the function shadows `_ENV` with a parameter or - # later `local _ENV = ...`, those local bindings still take precedence - # for free-name resolution, which is the correct Lua 5.3 behaviour. + # Every nested function inherits `_ENV` as an upvalue. Resolve it + # eagerly before the body is processed so codegen paths that read + # free names through `_ENV` can rely on the upvalue being present + # with a known index. A parameter or `local _ENV = ...` still takes + # precedence for free-name resolution, which is the correct Lua 5.3 + # behaviour. state = if Map.has_key?(state.locals, "_ENV") do # `_ENV` is a parameter — no upvalue allocation needed at this entry diff --git a/test/lua/vm/func_decl_test.exs b/test/lua/vm/func_decl_test.exs new file mode 100644 index 00000000..e983bae2 --- /dev/null +++ b/test/lua/vm/func_decl_test.exs @@ -0,0 +1,135 @@ +defmodule Lua.VM.FuncDeclTest do + use ExUnit.Case, async: true + + alias Lua.Compiler + alias Lua.Parser + alias Lua.VM + alias Lua.VM.State + + # Per Lua 5.3 §3.4.11: + # `function a.b.c(...) end` is sugar for `a.b.c = function(...) end` + # `function a:m(...) end` is sugar for `a.m = function(self, ...) end`. + # The head name `a` must resolve against the live scope at the point of + # declaration, *not* against the post-block function-scope snapshot used + # by codegen. + + describe "multi-name FuncDecl head resolves against live scope" do + test "method sugar against block-local shadowing a global" do + code = """ + a = {i=10} + local result + do + local a = {x=0} + function a:add(x) + self.x = self.x + x + return self + end + result = a:add(10).x + end + return result + """ + + assert {:ok, ast} = Parser.parse(code) + assert {:ok, proto} = Compiler.compile(ast, source: "test.lua") + state = State.new() + assert {:ok, [10], _state} = VM.execute(proto, state) + end + + test "dotted multi-name against block-local" do + code = """ + local result + do + local a = {} + function a.b() + return 42 + end + result = a.b() + end + return result + """ + + assert {:ok, ast} = Parser.parse(code) + assert {:ok, proto} = Compiler.compile(ast, source: "test.lua") + state = State.new() + assert {:ok, [42], _state} = VM.execute(proto, state) + end + + test "three-deep dotted multi-name against block-local" do + code = """ + local result + do + local a = {b = {}} + function a.b.c() + return "ok" + end + result = a.b.c() + end + return result + """ + + assert {:ok, ast} = Parser.parse(code) + assert {:ok, proto} = Compiler.compile(ast, source: "test.lua") + state = State.new() + assert {:ok, ["ok"], _state} = VM.execute(proto, state) + end + + test "head name resolves as an upvalue when captured by an inner function" do + code = """ + local function outer() + local a = {} + local function inner() + function a.b() return 7 end + return a.b() + end + return inner() + end + return outer() + """ + + assert {:ok, ast} = Parser.parse(code) + assert {:ok, proto} = Compiler.compile(ast, source: "test.lua") + state = State.new() + assert {:ok, [7], _state} = VM.execute(proto, state) + end + + test "method captures head local via upvalue when body references head name" do + # Mirrors calls.lua lines 65-69: the method body references `a` itself, + # so `a` is captured by the function. The FuncDecl head must read the + # same captured-local cell so the chain operates on the live table. + code = """ + local result_x, result_y + do + local a = {x=0} + function a:add(n) + self.x, a.y = self.x + n, 20 + return self + end + local chained = a:add(10):add(20):add(30) + result_x = chained.x + result_y = a.y + end + return result_x, result_y + """ + + assert {:ok, ast} = Parser.parse(code) + assert {:ok, proto} = Compiler.compile(ast, source: "test.lua") + state = State.new() + assert {:ok, [60, 20], _state} = VM.execute(proto, state) + end + + test "head name with no local in scope still writes through _ENV" do + code = """ + g = {} + function g.h() + return "global" + end + return g.h() + """ + + assert {:ok, ast} = Parser.parse(code) + assert {:ok, proto} = Compiler.compile(ast, source: "test.lua") + state = State.new() + assert {:ok, ["global"], _state} = VM.execute(proto, state) + end + end +end diff --git a/test/lua53_skips.exs b/test/lua53_skips.exs index 3c722e0a..24112899 100644 --- a/test/lua53_skips.exs +++ b/test/lua53_skips.exs @@ -42,10 +42,42 @@ ], "calls.lua" => [ %{ - lines: :all, + lines: 24..36, + category: :stdlib, + reason: "print does not call user-overridden _ENV.tostring at each invocation", + issue: nil + }, + %{ + lines: 65..69, + category: :executor, + reason: + "stale upvalue cell across do blocks: register reused for a new local still resolves through the previous block's open_upvalues entry", + issue: nil + }, + %{ + lines: 135..137, + category: :parser, + reason: + "parser treats `(fn)(args)` on a new line as a call on the previous expression (Lua 5.3 §3.3.1 ambiguity wart); test expects two statements", + issue: nil + }, + %{ + lines: 207..208, category: :semantic, + reason: "parenthesized call/vararg does not adjust to a single value (Lua 5.3 §3.4)", + issue: 254 + }, + %{ + lines: 217..218, + category: :stdlib, + reason: "math.sin / table.sort reject extra args; PUC-Lua silently ignores them", + issue: nil + }, + %{ + lines: 219..401, + category: :unimplemented, reason: - "FuncDecl target name resolved at codegen against post-block scope; print does not call user-overridden tostring", + "load() / string.dump / debug.getupvalue / coroutine.wrap and tail-call counting all exercised below this line; needs its own triage pass", issue: nil } ], From 4d1928a9bf73e59ab69896b22bd0bb8d2b4e4aa5 Mon Sep 17 00:00:00 2001 From: Dave Lucia Date: Thu, 28 May 2026 08:07:03 -0400 Subject: [PATCH 4/5] chore(A41): mark plan as review --- .agents/plans/A41-funcdecl-head-name.md | 78 +++++++++++++++++++++++-- 1 file changed, 74 insertions(+), 4 deletions(-) diff --git a/.agents/plans/A41-funcdecl-head-name.md b/.agents/plans/A41-funcdecl-head-name.md index 1ab941d3..1ef94710 100644 --- a/.agents/plans/A41-funcdecl-head-name.md +++ b/.agents/plans/A41-funcdecl-head-name.md @@ -2,10 +2,10 @@ id: A41 title: Resolve FuncDecl head names at scope analysis (not post-block codegen) issue: 255 -pr: null +pr: 274 branch: fix/funcdecl-head-name base: main -status: in-progress +status: review direction: A unlocks: - calls.lua @@ -166,8 +166,78 @@ delta in the PR body. ## Discoveries -(populated during implementation) +1. **Body-first ordering matters.** The first draft of the multi-name + FuncDecl scope clause resolved the head name *before* calling + `resolve_function_scope/4`. That made the head's `captured_locals` + check race the body, so a head that the body captured (e.g. + `function a:add (x) ... a.y = 20 ... end`) was tagged as a plain + `{:register, _}` instead of `{:captured_local, _}`. Single-name + FuncDecl already resolves *after* the body for this exact reason + (see the comment at `scope.ex:261`). Multi-name now matches. + +2. **`gen_var_by_name/2` was the only chunk-level by-name caller.** + With the multi-name FuncDecl head migrated to the var_map, the + `gen_var_by_name/2` / `compute_env_var_ref_at_codegen/1` / + `current_function_env_upvalue_index/1` trio in + `lib/lua/compiler/codegen.ex` had no remaining callers and tripped + `--warnings-as-errors`. Removed all three; the var_map approach is + strictly more general (it walks the upvalue chain, where + `gen_var_by_name` could only see chunk-level locals). + +3. **Pre-existing open-upvalue staleness across `do` blocks.** + `pm.lua` was lucky; `calls.lua:65–69` is not. When a `do` block + declares a captured local (e.g. `local function fact (n) ... + fact(n-1) ... end`), the executor's `state.open_upvalues` map + records a cell at that register. The mapping is never closed when + the block ends. The *next* `do` block to reuse the same register + for a different captured local (e.g. `local a = {x=0}` followed by + `function a:add (x) ... a.y = ... end`) inherits the stale cell — + the closure handler at `executor.ex:707` reuses the existing cell + ref, so reads of `a` through `get_open_upvalue` return the prior + block's value. This is unrelated to FuncDecl head resolution and + blocks `calls.lua:65–69`. Narrowed via skip; needs its own plan + (likely "close open upvalues at block end"). + +4. **calls.lua progress is bigger than the FuncDecl head fix alone.** + After landing this PR, `calls.lua` runs lines 1–218 cleanly (the + first ~54% of the file). The remaining ranges are deferred for + reasons documented inline in `test/lua53_skips.exs` — three of the + five new skip ranges correspond to known concerns (#254 + parenthesized adjustment, parser ambiguity wart, stdlib + extra-arg-tolerance) and one (`lines: 65..69`) is the new + open-upvalue staleness discovery above. ## What changed -(populated when PR opens) +PR: [#274](https://github.com/tv-labs/lua/pull/274) + +Files touched: + +- `lib/lua/compiler/scope.ex` — new `Statement.FuncDecl` clause for + multi-name (`[first | rest]` with `rest != []`). Resolves the head + name via the new `resolve_func_decl_head/3` helper *after* + `resolve_function_scope/4` so `captured_locals` is fully populated. + Removed the stale `gen_var_by_name`-era comment from the + `_ENV` upvalue preamble. + +- `lib/lua/compiler/codegen.ex` — multi-name arm of the + `Statement.FuncDecl` codegen now loads the head value via the + new `gen_func_decl_head/2` helper (`var_map` lookup), mirroring the + four `Expr.Var` cases. Deleted `gen_var_by_name/2`, + `compute_env_var_ref_at_codegen/1`, and + `current_function_env_upvalue_index/1` (no remaining callers). + +- `test/lua/vm/func_decl_test.exs` — new file, six regression tests + covering: method-sugar against block-local, dotted multi-name + against block-local, three-deep dotted name, head-as-upvalue (inner + function), body captures head (calls.lua:65–69 shape), and the + no-local-in-scope `_ENV` fallback. + +- `test/lua53_skips.exs` — replaced `calls.lua` `:all` skip with five + narrowed ranges. The file now runs lines 1–218 (54% of 401 lines); + remaining skips are categorised by underlying cause. + +Suite delta: unit suite 1963 → 1970 passed (+7, no regressions, six +new tests in `func_decl_test.exs` plus one previously-skipped suite +test now running). lua53 official suite: `calls.lua` moved out of the +`:all`/`@tag :skip` bucket into the ranged-skip bucket. From 3a48d1b50fa0cf606afc9e7586da4cbf082e685e Mon Sep 17 00:00:00 2001 From: Dave Lucia Date: Thu, 28 May 2026 08:45:22 -0400 Subject: [PATCH 5/5] chore(suite): link calls.lua open-upvalue skip to #276 --- test/lua53_skips.exs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/lua53_skips.exs b/test/lua53_skips.exs index 24112899..2892b728 100644 --- a/test/lua53_skips.exs +++ b/test/lua53_skips.exs @@ -52,7 +52,7 @@ category: :executor, reason: "stale upvalue cell across do blocks: register reused for a new local still resolves through the previous block's open_upvalues entry", - issue: nil + issue: 276 }, %{ lines: 135..137,