Fix initialization of object's INSTANCE$ field

Call object's constructor in <clinit> and ignore the result, but in the first
line of <init> save the current "this" to INSTANCE$

 #KT-4516 Fixed
 #KT-4543 Fixed
 #KT-5291 Fixed
 #KT-5523 Fixed
 #KT-5582 Fixed
This commit is contained in:
Alexander Udalov
2014-09-11 14:19:32 +04:00
parent b907f35995
commit 81004889eb
10 changed files with 175 additions and 34 deletions
@@ -0,0 +1,14 @@
trait T {
fun foo(): String
}
val o = object : T {
val a = "OK"
val f = {
a
}()
override fun foo() = f
}
fun box() = o.foo()
@@ -0,0 +1,18 @@
import java.util.HashMap
object O {
val mmmap = HashMap<String, Int>();
{
fun doStuff() {
mmmap.put("two", 2)
}
doStuff()
}
}
fun box(): String {
val r = O.mmmap["two"]
if (r != 2) return "Fail: $r"
return "OK"
}
@@ -0,0 +1,6 @@
object A {
val a = "OK"
val b = A.a
}
fun box() = A.b
@@ -0,0 +1,24 @@
public inline fun <T> T.with(f: T.() -> Unit): T {
this.f()
return this
}
public class Cls {
val string = "Cls"
val buffer = StringBuilder().with {
append(string)
}
}
public object Obj {
val string = "Obj"
val buffer = StringBuilder().with {
append(string)
}
}
fun box(): String {
if (Cls().buffer.toString() != "Cls") return "Fail class"
if (Obj.buffer.toString() != "Obj") return "Fail object"
return "OK"
}
@@ -0,0 +1,16 @@
class A(val result: String)
fun a(body: A.() -> String): String {
val r = A("OK")
return r.body()
}
object C {
private fun A.f() = result
val g = a {
f()
}
}
fun box() = C.g
@@ -0,0 +1,20 @@
trait T
object Foo {
private fun foo(p: T) = p
private val v: Int = {
val x = foo(O)
42
}()
private object O : T
val result = v
}
fun box(): String {
val foo = Foo
if (foo.result != 42) return "Fail: ${foo.result}"
return "OK"
}