feat: stop closure upvalue search at method boundaries and treat capitalized names as globals
Modify `findUpvalue` in the compiler to return -1 when the enclosing function is a method, preventing closures from capturing outer local variables. Add a new `nonlocal` variable resolution path that treats capitalized identifiers as global variables rather than method receivers. Update existing test files to use capitalized global names and add new test cases for nonlocal assignment, duplicate nonlocal declarations, block scoping, and method-local variable shadowing.
This commit is contained in:
@@ -0,0 +1,18 @@
|
||||
var Nonlocal = "before"
|
||||
IO.print(Nonlocal) // expect: before
|
||||
Nonlocal = "after"
|
||||
IO.print(Nonlocal) // expect: after
|
||||
|
||||
class Foo {
|
||||
static method {
|
||||
Nonlocal = "method"
|
||||
}
|
||||
}
|
||||
|
||||
Foo.method
|
||||
IO.print(Nonlocal) // expect: method
|
||||
|
||||
new Fn {
|
||||
Nonlocal = "fn"
|
||||
}.call
|
||||
IO.print(Nonlocal) // expect: fn
|
||||
@@ -0,0 +1,6 @@
|
||||
class Foo {
|
||||
bar {
|
||||
var A = "value"
|
||||
var A = "other" // expect error
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
var Nonlocal = "outer"
|
||||
|
||||
{
|
||||
var Nonlocal = "inner"
|
||||
IO.print(Nonlocal) // expect: inner
|
||||
}
|
||||
|
||||
IO.print(Nonlocal) // expect: outer
|
||||
@@ -0,0 +1,6 @@
|
||||
var Global = "global"
|
||||
// TODO: Forward reference to global declared after use.
|
||||
|
||||
new Fn {
|
||||
IO.print(Global) // expect: global
|
||||
}.call
|
||||
@@ -0,0 +1,15 @@
|
||||
var Global = "global"
|
||||
// TODO: Forward reference to global declared after use.
|
||||
|
||||
class Foo {
|
||||
method {
|
||||
IO.print(Global)
|
||||
}
|
||||
|
||||
static classMethod {
|
||||
IO.print(Global)
|
||||
}
|
||||
}
|
||||
|
||||
(new Foo).method // expect: global
|
||||
Foo.classMethod // expect: global
|
||||
Reference in New Issue
Block a user