From 3c7225e83a48678601e5c9133b0e60f7509b1203 Mon Sep 17 00:00:00 2001 From: Mikhail Glukhikh Date: Tue, 19 May 2015 15:01:33 +0300 Subject: [PATCH] An implementation of KT-7753: now enum constructors cannot be called explicitly. Several tests are provided. #KT-7753 Fixed. --- .../jetbrains/kotlin/diagnostics/Errors.java | 1 + .../rendering/DefaultErrorMessages.java | 2 + .../resolve/calls/CallExpressionResolver.java | 9 ++- .../tests/enum/ConstructorCallFromOutside.kt | 10 +++ .../tests/enum/ConstructorCallFromOutside.txt | 40 +++++++++++ .../tests/enum/ExplicitConstructorCall.kt | 10 +++ .../tests/enum/ExplicitConstructorCall.txt | 41 ++++++++++++ .../tests/enum/InsideEntryConstructorCall.kt | 15 +++++ .../tests/enum/InsideEntryConstructorCall.txt | 41 ++++++++++++ .../enum/InsideSecondaryConstructorCall.kt | 13 ++++ .../enum/InsideSecondaryConstructorCall.txt | 66 +++++++++++++++++++ .../tests/enum/OldSyntaxConstructorCall.kt | 10 +++ .../tests/enum/OldSyntaxConstructorCall.txt | 41 ++++++++++++ .../tests/enum/SecondaryConstructorCall.kt | 9 +++ .../tests/enum/SecondaryConstructorCall.txt | 61 +++++++++++++++++ .../checkers/JetDiagnosticsTestGenerated.java | 36 ++++++++++ 16 files changed, 402 insertions(+), 3 deletions(-) create mode 100644 compiler/testData/diagnostics/tests/enum/ConstructorCallFromOutside.kt create mode 100644 compiler/testData/diagnostics/tests/enum/ConstructorCallFromOutside.txt create mode 100644 compiler/testData/diagnostics/tests/enum/ExplicitConstructorCall.kt create mode 100644 compiler/testData/diagnostics/tests/enum/ExplicitConstructorCall.txt create mode 100644 compiler/testData/diagnostics/tests/enum/InsideEntryConstructorCall.kt create mode 100644 compiler/testData/diagnostics/tests/enum/InsideEntryConstructorCall.txt create mode 100644 compiler/testData/diagnostics/tests/enum/InsideSecondaryConstructorCall.kt create mode 100644 compiler/testData/diagnostics/tests/enum/InsideSecondaryConstructorCall.txt create mode 100644 compiler/testData/diagnostics/tests/enum/OldSyntaxConstructorCall.kt create mode 100644 compiler/testData/diagnostics/tests/enum/OldSyntaxConstructorCall.txt create mode 100644 compiler/testData/diagnostics/tests/enum/SecondaryConstructorCall.kt create mode 100644 compiler/testData/diagnostics/tests/enum/SecondaryConstructorCall.txt diff --git a/compiler/frontend/src/org/jetbrains/kotlin/diagnostics/Errors.java b/compiler/frontend/src/org/jetbrains/kotlin/diagnostics/Errors.java index d17fb7e6d2d..5a206a364db 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/diagnostics/Errors.java +++ b/compiler/frontend/src/org/jetbrains/kotlin/diagnostics/Errors.java @@ -217,6 +217,7 @@ public interface Errors { DiagnosticFactory2 ENUM_ENTRY_USES_DEPRECATED_OR_NO_DELIMITER = DiagnosticFactory2.create(WARNING, DECLARATION_NAME); DiagnosticFactory1 ENUM_ENTRY_USES_DEPRECATED_SUPER_CONSTRUCTOR = DiagnosticFactory1.create(WARNING, DELEGATOR_SUPER_CALL); DiagnosticFactory1 ENUM_ENTRY_AFTER_ENUM_MEMBER = DiagnosticFactory1.create(WARNING, DECLARATION_NAME); + DiagnosticFactory0 ENUM_CLASS_CONSTRUCTOR_CALL = DiagnosticFactory0.create(ERROR); // Companion objects diff --git a/compiler/frontend/src/org/jetbrains/kotlin/diagnostics/rendering/DefaultErrorMessages.java b/compiler/frontend/src/org/jetbrains/kotlin/diagnostics/rendering/DefaultErrorMessages.java index ab68afa0650..50b3c468d8e 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/diagnostics/rendering/DefaultErrorMessages.java +++ b/compiler/frontend/src/org/jetbrains/kotlin/diagnostics/rendering/DefaultErrorMessages.java @@ -261,6 +261,8 @@ public class DefaultErrorMessages { MAP.put(ENUM_ENTRY_USES_DEPRECATED_OR_NO_DELIMITER, "Enum entry ''{0}'' should have a correct delimiter ''{1}'' after it", NAME, STRING); MAP.put(ENUM_ENTRY_USES_DEPRECATED_SUPER_CONSTRUCTOR, "Enum entry ''{0}'' uses deprecated super constructor syntax, use ENTRY(arguments) instead", NAME); MAP.put(ENUM_ENTRY_AFTER_ENUM_MEMBER, "Enum entry ''{0}'' is not allowed after a member", NAME); + MAP.put(ENUM_CLASS_CONSTRUCTOR_CALL, "Enum types cannot be instantiated"); + MAP.put(DELEGATION_IN_TRAIT, "Interfaces cannot use delegation"); MAP.put(DELEGATION_NOT_TO_TRAIT, "Only interfaces can be delegated to"); MAP.put(DEPRECATED_TRAIT_KEYWORD, "'trait' keyword is deprecated, use 'interface' instead"); diff --git a/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/CallExpressionResolver.java b/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/CallExpressionResolver.java index 3a3b2b85565..3fb4273bdc3 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/CallExpressionResolver.java +++ b/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/CallExpressionResolver.java @@ -210,11 +210,14 @@ public class CallExpressionResolver { if (functionDescriptor == null) { return TypeInfoFactoryPackage.noTypeInfo(context); } - if (functionDescriptor instanceof ConstructorDescriptor && - DescriptorUtils.isAnnotationClass(functionDescriptor.getContainingDeclaration())) { - if (!canInstantiateAnnotationClass(callExpression)) { + if (functionDescriptor instanceof ConstructorDescriptor) { + if (DescriptorUtils.isAnnotationClass(functionDescriptor.getContainingDeclaration()) + && !canInstantiateAnnotationClass(callExpression)) { context.trace.report(ANNOTATION_CLASS_CONSTRUCTOR_CALL.on(callExpression)); } + if (DescriptorUtils.isEnumClass(functionDescriptor.getContainingDeclaration())) { + context.trace.report(ENUM_CLASS_CONSTRUCTOR_CALL.on(callExpression)); + } } JetType type = functionDescriptor.getReturnType(); diff --git a/compiler/testData/diagnostics/tests/enum/ConstructorCallFromOutside.kt b/compiler/testData/diagnostics/tests/enum/ConstructorCallFromOutside.kt new file mode 100644 index 00000000000..1f70e5c5c40 --- /dev/null +++ b/compiler/testData/diagnostics/tests/enum/ConstructorCallFromOutside.kt @@ -0,0 +1,10 @@ +// KT-7753: attempt to call enum constructor explicitly +enum class A(val c: Int) { + ONE(1), + TWO(2); +} + +fun createA(): A { + // Error should be here! + return A(10) +} diff --git a/compiler/testData/diagnostics/tests/enum/ConstructorCallFromOutside.txt b/compiler/testData/diagnostics/tests/enum/ConstructorCallFromOutside.txt new file mode 100644 index 00000000000..40e11906b61 --- /dev/null +++ b/compiler/testData/diagnostics/tests/enum/ConstructorCallFromOutside.txt @@ -0,0 +1,40 @@ +package + +internal fun createA(): A + +internal final enum class A : kotlin.Enum { + public enum entry ONE : A { + private constructor ONE() + internal final override /*1*/ /*fake_override*/ val c: kotlin.Int + public final override /*1*/ /*fake_override*/ fun compareTo(/*0*/ other: A): 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 + } + + public enum entry TWO : A { + private constructor TWO() + internal final override /*1*/ /*fake_override*/ val c: kotlin.Int + public final override /*1*/ /*fake_override*/ fun compareTo(/*0*/ other: A): 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 + } + + private constructor A(/*0*/ c: kotlin.Int) + internal final val c: kotlin.Int + public final override /*1*/ /*fake_override*/ fun compareTo(/*0*/ other: A): 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): A + public final /*synthesized*/ fun values(): kotlin.Array +} diff --git a/compiler/testData/diagnostics/tests/enum/ExplicitConstructorCall.kt b/compiler/testData/diagnostics/tests/enum/ExplicitConstructorCall.kt new file mode 100644 index 00000000000..53374c1f66f --- /dev/null +++ b/compiler/testData/diagnostics/tests/enum/ExplicitConstructorCall.kt @@ -0,0 +1,10 @@ +// KT-7753: attempt to call enum constructor explicitly +enum class A(val c: Int) { + ONE(1), + TWO(2); + + fun createA(): A { + // Error should be here! + return A(10) + } +} \ No newline at end of file diff --git a/compiler/testData/diagnostics/tests/enum/ExplicitConstructorCall.txt b/compiler/testData/diagnostics/tests/enum/ExplicitConstructorCall.txt new file mode 100644 index 00000000000..e3d55fad4c1 --- /dev/null +++ b/compiler/testData/diagnostics/tests/enum/ExplicitConstructorCall.txt @@ -0,0 +1,41 @@ +package + +internal final enum class A : kotlin.Enum { + public enum entry ONE : A { + private constructor ONE() + internal final override /*1*/ /*fake_override*/ val c: kotlin.Int + public final override /*1*/ /*fake_override*/ fun compareTo(/*0*/ other: A): kotlin.Int + internal final override /*1*/ /*fake_override*/ fun createA(): A + 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 + } + + public enum entry TWO : A { + private constructor TWO() + internal final override /*1*/ /*fake_override*/ val c: kotlin.Int + public final override /*1*/ /*fake_override*/ fun compareTo(/*0*/ other: A): kotlin.Int + internal final override /*1*/ /*fake_override*/ fun createA(): A + 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 + } + + private constructor A(/*0*/ c: kotlin.Int) + internal final val c: kotlin.Int + public final override /*1*/ /*fake_override*/ fun compareTo(/*0*/ other: A): kotlin.Int + internal final fun createA(): A + 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): A + public final /*synthesized*/ fun values(): kotlin.Array +} diff --git a/compiler/testData/diagnostics/tests/enum/InsideEntryConstructorCall.kt b/compiler/testData/diagnostics/tests/enum/InsideEntryConstructorCall.kt new file mode 100644 index 00000000000..5660949f479 --- /dev/null +++ b/compiler/testData/diagnostics/tests/enum/InsideEntryConstructorCall.kt @@ -0,0 +1,15 @@ +// KT-7753: attempt to call enum constructor explicitly +enum class A(val c: Int) { + ONE(1) { + override fun selfOrFriend(): A { + return this + } + }, + TWO(2) { + override fun selfOrFriend(): A { + return A(42) + } + }; + + abstract fun selfOrFriend(): A +} \ No newline at end of file diff --git a/compiler/testData/diagnostics/tests/enum/InsideEntryConstructorCall.txt b/compiler/testData/diagnostics/tests/enum/InsideEntryConstructorCall.txt new file mode 100644 index 00000000000..bec230f5ce7 --- /dev/null +++ b/compiler/testData/diagnostics/tests/enum/InsideEntryConstructorCall.txt @@ -0,0 +1,41 @@ +package + +internal final enum class A : kotlin.Enum { + public enum entry ONE : A { + private constructor ONE() + internal final override /*1*/ /*fake_override*/ val c: kotlin.Int + public final override /*1*/ /*fake_override*/ fun compareTo(/*0*/ other: A): 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 + internal open override /*1*/ fun selfOrFriend(): A + public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String + } + + public enum entry TWO : A { + private constructor TWO() + internal final override /*1*/ /*fake_override*/ val c: kotlin.Int + public final override /*1*/ /*fake_override*/ fun compareTo(/*0*/ other: A): 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 + internal open override /*1*/ fun selfOrFriend(): A + public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String + } + + private constructor A(/*0*/ c: kotlin.Int) + internal final val c: kotlin.Int + public final override /*1*/ /*fake_override*/ fun compareTo(/*0*/ other: A): 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 + internal abstract fun selfOrFriend(): A + public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String + + // Static members + public final /*synthesized*/ fun valueOf(/*0*/ value: kotlin.String): A + public final /*synthesized*/ fun values(): kotlin.Array +} diff --git a/compiler/testData/diagnostics/tests/enum/InsideSecondaryConstructorCall.kt b/compiler/testData/diagnostics/tests/enum/InsideSecondaryConstructorCall.kt new file mode 100644 index 00000000000..52ff43276a6 --- /dev/null +++ b/compiler/testData/diagnostics/tests/enum/InsideSecondaryConstructorCall.kt @@ -0,0 +1,13 @@ +// KT-7753: attempt to call enum constructor explicitly +enum class A(val c: Int) { + ONE(1), + TWO(2), + THREE(3), + FOURTY_TWO(); + + var last: A? = null + + constructor(): this(42) { + last = A(13) + } +} \ No newline at end of file diff --git a/compiler/testData/diagnostics/tests/enum/InsideSecondaryConstructorCall.txt b/compiler/testData/diagnostics/tests/enum/InsideSecondaryConstructorCall.txt new file mode 100644 index 00000000000..ae2cf6af510 --- /dev/null +++ b/compiler/testData/diagnostics/tests/enum/InsideSecondaryConstructorCall.txt @@ -0,0 +1,66 @@ +package + +internal final enum class A : kotlin.Enum { + public enum entry ONE : A { + private constructor ONE() + internal final override /*1*/ /*fake_override*/ val c: kotlin.Int + internal final override /*1*/ /*fake_override*/ var last: A? + public final override /*1*/ /*fake_override*/ fun compareTo(/*0*/ other: A): 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 + } + + public enum entry TWO : A { + private constructor TWO() + internal final override /*1*/ /*fake_override*/ val c: kotlin.Int + internal final override /*1*/ /*fake_override*/ var last: A? + public final override /*1*/ /*fake_override*/ fun compareTo(/*0*/ other: A): 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 + } + + public enum entry THREE : A { + private constructor THREE() + internal final override /*1*/ /*fake_override*/ val c: kotlin.Int + internal final override /*1*/ /*fake_override*/ var last: A? + public final override /*1*/ /*fake_override*/ fun compareTo(/*0*/ other: A): 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 + } + + public enum entry FOURTY_TWO : A { + private constructor FOURTY_TWO() + internal final override /*1*/ /*fake_override*/ val c: kotlin.Int + internal final override /*1*/ /*fake_override*/ var last: A? + public final override /*1*/ /*fake_override*/ fun compareTo(/*0*/ other: A): 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 + } + + private constructor A() + private constructor A(/*0*/ c: kotlin.Int) + internal final val c: kotlin.Int + internal final var last: A? + public final override /*1*/ /*fake_override*/ fun compareTo(/*0*/ other: A): 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): A + public final /*synthesized*/ fun values(): kotlin.Array +} diff --git a/compiler/testData/diagnostics/tests/enum/OldSyntaxConstructorCall.kt b/compiler/testData/diagnostics/tests/enum/OldSyntaxConstructorCall.kt new file mode 100644 index 00000000000..0a311010dfa --- /dev/null +++ b/compiler/testData/diagnostics/tests/enum/OldSyntaxConstructorCall.kt @@ -0,0 +1,10 @@ +// KT-7753: attempt to call enum constructor explicitly +enum class A(val c: Int) { + // No errors at both places, but warnings about deprecated + ONE: A(1), + TWO: A(2); + + fun getA(): A { + return ONE + } +} \ No newline at end of file diff --git a/compiler/testData/diagnostics/tests/enum/OldSyntaxConstructorCall.txt b/compiler/testData/diagnostics/tests/enum/OldSyntaxConstructorCall.txt new file mode 100644 index 00000000000..b8dc30290fa --- /dev/null +++ b/compiler/testData/diagnostics/tests/enum/OldSyntaxConstructorCall.txt @@ -0,0 +1,41 @@ +package + +internal final enum class A : kotlin.Enum { + public enum entry ONE : A { + private constructor ONE() + internal final override /*1*/ /*fake_override*/ val c: kotlin.Int + public final override /*1*/ /*fake_override*/ fun compareTo(/*0*/ other: A): kotlin.Int + public final override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean + internal final override /*1*/ /*fake_override*/ fun getA(): A + 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 + } + + public enum entry TWO : A { + private constructor TWO() + internal final override /*1*/ /*fake_override*/ val c: kotlin.Int + public final override /*1*/ /*fake_override*/ fun compareTo(/*0*/ other: A): kotlin.Int + public final override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean + internal final override /*1*/ /*fake_override*/ fun getA(): A + 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 + } + + private constructor A(/*0*/ c: kotlin.Int) + internal final val c: kotlin.Int + public final override /*1*/ /*fake_override*/ fun compareTo(/*0*/ other: A): kotlin.Int + public final override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean + internal final fun getA(): A + 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): A + public final /*synthesized*/ fun values(): kotlin.Array +} diff --git a/compiler/testData/diagnostics/tests/enum/SecondaryConstructorCall.kt b/compiler/testData/diagnostics/tests/enum/SecondaryConstructorCall.kt new file mode 100644 index 00000000000..04a111c1625 --- /dev/null +++ b/compiler/testData/diagnostics/tests/enum/SecondaryConstructorCall.kt @@ -0,0 +1,9 @@ +// KT-7753 false positive: enum constructor can be called from secondary constructor +enum class A(val c: Int) { + ONE(1), + TWO(2), + THREE(3), + FOURTY_TWO(); + + constructor(): this(42) +} \ No newline at end of file diff --git a/compiler/testData/diagnostics/tests/enum/SecondaryConstructorCall.txt b/compiler/testData/diagnostics/tests/enum/SecondaryConstructorCall.txt new file mode 100644 index 00000000000..e0453734f1c --- /dev/null +++ b/compiler/testData/diagnostics/tests/enum/SecondaryConstructorCall.txt @@ -0,0 +1,61 @@ +package + +internal final enum class A : kotlin.Enum { + public enum entry ONE : A { + private constructor ONE() + internal final override /*1*/ /*fake_override*/ val c: kotlin.Int + public final override /*1*/ /*fake_override*/ fun compareTo(/*0*/ other: A): 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 + } + + public enum entry TWO : A { + private constructor TWO() + internal final override /*1*/ /*fake_override*/ val c: kotlin.Int + public final override /*1*/ /*fake_override*/ fun compareTo(/*0*/ other: A): 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 + } + + public enum entry THREE : A { + private constructor THREE() + internal final override /*1*/ /*fake_override*/ val c: kotlin.Int + public final override /*1*/ /*fake_override*/ fun compareTo(/*0*/ other: A): 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 + } + + public enum entry FOURTY_TWO : A { + private constructor FOURTY_TWO() + internal final override /*1*/ /*fake_override*/ val c: kotlin.Int + public final override /*1*/ /*fake_override*/ fun compareTo(/*0*/ other: A): 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 + } + + private constructor A() + private constructor A(/*0*/ c: kotlin.Int) + internal final val c: kotlin.Int + public final override /*1*/ /*fake_override*/ fun compareTo(/*0*/ other: A): 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): A + public final /*synthesized*/ fun values(): kotlin.Array +} diff --git a/compiler/tests/org/jetbrains/kotlin/checkers/JetDiagnosticsTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/checkers/JetDiagnosticsTestGenerated.java index 4439504a88e..3ae8b527d2f 100644 --- a/compiler/tests/org/jetbrains/kotlin/checkers/JetDiagnosticsTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/checkers/JetDiagnosticsTestGenerated.java @@ -4185,6 +4185,12 @@ public class JetDiagnosticsTestGenerated extends AbstractJetDiagnosticsTest { doTest(fileName); } + @TestMetadata("ConstructorCallFromOutside.kt") + public void testConstructorCallFromOutside() throws Exception { + String fileName = JetTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/enum/ConstructorCallFromOutside.kt"); + doTest(fileName); + } + @TestMetadata("dontCreatePackageTypeForEnumEntry.kt") public void testDontCreatePackageTypeForEnumEntry() throws Exception { String fileName = JetTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/enum/dontCreatePackageTypeForEnumEntry.kt"); @@ -4329,6 +4335,12 @@ public class JetDiagnosticsTestGenerated extends AbstractJetDiagnosticsTest { doTest(fileName); } + @TestMetadata("ExplicitConstructorCall.kt") + public void testExplicitConstructorCall() throws Exception { + String fileName = JetTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/enum/ExplicitConstructorCall.kt"); + doTest(fileName); + } + @TestMetadata("extensionNamedAsEnumEntry.kt") public void testExtensionNamedAsEnumEntry() throws Exception { String fileName = JetTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/enum/extensionNamedAsEnumEntry.kt"); @@ -4353,6 +4365,18 @@ public class JetDiagnosticsTestGenerated extends AbstractJetDiagnosticsTest { doTest(fileName); } + @TestMetadata("InsideEntryConstructorCall.kt") + public void testInsideEntryConstructorCall() throws Exception { + String fileName = JetTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/enum/InsideEntryConstructorCall.kt"); + doTest(fileName); + } + + @TestMetadata("InsideSecondaryConstructorCall.kt") + public void testInsideSecondaryConstructorCall() throws Exception { + String fileName = JetTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/enum/InsideSecondaryConstructorCall.kt"); + doTest(fileName); + } + @TestMetadata("javaEnumValueOfMethod.kt") public void testJavaEnumValueOfMethod() throws Exception { String fileName = JetTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/enum/javaEnumValueOfMethod.kt"); @@ -4407,6 +4431,12 @@ public class JetDiagnosticsTestGenerated extends AbstractJetDiagnosticsTest { doTest(fileName); } + @TestMetadata("OldSyntaxConstructorCall.kt") + public void testOldSyntaxConstructorCall() throws Exception { + String fileName = JetTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/enum/OldSyntaxConstructorCall.kt"); + doTest(fileName); + } + @TestMetadata("openMemberInEnum.kt") public void testOpenMemberInEnum() throws Exception { String fileName = JetTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/enum/openMemberInEnum.kt"); @@ -4419,6 +4449,12 @@ public class JetDiagnosticsTestGenerated extends AbstractJetDiagnosticsTest { doTest(fileName); } + @TestMetadata("SecondaryConstructorCall.kt") + public void testSecondaryConstructorCall() throws Exception { + String fileName = JetTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/enum/SecondaryConstructorCall.kt"); + doTest(fileName); + } + @TestMetadata("starImportNestedClassAndEntries.kt") public void testStarImportNestedClassAndEntries() throws Exception { String fileName = JetTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/enum/starImportNestedClassAndEntries.kt");