[ULC] Fix invalid primitive type annotating

Fixed #KT-45417
This commit is contained in:
Igor Yakovlev
2021-03-11 17:44:18 +01:00
parent ff5b2404af
commit 023b7fbb8f
3 changed files with 26 additions and 6 deletions
@@ -191,7 +191,8 @@ private fun annotateByKotlinType(
kotlinType: KotlinType, kotlinType: KotlinType,
psiContext: PsiElement, psiContext: PsiElement,
ultraLightSupport: KtUltraLightSupport ultraLightSupport: KtUltraLightSupport
) { ): PsiType {
fun KotlinType.getAnnotationsSequence(): Sequence<List<PsiAnnotation>> = fun KotlinType.getAnnotationsSequence(): Sequence<List<PsiAnnotation>> =
sequence { sequence {
yield(annotations.mapNotNull { it.toLightAnnotation(ultraLightSupport, psiContext) }) yield(annotations.mapNotNull { it.toLightAnnotation(ultraLightSupport, psiContext) })
@@ -201,11 +202,20 @@ private fun annotateByKotlinType(
} }
val annotationsIterator = kotlinType.getAnnotationsSequence().iterator() val annotationsIterator = kotlinType.getAnnotationsSequence().iterator()
if (!annotationsIterator.hasNext()) return psiType
if (psiType is PsiPrimitiveType) {
val annotation = annotationsIterator.next()
val provider = TypeAnnotationProvider.Static.create(annotation.toTypedArray())
return psiType.annotate(provider)
}
fun recursiveAnnotator(psiType: PsiType) { fun recursiveAnnotator(psiType: PsiType) {
if (!annotationsIterator.hasNext()) return if (!annotationsIterator.hasNext()) return
val typeAnnotations = annotationsIterator.next() val typeAnnotations = annotationsIterator.next()
if (psiType is PsiPrimitiveType) return //Primitive type cannot be type parameter so we skip it
if (psiType is PsiClassType) { if (psiType is PsiClassType) {
for (parameterType in psiType.parameters) { for (parameterType in psiType.parameters) {
recursiveAnnotator(parameterType) recursiveAnnotator(parameterType)
@@ -217,11 +227,11 @@ private fun annotateByKotlinType(
if (typeAnnotations.isEmpty()) return if (typeAnnotations.isEmpty()) return
val provider = TypeAnnotationProvider.Static.create(typeAnnotations.toTypedArray()) val provider = TypeAnnotationProvider.Static.create(typeAnnotations.toTypedArray())
setPsiTypeAnnotationProvider(psiType, provider) setPsiTypeAnnotationProvider(psiType, provider)
} }
recursiveAnnotator(psiType) recursiveAnnotator(psiType)
return psiType
} }
internal fun KtUltraLightSupport.mapType( internal fun KtUltraLightSupport.mapType(
@@ -246,10 +256,7 @@ private fun KtUltraLightSupport.createTypeFromCanonicalText(
val typeText = TypeInfo.createTypeText(typeInfo) ?: return PsiType.NULL val typeText = TypeInfo.createTypeText(typeInfo) ?: return PsiType.NULL
val typeElement = ClsTypeElementImpl(psiContext, typeText, '\u0000') val typeElement = ClsTypeElementImpl(psiContext, typeText, '\u0000')
val type = typeElement.type val type = if (kotlinType != null) annotateByKotlinType(typeElement.type, kotlinType, typeElement, this) else typeElement.type
if (kotlinType != null) {
annotateByKotlinType(type, kotlinType, typeElement, this)
}
if (type is PsiArrayType && psiContext is KtUltraLightParameter && psiContext.isVarArgs) { if (type is PsiArrayType && psiContext is KtUltraLightParameter && psiContext.isVarArgs) {
return PsiEllipsisType(type.componentType, type.annotationProvider) return PsiEllipsisType(type.componentType, type.annotationProvider)
@@ -54,9 +54,19 @@ public final class Y /* Y*/ {
} }
public final class klass /* klass*/ { public final class klass /* klass*/ {
@org.jetbrains.annotations.Nullable()
private final java.util.List<@A0() java.lang.Integer> y;
private final @A0() int x;
@org.jetbrains.annotations.NotNull() @org.jetbrains.annotations.NotNull()
public final @A6() X annotatedMethod(@org.jetbrains.annotations.NotNull() @A0() P<@A1() X, P<@A2() @A3() X, @A4() Y>>, @org.jetbrains.annotations.NotNull() @A5() Y[]);// annotatedMethod(@A0() P<@A1() X, P<@A2() @A3() X, @A4() Y>>, @A5() Y[]) public final @A6() X annotatedMethod(@org.jetbrains.annotations.NotNull() @A0() P<@A1() X, P<@A2() @A3() X, @A4() Y>>, @org.jetbrains.annotations.NotNull() @A5() Y[]);// annotatedMethod(@A0() P<@A1() X, P<@A2() @A3() X, @A4() Y>>, @A5() Y[])
@org.jetbrains.annotations.Nullable()
public final java.util.List<@A0() java.lang.Integer> getY();// getY()
public klass();// .ctor() public klass();// .ctor()
public final @A0() int getX();// getX()
} }
@@ -23,4 +23,7 @@ class klass {
fun annotatedMethod(x: @A0 P<@A1 X, P<@A2 @A3 X, @A4 Y>>, y: Array<@A5 Y>): @A6 X { fun annotatedMethod(x: @A0 P<@A1 X, P<@A2 @A3 X, @A4 Y>>, y: Array<@A5 Y>): @A6 X {
return "" return ""
} }
val x: @A0 Int = 2
val y: List<@A0 Int>? = null
} }