Skip to content

FuncDecl target name resolves against post-block scope #255

Closed

Description

@davydog187

Problem

For function a.b.c(...) or function a:m(...), codegen resolves the head name a at codegen time against ctx.scope.locals, which is the function-scope snapshot stored after the body has been resolved. If a was declared inside a nested block whose scope has since been restored, the lookup misses and the assignment is emitted against _ENV.a (the global) instead of the local.

Repro

a = {i=10}                                -- global a
do
  local a = {x=0}                          -- shadows global a
  function a:add (x)                       -- expected: local a.add = ...
    self.x, a.y = self.x+x, 20
    return self
  end
  return a:add(10)                         -- a.add is nil → "attempt to call a nil value"
end

This is the blocker that keeps calls.lua deferred (currently :all in test/lua53_skips.exs).

Why it surfaces now

Pre-existing for do blocks (which already saved/restored scope). PR #251 added save/restore for if/while/repeat/for, so the same bug now applies to those forms too.

Suggested approach

  1. In lib/lua/compiler/scope.ex, when processing Statement.FuncDecl (both single-name and multi-name clauses), resolve the head of the name path the same way resolve_expr(%Expr.Var{}) does, and store the result under a stable var_map key (e.g. {:func_decl_head, decl}). The single-name clause already does most of this; extend it to multi-name (a.b.c.f, a:m).
  2. In lib/lua/compiler/codegen.ex, gen_var_by_name/2 is currently invoked from FuncDecl statement codegen. Replace that with a var_map lookup keyed on the FuncDecl node.
  3. Remove the now-unused gen_var_by_name/2 path if nothing else calls it, otherwise leave it for the chunk-level top-of-function case where the scope snapshot is still authoritative.

Acceptance

  • Regression test in test/lua/vm/ reproducing the snippet above (with do/local/function ... :m).
  • calls.lua either passes or progresses to a separately-tracked failure. Replace its :all skip with a narrower range or drop it entirely.

Notes

  • The same root cause would affect any name-resolution call that happens after block scope has been restored. The fix migrates FuncDecl head names from "live scope at codegen" to "resolved-and-stashed at scope analysis", which is the model the rest of the resolver already uses for Expr.Var.

Activity

  1. added
    bugSomething isn't working
    direction:ADirection A: suite triage to 0.5.0
    kind:suiteLua 5.3 official test suite work
    on May 28, 2026
  2. added this to the 1.0.0 milestone on May 28, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingdirection:ADirection A: suite triage to 0.5.0kind:suiteLua 5.3 official test suite work

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions