Repository navigation
fix(compiler): resolve FuncDecl head names at scope analysis - #274
Merged
Merged
Conversation
`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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Resolve FuncDecl head names at scope analysis (not post-block codegen)
Plan:
.agents/plans/A41-funcdecl-head-name.mdCloses #255
Goal
Fix the multi-name
FuncDeclcodegen path (function a.b.c(...)/function a:m(...)) so the head namearesolves against the livescope 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.luadeferred at:allintest/lua53_skips.exs.The single-name
FuncDeclalready does the right thing — it resolvesthe target during scope analysis and stashes the result under
{:func_decl_target, decl}. This PR extends that same pattern tomulti-name FuncDecl heads.
Success criteria
do / local a = {x=0} / function a:add(x) ... end / a:add(10).function a.b.c.f(...)(dotted multi-name).mix testpasses with no regressions (1963 → 1970 passed, no failures).test/lua/vm/func_decl_test.exscovering six shape variants.calls.luano 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
Approach
lib/lua/compiler/scope.ex— newStatement.FuncDeclclause for the multi-name case ([first | rest]withrest != []). Resolves the head name via a newresolve_func_decl_head/3helper afterresolve_function_scope/4has run, sostate.captured_localscorrectly reflects whether the head was captured by the body. Mirrors the ordering used by the single-name FuncDecl clause.lib/lua/compiler/codegen.ex— multi-name arm ofStatement.FuncDeclcodegen now loads the head via a newgen_func_decl_head/2helper that performs avar_maplookup (the same four cases asgen_expron%Expr.Var{}). Deleted the now-deadgen_var_by_name/2,compute_env_var_ref_at_codegen/1, andcurrent_function_env_upvalue_index/1helpers — the var_map approach is strictly more general (it can resolve an upvalue head, which the old helper could not).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.test/lua53_skips.exs— replaced thecalls.lua:allskip 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, aload()/string.dump cluster at lines 219–401). One —lines: 65..69— is a new discovery: open-upvalue staleness acrossdoblocks, 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)
captured_localsisn't populated yet. Single-name FuncDecl already documents why body-first is required (scope.ex:261comment); multi-name now matches.gen_var_by_name/2had no other callers. With multi-name migrated to var_map, the entire helper trio became dead.--warnings-as-errorsflagged it; removed.doblocks — pre-existing, exposed by this fix atcalls.lua:65–69. Filed as Open-upvalue cells leak across do/block boundaries when registers are reused #276;test/lua53_skips.exs:65..69links to it.Verification
Out of scope (intentional)
gen_var_by_name/2for non-existent other callers (we removed it instead).calls.luafollow-up failures (now documented inline intest/lua53_skips.exs).