Support annotations on class type parameters

#KT-43714
This commit is contained in:
Mikhael Bogdanov
2021-05-21 20:48:21 +02:00
parent 9091ca7b51
commit a8186d19d6
18 changed files with 222 additions and 57 deletions
@@ -990,9 +990,15 @@ public class FirOldFrontendDiagnosticsTestGenerated extends AbstractFirDiagnosti
}
@Test
@TestMetadata("AnnotationForClassTypeParameter.kt")
public void testAnnotationForClassTypeParameter() throws Exception {
runTest("compiler/testData/diagnostics/tests/annotations/AnnotationForClassTypeParameter.kt");
@TestMetadata("AnnotationForClassTypeParameter_15.kt")
public void testAnnotationForClassTypeParameter_15() throws Exception {
runTest("compiler/testData/diagnostics/tests/annotations/AnnotationForClassTypeParameter_15.kt");
}
@Test
@TestMetadata("AnnotationForClassTypeParameter_16.kt")
public void testAnnotationForClassTypeParameter_16() throws Exception {
runTest("compiler/testData/diagnostics/tests/annotations/AnnotationForClassTypeParameter_16.kt");
}
@Test
@@ -990,9 +990,15 @@ public class FirOldFrontendDiagnosticsWithLightTreeTestGenerated extends Abstrac
}
@Test
@TestMetadata("AnnotationForClassTypeParameter.kt")
public void testAnnotationForClassTypeParameter() throws Exception {
runTest("compiler/testData/diagnostics/tests/annotations/AnnotationForClassTypeParameter.kt");
@TestMetadata("AnnotationForClassTypeParameter_15.kt")
public void testAnnotationForClassTypeParameter_15() throws Exception {
runTest("compiler/testData/diagnostics/tests/annotations/AnnotationForClassTypeParameter_15.kt");
}
@Test
@TestMetadata("AnnotationForClassTypeParameter_16.kt")
public void testAnnotationForClassTypeParameter_16() throws Exception {
runTest("compiler/testData/diagnostics/tests/annotations/AnnotationForClassTypeParameter_16.kt");
}
@Test
@@ -518,9 +518,10 @@ class DeclarationsChecker(
}
private fun checkTypeParameters(typeParameterListOwner: KtTypeParameterListOwner) {
// TODO: Support annotation for type parameters
for (jetTypeParameter in typeParameterListOwner.typeParameters) {
AnnotationResolverImpl.reportUnsupportedAnnotationForTypeParameter(jetTypeParameter, trace)
if (!languageVersionSettings.supportsFeature(LanguageFeature.ClassTypeParameterAnnotations)) {
AnnotationResolverImpl.reportUnsupportedAnnotationForTypeParameter(jetTypeParameter, trace)
}
trace.get(TYPE_PARAMETER, jetTypeParameter)?.let { DescriptorResolver.checkConflictingUpperBounds(trace, it, jetTypeParameter) }
}
@@ -252,10 +252,32 @@ public class LazyClassDescriptor extends ClassDescriptorBase implements ClassDes
List<KtTypeParameter> typeParameters = typeParameterList.getParameters();
if (typeParameters.isEmpty()) return Collections.emptyList();
boolean supportClassTypeParameterAnnotations = c.getLanguageVersionSettings().supportsFeature(LanguageFeature.ClassTypeParameterAnnotations);
List<TypeParameterDescriptor> parameters = new ArrayList<>(typeParameters.size());
for (int i = 0; i < typeParameters.size(); i++) {
parameters.add(new LazyTypeParameterDescriptor(c, this, typeParameters.get(i), i));
KtTypeParameter parameter = typeParameters.get(i);
Annotations lazyAnnotations;
if (supportClassTypeParameterAnnotations) {
lazyAnnotations = new LazyAnnotations(
new LazyAnnotationsContext(
c.getAnnotationResolver(),
storageManager,
c.getTrace()
) {
@NotNull
@Override
public LexicalScope getScope() {
return getOuterScope();
}
},
parameter.getAnnotationEntries()
);
} else {
lazyAnnotations = Annotations.Companion.getEMPTY();
}
parameters.add(new LazyTypeParameterDescriptor(c, this, parameter, lazyAnnotations, i));
}
return parameters;
@@ -18,6 +18,7 @@ package org.jetbrains.kotlin.resolve.lazy.descriptors;
import kotlin.collections.CollectionsKt;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.kotlin.descriptors.annotations.Annotations;
import org.jetbrains.kotlin.descriptors.impl.AbstractLazyTypeParameterDescriptor;
import org.jetbrains.kotlin.diagnostics.Errors;
import org.jetbrains.kotlin.lexer.KtTokens;
@@ -43,11 +44,13 @@ public class LazyTypeParameterDescriptor extends AbstractLazyTypeParameterDescri
@NotNull LazyClassContext c,
@NotNull LazyClassDescriptor containingDeclaration,
@NotNull KtTypeParameter typeParameter,
@NotNull Annotations annotations,
int index
) {
super(
c.getStorageManager(),
containingDeclaration,
annotations,
typeParameter.getNameAsSafeName(),
typeParameter.getVariance(),
typeParameter.hasModifier(KtTokens.REIFIED_KEYWORD),
@@ -3,6 +3,7 @@
// IGNORE_BACKEND_FIR: JVM_IR
// TARGET_BACKEND: JVM
// EMIT_JVM_TYPE_ANNOTATIONS
// !LANGUAGE: +ClassTypeParameterAnnotations
// JVM_TARGET: 1.8
// WITH_REFLECT
// FULL_JDK
@@ -29,41 +30,36 @@ class SimpleClass
interface Generic<G>
class GenericClass<G>
@Suppress("UNSUPPORTED")
class SimpleParameter<@TypeParameterAnn @TypeParameterAnnBinary T> {}
@Suppress("UNSUPPORTED")
class InterfaceBound<@TypeParameterAnn T : @TypeAnn("Simple") Simple> {}
@Suppress("UNSUPPORTED")
class ClassBound<@TypeParameterAnn T : @TypeAnn("Simple") SimpleClass>
class InterfaceBoundGeneric<T : @TypeAnn("Generic") Generic<@TypeAnn("Simple") Simple>> {}
class ClassBoundGeneric<T : @TypeAnn("GenericClass") GenericClass<@TypeAnn("SimpleClass") SimpleClass>>
@Suppress("UNSUPPORTED")
class TypeParameterAsBound<Y, @TypeParameterAnn T : @TypeAnn("Y as Bound") Y>
fun box(): String {
//foo
// checkTypeParameterAnnotation(
// SimpleParameter::class.java.typeParameters.single(),
// "T",
// "@foo.TypeParameterAnn()",
// "foo"
// )
checkTypeParameterAnnotation(
SimpleParameter::class.java.typeParameters.single(),
"T",
"@foo.TypeParameterAnn()",
"foo"
)
//interfaceBound
val interfaceBound = InterfaceBound::class.java
// checkTypeParameterAnnotation(
// InterfaceBound.typeParameters.single(),
// "T",
// "@foo.TypeParameterAnn()",
// "interfaceBound type parameter"
// )
checkTypeParameterAnnotation(
interfaceBound.typeParameters.single(),
"T",
"@foo.TypeParameterAnn()",
"interfaceBound type parameter"
)
checkTypeAnnotation(
interfaceBound.typeParameters.single().annotatedBounds.single(),
@@ -74,12 +70,12 @@ fun box(): String {
//classBound
val classBound = ClassBound::class.java
// checkTypeParameterAnnotation(
// classBound.typeParameters.single(),
// "T",
// "@foo.TypeParameterAnn()",
// "classBound type parameter"
// )
checkTypeParameterAnnotation(
classBound.typeParameters.single(),
"T",
"@foo.TypeParameterAnn()",
"classBound type parameter"
)
checkTypeAnnotation(
classBound.typeParameters.single().annotatedBounds.single(),
@@ -91,12 +87,12 @@ fun box(): String {
//interfaceBoundGeneric
val interfaceBoundGeneric = InterfaceBoundGeneric::class.java
// checkTypeAnnotation(
// interfaceBoundGeneric.typeParameters.single().annotatedBounds.single(),
// "foo.Generic<foo.Simple>",
// "@foo.TypeAnn(name=Generic)",
// "interfaceBoundGeneric bound"
// )
checkTypeAnnotation(
interfaceBoundGeneric.typeParameters.single().annotatedBounds.single(),
"foo.Generic<foo.Simple>",
"@foo.TypeAnn(name=Generic)",
"interfaceBoundGeneric bound"
)
checkTypeAnnotation(
(interfaceBoundGeneric.typeParameters.single().annotatedBounds.single() as AnnotatedParameterizedType).getAnnotatedActualTypeArguments().single(),
@@ -124,12 +120,12 @@ fun box(): String {
//typeParameterTypeParameterBound
val typeParameterTypeParameterBound = TypeParameterAsBound::class.java
// checkTypeParameterAnnotation(
// typeParameterTypeParameterBound.typeParameters[1]!!,
// "T",
// "@foo.TypeParameterAnn()",
// "typeParameterTypeParameterBound type parameter"
// )
checkTypeParameterAnnotation(
typeParameterTypeParameterBound.typeParameters[1]!!,
"T",
"@foo.TypeParameterAnn()",
"typeParameterTypeParameterBound type parameter"
)
// Works on JDK 15
// checkTypeAnnotation(
// typeParameterTypeParameterBound.typeParameters[1]!!.annotatedBounds.single(),
@@ -1,3 +1,4 @@
// !LANGUAGE: -ClassTypeParameterAnnotations
annotation class A1
annotation class A2(val some: Int = 12)
@@ -1,3 +1,4 @@
// !LANGUAGE: -ClassTypeParameterAnnotations
annotation class A1
annotation class A2(val some: Int = 12)
@@ -0,0 +1,24 @@
// !LANGUAGE: +ClassTypeParameterAnnotations
annotation class A1
annotation class A2(val some: Int = 12)
@Target(AnnotationTarget.TYPE)
annotation class TA1
@Target(AnnotationTarget.TYPE_PARAMETER)
annotation class TA2(val some: Int = 12)
class TopLevelClass<@A1 @A2(3) @A2 @A1(12) @A2("Test") T> {
class InnerClass<@A1 @A2(3) @A2 @A1(12) @A2("Test") T> {
fun test() {
class InFun<@A1 @A2(3) @A2 @A1(12) @A2("Test") T>
}
}
}
class TTopLevelClass<@TA1 @TA2(3) @TA2 @TA1(12) @TA2("Test") T> {
class TInnerClass<@TA1 @TA2(3) @TA2 @TA1(12) @TA2("Test") T> {
fun test() {
class TInFun<@TA1 @TA2(3) @TA2 @TA1(12) @TA2("Test") T>
}
}
}
@@ -0,0 +1,25 @@
// !LANGUAGE: +ClassTypeParameterAnnotations
annotation class A1
annotation class A2(val some: Int = 12)
@Target(AnnotationTarget.TYPE)
annotation class TA1
@Target(AnnotationTarget.TYPE_PARAMETER)
annotation class TA2(val some: Int = 12)
class TopLevelClass<<!WRONG_ANNOTATION_TARGET!>@A1<!> <!WRONG_ANNOTATION_TARGET!>@A2(3)<!> <!REPEATED_ANNOTATION, WRONG_ANNOTATION_TARGET!>@A2<!> <!REPEATED_ANNOTATION, WRONG_ANNOTATION_TARGET!>@A1(<!TOO_MANY_ARGUMENTS!>12<!>)<!> <!REPEATED_ANNOTATION, WRONG_ANNOTATION_TARGET!>@A2(<!TYPE_MISMATCH!>"Test"<!>)<!> T> {
class InnerClass<<!WRONG_ANNOTATION_TARGET!>@A1<!> <!WRONG_ANNOTATION_TARGET!>@A2(3)<!> <!REPEATED_ANNOTATION, WRONG_ANNOTATION_TARGET!>@A2<!> <!REPEATED_ANNOTATION, WRONG_ANNOTATION_TARGET!>@A1(<!TOO_MANY_ARGUMENTS!>12<!>)<!> <!REPEATED_ANNOTATION, WRONG_ANNOTATION_TARGET!>@A2(<!TYPE_MISMATCH!>"Test"<!>)<!> T> {
fun test() {
class InFun<<!WRONG_ANNOTATION_TARGET!>@A1<!> <!WRONG_ANNOTATION_TARGET!>@A2(3)<!> <!REPEATED_ANNOTATION, WRONG_ANNOTATION_TARGET!>@A2<!> <!REPEATED_ANNOTATION, WRONG_ANNOTATION_TARGET!>@A1(<!TOO_MANY_ARGUMENTS!>12<!>)<!> <!REPEATED_ANNOTATION, WRONG_ANNOTATION_TARGET!>@A2(<!TYPE_MISMATCH!>"Test"<!>)<!> T>
}
}
}
class TTopLevelClass<<!WRONG_ANNOTATION_TARGET!>@TA1<!> @TA2(3) <!REPEATED_ANNOTATION!>@TA2<!> <!REPEATED_ANNOTATION, WRONG_ANNOTATION_TARGET!>@TA1(<!TOO_MANY_ARGUMENTS!>12<!>)<!> <!REPEATED_ANNOTATION!>@TA2(<!TYPE_MISMATCH!>"Test"<!>)<!> T> {
class TInnerClass<<!WRONG_ANNOTATION_TARGET!>@TA1<!> @TA2(3) <!REPEATED_ANNOTATION!>@TA2<!> <!REPEATED_ANNOTATION, WRONG_ANNOTATION_TARGET!>@TA1(<!TOO_MANY_ARGUMENTS!>12<!>)<!> <!REPEATED_ANNOTATION!>@TA2(<!TYPE_MISMATCH!>"Test"<!>)<!> T> {
fun test() {
class TInFun<<!WRONG_ANNOTATION_TARGET!>@TA1<!> @TA2(3) <!REPEATED_ANNOTATION!>@TA2<!> <!REPEATED_ANNOTATION, WRONG_ANNOTATION_TARGET!>@TA1(<!TOO_MANY_ARGUMENTS!>12<!>)<!> <!REPEATED_ANNOTATION!>@TA2(<!TYPE_MISMATCH!>"Test"<!>)<!> T>
}
}
}
@@ -0,0 +1,62 @@
package
public final annotation class A1 : kotlin.Annotation {
public constructor A1()
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 annotation class A2 : kotlin.Annotation {
public constructor A2(/*0*/ some: kotlin.Int = ...)
public final val some: 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
}
@kotlin.annotation.Target(allowedTargets = {AnnotationTarget.TYPE}) public final annotation class TA1 : kotlin.Annotation {
public constructor TA1()
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
}
@kotlin.annotation.Target(allowedTargets = {AnnotationTarget.TYPE_PARAMETER}) public final annotation class TA2 : kotlin.Annotation {
public constructor TA2(/*0*/ some: kotlin.Int = ...)
public final val some: 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 final class TTopLevelClass</*0*/ @TA1 @TA2(some = 3) @TA2 @TA1 @TA2(some = "Test") T> {
public constructor TTopLevelClass</*0*/ @TA1 @TA2(some = 3) @TA2 @TA1 @TA2(some = "Test") T>()
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 TInnerClass</*0*/ @TA1 @TA2(some = 3) @TA2 @TA1 @TA2(some = "Test") T> {
public constructor TInnerClass</*0*/ @TA1 @TA2(some = 3) @TA2 @TA1 @TA2(some = "Test") T>()
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 final fun test(): kotlin.Unit
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
}
}
public final class TopLevelClass</*0*/ @A1 @A2(some = 3) @A2 @A1 @A2(some = "Test") T> {
public constructor TopLevelClass</*0*/ @A1 @A2(some = 3) @A2 @A1 @A2(some = "Test") T>()
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 InnerClass</*0*/ @A1 @A2(some = 3) @A2 @A1 @A2(some = "Test") T> {
public constructor InnerClass</*0*/ @A1 @A2(some = 3) @A2 @A1 @A2(some = "Test") T>()
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 final fun test(): kotlin.Unit
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
}
}
@@ -990,9 +990,15 @@ public class DiagnosticTestGenerated extends AbstractDiagnosticTest {
}
@Test
@TestMetadata("AnnotationForClassTypeParameter.kt")
public void testAnnotationForClassTypeParameter() throws Exception {
runTest("compiler/testData/diagnostics/tests/annotations/AnnotationForClassTypeParameter.kt");
@TestMetadata("AnnotationForClassTypeParameter_15.kt")
public void testAnnotationForClassTypeParameter_15() throws Exception {
runTest("compiler/testData/diagnostics/tests/annotations/AnnotationForClassTypeParameter_15.kt");
}
@Test
@TestMetadata("AnnotationForClassTypeParameter_16.kt")
public void testAnnotationForClassTypeParameter_16() throws Exception {
runTest("compiler/testData/diagnostics/tests/annotations/AnnotationForClassTypeParameter_16.kt");
}
@Test
@@ -208,6 +208,7 @@ enum class LanguageFeature(
SuspendFunctionAsSupertype(KOTLIN_1_6),
UnrestrictedBuilderInference(KOTLIN_1_6),
ProperTypeInferenceConstraintsProcessing(KOTLIN_1_6, kind = BUG_FIX),
ClassTypeParameterAnnotations(KOTLIN_1_6),
// Temporarily disabled, see KT-27084/KT-22379
SoundSmartcastFromLoopConditionForLoopAssignedVariables(sinceVersion = null, kind = BUG_FIX),
@@ -36,13 +36,14 @@ class LazyJavaTypeParameterDescriptor(
) : AbstractLazyTypeParameterDescriptor(
c.storageManager,
containingDeclaration,
LazyJavaAnnotations(c, javaTypeParameter),
javaTypeParameter.name,
Variance.INVARIANT,
/* isReified = */ false,
/* isReified = */
false,
index,
SourceElement.NO_SOURCE, c.components.supertypeLoopChecker
SourceElement.NO_SOURCE, c.components.supertypeLoopChecker,
) {
override val annotations = LazyJavaAnnotations(c, javaTypeParameter)
override fun resolveUpperBounds(): List<KotlinType> {
return computeNotEnhancedBounds()
@@ -29,6 +29,7 @@ public abstract class AbstractLazyTypeParameterDescriptor extends AbstractTypePa
public AbstractLazyTypeParameterDescriptor(
@NotNull StorageManager storageManager,
@NotNull DeclarationDescriptor containingDeclaration,
@NotNull Annotations annotations,
@NotNull Name name,
@NotNull Variance variance,
boolean isReified,
@@ -36,7 +37,7 @@ public abstract class AbstractLazyTypeParameterDescriptor extends AbstractTypePa
@NotNull SourceElement source,
@NotNull SupertypeLoopChecker supertypeLoopChecker
) {
super(storageManager, containingDeclaration, Annotations.Companion.getEMPTY() /* TODO */, name, variance, isReified, index, source,
super(storageManager, containingDeclaration, annotations, name, variance, isReified, index, source,
supertypeLoopChecker);
}
@@ -18,6 +18,7 @@ package org.jetbrains.kotlin.serialization.deserialization.descriptors
import org.jetbrains.kotlin.descriptors.SourceElement
import org.jetbrains.kotlin.descriptors.SupertypeLoopChecker
import org.jetbrains.kotlin.descriptors.annotations.Annotations
import org.jetbrains.kotlin.descriptors.impl.AbstractLazyTypeParameterDescriptor
import org.jetbrains.kotlin.metadata.ProtoBuf
import org.jetbrains.kotlin.metadata.deserialization.upperBounds
@@ -32,8 +33,11 @@ class DeserializedTypeParameterDescriptor(
val proto: ProtoBuf.TypeParameter,
index: Int
) : AbstractLazyTypeParameterDescriptor(
c.storageManager, c.containingDeclaration, c.nameResolver.getName(proto.name),
ProtoEnumFlags.variance(proto.variance), proto.reified, index, SourceElement.NO_SOURCE, SupertypeLoopChecker.EMPTY
c.storageManager, c.containingDeclaration,
/*TODO: support deserialized type annotations: now they are unused, even not accessible via reflection (KT-46932)*/
Annotations.EMPTY,
c.nameResolver.getName(proto.name),
ProtoEnumFlags.variance(proto.variance), proto.reified, index, SourceElement.NO_SOURCE, SupertypeLoopChecker.EMPTY,
) {
override val annotations = DeserializedAnnotations(c.storageManager) {
c.components.annotationAndConstantLoader.loadTypeParameterAnnotations(proto, c.nameResolver).toList()
@@ -838,9 +838,14 @@ public class DiagnosisCompilerTestFE10TestdataTestGenerated extends AbstractDiag
runTest("compiler/testData/diagnostics/tests/annotations/AnnotationAsDefaultParameter.kt");
}
@TestMetadata("AnnotationForClassTypeParameter.kt")
public void testAnnotationForClassTypeParameter() throws Exception {
runTest("compiler/testData/diagnostics/tests/annotations/AnnotationForClassTypeParameter.kt");
@TestMetadata("AnnotationForClassTypeParameter_15.kt")
public void testAnnotationForClassTypeParameter_15() throws Exception {
runTest("compiler/testData/diagnostics/tests/annotations/AnnotationForClassTypeParameter_15.kt");
}
@TestMetadata("AnnotationForClassTypeParameter_16.kt")
public void testAnnotationForClassTypeParameter_16() throws Exception {
runTest("compiler/testData/diagnostics/tests/annotations/AnnotationForClassTypeParameter_16.kt");
}
@TestMetadata("AnnotationForFunctionTypeParameter.kt")