Skip to content

Nested local _ENV redirection leaks scope (locals.lua:148-154) #284

Closed

Description

@davydog187

Problem

In the Lua 5.3 suite file locals.lua, the nested lexical-environment block at lines 148-153 fails:

do local _ENV = {assert=assert, A=10};
  do local _ENV = {assert=assert, A=20};
    assert(A==20);x=A      -- line 150: attempt to call a nil value (global 'assert')
  end
  assert(A==10 and x==20)
end

At line 150 the free name assert (compiled as _ENV.assert) resolves to nil, even though the innermost _ENV table is {assert=assert, A=20} and the enclosing _ENV.assert is the real assert function.

This is independent of debug.getupvalue/setupvalue and load — the block uses only local _ENV = {table} rebinding and free-name resolution. It surfaced while implementing #256 (which is otherwise complete: lines 71-83 and 144 of locals.lua now pass).

Notes

  • Context-dependent. Reproducing lines 148-153 in isolation (single- and two-level nesting, with and without a top-level local _ENV = (function(...) return ... end)(_G) rebind) all pass. Only the full accumulated context of locals.lua (lines 1-147, in particular the _ENV = mt / foo block at 137-146) triggers it. This points to a scope/state leak in nested local _ENV redirection rather than a bug in the two-level pattern itself.
  • Likely lives in lib/lua/compiler/scope.ex / codegen.ex _ENV resolution (see Plan A16 _ENV-as-register-0 design).

Current state

locals.lua is partial-skipped at lines: 148..154 (category: :semantic) in test/lua53_skips.exs. The rest of the file passes. Lift the skip once this is fixed.

Acceptance

  • Lines 148-154 of locals.lua pass.
  • Remove the 148..154 partial-skip entry for locals.lua.
  • A focused unit test reproducing the nested local _ENV scope leak.

Activity

  1. added
    bugSomething isn't working
    kind:suiteLua 5.3 official test suite work
    on May 31, 2026
  2. added this to the 1.0.0 milestone on May 31, 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 workingkind:suiteLua 5.3 official test suite work

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions