JS: fix object expression constructor delegation to secondary constructors with default arguments (KT-30517 fixed)
This commit is contained in:
committed by
Anton Bannykh
parent
419d414681
commit
9a971172c9
@@ -0,0 +1,27 @@
|
||||
// IGNORE_BACKEND_FIR: JVM_IR
|
||||
|
||||
open class Foo(val args: String){
|
||||
constructor(arg: Any = 1) : this(arg.toString()) {
|
||||
}
|
||||
}
|
||||
open class Base(val baseArgs: String)
|
||||
open class Bar(arg: Any = 1) : Base(arg.toString()) {
|
||||
val args = arg.toString()
|
||||
}
|
||||
object TF : Foo() {}
|
||||
object TF2 : Foo(2) {}
|
||||
|
||||
fun box(): String {
|
||||
val f = object : Foo() {}
|
||||
val f2 = object : Foo(2) {}
|
||||
val b = object : Bar() {}
|
||||
|
||||
f.args.let { if (it != "1") return "Fail 1: $it" }
|
||||
f2.args.let { if (it != "2") return "Fail 2: $it" }
|
||||
TF.args.let { if (it != "1") return "Fail 3: $it" }
|
||||
TF2.args.let { if (it != "2") return "Fail 4: $it" }
|
||||
b.args.let { if (it != "1") return "Fail 5: $it" }
|
||||
b.baseArgs.let { if (it != "1") return "Fail 6: $it" }
|
||||
|
||||
return "OK"
|
||||
}
|
||||
+22
@@ -0,0 +1,22 @@
|
||||
// IGNORE_BACKEND_FIR: JVM_IR
|
||||
|
||||
abstract class O(val value: String) {
|
||||
constructor(o: Char = 'O') : this("$o")
|
||||
}
|
||||
|
||||
abstract class K {
|
||||
val value: String
|
||||
|
||||
constructor(k: Char = 'K') {
|
||||
value = "$k"
|
||||
}
|
||||
}
|
||||
|
||||
fun box(): String {
|
||||
|
||||
val o = object : O() {}
|
||||
|
||||
val k = object : K() {}
|
||||
|
||||
return o.value + k.value
|
||||
}
|
||||
+10
@@ -9234,6 +9234,11 @@ public class BlackBoxCodegenTestGenerated extends AbstractBlackBoxCodegenTest {
|
||||
runTest("compiler/testData/codegen/box/defaultArguments/constructor/kt2852.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("kt30517.kt")
|
||||
public void testKt30517() throws Exception {
|
||||
runTest("compiler/testData/codegen/box/defaultArguments/constructor/kt30517.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("kt3060.kt")
|
||||
public void testKt3060() throws Exception {
|
||||
runTest("compiler/testData/codegen/box/defaultArguments/constructor/kt3060.kt");
|
||||
@@ -9243,6 +9248,11 @@ public class BlackBoxCodegenTestGenerated extends AbstractBlackBoxCodegenTest {
|
||||
public void testManyArgs() throws Exception {
|
||||
runTest("compiler/testData/codegen/box/defaultArguments/constructor/manyArgs.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("objectExpressionDelegatingToSecondaryConstructor.kt")
|
||||
public void testObjectExpressionDelegatingToSecondaryConstructor() throws Exception {
|
||||
runTest("compiler/testData/codegen/box/defaultArguments/constructor/objectExpressionDelegatingToSecondaryConstructor.kt");
|
||||
}
|
||||
}
|
||||
|
||||
@TestMetadata("compiler/testData/codegen/box/defaultArguments/convention")
|
||||
|
||||
+10
@@ -9234,6 +9234,11 @@ public class LightAnalysisModeTestGenerated extends AbstractLightAnalysisModeTes
|
||||
runTest("compiler/testData/codegen/box/defaultArguments/constructor/kt2852.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("kt30517.kt")
|
||||
public void testKt30517() throws Exception {
|
||||
runTest("compiler/testData/codegen/box/defaultArguments/constructor/kt30517.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("kt3060.kt")
|
||||
public void testKt3060() throws Exception {
|
||||
runTest("compiler/testData/codegen/box/defaultArguments/constructor/kt3060.kt");
|
||||
@@ -9243,6 +9248,11 @@ public class LightAnalysisModeTestGenerated extends AbstractLightAnalysisModeTes
|
||||
public void testManyArgs() throws Exception {
|
||||
runTest("compiler/testData/codegen/box/defaultArguments/constructor/manyArgs.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("objectExpressionDelegatingToSecondaryConstructor.kt")
|
||||
public void testObjectExpressionDelegatingToSecondaryConstructor() throws Exception {
|
||||
runTest("compiler/testData/codegen/box/defaultArguments/constructor/objectExpressionDelegatingToSecondaryConstructor.kt");
|
||||
}
|
||||
}
|
||||
|
||||
@TestMetadata("compiler/testData/codegen/box/defaultArguments/convention")
|
||||
|
||||
+10
@@ -8084,6 +8084,11 @@ public class FirBlackBoxCodegenTestGenerated extends AbstractFirBlackBoxCodegenT
|
||||
runTest("compiler/testData/codegen/box/defaultArguments/constructor/kt2852.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("kt30517.kt")
|
||||
public void testKt30517() throws Exception {
|
||||
runTest("compiler/testData/codegen/box/defaultArguments/constructor/kt30517.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("kt3060.kt")
|
||||
public void testKt3060() throws Exception {
|
||||
runTest("compiler/testData/codegen/box/defaultArguments/constructor/kt3060.kt");
|
||||
@@ -8093,6 +8098,11 @@ public class FirBlackBoxCodegenTestGenerated extends AbstractFirBlackBoxCodegenT
|
||||
public void testManyArgs() throws Exception {
|
||||
runTest("compiler/testData/codegen/box/defaultArguments/constructor/manyArgs.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("objectExpressionDelegatingToSecondaryConstructor.kt")
|
||||
public void testObjectExpressionDelegatingToSecondaryConstructor() throws Exception {
|
||||
runTest("compiler/testData/codegen/box/defaultArguments/constructor/objectExpressionDelegatingToSecondaryConstructor.kt");
|
||||
}
|
||||
}
|
||||
|
||||
@TestMetadata("compiler/testData/codegen/box/defaultArguments/convention")
|
||||
|
||||
+10
@@ -8084,6 +8084,11 @@ public class IrBlackBoxCodegenTestGenerated extends AbstractIrBlackBoxCodegenTes
|
||||
runTest("compiler/testData/codegen/box/defaultArguments/constructor/kt2852.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("kt30517.kt")
|
||||
public void testKt30517() throws Exception {
|
||||
runTest("compiler/testData/codegen/box/defaultArguments/constructor/kt30517.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("kt3060.kt")
|
||||
public void testKt3060() throws Exception {
|
||||
runTest("compiler/testData/codegen/box/defaultArguments/constructor/kt3060.kt");
|
||||
@@ -8093,6 +8098,11 @@ public class IrBlackBoxCodegenTestGenerated extends AbstractIrBlackBoxCodegenTes
|
||||
public void testManyArgs() throws Exception {
|
||||
runTest("compiler/testData/codegen/box/defaultArguments/constructor/manyArgs.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("objectExpressionDelegatingToSecondaryConstructor.kt")
|
||||
public void testObjectExpressionDelegatingToSecondaryConstructor() throws Exception {
|
||||
runTest("compiler/testData/codegen/box/defaultArguments/constructor/objectExpressionDelegatingToSecondaryConstructor.kt");
|
||||
}
|
||||
}
|
||||
|
||||
@TestMetadata("compiler/testData/codegen/box/defaultArguments/convention")
|
||||
|
||||
Generated
+10
@@ -6954,10 +6954,20 @@ public class IrJsCodegenBoxTestGenerated extends AbstractIrJsCodegenBoxTest {
|
||||
runTest("compiler/testData/codegen/box/defaultArguments/constructor/kt2852.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("kt30517.kt")
|
||||
public void testKt30517() throws Exception {
|
||||
runTest("compiler/testData/codegen/box/defaultArguments/constructor/kt30517.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("kt3060.kt")
|
||||
public void testKt3060() throws Exception {
|
||||
runTest("compiler/testData/codegen/box/defaultArguments/constructor/kt3060.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("objectExpressionDelegatingToSecondaryConstructor.kt")
|
||||
public void testObjectExpressionDelegatingToSecondaryConstructor() throws Exception {
|
||||
runTest("compiler/testData/codegen/box/defaultArguments/constructor/objectExpressionDelegatingToSecondaryConstructor.kt");
|
||||
}
|
||||
}
|
||||
|
||||
@TestMetadata("compiler/testData/codegen/box/defaultArguments/convention")
|
||||
|
||||
+10
@@ -8029,10 +8029,20 @@ public class JsCodegenBoxTestGenerated extends AbstractJsCodegenBoxTest {
|
||||
runTest("compiler/testData/codegen/box/defaultArguments/constructor/kt2852.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("kt30517.kt")
|
||||
public void testKt30517() throws Exception {
|
||||
runTest("compiler/testData/codegen/box/defaultArguments/constructor/kt30517.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("kt3060.kt")
|
||||
public void testKt3060() throws Exception {
|
||||
runTest("compiler/testData/codegen/box/defaultArguments/constructor/kt3060.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("objectExpressionDelegatingToSecondaryConstructor.kt")
|
||||
public void testObjectExpressionDelegatingToSecondaryConstructor() throws Exception {
|
||||
runTest("compiler/testData/codegen/box/defaultArguments/constructor/objectExpressionDelegatingToSecondaryConstructor.kt");
|
||||
}
|
||||
}
|
||||
|
||||
@TestMetadata("compiler/testData/codegen/box/defaultArguments/convention")
|
||||
|
||||
+13
-9
@@ -242,16 +242,20 @@ public final class ClassInitializerTranslator extends AbstractTranslator {
|
||||
addCallToSuperMethod(arguments, initializer, superCall.getCall().getCallElement());
|
||||
}
|
||||
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);
|
||||
// Add `void 0` for the trailing default arguments
|
||||
// Anonymous object constructor already has all the parameters proxied, including ones with default values
|
||||
if (!DescriptorUtils.isAnonymousObject(classDescriptor)) {
|
||||
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());
|
||||
}
|
||||
}
|
||||
int padSize = superDescriptor.getValueParameters().size() - maxValueArgumentIndex;
|
||||
while (padSize-- > 0) {
|
||||
arguments.add(Namer.getUndefinedExpression());
|
||||
}
|
||||
addCallToSuperSecondaryConstructor(arguments, superDescriptor);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user