Enum entries now can have no initializer if default secondary constructor is available. #KT-8484 Fixed

This commit is contained in:
Mikhail Glukhikh
2015-09-02 15:48:24 +03:00
parent 0fc8023f18
commit fa292ec0e0
9 changed files with 90 additions and 2 deletions
@@ -507,8 +507,7 @@ public class DeclarationsChecker {
if (DescriptorUtils.isEnumClass(declaration)) {
ClassDescriptor enumClass = (ClassDescriptor) declaration;
ConstructorDescriptor constructor = enumClass.getUnsubstitutedPrimaryConstructor();
if ((constructor == null || !constructor.getValueParameters().isEmpty()) && !enumEntry.hasInitializer()) {
if (!enumEntry.hasInitializer() && !DescriptorUtils.hasDefaultConstructor(enumClass)) {
trace.report(ENUM_ENTRY_SHOULD_BE_INITIALIZED.on(enumEntry));
}
}
@@ -0,0 +1,8 @@
package test
enum class My(val s: String) {
ENTRY;
constructor(): this("OK")
}
fun box() = My.ENTRY.s
@@ -0,0 +1,4 @@
enum class E {
A; // no constructor call needed
constructor()
}
@@ -0,0 +1,18 @@
package
internal final enum class E : kotlin.Enum<E> {
enum entry A
private constructor E()
protected final override /*1*/ /*fake_override*/ fun clone(): kotlin.Any
public final override /*1*/ /*fake_override*/ fun compareTo(/*0*/ other: E): kotlin.Int
public final override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
public final override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int
public final override /*1*/ /*fake_override*/ fun name(): kotlin.String
public final override /*1*/ /*fake_override*/ fun ordinal(): kotlin.Int
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
// Static members
public final /*synthesized*/ fun valueOf(/*0*/ value: kotlin.String): E
public final /*synthesized*/ fun values(): kotlin.Array<E>
}
@@ -0,0 +1,8 @@
enum class E(val x: Int, val y: Int) {
A(1, 2),
B(1),
C; // no constructor call needed even here
constructor(): this(0, 0)
constructor(x: Int): this(x, 0)
}
@@ -0,0 +1,26 @@
package
internal final enum class E : kotlin.Enum<E> {
enum entry A
enum entry B
enum entry C
private constructor E()
private constructor E(/*0*/ x: kotlin.Int)
private constructor E(/*0*/ x: kotlin.Int, /*1*/ y: kotlin.Int)
internal final val x: kotlin.Int
internal final val y: kotlin.Int
protected final override /*1*/ /*fake_override*/ fun clone(): kotlin.Any
public final override /*1*/ /*fake_override*/ fun compareTo(/*0*/ other: E): kotlin.Int
public final override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
public final override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int
public final override /*1*/ /*fake_override*/ fun name(): kotlin.String
public final override /*1*/ /*fake_override*/ fun ordinal(): kotlin.Int
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
// Static members
public final /*synthesized*/ fun valueOf(/*0*/ value: kotlin.String): E
public final /*synthesized*/ fun values(): kotlin.Array<E>
}
@@ -5199,6 +5199,12 @@ public class JetDiagnosticsTestGenerated extends AbstractJetDiagnosticsTest {
doTest(fileName);
}
@TestMetadata("emptyConstructor.kt")
public void testEmptyConstructor() throws Exception {
String fileName = JetTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/enum/emptyConstructor.kt");
doTest(fileName);
}
@TestMetadata("entryShouldBeOfEnumType.kt")
public void testEntryShouldBeOfEnumType() throws Exception {
String fileName = JetTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/enum/entryShouldBeOfEnumType.kt");
@@ -5385,6 +5391,12 @@ public class JetDiagnosticsTestGenerated extends AbstractJetDiagnosticsTest {
doTest(fileName);
}
@TestMetadata("multipleConstructors.kt")
public void testMultipleConstructors() throws Exception {
String fileName = JetTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/enum/multipleConstructors.kt");
doTest(fileName);
}
@TestMetadata("openMemberInEnum.kt")
public void testOpenMemberInEnum() throws Exception {
String fileName = JetTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/enum/openMemberInEnum.kt");
@@ -3415,6 +3415,12 @@ public class BlackBoxCodegenTestGenerated extends AbstractBlackBoxCodegenTest {
doTest(fileName);
}
@TestMetadata("emptyConstructor.kt")
public void testEmptyConstructor() throws Exception {
String fileName = JetTestUtils.navigationMetadata("compiler/testData/codegen/box/enum/emptyConstructor.kt");
doTest(fileName);
}
@TestMetadata("emptyEnumValuesValueOf.kt")
public void testEmptyEnumValuesValueOf() throws Exception {
String fileName = JetTestUtils.navigationMetadata("compiler/testData/codegen/box/enum/emptyEnumValuesValueOf.kt");
@@ -540,6 +540,13 @@ public class DescriptorUtils {
return !isSingletonOrAnonymousObject(classDescriptor) && !isTrait(classDescriptor);
}
public static boolean hasDefaultConstructor(@NotNull ClassDescriptor classDescriptor) {
for (ConstructorDescriptor constructor : classDescriptor.getConstructors()) {
if (constructor.getValueParameters().isEmpty()) return true;
}
return false;
}
public static Set<FqName> getPackagesFqNames(ModuleDescriptor module) {
Set<FqName> result = getSubPackagesFqNames(module.getPackage(FqName.ROOT));
result.add(FqName.ROOT);