KT-12873: add temporary variables generated by delegation expression to class initializer. Fix #KT-12873
This commit is contained in:
@@ -99,4 +99,8 @@ public class DelegationTest extends SingleFileTranslationTest {
|
||||
public void testOnObject() throws Exception {
|
||||
checkFooBoxIsOk();
|
||||
}
|
||||
|
||||
public void testComplexDelegation() throws Exception {
|
||||
checkFooBoxIsOk();
|
||||
}
|
||||
}
|
||||
|
||||
+4
-2
@@ -73,8 +73,10 @@ class DelegationTranslator(
|
||||
val field = fields[specifier]!!
|
||||
if (field.generateField) {
|
||||
val expression = specifier.delegateExpression!!
|
||||
val delegateInitExpr = Translation.translateAsExpression(expression, context())
|
||||
statements.add(JsAstUtils.defineSimpleProperty(field.name, delegateInitExpr))
|
||||
val context = context().innerBlock()
|
||||
val delegateInitExpr = Translation.translateAsExpression(expression, context)
|
||||
statements += context.dynamicContext().jsBlock().statements
|
||||
statements += JsAstUtils.defineSimpleProperty(field.name, delegateInitExpr)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,29 @@
|
||||
package foo
|
||||
|
||||
interface C {
|
||||
fun f(): String
|
||||
}
|
||||
|
||||
class B(val value: String) : C {
|
||||
override fun f() = value
|
||||
}
|
||||
|
||||
val b: Any = B("O")
|
||||
|
||||
val x = B("failure1")
|
||||
val y = B("K")
|
||||
val z = B("failure2")
|
||||
|
||||
fun selector() = 2
|
||||
|
||||
class A : C by (b as C)
|
||||
|
||||
class D : C by when (selector()) {
|
||||
1 -> x
|
||||
2 -> y
|
||||
else -> z
|
||||
}
|
||||
|
||||
fun box(): String {
|
||||
return A().f() + D().f()
|
||||
}
|
||||
Reference in New Issue
Block a user