[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,
psiContext: PsiElement,
ultraLightSupport: KtUltraLightSupport
) {
): PsiType {
fun KotlinType.getAnnotationsSequence(): Sequence<List<PsiAnnotation>> =
sequence {
yield(annotations.mapNotNull { it.toLightAnnotation(ultraLightSupport, psiContext) })
@@ -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)
@@ -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(
@@ -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)