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
@@ -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),