Recapture shared variables when calling local class constructor
When a local function or class A creates an instance of a local class B capturing an outer variable 'x', it should use ref for 'x', but not the value of 'x'.
This commit is contained in:
+12
@@ -0,0 +1,12 @@
|
||||
fun box(): String {
|
||||
var x = ""
|
||||
|
||||
class CapturesX {
|
||||
override fun toString() = x
|
||||
}
|
||||
|
||||
fun localFun() = CapturesX()
|
||||
|
||||
x = "OK"
|
||||
return localFun().toString()
|
||||
}
|
||||
+15
@@ -0,0 +1,15 @@
|
||||
fun box(): String {
|
||||
var x = ""
|
||||
|
||||
class CapturesX {
|
||||
override fun toString() = x
|
||||
}
|
||||
|
||||
fun outerFun1(): CapturesX {
|
||||
fun localFun() = CapturesX()
|
||||
return localFun()
|
||||
}
|
||||
|
||||
x = "OK"
|
||||
return outerFun1().toString()
|
||||
}
|
||||
+14
@@ -0,0 +1,14 @@
|
||||
fun box(): String {
|
||||
var x = ""
|
||||
|
||||
class CapturesX {
|
||||
override fun toString() = x
|
||||
}
|
||||
|
||||
class LocalClass {
|
||||
fun foo() = CapturesX()
|
||||
}
|
||||
|
||||
x = "OK"
|
||||
return LocalClass().foo().toString()
|
||||
}
|
||||
Reference in New Issue
Block a user