From 17fc41e0f98665b2fdd2c71b6773489f0ff32f41 Mon Sep 17 00:00:00 2001 From: Alexander Udalov Date: Thu, 4 Jan 2018 19:06:56 +0100 Subject: [PATCH] Do not create primary constructor for enum entry synthetic class The change in DescriptorSerializer is needed so that serialized protos of enum entry classes which are resolved in sources (LazyClassDescriptor) and are deserialized from binaries (EnumEntrySyntheticClassDescriptor) are the same. There are tests on incremental compilation in JS that check that the serialized proto is exactly the same after rebuild and after an incremental build. #KT-22048 Fixed --- .../serialization/DescriptorSerializer.kt | 6 +++-- .../box/reflection/constructors/enumEntry.kt | 24 +++++++++++++++++++ .../ir/IrBlackBoxCodegenTestGenerated.java | 5 ++++ .../codegen/BlackBoxCodegenTestGenerated.java | 5 ++++ .../LightAnalysisModeTestGenerated.java | 5 ++++ .../EnumEntrySyntheticClassDescriptor.java | 10 ++------ .../IrJsCodegenBoxTestGenerated.java | 5 ++++ .../semantics/JsCodegenBoxTestGenerated.java | 5 ++++ 8 files changed, 55 insertions(+), 10 deletions(-) create mode 100644 compiler/testData/codegen/box/reflection/constructors/enumEntry.kt diff --git a/compiler/serialization/src/org/jetbrains/kotlin/serialization/DescriptorSerializer.kt b/compiler/serialization/src/org/jetbrains/kotlin/serialization/DescriptorSerializer.kt index 014e929fcd5..0bae3e96fca 100644 --- a/compiler/serialization/src/org/jetbrains/kotlin/serialization/DescriptorSerializer.kt +++ b/compiler/serialization/src/org/jetbrains/kotlin/serialization/DescriptorSerializer.kt @@ -89,8 +89,10 @@ class DescriptorSerializer private constructor( } } - for (descriptor in classDescriptor.constructors) { - builder.addConstructor(constructorProto(descriptor)) + if (classDescriptor.kind != ClassKind.ENUM_ENTRY) { + for (descriptor in classDescriptor.constructors) { + builder.addConstructor(constructorProto(descriptor)) + } } val callableMembers = diff --git a/compiler/testData/codegen/box/reflection/constructors/enumEntry.kt b/compiler/testData/codegen/box/reflection/constructors/enumEntry.kt new file mode 100644 index 00000000000..9240216692f --- /dev/null +++ b/compiler/testData/codegen/box/reflection/constructors/enumEntry.kt @@ -0,0 +1,24 @@ +// IGNORE_BACKEND: JS_IR, JS, NATIVE +// WITH_REFLECT + +import kotlin.test.assertEquals + +enum class TestEnum(val id: String? = null) { + ENUM1(id = "enum1_id"), + + ENUM2(id = "enum2_id") { + override fun test() { + ENUM1.test() + } + }; + + open fun test() { + } +} + +fun box(): String { + assertEquals(listOf("fun (kotlin.String?): TestEnum"), TestEnum.ENUM1::class.constructors.map { it.toString() }) + assertEquals(listOf(), TestEnum.ENUM2::class.constructors.map { it.toString() }) + + return "OK" +} 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 2cabe92cc4a..55849084d15 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 @@ -16890,6 +16890,11 @@ public class IrBlackBoxCodegenTestGenerated extends AbstractIrBlackBoxCodegenTes runTest("compiler/testData/codegen/box/reflection/constructors/constructorName.kt"); } + @TestMetadata("enumEntry.kt") + public void testEnumEntry() throws Exception { + runTest("compiler/testData/codegen/box/reflection/constructors/enumEntry.kt"); + } + @TestMetadata("primaryConstructor.kt") public void testPrimaryConstructor() throws Exception { runTest("compiler/testData/codegen/box/reflection/constructors/primaryConstructor.kt"); diff --git a/compiler/tests/org/jetbrains/kotlin/codegen/BlackBoxCodegenTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/codegen/BlackBoxCodegenTestGenerated.java index 3850d1738d2..aa0d57d033c 100644 --- a/compiler/tests/org/jetbrains/kotlin/codegen/BlackBoxCodegenTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/codegen/BlackBoxCodegenTestGenerated.java @@ -16890,6 +16890,11 @@ public class BlackBoxCodegenTestGenerated extends AbstractBlackBoxCodegenTest { runTest("compiler/testData/codegen/box/reflection/constructors/constructorName.kt"); } + @TestMetadata("enumEntry.kt") + public void testEnumEntry() throws Exception { + runTest("compiler/testData/codegen/box/reflection/constructors/enumEntry.kt"); + } + @TestMetadata("primaryConstructor.kt") public void testPrimaryConstructor() throws Exception { runTest("compiler/testData/codegen/box/reflection/constructors/primaryConstructor.kt"); diff --git a/compiler/tests/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java index ccd9732193a..28b877b5e80 100644 --- a/compiler/tests/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java @@ -16890,6 +16890,11 @@ public class LightAnalysisModeTestGenerated extends AbstractLightAnalysisModeTes runTest("compiler/testData/codegen/box/reflection/constructors/constructorName.kt"); } + @TestMetadata("enumEntry.kt") + public void testEnumEntry() throws Exception { + runTest("compiler/testData/codegen/box/reflection/constructors/enumEntry.kt"); + } + @TestMetadata("primaryConstructor.kt") public void testPrimaryConstructor() throws Exception { runTest("compiler/testData/codegen/box/reflection/constructors/primaryConstructor.kt"); diff --git a/core/descriptors/src/org/jetbrains/kotlin/descriptors/impl/EnumEntrySyntheticClassDescriptor.java b/core/descriptors/src/org/jetbrains/kotlin/descriptors/impl/EnumEntrySyntheticClassDescriptor.java index cd0df44073c..655a800ac00 100644 --- a/core/descriptors/src/org/jetbrains/kotlin/descriptors/impl/EnumEntrySyntheticClassDescriptor.java +++ b/core/descriptors/src/org/jetbrains/kotlin/descriptors/impl/EnumEntrySyntheticClassDescriptor.java @@ -14,7 +14,6 @@ import org.jetbrains.kotlin.descriptors.annotations.Annotations; import org.jetbrains.kotlin.incremental.components.LookupLocation; import org.jetbrains.kotlin.incremental.components.NoLookupLocation; import org.jetbrains.kotlin.name.Name; -import org.jetbrains.kotlin.resolve.DescriptorFactory; import org.jetbrains.kotlin.resolve.NonReportingOverrideStrategy; import org.jetbrains.kotlin.resolve.OverridingUtil; import org.jetbrains.kotlin.resolve.scopes.DescriptorKindFilter; @@ -32,7 +31,6 @@ import java.util.*; public class EnumEntrySyntheticClassDescriptor extends ClassDescriptorBase { private final TypeConstructor typeConstructor; - private final ClassConstructorDescriptor primaryConstructor; private final MemberScope scope; private final NotNullLazyValue> enumMemberNames; private final Annotations annotations; @@ -74,10 +72,6 @@ public class EnumEntrySyntheticClassDescriptor extends ClassDescriptorBase { this.scope = new EnumEntryScope(storageManager); this.enumMemberNames = enumMemberNames; - - ClassConstructorDescriptorImpl primaryConstructor = DescriptorFactory.createPrimaryConstructorForObject(this, source); - primaryConstructor.setReturnType(getDefaultType()); - this.primaryConstructor = primaryConstructor; } @NotNull @@ -95,7 +89,7 @@ public class EnumEntrySyntheticClassDescriptor extends ClassDescriptorBase { @NotNull @Override public Collection getConstructors() { - return Collections.singleton(primaryConstructor); + return Collections.emptyList(); } @NotNull @@ -161,7 +155,7 @@ public class EnumEntrySyntheticClassDescriptor extends ClassDescriptorBase { @Nullable @Override public ClassConstructorDescriptor getUnsubstitutedPrimaryConstructor() { - return primaryConstructor; + return null; } @NotNull diff --git a/js/js.tests/test/org/jetbrains/kotlin/js/test/semantics/IrJsCodegenBoxTestGenerated.java b/js/js.tests/test/org/jetbrains/kotlin/js/test/semantics/IrJsCodegenBoxTestGenerated.java index f5e6673bad5..d86e9dace9f 100644 --- a/js/js.tests/test/org/jetbrains/kotlin/js/test/semantics/IrJsCodegenBoxTestGenerated.java +++ b/js/js.tests/test/org/jetbrains/kotlin/js/test/semantics/IrJsCodegenBoxTestGenerated.java @@ -15205,6 +15205,11 @@ public class IrJsCodegenBoxTestGenerated extends AbstractIrJsCodegenBoxTest { runTest("compiler/testData/codegen/box/reflection/constructors/constructorName.kt"); } + @TestMetadata("enumEntry.kt") + public void testEnumEntry() throws Exception { + runTest("compiler/testData/codegen/box/reflection/constructors/enumEntry.kt"); + } + @TestMetadata("primaryConstructor.kt") public void testPrimaryConstructor() throws Exception { runTest("compiler/testData/codegen/box/reflection/constructors/primaryConstructor.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 7ee50a6bb70..a644c22cd69 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 @@ -15205,6 +15205,11 @@ public class JsCodegenBoxTestGenerated extends AbstractJsCodegenBoxTest { runTest("compiler/testData/codegen/box/reflection/constructors/constructorName.kt"); } + @TestMetadata("enumEntry.kt") + public void testEnumEntry() throws Exception { + runTest("compiler/testData/codegen/box/reflection/constructors/enumEntry.kt"); + } + @TestMetadata("primaryConstructor.kt") public void testPrimaryConstructor() throws Exception { runTest("compiler/testData/codegen/box/reflection/constructors/primaryConstructor.kt");