Repository navigation
fix(compiler): capture _ENV only in closures that use a global - #433
Open
dbernheisel wants to merge 1 commit into
Open
dbernheisel wants to merge 1 commit into
dbernheisel wants to merge 1 commit into
Conversation
Every nested function took _ENV as upvalue 1, even when it referenced no global. debug.getupvalue and debug.setupvalue indices were therefore off by one against PUC-Lua, and debug.setupvalue(f, 1, v) on a closure over a local overwrote _ENV instead of that local. Scope resolution no longer allocates _ENV at function entry. The first free-name reference allocates it through the existing upvalue chain, so numbering follows first use, as in PUC-Lua.
dbernheisel
marked this pull request as ready for review
October 19, 2026 17:04
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.
Summary
_ENVupvalue at the entry of each nested function inLua.Compiler.Scope._ENVonly at its first reference to a global. The existing upvalue chain (resolve_env_ref/1andensure_upvalue/4) does this.local chascin slot 1. A closure that readscand thenprinthascin slot 1 and_ENVin slot 2.debug.setupvalue(f, 1, v)now changesc. Before, it changed_ENV.luabinary gave the new expected values._ENVforwarding through a parent that does not use globals, for an_ENVparameter, and fordebug.setupvaluein a later evaluation.Evidence
iex
PUC-Lua 5.5.1 gives the same result for the same source:
debug.setupvalue(getc, 1, 99)returnsc, andgetc()returns99.