From 913724f0e9444ff27341e0099c593a17487bc7ad Mon Sep 17 00:00:00 2001 From: "Pavel V. Talanov" Date: Thu, 5 Mar 2015 16:43:26 +0300 Subject: [PATCH] Allow default object to be denoted with 'default' modifier Report 'default' modifier in illegal positions --- .../kotlin/psi/JetObjectDeclaration.java | 2 +- .../kotlin/resolve/ModifiersChecker.java | 20 +++ .../lazy/data/JetClassOrObjectInfo.java | 2 +- .../resolve/lazy/data/JetObjectInfo.java | 3 +- .../multipleDissallowedDefaultObjects.kt | 17 ++ .../multipleDissallowedDefaultObjects.txt | 64 +++++++ .../tests/modifiers/defaultModifier.kt | 55 ++++++ .../tests/modifiers/defaultModifier.txt | 165 ++++++++++++++++++ .../checkers/JetDiagnosticsTestGenerated.java | 12 ++ 9 files changed, 337 insertions(+), 3 deletions(-) create mode 100644 compiler/testData/diagnostics/tests/classObjects/multipleDissallowedDefaultObjects.kt create mode 100644 compiler/testData/diagnostics/tests/classObjects/multipleDissallowedDefaultObjects.txt create mode 100644 compiler/testData/diagnostics/tests/modifiers/defaultModifier.kt create mode 100644 compiler/testData/diagnostics/tests/modifiers/defaultModifier.txt diff --git a/compiler/frontend/src/org/jetbrains/kotlin/psi/JetObjectDeclaration.java b/compiler/frontend/src/org/jetbrains/kotlin/psi/JetObjectDeclaration.java index a6dd477701d..c5c097808d2 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/psi/JetObjectDeclaration.java +++ b/compiler/frontend/src/org/jetbrains/kotlin/psi/JetObjectDeclaration.java @@ -91,7 +91,7 @@ public class JetObjectDeclaration extends JetNamedDeclarationStub { } public boolean isDefaultObject() { - return element.isDefault(); + return element.isDefault() && ModifiersChecker.isDefaultModifierAllowed(element); } } diff --git a/compiler/testData/diagnostics/tests/classObjects/multipleDissallowedDefaultObjects.kt b/compiler/testData/diagnostics/tests/classObjects/multipleDissallowedDefaultObjects.kt new file mode 100644 index 00000000000..0f715883c8f --- /dev/null +++ b/compiler/testData/diagnostics/tests/classObjects/multipleDissallowedDefaultObjects.kt @@ -0,0 +1,17 @@ +class A { + inner class I { + default object A + + default object B + + default object C + } +} + +object O { + default object A + + default object B + + default object C +} \ No newline at end of file diff --git a/compiler/testData/diagnostics/tests/classObjects/multipleDissallowedDefaultObjects.txt b/compiler/testData/diagnostics/tests/classObjects/multipleDissallowedDefaultObjects.txt new file mode 100644 index 00000000000..d8c08d5522c --- /dev/null +++ b/compiler/testData/diagnostics/tests/classObjects/multipleDissallowedDefaultObjects.txt @@ -0,0 +1,64 @@ +package + +internal final class A { + public constructor A() + public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean + public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int + public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String + + internal final inner class I { + public constructor I() + public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean + public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int + public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String + + internal class object A { + private constructor A() + public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean + public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int + public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String + } + + internal class object B { + private constructor B() + public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean + public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int + public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String + } + + internal class object C { + private constructor C() + public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean + public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int + public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String + } + } +} + +internal object O { + private constructor O() + public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean + public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int + public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String + + internal class object A { + private constructor A() + public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean + public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int + public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String + } + + internal class object B { + private constructor B() + public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean + public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int + public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String + } + + internal class object C { + private constructor C() + public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean + public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int + public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String + } +} diff --git a/compiler/testData/diagnostics/tests/modifiers/defaultModifier.kt b/compiler/testData/diagnostics/tests/modifiers/defaultModifier.kt new file mode 100644 index 00000000000..6de9e74d10e --- /dev/null +++ b/compiler/testData/diagnostics/tests/modifiers/defaultModifier.kt @@ -0,0 +1,55 @@ +default class A { + default object { + + } +} + +class B { + default object + + val c: Int = 1 +} + +class C { + default object A { + + } +} + +class D { + default object A { + default object { + } + } +} + +default object G { + default object +} + +default trait H { + default object +} + +class J { + default object C { + default object + } +} + +default enum class Enum { + E1 + E2 + + default object +} + +default fun main() { + +} + +default var prop: Int = 1 + default get + default set + +class Z(default val c: Int) \ No newline at end of file diff --git a/compiler/testData/diagnostics/tests/modifiers/defaultModifier.txt b/compiler/testData/diagnostics/tests/modifiers/defaultModifier.txt new file mode 100644 index 00000000000..e4ecd964692 --- /dev/null +++ b/compiler/testData/diagnostics/tests/modifiers/defaultModifier.txt @@ -0,0 +1,165 @@ +package + +internal var prop: kotlin.Int +internal fun main(): kotlin.Unit + +internal final class A { + public constructor A() + public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean + public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int + public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String + + internal class object Default { + private constructor Default() + public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean + public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int + public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String + } +} + +internal final class B { + public constructor B() + internal final val c: kotlin.Int = 1 + public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean + public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int + public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String + + internal class object Default { + private constructor Default() + public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean + public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int + public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String + } +} + +internal final class C { + public constructor C() + public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean + public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int + public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String + + internal class object A { + private constructor A() + public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean + public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int + public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String + } +} + +internal final class D { + public constructor D() + public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean + public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int + public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String + + internal class object A { + private constructor A() + public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean + public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int + public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String + + internal class object Default { + private constructor Default() + public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean + public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int + public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String + } + } +} + +internal final enum class Enum : kotlin.Enum { + public enum entry E1 : Enum { + private constructor E1() + public final override /*1*/ /*fake_override*/ fun compareTo(/*0*/ other: Enum): 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 E2 : Enum { + private constructor E2() + public final override /*1*/ /*fake_override*/ fun compareTo(/*0*/ other: Enum): 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 Enum() + public final override /*1*/ /*fake_override*/ fun compareTo(/*0*/ other: Enum): 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 + + internal class object Default { + private constructor Default() + public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean + public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int + public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String + } + + // Static members + public final /*synthesized*/ fun valueOf(/*0*/ value: kotlin.String): Enum + public final /*synthesized*/ fun values(): kotlin.Array +} + +internal object G { + private constructor G() + public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean + public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int + public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String + + internal class object Default { + private constructor Default() + public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean + public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int + public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String + } +} + +internal trait H { + public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean + public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int + public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String + + internal class object Default { + private constructor Default() + public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean + public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int + public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String + } +} + +internal final class J { + public constructor J() + public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean + public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int + public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String + + internal class object C { + private constructor C() + public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean + public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int + public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String + + internal class object Default { + private constructor Default() + public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean + public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int + public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String + } + } +} + +internal final class Z { + public constructor Z(/*0*/ c: kotlin.Int) + internal final val c: kotlin.Int + public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean + public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int + public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String +} diff --git a/compiler/tests/org/jetbrains/kotlin/checkers/JetDiagnosticsTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/checkers/JetDiagnosticsTestGenerated.java index 18b45b26634..372ed030947 100644 --- a/compiler/tests/org/jetbrains/kotlin/checkers/JetDiagnosticsTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/checkers/JetDiagnosticsTestGenerated.java @@ -1576,6 +1576,12 @@ public class JetDiagnosticsTestGenerated extends AbstractJetDiagnosticsTest { doTest(fileName); } + @TestMetadata("multipleDissallowedDefaultObjects.kt") + public void testMultipleDissallowedDefaultObjects() throws Exception { + String fileName = JetTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/classObjects/multipleDissallowedDefaultObjects.kt"); + doTest(fileName); + } + @TestMetadata("nestedClassInPrivateClassObject.kt") public void testNestedClassInPrivateClassObject() throws Exception { String fileName = JetTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/classObjects/nestedClassInPrivateClassObject.kt"); @@ -7380,6 +7386,12 @@ public class JetDiagnosticsTestGenerated extends AbstractJetDiagnosticsTest { JetTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("compiler/testData/diagnostics/tests/modifiers"), Pattern.compile("^(.+)\\.kt$"), true); } + @TestMetadata("defaultModifier.kt") + public void testDefaultModifier() throws Exception { + String fileName = JetTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/modifiers/defaultModifier.kt"); + doTest(fileName); + } + @TestMetadata("IllegalModifiers.kt") public void testIllegalModifiers() throws Exception { String fileName = JetTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/modifiers/IllegalModifiers.kt");