fix: resolve local names in methods as self sends instead of upvalues
When a method references a name that exists as a local variable outside the method boundary, the compiler now correctly treats it as a self send rather than closing over the outer local. This fixes the closure resolution logic in `findUpvalue` to stop searching for upvalues when hitting a method boundary, unless the name starts with '_' indicating a static field access. The change removes two incorrect test cases that expected outer local closure behavior and adds a new test verifying that both instance and static methods resolve local names to their own methods.
This commit is contained in:
@@ -1230,9 +1230,13 @@ static int addUpvalue(Compiler* compiler, bool isLocal, int index)
|
||||
// not close over local variables.
|
||||
static int findUpvalue(Compiler* compiler, const char* name, int length)
|
||||
{
|
||||
// If we are at a method boundary or the top level, we didn't find it.
|
||||
if (compiler->parent == NULL || compiler->enclosingClass != NULL) return -1;
|
||||
|
||||
// If we are at the top level, we didn't find it.
|
||||
if (compiler->parent == NULL) return -1;
|
||||
|
||||
// If we hit the method boundary (and the name isn't a static field), then
|
||||
// stop looking for it. We'll instead treat it as a self send.
|
||||
if (name[0] != '_' && compiler->parent->enclosingClass != NULL) return -1;
|
||||
|
||||
// See if it's a local variable in the immediately enclosing function.
|
||||
int local = resolveLocal(compiler->parent, name, length);
|
||||
if (local != -1)
|
||||
|
||||
Reference in New Issue
Block a user