Repository navigation
Closed
Labels
bugSomething isn't workingSomething isn't workingdirection:ADirection A: suite triage to 0.5.0Direction A: suite triage to 0.5.0kind:suiteLua 5.3 official test suite workLua 5.3 official test suite work
Milestone
Description
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"
endThis 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
- In
lib/lua/compiler/scope.ex, when processingStatement.FuncDecl(both single-name and multi-name clauses), resolve the head of the name path the same wayresolve_expr(%Expr.Var{})does, and store the result under a stablevar_mapkey (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). - In
lib/lua/compiler/codegen.ex,gen_var_by_name/2is currently invoked from FuncDecl statement codegen. Replace that with avar_maplookup keyed on the FuncDecl node. - Remove the now-unused
gen_var_by_name/2path 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 (withdo/local/function ... :m). calls.luaeither passes or progresses to a separately-tracked failure. Replace its:allskip 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.
Metadata
Metadata
Assignees
Labels
bugSomething isn't workingSomething isn't workingdirection:ADirection A: suite triage to 0.5.0Direction A: suite triage to 0.5.0kind:suiteLua 5.3 official test suite workLua 5.3 official test suite work
Activity