From 60bdae9d753a5f0bb7c7c9177735ddf8afec34b6 Mon Sep 17 00:00:00 2001 From: "Natalia.Ukhorskaya" Date: Thu, 13 Sep 2012 16:26:07 +0400 Subject: [PATCH] Prohibit body for annotation class #KT-1886 Fixed --- .../jet/lang/diagnostics/Errors.java | 2 +- .../rendering/DefaultErrorMessages.java | 1 + .../jet/lang/resolve/DeclarationResolver.java | 5 +++ .../tests/annotations/kt1860-positive.kt | 4 +-- .../tests/annotations/kt1886annotationBody.kt | 29 ++++++++++++++++++ compiler/testData/renderer/Classes.kt | 5 ++- .../checkers/JetDiagnosticsTestGenerated.java | 5 +++ .../compilerMessages/k2js/lib/src/lib.zip | Bin 184 -> 218 bytes idea/testData/debugger/annotation.kt | 5 --- .../debugger/JetPositionManagerTest.java | 4 --- .../jet/plugin/stubs/JetStubsTest.java | 2 +- js/js.libraries/src/core/annotations.kt | 6 ++-- js/js.libraries/src/junit/core.kt | 2 +- .../src/kotlin2/BindExtensionFunctions.kt | 3 +- libraries/stdlib/src/js/Annotations.kt | 4 +-- 15 files changed, 53 insertions(+), 24 deletions(-) create mode 100644 compiler/testData/diagnostics/tests/annotations/kt1886annotationBody.kt delete mode 100644 idea/testData/debugger/annotation.kt diff --git a/compiler/frontend/src/org/jetbrains/jet/lang/diagnostics/Errors.java b/compiler/frontend/src/org/jetbrains/jet/lang/diagnostics/Errors.java index a27e6dc675d..627140be7d7 100644 --- a/compiler/frontend/src/org/jetbrains/jet/lang/diagnostics/Errors.java +++ b/compiler/frontend/src/org/jetbrains/jet/lang/diagnostics/Errors.java @@ -360,7 +360,7 @@ public interface Errors { SimpleDiagnosticFactory DANGLING_FUNCTION_LITERAL_ARGUMENT_SUSPECTED = SimpleDiagnosticFactory.create(WARNING); DiagnosticFactory1 NOT_AN_ANNOTATION_CLASS = DiagnosticFactory1.create(ERROR); - + SimpleDiagnosticFactory ANNOTATION_CLASS_WITH_BODY = SimpleDiagnosticFactory.create(ERROR); // This field is needed to make the Initializer class load (interfaces cannot have static initializers) @SuppressWarnings("UnusedDeclaration") diff --git a/compiler/frontend/src/org/jetbrains/jet/lang/diagnostics/rendering/DefaultErrorMessages.java b/compiler/frontend/src/org/jetbrains/jet/lang/diagnostics/rendering/DefaultErrorMessages.java index 44a79a3b0c8..c61c72225ff 100644 --- a/compiler/frontend/src/org/jetbrains/jet/lang/diagnostics/rendering/DefaultErrorMessages.java +++ b/compiler/frontend/src/org/jetbrains/jet/lang/diagnostics/rendering/DefaultErrorMessages.java @@ -414,6 +414,7 @@ public class DefaultErrorMessages { "Separate it with a semicolon (;) if it is not intended to be an argument."); MAP.put(NOT_AN_ANNOTATION_CLASS, "''{0}'' is not an annotation class", TO_STRING); + MAP.put(ANNOTATION_CLASS_WITH_BODY, "Body is not allowed for annotation class"); MAP.put(DEFAULT_VALUE_NOT_ALLOWED_IN_OVERRIDE, "An overriding function is not allowed to specify default values for its parameters"); diff --git a/compiler/frontend/src/org/jetbrains/jet/lang/resolve/DeclarationResolver.java b/compiler/frontend/src/org/jetbrains/jet/lang/resolve/DeclarationResolver.java index f756a3d4970..e108a350e77 100644 --- a/compiler/frontend/src/org/jetbrains/jet/lang/resolve/DeclarationResolver.java +++ b/compiler/frontend/src/org/jetbrains/jet/lang/resolve/DeclarationResolver.java @@ -139,6 +139,11 @@ public class DeclarationResolver { JetClass jetClass = entry.getKey(); MutableClassDescriptor classDescriptor = entry.getValue(); + JetClassBody jetClassBody = jetClass.getBody(); + if (classDescriptor.getKind() == ClassKind.ANNOTATION_CLASS && jetClassBody != null) { + trace.report(ANNOTATION_CLASS_WITH_BODY.on(jetClassBody)); + } + resolveFunctionAndPropertyHeaders( jetClass.getDeclarations(), classDescriptor.getScopeForMemberResolution(), classDescriptor.getScopeForInitializers(), classDescriptor.getScopeForMemberResolution(), diff --git a/compiler/testData/diagnostics/tests/annotations/kt1860-positive.kt b/compiler/testData/diagnostics/tests/annotations/kt1860-positive.kt index bf00815f64a..afa0b2ca0a7 100644 --- a/compiler/testData/diagnostics/tests/annotations/kt1860-positive.kt +++ b/compiler/testData/diagnostics/tests/annotations/kt1860-positive.kt @@ -1,4 +1,4 @@ -annotation class test {} +annotation class test fun foo(test f : Int) {} @@ -8,4 +8,4 @@ var bar : Int = 1 val x : (Int) -> Int = {([test] x : Int) -> x} class Hello(test args: Any) { -} \ No newline at end of file +} diff --git a/compiler/testData/diagnostics/tests/annotations/kt1886annotationBody.kt b/compiler/testData/diagnostics/tests/annotations/kt1886annotationBody.kt new file mode 100644 index 00000000000..d298cdb4e4c --- /dev/null +++ b/compiler/testData/diagnostics/tests/annotations/kt1886annotationBody.kt @@ -0,0 +1,29 @@ +annotation class Annotation2() { + public val s: String = "" +} + +annotation class Annotation3() { + public fun foo() {} +} + +annotation class Annotation4() { + class Foo() {} +} + +annotation class Annotation5() { + class object {} +} + +annotation class Annotation6() { + {} +} + +annotation class Annotation1() {} + +annotation class Annotation7(val name: String) {} + +annotation class Annotation8(var name: String = "") {} + +annotation class Annotation9(name: String) + +annotation class Annotation10 diff --git a/compiler/testData/renderer/Classes.kt b/compiler/testData/renderer/Classes.kt index 9c44c5e2892..90667a8bb18 100644 --- a/compiler/testData/renderer/Classes.kt +++ b/compiler/testData/renderer/Classes.kt @@ -1,7 +1,6 @@ package rendererTest -annotation class TheAnnotation { -} +annotation class TheAnnotation [TheAnnotation] public open class TheClass { @@ -38,4 +37,4 @@ trait TheTrait { //internal trait TheTrait defined in rendererTest //internal abstract fun abstractFun() : Unit defined in rendererTest.TheTrait //class object : rendererTest.TheClass defined in rendererTest.TheTrait -//internal final fun classObjectFunction() : jet.Int defined in rendererTest.TheTrait. \ No newline at end of file +//internal final fun classObjectFunction() : jet.Int defined in rendererTest.TheTrait. diff --git a/compiler/tests/org/jetbrains/jet/checkers/JetDiagnosticsTestGenerated.java b/compiler/tests/org/jetbrains/jet/checkers/JetDiagnosticsTestGenerated.java index 2453b1f30b4..74dfbb6f151 100644 --- a/compiler/tests/org/jetbrains/jet/checkers/JetDiagnosticsTestGenerated.java +++ b/compiler/tests/org/jetbrains/jet/checkers/JetDiagnosticsTestGenerated.java @@ -567,6 +567,11 @@ public class JetDiagnosticsTestGenerated extends AbstractDiagnosticsTestWithEage doTest("compiler/testData/diagnostics/tests/annotations/kt1860-positive.kt"); } + @TestMetadata("kt1886annotationBody.kt") + public void testKt1886annotationBody() throws Exception { + doTest("compiler/testData/diagnostics/tests/annotations/kt1886annotationBody.kt"); + } + @TestMetadata("NonAnnotationClass.kt") public void testNonAnnotationClass() throws Exception { doTest("compiler/testData/diagnostics/tests/annotations/NonAnnotationClass.kt"); diff --git a/idea/testData/compilerMessages/k2js/lib/src/lib.zip b/idea/testData/compilerMessages/k2js/lib/src/lib.zip index c3eb830332716491e059fc1d2891628a4a7adcf8..77d73e6814c86feda4cfe29204477fb7c358eb0f 100644 GIT binary patch literal 218 zcmWIWW@Zs#U|`^2(5usR>@5E}*#pS)0b({F&dE&D%PvtpZphc5Ai#3qU))C3D`o!< zq|agD@e*Th@BR3G){V1sr1khtr~S#ecS%bWFPb>%tS+iMs@CS{ r(0y%o!or_S0p5&EA`H0g16m0L3Sbgudw@49$SDkrKxhJ_^T4tIHE}Yw diff --git a/idea/testData/debugger/annotation.kt b/idea/testData/debugger/annotation.kt deleted file mode 100644 index 2be7e7d2703..00000000000 --- a/idea/testData/debugger/annotation.kt +++ /dev/null @@ -1,5 +0,0 @@ -annotation class A { - fun foo() { - "" // A - } -} diff --git a/idea/tests/org/jetbrains/jet/plugin/debugger/JetPositionManagerTest.java b/idea/tests/org/jetbrains/jet/plugin/debugger/JetPositionManagerTest.java index 408f509bd40..82c58b3c196 100644 --- a/idea/tests/org/jetbrains/jet/plugin/debugger/JetPositionManagerTest.java +++ b/idea/tests/org/jetbrains/jet/plugin/debugger/JetPositionManagerTest.java @@ -61,10 +61,6 @@ public class JetPositionManagerTest extends PositionManagerTestCase { doMultiTest("multiFileNamespace/a.kt", "multiFileNamespace/b.kt"); } - public void testAnnotation() { - doTest(); - } - public void testAnonymousFunction() { doTest(); } diff --git a/idea/tests/org/jetbrains/jet/plugin/stubs/JetStubsTest.java b/idea/tests/org/jetbrains/jet/plugin/stubs/JetStubsTest.java index 39123605a96..d39cadffcde 100644 --- a/idea/tests/org/jetbrains/jet/plugin/stubs/JetStubsTest.java +++ b/idea/tests/org/jetbrains/jet/plugin/stubs/JetStubsTest.java @@ -152,7 +152,7 @@ public class JetStubsTest extends LightCodeInsightFixtureTestCase { } public void testAnnotationClass() { - doBuildTest("annotation class Test {}", + doBuildTest("annotation class Test", "PsiJetFileStubImpl[package=]\n" + " CLASS:PsiJetClassStubImpl[isAnnotation name=Test fqn=Test superNames=[]]\n" + " TYPE_PARAMETER_LIST:PsiJetTypeParameterListStubImpl\n"); diff --git a/js/js.libraries/src/core/annotations.kt b/js/js.libraries/src/core/annotations.kt index f1a1793fc7a..1a86d5a9095 100644 --- a/js/js.libraries/src/core/annotations.kt +++ b/js/js.libraries/src/core/annotations.kt @@ -1,8 +1,8 @@ package js; native -public annotation class native(name : String = "") {} +public annotation class native(name : String = "") native -public annotation class library(name : String = "") {} +public annotation class library(name : String = "") native -public annotation class enumerable() {} \ No newline at end of file +public annotation class enumerable() diff --git a/js/js.libraries/src/junit/core.kt b/js/js.libraries/src/junit/core.kt index 5d9b66e5028..bc11c43b19e 100644 --- a/js/js.libraries/src/junit/core.kt +++ b/js/js.libraries/src/junit/core.kt @@ -1,5 +1,5 @@ package org.junit; native -public annotation class Test(name : String = "") {} +public annotation class Test(name : String = "") diff --git a/libraries/sandbox/extensionTraits/src/kotlin2/BindExtensionFunctions.kt b/libraries/sandbox/extensionTraits/src/kotlin2/BindExtensionFunctions.kt index 148d7e90b7b..74670e4b331 100644 --- a/libraries/sandbox/extensionTraits/src/kotlin2/BindExtensionFunctions.kt +++ b/libraries/sandbox/extensionTraits/src/kotlin2/BindExtensionFunctions.kt @@ -5,8 +5,7 @@ import jet.runtime.ArrayIterator /** * Annotates a class used to implement extension functions */ -annotation class extension() { -} +annotation class extension() /** * Defines the extension functions on a List diff --git a/libraries/stdlib/src/js/Annotations.kt b/libraries/stdlib/src/js/Annotations.kt index 2be217ef843..3cd8d930ffd 100644 --- a/libraries/stdlib/src/js/Annotations.kt +++ b/libraries/stdlib/src/js/Annotations.kt @@ -4,10 +4,10 @@ package js * This annotation marks code as being a native JavaScript expression */ native -annotation class native(name : String = "") {} +annotation class native(name : String = "") /** * Represents a function in the standard library */ native -annotation class library(name : String = "") {} \ No newline at end of file +annotation class library(name : String = "")