Support nested classes in annotation classes

#KT-16962 In Progress
This commit is contained in:
Alexander Udalov
2017-12-28 16:51:42 +01:00
parent 5947b4eb8e
commit a46a2b9b1c
20 changed files with 207 additions and 34 deletions
@@ -23,14 +23,13 @@ import org.jetbrains.kotlin.fileClasses.isInsideJvmMultifileClassFile
import org.jetbrains.kotlin.psi.KtDeclaration
import org.jetbrains.kotlin.psi.KtProperty
import org.jetbrains.kotlin.resolve.BindingContext
import org.jetbrains.kotlin.resolve.checkers.SimpleDeclarationChecker
import org.jetbrains.kotlin.resolve.DescriptorToSourceUtils
import org.jetbrains.kotlin.resolve.DescriptorUtils
import org.jetbrains.kotlin.resolve.checkers.SimpleDeclarationChecker
import org.jetbrains.kotlin.resolve.jvm.annotations.findJvmFieldAnnotation
import org.jetbrains.kotlin.resolve.jvm.checkers.JvmFieldApplicabilityChecker.Problem.*
import org.jetbrains.kotlin.resolve.jvm.diagnostics.ErrorsJvm
class JvmFieldApplicabilityChecker : SimpleDeclarationChecker {
internal enum class Problem(val errorMessage: String) {
@@ -83,7 +82,7 @@ class JvmFieldApplicabilityChecker : SimpleDeclarationChecker {
val containingClass = containingDeclaration as? ClassDescriptor ?: return false
if (!DescriptorUtils.isCompanionObject(containingClass)) return false
val outerClassForObject = containingClass.containingDeclaration as? ClassDescriptor ?: return false
return DescriptorUtils.isInterface(outerClassForObject)
val outerClassKind = (containingClass.containingDeclaration as? ClassDescriptor)?.kind
return outerClassKind == ClassKind.INTERFACE || outerClassKind == ClassKind.ANNOTATION_CLASS
}
}
@@ -225,7 +225,7 @@ public interface Errors {
DiagnosticFactory0<KtParameter> VAR_ANNOTATION_PARAMETER = DiagnosticFactory0.create(ERROR, VAL_OR_VAR_NODE);
DiagnosticFactory0<KtCallExpression> ANNOTATION_CLASS_CONSTRUCTOR_CALL = DiagnosticFactory0.create(ERROR);
DiagnosticFactory1<KtAnnotationEntry, DeclarationDescriptor> NOT_AN_ANNOTATION_CLASS = DiagnosticFactory1.create(ERROR);
DiagnosticFactory0<PsiElement> ANNOTATION_CLASS_WITH_BODY = DiagnosticFactory0.create(ERROR);
DiagnosticFactory0<PsiElement> ANNOTATION_CLASS_MEMBER = DiagnosticFactory0.create(ERROR);
DiagnosticFactory0<KtTypeReference> INVALID_TYPE_OF_ANNOTATION_MEMBER = DiagnosticFactory0.create(ERROR);
DiagnosticFactory0<KtTypeReference> NULLABLE_TYPE_OF_ANNOTATION_MEMBER = DiagnosticFactory0.create(ERROR);
DiagnosticFactory0<KtExpression> ANNOTATION_PARAMETER_MUST_BE_CONST = DiagnosticFactory0.create(ERROR);
@@ -790,7 +790,7 @@ public class DefaultErrorMessages {
MAP.put(VAR_ANNOTATION_PARAMETER, "An annotation parameter cannot be 'var'");
MAP.put(ANNOTATION_CLASS_CONSTRUCTOR_CALL, "Annotation class cannot be instantiated");
MAP.put(NOT_AN_ANNOTATION_CLASS, "''{0}'' is not an annotation class", NAME);
MAP.put(ANNOTATION_CLASS_WITH_BODY, "Body is not allowed for annotation class");
MAP.put(ANNOTATION_CLASS_MEMBER, "Members are not allowed in annotation class");
MAP.put(INVALID_TYPE_OF_ANNOTATION_MEMBER, "Invalid type of annotation member");
MAP.put(NULLABLE_TYPE_OF_ANNOTATION_MEMBER, "An annotation parameter cannot be nullable");
MAP.put(ANNOTATION_PARAMETER_MUST_BE_CONST, "An annotation parameter must be a compile-time constant");
@@ -449,7 +449,7 @@ class DeclarationsChecker(
}
}
classDescriptor.kind == ClassKind.ANNOTATION_CLASS -> {
checkAnnotationClassWithBody(aClass)
checkAnnotationClassMembers(aClass)
checkValOnAnnotationParameter(aClass)
}
aClass is KtEnumEntry -> checkEnumEntry(aClass, classDescriptor)
@@ -524,8 +524,13 @@ class DeclarationsChecker(
}
}
private fun checkAnnotationClassWithBody(classOrObject: KtClassOrObject) {
classOrObject.getBody()?.let { trace.report(ANNOTATION_CLASS_WITH_BODY.on(it)) }
private fun checkAnnotationClassMembers(classOrObject: KtClassOrObject) {
for (declaration in classOrObject.declarations) {
if (declaration !is KtClassOrObject ||
!languageVersionSettings.supportsFeature(LanguageFeature.NestedClassesInAnnotations)) {
trace.report(ANNOTATION_CLASS_MEMBER.on(declaration))
}
}
}
private fun checkValOnAnnotationParameter(aClass: KtClass) {
@@ -158,7 +158,7 @@ object ModifierCheckerCore {
PROTECTED_KEYWORD to always(CLASS_ONLY, LOCAL_CLASS, ENUM_CLASS, COMPANION_OBJECT),
INTERNAL_KEYWORD to always(CLASS_ONLY, LOCAL_CLASS, OBJECT, OBJECT_LITERAL, ENUM_CLASS, ENUM_ENTRY, FILE),
PRIVATE_KEYWORD to always(CLASS_ONLY, LOCAL_CLASS, OBJECT, OBJECT_LITERAL, INTERFACE, ENUM_CLASS, ENUM_ENTRY, FILE),
COMPANION_KEYWORD to always(CLASS_ONLY, ENUM_CLASS, INTERFACE),
COMPANION_KEYWORD to always(CLASS_ONLY, INTERFACE, ENUM_CLASS, ANNOTATION_CLASS),
FINAL_KEYWORD to always(CLASS_ONLY, LOCAL_CLASS, OBJECT, OBJECT_LITERAL, ENUM_CLASS, ENUM_ENTRY, ANNOTATION_CLASS, FILE),
VARARG_KEYWORD to always(CONSTRUCTOR, FUNCTION, CLASS)
)
@@ -0,0 +1,10 @@
// !LANGUAGE: +NestedClassesInAnnotations
annotation class Foo(val kind: Kind) {
enum class Kind { FAIL, OK }
}
@Foo(Foo.Kind.OK)
fun box(): String {
return Foo.Kind.OK.name
}
@@ -1,28 +1,28 @@
annotation class Annotation2() <!ANNOTATION_CLASS_WITH_BODY!>{
public val s: String = ""
}<!>
annotation class Annotation2() {
<!ANNOTATION_CLASS_MEMBER!>public val s: String = ""<!>
}
annotation class Annotation3() <!ANNOTATION_CLASS_WITH_BODY!>{
public fun foo() {}
}<!>
annotation class Annotation3() {
<!ANNOTATION_CLASS_MEMBER!>public fun foo() {}<!>
}
annotation class Annotation4() <!ANNOTATION_CLASS_WITH_BODY!>{
class Foo() {}
}<!>
annotation class Annotation4() {
<!ANNOTATION_CLASS_MEMBER!>class Foo() {}<!>
}
annotation class Annotation5() <!ANNOTATION_CLASS_WITH_BODY!>{
<!WRONG_MODIFIER_CONTAINING_DECLARATION!>companion<!> object {}
}<!>
annotation class Annotation5() {
companion <!ANNOTATION_CLASS_MEMBER!>object<!> {}
}
annotation class Annotation6() <!ANNOTATION_CLASS_WITH_BODY!>{
init {}
}<!>
annotation class Annotation6() {
<!ANNOTATION_CLASS_MEMBER!>init {}<!>
}
annotation class Annotation1() <!ANNOTATION_CLASS_WITH_BODY!>{}<!>
annotation class Annotation1() {}
annotation class Annotation7(val name: String) <!ANNOTATION_CLASS_WITH_BODY!>{}<!>
annotation class Annotation7(val name: String) {}
annotation class Annotation8(<!VAR_ANNOTATION_PARAMETER!>var<!> name: String = "") <!ANNOTATION_CLASS_WITH_BODY!>{}<!>
annotation class Annotation8(<!VAR_ANNOTATION_PARAMETER!>var<!> name: String = "") {}
annotation class Annotation9(val name: String)
@@ -0,0 +1,23 @@
// !LANGUAGE: +NestedClassesInAnnotations
annotation class Foo {
class Nested
<!WRONG_MODIFIER_CONTAINING_DECLARATION!>inner<!> class Inner
enum class E { A, B }
object O
interface I
annotation class Anno(val e: E)
companion object {
val x = 1
const val y = ""
}
<!ANNOTATION_CLASS_MEMBER!>constructor(<!UNUSED_PARAMETER!>s<!>: Int) {}<!>
<!ANNOTATION_CLASS_MEMBER!>init {}<!>
<!ANNOTATION_CLASS_MEMBER!>fun function() {}<!>
<!ANNOTATION_CLASS_MEMBER!>val property get() = Unit<!>
}
@@ -0,0 +1,75 @@
package
public final annotation class Foo : kotlin.Annotation {
public constructor Foo(/*0*/ s: kotlin.Int)
public final val property: kotlin.Unit
public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
public final fun function(): kotlin.Unit
public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
public final annotation class Anno : kotlin.Annotation {
public constructor Anno(/*0*/ e: Foo.E)
public final val e: Foo.E
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
}
public companion object Companion {
private constructor Companion()
public final val x: kotlin.Int = 1
public const final val y: kotlin.String = ""
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
}
public final enum class E : kotlin.Enum<Foo.E> {
enum entry A
enum entry B
private constructor E()
public final override /*1*/ /*fake_override*/ val name: kotlin.String
public final override /*1*/ /*fake_override*/ val ordinal: kotlin.Int
protected final override /*1*/ /*fake_override*/ fun clone(): kotlin.Any
public final override /*1*/ /*fake_override*/ fun compareTo(/*0*/ other: Foo.E): kotlin.Int
public final override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
protected/*protected and package*/ final override /*1*/ /*fake_override*/ /*isHiddenForResolutionEverywhereBesideSupercalls*/ fun finalize(): kotlin.Unit
public final override /*1*/ /*fake_override*/ /*isHiddenForResolutionEverywhereBesideSupercalls*/ fun getDeclaringClass(): java.lang.Class<Foo.E!>!
public final 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): Foo.E
public final /*synthesized*/ fun values(): kotlin.Array<Foo.E>
}
public interface 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
}
public final inner class Inner {
public constructor Inner()
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
}
public final class Nested {
public constructor Nested()
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
}
public 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
}
}
@@ -25,9 +25,9 @@ enum class<!SYNTAX!><!> {
}
annotation class<!SYNTAX!><!> <!ANNOTATION_CLASS_WITH_BODY!>{
annotation class<!SYNTAX!><!> {
}<!>
}
class Outer {
<!FUNCTION_DECLARATION_WITH_NO_NAME!>fun ()<!> {
@@ -52,9 +52,9 @@ class Outer {
}<!>
<!REDECLARATION!>annotation class<!SYNTAX!><!> <!ANNOTATION_CLASS_WITH_BODY!>{
<!REDECLARATION!>annotation class<!SYNTAX!><!> {
}<!><!>
}<!>
}
fun outerFun() {
@@ -23,4 +23,4 @@ object<!SYNTAX!><!> {
enum class<!SYNTAX!><!> {}
annotation class<!SYNTAX!><!> <!ANNOTATION_CLASS_WITH_BODY!>{}<!>
annotation class<!SYNTAX!><!> {}
@@ -1,4 +1,6 @@
// !LANGUAGE: +NestedClassesInAnnotations
// !DIAGNOSTICS: -UNUSED_PARAMETER -UNUSED_VARIABLE
<!WRONG_ANNOTATION_TARGET!>@kotlin.jvm.JvmField<!>
fun foo() {
<!WRONG_ANNOTATION_TARGET!>@kotlin.jvm.JvmField<!> val x = "A"
@@ -110,10 +112,17 @@ open class KKK : K {
override final val j: Int = 0
}
annotation class L {
companion object {
<!INAPPLICABLE_JVM_FIELD!>@JvmField<!>
var c = 3
}
}
object O {
@JvmField
val c = 3
}
<!INAPPLICABLE_JVM_FIELD!>@JvmField<!>
private val private = 3
private val private = 3
@@ -99,6 +99,21 @@ public open class KKK : K {
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
}
public final annotation class L : kotlin.Annotation {
public constructor L()
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
public companion object Companion {
private constructor Companion()
@kotlin.jvm.JvmField public final var 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
}
}
public object O {
private constructor O()
@kotlin.jvm.JvmField public final val c: kotlin.Int = 3
@@ -117,6 +117,12 @@ public class IrBlackBoxCodegenTestGenerated extends AbstractIrBlackBoxCodegenTes
doTest(fileName);
}
@TestMetadata("nestedClassesInAnnotations.kt")
public void testNestedClassesInAnnotations() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/annotations/nestedClassesInAnnotations.kt");
doTest(fileName);
}
@TestMetadata("parameterAnnotationInDefaultImpls.kt")
public void testParameterAnnotationInDefaultImpls() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/annotations/parameterAnnotationInDefaultImpls.kt");
@@ -1151,6 +1151,12 @@ public class DiagnosticsTestGenerated extends AbstractDiagnosticsTest {
doTest(fileName);
}
@TestMetadata("nestedClassesInAnnotations.kt")
public void testNestedClassesInAnnotations() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/annotations/nestedClassesInAnnotations.kt");
doTest(fileName);
}
@TestMetadata("noNameProperty.kt")
public void testNoNameProperty() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/annotations/noNameProperty.kt");
@@ -1151,6 +1151,12 @@ public class DiagnosticsUsingJavacTestGenerated extends AbstractDiagnosticsUsing
doTest(fileName);
}
@TestMetadata("nestedClassesInAnnotations.kt")
public void testNestedClassesInAnnotations() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/annotations/nestedClassesInAnnotations.kt");
doTest(fileName);
}
@TestMetadata("noNameProperty.kt")
public void testNoNameProperty() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/annotations/noNameProperty.kt");
@@ -117,6 +117,12 @@ public class BlackBoxCodegenTestGenerated extends AbstractBlackBoxCodegenTest {
doTest(fileName);
}
@TestMetadata("nestedClassesInAnnotations.kt")
public void testNestedClassesInAnnotations() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/annotations/nestedClassesInAnnotations.kt");
doTest(fileName);
}
@TestMetadata("parameterAnnotationInDefaultImpls.kt")
public void testParameterAnnotationInDefaultImpls() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/annotations/parameterAnnotationInDefaultImpls.kt");
@@ -129,6 +129,12 @@ public class LightAnalysisModeTestGenerated extends AbstractLightAnalysisModeTes
doTest(fileName);
}
@TestMetadata("nestedClassesInAnnotations.kt")
public void testNestedClassesInAnnotations() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/annotations/nestedClassesInAnnotations.kt");
doTest(fileName);
}
@TestMetadata("parameterAnnotationInDefaultImpls.kt")
public void testParameterAnnotationInDefaultImpls() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/annotations/parameterAnnotationInDefaultImpls.kt");
@@ -76,6 +76,7 @@ enum class LanguageFeature(
ProhibitInnerClassesOfGenericClassExtendingThrowable(KOTLIN_1_3),
ProperVisibilityForCompanionObjectInstanceField(KOTLIN_1_3),
ProperForInArrayLoopRangeVariableAssignmentSemantic(KOTLIN_1_3),
NestedClassesInAnnotations(KOTLIN_1_3),
StrictJavaNullabilityAssertions(sinceVersion = null, defaultState = State.DISABLED),
@@ -201,6 +201,12 @@ public class JsCodegenBoxTestGenerated extends AbstractJsCodegenBoxTest {
throw new AssertionError("Looks like this test can be unmuted. Remove IGNORE_BACKEND directive for that.");
}
@TestMetadata("nestedClassesInAnnotations.kt")
public void testNestedClassesInAnnotations() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/annotations/nestedClassesInAnnotations.kt");
doTest(fileName);
}
@TestMetadata("parameterAnnotationInDefaultImpls.kt")
public void testParameterAnnotationInDefaultImpls() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/annotations/parameterAnnotationInDefaultImpls.kt");