diff --git a/compiler/testData/codegen/box/dataClasses/copy/withSecondaryConstructor.kt b/compiler/testData/codegen/box/dataClasses/copy/withSecondaryConstructor.kt new file mode 100644 index 00000000000..f997cea7708 --- /dev/null +++ b/compiler/testData/codegen/box/dataClasses/copy/withSecondaryConstructor.kt @@ -0,0 +1,9 @@ +data class A(val o: String, val k: String) { + constructor() : this("O", "k") +} + +fun box(): String { + val a = A().copy(k = "K") + return a.o + a.k +} + diff --git a/compiler/testData/codegen/light-analysis/dataClasses/copy/withSecondaryConstructor.txt b/compiler/testData/codegen/light-analysis/dataClasses/copy/withSecondaryConstructor.txt new file mode 100644 index 00000000000..15feed8cf71 --- /dev/null +++ b/compiler/testData/codegen/light-analysis/dataClasses/copy/withSecondaryConstructor.txt @@ -0,0 +1,21 @@ +@kotlin.Metadata +public final class A { + private final @org.jetbrains.annotations.NotNull field k: java.lang.String + private final @org.jetbrains.annotations.NotNull field o: java.lang.String + public method (): void + public method (@org.jetbrains.annotations.NotNull p0: java.lang.String, @org.jetbrains.annotations.NotNull p1: java.lang.String): void + public final @org.jetbrains.annotations.NotNull method component1(): java.lang.String + public final @org.jetbrains.annotations.NotNull method component2(): java.lang.String + public synthetic static method copy$default(p0: A, p1: java.lang.String, p2: java.lang.String, p3: int, p4: java.lang.Object): A + public final @org.jetbrains.annotations.NotNull method copy(@org.jetbrains.annotations.NotNull p0: java.lang.String, @org.jetbrains.annotations.NotNull p1: java.lang.String): A + public method equals(p0: java.lang.Object): boolean + public final @org.jetbrains.annotations.NotNull method getK(): java.lang.String + public final @org.jetbrains.annotations.NotNull method getO(): java.lang.String + public method hashCode(): int + public method toString(): java.lang.String +} + +@kotlin.Metadata +public final class WithSecondaryConstructorKt { + public final static @org.jetbrains.annotations.NotNull method box(): java.lang.String +} 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 5f8d8ce2c30..f39c23c5a5d 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 @@ -5787,6 +5787,12 @@ public class IrBlackBoxCodegenTestGenerated extends AbstractIrBlackBoxCodegenTes String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/dataClasses/copy/withGenericParameter.kt"); doTest(fileName); } + + @TestMetadata("withSecondaryConstructor.kt") + public void testWithSecondaryConstructor() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/dataClasses/copy/withSecondaryConstructor.kt"); + doTest(fileName); + } } @TestMetadata("compiler/testData/codegen/box/dataClasses/equals") diff --git a/compiler/tests/org/jetbrains/kotlin/codegen/BlackBoxCodegenTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/codegen/BlackBoxCodegenTestGenerated.java index 42006c1342e..c598ec8d622 100644 --- a/compiler/tests/org/jetbrains/kotlin/codegen/BlackBoxCodegenTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/codegen/BlackBoxCodegenTestGenerated.java @@ -5787,6 +5787,12 @@ public class BlackBoxCodegenTestGenerated extends AbstractBlackBoxCodegenTest { String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/dataClasses/copy/withGenericParameter.kt"); doTest(fileName); } + + @TestMetadata("withSecondaryConstructor.kt") + public void testWithSecondaryConstructor() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/dataClasses/copy/withSecondaryConstructor.kt"); + doTest(fileName); + } } @TestMetadata("compiler/testData/codegen/box/dataClasses/equals") 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 17e3873aad4..ca7574e46fe 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 @@ -6520,6 +6520,12 @@ public class JsCodegenBoxTestGenerated extends AbstractJsCodegenBoxTest { String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/dataClasses/copy/withGenericParameter.kt"); doTest(fileName); } + + @TestMetadata("withSecondaryConstructor.kt") + public void testWithSecondaryConstructor() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/dataClasses/copy/withSecondaryConstructor.kt"); + doTest(fileName); + } } @TestMetadata("compiler/testData/codegen/box/dataClasses/equals") diff --git a/js/js.translator/src/org/jetbrains/kotlin/js/translate/declaration/JsDataClassGenerator.java b/js/js.translator/src/org/jetbrains/kotlin/js/translate/declaration/JsDataClassGenerator.java index 61af75ce887..4821bd524c9 100644 --- a/js/js.translator/src/org/jetbrains/kotlin/js/translate/declaration/JsDataClassGenerator.java +++ b/js/js.translator/src/org/jetbrains/kotlin/js/translate/declaration/JsDataClassGenerator.java @@ -91,7 +91,8 @@ class JsDataClassGenerator extends DataClassMethodGenerator { } ClassDescriptor classDescriptor = (ClassDescriptor) function.getContainingDeclaration(); - ClassConstructorDescriptor constructor = classDescriptor.getConstructors().iterator().next(); + ClassConstructorDescriptor constructor = classDescriptor.getUnsubstitutedPrimaryConstructor(); + assert constructor != null : "Data class should have primary constructor: " + classDescriptor; JsExpression constructorRef = context.getInnerReference(constructor);