KT-11611 Prevent duplicate generation of default values of secondary constructor parameters

This commit is contained in:
Alexey Andreev
2016-03-26 17:43:35 +03:00
parent 7afca74b94
commit a5e0c70988
4 changed files with 33 additions and 1 deletions
@@ -0,0 +1,15 @@
var global = 0
fun sideEffect() = global++
class A(val x: String) {
constructor(y: Int = sideEffect(), z: (Int) -> Int = { it + sideEffect() }) : this("$y:${z(y)}") {}
}
fun box(): String {
var a = A()
if (a.x != "0:1") return "failed1: ${a.x}"
if (global != 2) return "failed2: ${global}"
return "OK"
}
@@ -12862,6 +12862,12 @@ public class BlackBoxCodegenTestGenerated extends AbstractBlackBoxCodegenTest {
doTest(fileName);
}
@TestMetadata("defaultParametersNotDuplicated.kt")
public void testDefaultParametersNotDuplicated() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/secondaryConstructors/defaultParametersNotDuplicated.kt");
doTest(fileName);
}
@TestMetadata("delegationWithPrimary.kt")
public void testDelegationWithPrimary() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/secondaryConstructors/delegationWithPrimary.kt");
@@ -113,6 +113,12 @@ public class SecondaryConstructorTestGenerated extends AbstractSecondaryConstruc
doTest(fileName);
}
@TestMetadata("defaultParametersNotDuplicated.kt")
public void testDefaultParametersNotDuplicated() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/secondaryConstructors/defaultParametersNotDuplicated.kt");
doTest(fileName);
}
@TestMetadata("delegationWithPrimary.kt")
public void testDelegationWithPrimary() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/secondaryConstructors/delegationWithPrimary.kt");
@@ -20,6 +20,7 @@ import com.google.dart.compiler.backend.js.ast.*;
import com.google.dart.compiler.backend.js.ast.metadata.MetadataProperties;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.kotlin.builtins.KotlinBuiltIns;
import org.jetbrains.kotlin.descriptors.ConstructorDescriptor;
import org.jetbrains.kotlin.descriptors.FunctionDescriptor;
import org.jetbrains.kotlin.descriptors.ValueParameterDescriptor;
import org.jetbrains.kotlin.js.translate.context.Namer;
@@ -88,7 +89,11 @@ public final class FunctionBodyTranslator extends AbstractTranslator {
private JsBlock translate() {
KtExpression jetBodyExpression = declaration.getBodyExpression();
assert jetBodyExpression != null : "Cannot translate a body of an abstract function.";
JsBlock jsBlock = new JsBlock(setDefaultValueForArguments(descriptor, context()));
JsBlock jsBlock = new JsBlock();
if (!(descriptor instanceof ConstructorDescriptor) || ((ConstructorDescriptor) descriptor).isPrimary()) {
jsBlock.getStatements().addAll(setDefaultValueForArguments(descriptor, context()));
}
jsBlock.getStatements().addAll(mayBeWrapWithReturn(Translation.translateExpression(jetBodyExpression, context(), jsBlock)).getStatements());
return jsBlock;
}