diff --git a/src/vm/wren_compiler.c b/src/vm/wren_compiler.c index e68d7323..f473a37d 100644 --- a/src/vm/wren_compiler.c +++ b/src/vm/wren_compiler.c @@ -1300,9 +1300,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) diff --git a/test/language/closure/closed_closure_in_method.wren b/test/language/closure/closed_closure_in_method.wren deleted file mode 100644 index 027e27aa..00000000 --- a/test/language/closure/closed_closure_in_method.wren +++ /dev/null @@ -1,17 +0,0 @@ -// TODO: Is this right? Shouldn't it resolve to this.local? -var foo = null - -{ - var local = "local" - class Foo { - construct new() {} - - method { - System.print(local) - } - } - - foo = Foo.new() -} - -foo.method // expect: local diff --git a/test/language/closure/open_closure_in_method.wren b/test/language/closure/open_closure_in_method.wren deleted file mode 100644 index 32abd318..00000000 --- a/test/language/closure/open_closure_in_method.wren +++ /dev/null @@ -1,13 +0,0 @@ -// TODO: Is this right? Shouldn't it resolve to this.local? -{ - var local = "local" - class Foo { - construct new() {} - - method { - System.print(local) - } - } - - Foo.new().method // expect: local -} diff --git a/test/language/variable/local_outside_method.wren b/test/language/variable/local_outside_method.wren new file mode 100644 index 00000000..bfb3662b --- /dev/null +++ b/test/language/variable/local_outside_method.wren @@ -0,0 +1,22 @@ +{ + var foo = "variable" + + class Foo { + construct new() {} + + foo { "method" } + + method { + System.print(foo) + } + + static foo { "class method" } + + static classMethod { + System.print(foo) + } + } + + Foo.new().method // expect: method + Foo.classMethod // expect: class method +}