KT-11960 Fix case of instantiation of local class via its inner class or via nested lambda. Move tests to more appropriate location. Fix bug in blackbox codegen generator for JVM, which does not allow to suppress tests.

This commit is contained in:
Alexey Andreev
2016-05-23 12:17:56 +03:00
parent bb8a3aa262
commit acc5303731
9 changed files with 56 additions and 45 deletions
@@ -4,17 +4,20 @@
fun box(): String {
val capturedInConstructor = 1
val capturedInBody = 10
var log = ""
class A(var x: Int) {
var y = 0
fun copy(): A {
log += "A.copy;"
val result = A(x)
result.y += capturedInBody
return result
}
init {
log += "A.<init>;"
y += x + capturedInConstructor
}
}
@@ -23,35 +26,36 @@ fun box(): String {
if (a.y != 111) return "fail1a: ${a.y}"
if (a.x != 100) return "fail1b: ${a.x}"
// This does not work in JS backend due to some unrelated issue with lambdas
/*
class B(var x: Int) {
var y = 0
fun copier(): () -> B = {
val result = B(x)
result.y += capturedInBody
result
fun copier(): () -> B {
log += "B.copier;"
return {
log += "B.copy;"
val result = B(x)
result.y += capturedInBody
result
}
}
init {
y += x + capturedInConstructor
log += "B.<init>;"
}
}
val b = B(100).copier()()
if (b.y != 111) return "fail2a: ${b.y}"
if (b.x != 100) return "fail2b: ${b.x}"
*/
// It's pretty hard to implement this properly for now. Presumably, one needs to inject closure fields into local classes
// after the entire code gets generated (which is possible, but not easy, in JS and, I believe, nearly impossible in JVM).
/*
class C(var x: Int) {
var y = 0
inner class D() {
fun copyOuter(): C {
log += "D.copyOuter;"
val result = C(x)
result.y += capturedInBody
return result
@@ -59,6 +63,7 @@ fun box(): String {
}
init {
log += "C.<init>;"
y += x + capturedInConstructor
}
}
@@ -66,7 +71,8 @@ fun box(): String {
val c = C(100).D().copyOuter()
if (c.y != 111) return "fail3a: ${c.y}"
if (c.x != 100) return "fail3b: ${c.x}"
*/
if (log != "A.<init>;A.copy;A.<init>;B.<init>;B.copier;B.copy;B.<init>;C.<init>;D.copyOuter;C.<init>;") return "fail_log: $log"
return "OK"
}
@@ -1,6 +1,3 @@
// Enable for JVM backend when KT-8120 gets fixed
// TARGET_BACKEND: JS
fun box(): String {
val capturedInConstructor = 1