KT-12707: support case when secondary super constructor has optional parameters
This commit is contained in:
+11
@@ -0,0 +1,11 @@
|
||||
open class A(val result: String) {
|
||||
constructor(x: Int = 11, y: Int = 22, z: Int = 33) : this("$x$y$z")
|
||||
}
|
||||
|
||||
class B() : A(y = 44)
|
||||
|
||||
fun box(): String {
|
||||
val result = B().result
|
||||
if (result != "114433") return "fail: $result"
|
||||
return "OK"
|
||||
}
|
||||
+11
@@ -0,0 +1,11 @@
|
||||
open class A(val result: String) {
|
||||
constructor(x: Int, y: Int = 99) : this("$x$y")
|
||||
}
|
||||
|
||||
class B(x: Int) : A(x)
|
||||
|
||||
fun box(): String {
|
||||
val result = B(11).result
|
||||
if (result != "1199") return "fail: $result"
|
||||
return "OK"
|
||||
}
|
||||
@@ -14482,6 +14482,18 @@ public class BlackBoxCodegenTestGenerated extends AbstractBlackBoxCodegenTest {
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("callFromPrimaryWithNamedArgs.kt")
|
||||
public void testCallFromPrimaryWithNamedArgs() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/secondaryConstructors/callFromPrimaryWithNamedArgs.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("callFromPrimaryWithOptionalArgs.kt")
|
||||
public void testCallFromPrimaryWithOptionalArgs() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/secondaryConstructors/callFromPrimaryWithOptionalArgs.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("callFromSubClass.kt")
|
||||
public void testCallFromSubClass() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/secondaryConstructors/callFromSubClass.kt");
|
||||
|
||||
+12
@@ -101,6 +101,18 @@ public class SecondaryConstructorTestGenerated extends AbstractSecondaryConstruc
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("callFromPrimaryWithNamedArgs.kt")
|
||||
public void testCallFromPrimaryWithNamedArgs() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/secondaryConstructors/callFromPrimaryWithNamedArgs.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("callFromPrimaryWithOptionalArgs.kt")
|
||||
public void testCallFromPrimaryWithOptionalArgs() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/secondaryConstructors/callFromPrimaryWithOptionalArgs.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("callFromSubClass.kt")
|
||||
public void testCallFromSubClass() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/secondaryConstructors/callFromSubClass.kt");
|
||||
|
||||
+13
@@ -36,7 +36,9 @@ import org.jetbrains.kotlin.psi.KtClassOrObject;
|
||||
import org.jetbrains.kotlin.psi.KtEnumEntry;
|
||||
import org.jetbrains.kotlin.psi.KtParameter;
|
||||
import org.jetbrains.kotlin.resolve.DescriptorUtils;
|
||||
import org.jetbrains.kotlin.resolve.calls.model.DefaultValueArgument;
|
||||
import org.jetbrains.kotlin.resolve.calls.model.ResolvedCall;
|
||||
import org.jetbrains.kotlin.resolve.calls.model.ResolvedValueArgument;
|
||||
import org.jetbrains.kotlin.types.KotlinType;
|
||||
|
||||
import java.util.ArrayList;
|
||||
@@ -206,6 +208,17 @@ public final class ClassInitializerTranslator extends AbstractTranslator {
|
||||
addCallToSuperMethod(arguments, initializer);
|
||||
}
|
||||
else {
|
||||
int maxValueArgumentIndex = 0;
|
||||
for (ValueParameterDescriptor arg : superCall.getValueArguments().keySet()) {
|
||||
ResolvedValueArgument resolvedArg = superCall.getValueArguments().get(arg);
|
||||
if (!(resolvedArg instanceof DefaultValueArgument)) {
|
||||
maxValueArgumentIndex = Math.max(maxValueArgumentIndex, arg.getIndex() + 1);
|
||||
}
|
||||
}
|
||||
int padSize = superDescriptor.getValueParameters().size() - maxValueArgumentIndex;
|
||||
while (padSize-- > 0) {
|
||||
arguments.add(Namer.getUndefinedExpression());
|
||||
}
|
||||
addCallToSuperSecondaryConstructor(arguments, superDescriptor);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user