From ba0ab38b835f8fc139e1e20a653dc2edd7566fac Mon Sep 17 00:00:00 2001 From: Mikhail Glukhikh Date: Mon, 26 Jun 2023 17:45:51 +0300 Subject: [PATCH] K2: set vararg type for Java annotations #KT-59464 Fixed --- .../fir/backend/Fir2IrImplicitCastInserter.kt | 4 ++-- .../java/deserialization/AnnotationsLoader.kt | 8 ++++++- .../kotlin/fir/java/javaAnnotationsMapping.kt | 21 ++++++++++++------- 3 files changed, 23 insertions(+), 10 deletions(-) diff --git a/compiler/fir/fir2ir/src/org/jetbrains/kotlin/fir/backend/Fir2IrImplicitCastInserter.kt b/compiler/fir/fir2ir/src/org/jetbrains/kotlin/fir/backend/Fir2IrImplicitCastInserter.kt index e0ad0028517..d5390b2c860 100644 --- a/compiler/fir/fir2ir/src/org/jetbrains/kotlin/fir/backend/Fir2IrImplicitCastInserter.kt +++ b/compiler/fir/fir2ir/src/org/jetbrains/kotlin/fir/backend/Fir2IrImplicitCastInserter.kt @@ -204,8 +204,8 @@ class Fir2IrImplicitCastInserter( insertImplicitCasts() } - val valueType = valueTypeRef.coneTypeSafe()?.fullyExpandedType(session) ?: return this - val expectedType = expectedTypeRef.coneTypeSafe()?.fullyExpandedType(session) ?: return this + val valueType = valueTypeRef.coneType.fullyExpandedType(session) + val expectedType = expectedTypeRef.coneType.fullyExpandedType(session) return when { expectedType.isUnit -> { diff --git a/compiler/fir/java/src/org/jetbrains/kotlin/fir/java/deserialization/AnnotationsLoader.kt b/compiler/fir/java/src/org/jetbrains/kotlin/fir/java/deserialization/AnnotationsLoader.kt index 5ad4e0aa168..8fb703a6d50 100644 --- a/compiler/fir/java/src/org/jetbrains/kotlin/fir/java/deserialization/AnnotationsLoader.kt +++ b/compiler/fir/java/src/org/jetbrains/kotlin/fir/java/deserialization/AnnotationsLoader.kt @@ -95,7 +95,13 @@ internal class AnnotationsLoader(private val session: FirSession, private val ko override fun visitEnd() { visitExpression(name, buildArrayOfCall { - guessArrayTypeIfNeeded(name, elements)?.let { typeRef = it } + guessArrayTypeIfNeeded(name, elements)?.let { + typeRef = it + } ?: elements.firstOrNull()?.typeRef?.coneType?.createOutArrayType()?.let { + typeRef = buildResolvedTypeRef { + type = it + } + } argumentList = buildArgumentList { arguments += elements } diff --git a/compiler/fir/java/src/org/jetbrains/kotlin/fir/java/javaAnnotationsMapping.kt b/compiler/fir/java/src/org/jetbrains/kotlin/fir/java/javaAnnotationsMapping.kt index 14ffed5aa06..a0aab91cdc8 100644 --- a/compiler/fir/java/src/org/jetbrains/kotlin/fir/java/javaAnnotationsMapping.kt +++ b/compiler/fir/java/src/org/jetbrains/kotlin/fir/java/javaAnnotationsMapping.kt @@ -34,6 +34,7 @@ import org.jetbrains.kotlin.fir.types.* import org.jetbrains.kotlin.fir.types.builder.buildErrorTypeRef import org.jetbrains.kotlin.fir.types.builder.buildResolvedTypeRef import org.jetbrains.kotlin.fir.types.impl.ConeClassLikeTypeImpl +import org.jetbrains.kotlin.fir.types.jvm.FirJavaTypeRef import org.jetbrains.kotlin.load.java.structure.* import org.jetbrains.kotlin.load.java.structure.impl.JavaElementImpl import org.jetbrains.kotlin.name.ClassId @@ -88,7 +89,9 @@ internal fun JavaAnnotationArgument.toFirExpression( is JavaLiteralAnnotationArgument -> value.createConstantOrError(session) is JavaArrayAnnotationArgument -> buildArrayOfCall { val argumentTypeRef = expectedTypeRef?.let { - typeRef = it + typeRef = if (it is FirJavaTypeRef) buildResolvedTypeRef { + type = it.toConeKotlinTypeProbablyFlexible(session, javaTypeParameterStack) + } else it buildResolvedTypeRef { type = it.coneTypeSafe()?.lowerBoundIfFlexible()?.arrayElementType() ?: ConeErrorType(ConeSimpleDiagnostic("expected type is not array type")) @@ -184,13 +187,17 @@ private fun List.mapJavaTargetArguments(session: FirSess } val classId = StandardClassIds.AnnotationTarget resultSet.mapTo(arguments) { buildEnumCall(session, classId, Name.identifier(it.name)) } + val elementConeType = ConeClassLikeTypeImpl( + classId.toLookupTag(), + emptyArray(), + isNullable = false, + ConeAttributes.Empty + ) + typeRef = buildResolvedTypeRef { + type = elementConeType + } varargElementType = buildResolvedTypeRef { - type = ConeClassLikeTypeImpl( - classId.toLookupTag(), - emptyArray(), - isNullable = false, - ConeAttributes.Empty - ).createOutArrayType() + type = elementConeType.createOutArrayType() } } }