diff --git a/compiler/testData/codegen/box/classes/superConstructorCallWithComplexArg.kt b/compiler/testData/codegen/box/classes/superConstructorCallWithComplexArg.kt new file mode 100644 index 00000000000..1d2c6d4646a --- /dev/null +++ b/compiler/testData/codegen/box/classes/superConstructorCallWithComplexArg.kt @@ -0,0 +1,19 @@ +var log = "" + +open class Base(val s: String) + +class A(i: Int) : Base("O" + if (i == 23) { + log += "logged" + "K" +} +else { + "fail" +}) + +fun box(): String { + val result = A(23).s + if (result != "OK") return "fail: $result" + if (log != "logged") return "fail log: $log" + + return "OK" +} \ No newline at end of file diff --git a/compiler/testData/codegen/box/secondaryConstructors/delegateWithComplexExpression.kt b/compiler/testData/codegen/box/secondaryConstructors/delegateWithComplexExpression.kt new file mode 100644 index 00000000000..999d944d7f1 --- /dev/null +++ b/compiler/testData/codegen/box/secondaryConstructors/delegateWithComplexExpression.kt @@ -0,0 +1,46 @@ +var log = "" + +open class Base(val s: String) + +class A(s: String) : Base(s) { + constructor(i: Int) : this("O" + if (i == 23) { + log += "logged1;" + "K" + } + else { + "fail" + }) + + constructor(i: Long) : this(if (i == 23L) { + log += "logged2;" + 23 + } + else { + 42 + }) +} + +class B : Base { + constructor(i: Int) : super("O" + if (i == 23) { + log += "logged3;" + "K" + } + else { + "fail" + }) +} + +fun box(): String { + var result = A(23).s + if (result != "OK") return "fail1: $result" + + result = A(23L).s + if (result != "OK") return "fail2: $result" + + result = B(23).s + if (result != "OK") return "fail3: $result" + + if (log != "logged1;logged2;logged1;logged3;") return "fail log: $log" + + return "OK" +} \ No newline at end of file diff --git a/compiler/testData/codegen/light-analysis/classes/superConstructorCallWithComplexArg.txt b/compiler/testData/codegen/light-analysis/classes/superConstructorCallWithComplexArg.txt new file mode 100644 index 00000000000..89c55c61ed4 --- /dev/null +++ b/compiler/testData/codegen/light-analysis/classes/superConstructorCallWithComplexArg.txt @@ -0,0 +1,18 @@ +public final class A { + public method (p0: int): void +} + + +public class Base { + private final @org.jetbrains.annotations.NotNull field s: java.lang.String + public method (@org.jetbrains.annotations.NotNull p0: java.lang.String): void + public final @org.jetbrains.annotations.NotNull method getS(): java.lang.String +} + + +public final class SuperConstructorCallWithComplexArgKt { + private static @org.jetbrains.annotations.NotNull field log: java.lang.String + public final static @org.jetbrains.annotations.NotNull method box(): java.lang.String + public final static @org.jetbrains.annotations.NotNull method getLog(): java.lang.String + public final static method setLog(@org.jetbrains.annotations.NotNull p0: java.lang.String): void +} diff --git a/compiler/testData/codegen/light-analysis/secondaryConstructors/delegateWithComplexExpression.txt b/compiler/testData/codegen/light-analysis/secondaryConstructors/delegateWithComplexExpression.txt new file mode 100644 index 00000000000..dfb7fd935c6 --- /dev/null +++ b/compiler/testData/codegen/light-analysis/secondaryConstructors/delegateWithComplexExpression.txt @@ -0,0 +1,25 @@ +public final class A { + public method (@org.jetbrains.annotations.NotNull p0: java.lang.String): void + public method (p0: int): void + public method (p0: long): void +} + + +public final class B { + public method (p0: int): void +} + + +public class Base { + private final @org.jetbrains.annotations.NotNull field s: java.lang.String + public method (@org.jetbrains.annotations.NotNull p0: java.lang.String): void + public final @org.jetbrains.annotations.NotNull method getS(): java.lang.String +} + + +public final class DelegateWithComplexExpressionKt { + private static @org.jetbrains.annotations.NotNull field log: java.lang.String + public final static @org.jetbrains.annotations.NotNull method box(): java.lang.String + public final static @org.jetbrains.annotations.NotNull method getLog(): java.lang.String + public final static method setLog(@org.jetbrains.annotations.NotNull p0: java.lang.String): void +} diff --git a/compiler/tests-ir-jvm/tests/org/jetbrains/kotlin/codegen/ir/IrBlackBoxCodegenTestGenerated.java b/compiler/tests-ir-jvm/tests/org/jetbrains/kotlin/codegen/ir/IrBlackBoxCodegenTestGenerated.java index 81c0cd28eef..19ab23da518 100644 --- a/compiler/tests-ir-jvm/tests/org/jetbrains/kotlin/codegen/ir/IrBlackBoxCodegenTestGenerated.java +++ b/compiler/tests-ir-jvm/tests/org/jetbrains/kotlin/codegen/ir/IrBlackBoxCodegenTestGenerated.java @@ -3245,6 +3245,12 @@ public class IrBlackBoxCodegenTestGenerated extends AbstractIrBlackBoxCodegenTes doTest(fileName); } + @TestMetadata("superConstructorCallWithComplexArg.kt") + public void testSuperConstructorCallWithComplexArg() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/classes/superConstructorCallWithComplexArg.kt"); + doTest(fileName); + } + @TestMetadata("typedDelegation.kt") public void testTypedDelegation() throws Exception { String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/classes/typedDelegation.kt"); @@ -15464,6 +15470,12 @@ public class IrBlackBoxCodegenTestGenerated extends AbstractIrBlackBoxCodegenTes doTest(fileName); } + @TestMetadata("delegateWithComplexExpression.kt") + public void testDelegateWithComplexExpression() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/secondaryConstructors/delegateWithComplexExpression.kt"); + doTest(fileName); + } + @TestMetadata("delegatedThisWithLambda.kt") public void testDelegatedThisWithLambda() throws Exception { String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/secondaryConstructors/delegatedThisWithLambda.kt"); diff --git a/compiler/tests/org/jetbrains/kotlin/codegen/BlackBoxCodegenTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/codegen/BlackBoxCodegenTestGenerated.java index c12a51f6199..47b55ea75c2 100644 --- a/compiler/tests/org/jetbrains/kotlin/codegen/BlackBoxCodegenTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/codegen/BlackBoxCodegenTestGenerated.java @@ -3245,6 +3245,12 @@ public class BlackBoxCodegenTestGenerated extends AbstractBlackBoxCodegenTest { doTest(fileName); } + @TestMetadata("superConstructorCallWithComplexArg.kt") + public void testSuperConstructorCallWithComplexArg() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/classes/superConstructorCallWithComplexArg.kt"); + doTest(fileName); + } + @TestMetadata("typedDelegation.kt") public void testTypedDelegation() throws Exception { String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/classes/typedDelegation.kt"); @@ -15464,6 +15470,12 @@ public class BlackBoxCodegenTestGenerated extends AbstractBlackBoxCodegenTest { doTest(fileName); } + @TestMetadata("delegateWithComplexExpression.kt") + public void testDelegateWithComplexExpression() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/secondaryConstructors/delegateWithComplexExpression.kt"); + doTest(fileName); + } + @TestMetadata("delegatedThisWithLambda.kt") public void testDelegatedThisWithLambda() throws Exception { String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/secondaryConstructors/delegatedThisWithLambda.kt"); diff --git a/compiler/tests/org/jetbrains/kotlin/codegen/LightAnalysisModeCodegenTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/codegen/LightAnalysisModeCodegenTestGenerated.java index 296d1f2d7d0..9e3fdc79605 100644 --- a/compiler/tests/org/jetbrains/kotlin/codegen/LightAnalysisModeCodegenTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/codegen/LightAnalysisModeCodegenTestGenerated.java @@ -3245,6 +3245,12 @@ public class LightAnalysisModeCodegenTestGenerated extends AbstractLightAnalysis doTest(fileName); } + @TestMetadata("superConstructorCallWithComplexArg.kt") + public void testSuperConstructorCallWithComplexArg() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/classes/superConstructorCallWithComplexArg.kt"); + doTest(fileName); + } + @TestMetadata("typedDelegation.kt") public void testTypedDelegation() throws Exception { String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/classes/typedDelegation.kt"); @@ -15464,6 +15470,12 @@ public class LightAnalysisModeCodegenTestGenerated extends AbstractLightAnalysis doTest(fileName); } + @TestMetadata("delegateWithComplexExpression.kt") + public void testDelegateWithComplexExpression() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/secondaryConstructors/delegateWithComplexExpression.kt"); + doTest(fileName); + } + @TestMetadata("delegatedThisWithLambda.kt") public void testDelegatedThisWithLambda() throws Exception { String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/secondaryConstructors/delegatedThisWithLambda.kt"); diff --git a/js/js.tests/test/org/jetbrains/kotlin/js/test/semantics/JsCodegenBoxTestGenerated.java b/js/js.tests/test/org/jetbrains/kotlin/js/test/semantics/JsCodegenBoxTestGenerated.java index 98d48f87f9d..eda49950260 100644 --- a/js/js.tests/test/org/jetbrains/kotlin/js/test/semantics/JsCodegenBoxTestGenerated.java +++ b/js/js.tests/test/org/jetbrains/kotlin/js/test/semantics/JsCodegenBoxTestGenerated.java @@ -3996,6 +3996,12 @@ public class JsCodegenBoxTestGenerated extends AbstractJsCodegenBoxTest { doTest(fileName); } + @TestMetadata("superConstructorCallWithComplexArg.kt") + public void testSuperConstructorCallWithComplexArg() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/classes/superConstructorCallWithComplexArg.kt"); + doTest(fileName); + } + @TestMetadata("typedDelegation.kt") public void testTypedDelegation() throws Exception { String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/classes/typedDelegation.kt"); @@ -19750,6 +19756,12 @@ public class JsCodegenBoxTestGenerated extends AbstractJsCodegenBoxTest { doTest(fileName); } + @TestMetadata("delegateWithComplexExpression.kt") + public void testDelegateWithComplexExpression() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/secondaryConstructors/delegateWithComplexExpression.kt"); + doTest(fileName); + } + @TestMetadata("delegatedThisWithLambda.kt") public void testDelegatedThisWithLambda() throws Exception { String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/secondaryConstructors/delegatedThisWithLambda.kt"); diff --git a/js/js.translator/src/org/jetbrains/kotlin/js/translate/declaration/ClassTranslator.kt b/js/js.translator/src/org/jetbrains/kotlin/js/translate/declaration/ClassTranslator.kt index e11fe0bbb24..31b35073033 100644 --- a/js/js.translator/src/org/jetbrains/kotlin/js/translate/declaration/ClassTranslator.kt +++ b/js/js.translator/src/org/jetbrains/kotlin/js/translate/declaration/ClassTranslator.kt @@ -214,12 +214,13 @@ class ClassTranslator private constructor( // Translate constructor body val constructorInitializer = context.getFunctionObject(constructorDescriptor) constructorInitializer.name = context.getInnerNameForDescriptor(constructorDescriptor) - context.addTopLevelStatement(constructorInitializer.makeStmt()) + context.addDeclarationStatement(constructorInitializer.makeStmt()) FunctionTranslator.newInstance(constructor, context, constructorInitializer).translateAsMethodWithoutMetadata() // Translate super/this call val superCallGenerators = mutableListOf<(MutableList) -> Unit>() val referenceToClass = context.getInnerReference(classDescriptor) + context = context.contextWithScope(constructorInitializer) superCallGenerators += { it += FunctionBodyTranslator.setDefaultValueForArguments(constructorDescriptor, context) } @@ -250,8 +251,11 @@ class ClassTranslator private constructor( if (resolvedCall != null && !KotlinBuiltIns.isAny(delegationClassDescriptor!!)) { superCallGenerators += { val delegationConstructor = resolvedCall.resultingDescriptor - it += CallTranslator.translate(context, resolvedCall) + val innerContext = context.innerBlock() + val statement = CallTranslator.translate(innerContext, resolvedCall) .toInvocationWith(leadingArgs, delegationConstructor.valueParameters.size, thisNameRef).makeStmt() + it += innerContext.currentBlock.statements + it += statement } }