Skip to content

Commit

Permalink
[ULC] Fix invalid primitive type annotating
Browse files Browse the repository at this point in the history
Fixed #KT-45417
  • Loading branch information
igoriakovlev committed Mar 12, 2021
1 parent 4dbf18c commit fac791f
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,8 @@ private fun annotateByKotlinType(
kotlinType: KotlinType,
psiContext: PsiElement,
ultraLightSupport: KtUltraLightSupport
) {
): PsiType {

fun KotlinType.getAnnotationsSequence(): Sequence<List<PsiAnnotation>> =
sequence {
yield(annotations.mapNotNull { it.toLightAnnotation(ultraLightSupport, psiContext) })
Expand All @@ -201,11 +202,20 @@ private fun annotateByKotlinType(
}

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) {
if (!annotationsIterator.hasNext()) return
val typeAnnotations = annotationsIterator.next()

if (psiType is PsiPrimitiveType) return //Primitive type cannot be type parameter so we skip it

if (psiType is PsiClassType) {
for (parameterType in psiType.parameters) {
recursiveAnnotator(parameterType)
Expand All @@ -217,11 +227,11 @@ private fun annotateByKotlinType(
if (typeAnnotations.isEmpty()) return

val provider = TypeAnnotationProvider.Static.create(typeAnnotations.toTypedArray())

setPsiTypeAnnotationProvider(psiType, provider)
}

recursiveAnnotator(psiType)
return psiType
}

internal fun KtUltraLightSupport.mapType(
Expand All @@ -246,10 +256,7 @@ private fun KtUltraLightSupport.createTypeFromCanonicalText(
val typeText = TypeInfo.createTypeText(typeInfo) ?: return PsiType.NULL

val typeElement = ClsTypeElementImpl(psiContext, typeText, '\u0000')
val type = typeElement.type
if (kotlinType != null) {
annotateByKotlinType(type, kotlinType, typeElement, this)
}
val type = if (kotlinType != null) annotateByKotlinType(typeElement.type, kotlinType, typeElement, this) else typeElement.type

if (type is PsiArrayType && psiContext is KtUltraLightParameter && psiContext.isVarArgs) {
return PsiEllipsisType(type.componentType, type.annotationProvider)
Expand Down
10 changes: 10 additions & 0 deletions compiler/testData/asJava/ultraLightClasses/typeAnnotations.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,19 @@ public final class Y /* Y*/ {
}

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()
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 final @A0() int getX();// getX()

}
3 changes: 3 additions & 0 deletions compiler/testData/asJava/ultraLightClasses/typeAnnotations.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
return ""
}

val x: @A0 Int = 2
val y: List<@A0 Int>? = null
}

0 comments on commit fac791f

Please sign in to comment.