[FIR] Fix expression types in java annotations

#KT-61518 Fixed
This commit is contained in:
Kirill Rakhman
2023-09-05 12:46:41 +02:00
committed by Space Team
parent f18d600dc8
commit 9a2307ad44
9 changed files with 79 additions and 4 deletions
@@ -19029,6 +19029,12 @@ public class FirLightTreeBlackBoxCodegenTestGenerated extends AbstractFirLightTr
runTest("compiler/testData/codegen/box/fir/JKEnumConstant.kt");
}
@Test
@TestMetadata("javaAnnotationWithDefaultValueForenumArray.kt")
public void testJavaAnnotationWithDefaultValueForenumArray() throws Exception {
runTest("compiler/testData/codegen/box/fir/javaAnnotationWithDefaultValueForenumArray.kt");
}
@Test
@TestMetadata("jvmFieldInLocalClass.kt")
public void testJvmFieldInLocalClass() throws Exception {
@@ -19029,6 +19029,12 @@ public class FirLightTreeBlackBoxCodegenWithIrFakeOverrideGeneratorTestGenerated
runTest("compiler/testData/codegen/box/fir/JKEnumConstant.kt");
}
@Test
@TestMetadata("javaAnnotationWithDefaultValueForenumArray.kt")
public void testJavaAnnotationWithDefaultValueForenumArray() throws Exception {
runTest("compiler/testData/codegen/box/fir/javaAnnotationWithDefaultValueForenumArray.kt");
}
@Test
@TestMetadata("jvmFieldInLocalClass.kt")
public void testJvmFieldInLocalClass() throws Exception {
@@ -19029,6 +19029,12 @@ public class FirPsiBlackBoxCodegenTestGenerated extends AbstractFirPsiBlackBoxCo
runTest("compiler/testData/codegen/box/fir/JKEnumConstant.kt");
}
@Test
@TestMetadata("javaAnnotationWithDefaultValueForenumArray.kt")
public void testJavaAnnotationWithDefaultValueForenumArray() throws Exception {
runTest("compiler/testData/codegen/box/fir/javaAnnotationWithDefaultValueForenumArray.kt");
}
@Test
@TestMetadata("jvmFieldInLocalClass.kt")
public void testJvmFieldInLocalClass() throws Exception {
@@ -107,10 +107,14 @@ internal fun JavaAnnotationArgument.toFirExpression(
)
is JavaArrayAnnotationArgument -> buildArrayLiteral {
val argumentTypeRef = expectedTypeRef?.let {
coneTypeOrNull =
if (it is FirJavaTypeRef) it.toConeKotlinTypeProbablyFlexible(session, javaTypeParameterStack) else it.coneType
val type = if (it is FirJavaTypeRef) {
it.toConeKotlinTypeProbablyFlexible(session, javaTypeParameterStack)
} else {
it.coneType
}
coneTypeOrNull = type
buildResolvedTypeRef {
type = it.coneTypeSafe<ConeKotlinType>()?.lowerBoundIfFlexible()?.arrayElementType()
this.type = type.lowerBoundIfFlexible().arrayElementType()
?: ConeErrorType(ConeSimpleDiagnostic("expected type is not array type"))
}
}
@@ -118,7 +122,15 @@ internal fun JavaAnnotationArgument.toFirExpression(
getElements().mapTo(arguments) { it.toFirExpression(session, javaTypeParameterStack, argumentTypeRef) }
}
}
is JavaEnumValueAnnotationArgument -> buildEnumCall(session, enumClassId, entryName)
is JavaEnumValueAnnotationArgument -> buildEnumCall(
session,
// enumClassId can be null when a java annotation uses a Kotlin enum as parameter and declares the default value using
// a static import. In this case, the parameter default initializer will not have its type set, which isn't usually an
// issue except in edge cases like KT-47702 where we do need to evaluate the default values of annotations.
// As a fallback, we use the expected type which should be the type of the enum.
classId = enumClassId ?: expectedTypeRef?.coneTypeOrNull?.lowerBoundIfFlexible()?.classId,
entryName = entryName
)
is JavaClassObjectAnnotationArgument -> buildGetClassCall {
val resolvedClassTypeRef = getReferencedType().toFirResolvedTypeRef(session, javaTypeParameterStack)
val resolvedTypeRef = buildResolvedTypeRef {
@@ -0,0 +1,22 @@
// IGNORE_LIGHT_ANALYSIS
// TARGET_BACKEND: JVM
// FILE: ArrayAnnEnumJava.java
package light.ann.array;
import static light.ann.array.AnnAuxEnum.ANN_ENUM_VAL_A;
import static light.ann.array.AnnAuxEnum.ANN_ENUM_VAL_B;
public @interface ArrayAnnEnumJava {
AnnAuxEnum[] enumValDef() default { ANN_ENUM_VAL_A, ANN_ENUM_VAL_B };
}
// FILE: ArrayAnnUsage.kt
package light.ann.array
import light.ann.array.AnnAuxEnum.ANN_ENUM_VAL_A;
import light.ann.array.AnnAuxEnum.ANN_ENUM_VAL_B;
enum class AnnAuxEnum { ANN_ENUM_VAL_A, ANN_ENUM_VAL_B }
@ArrayAnnEnumJava(enumValDef = arrayOf(ANN_ENUM_VAL_A, ANN_ENUM_VAL_B))
fun box() = "OK"
@@ -18117,6 +18117,12 @@ public class BlackBoxCodegenTestGenerated extends AbstractBlackBoxCodegenTest {
runTest("compiler/testData/codegen/box/fir/IrBuiltIns.kt");
}
@Test
@TestMetadata("javaAnnotationWithDefaultValueForenumArray.kt")
public void testJavaAnnotationWithDefaultValueForenumArray() throws Exception {
runTest("compiler/testData/codegen/box/fir/javaAnnotationWithDefaultValueForenumArray.kt");
}
@Test
@TestMetadata("KotlinDocumentationProvider.kt")
public void testKotlinDocumentationProvider() throws Exception {
@@ -19029,6 +19029,12 @@ public class IrBlackBoxCodegenTestGenerated extends AbstractIrBlackBoxCodegenTes
runTest("compiler/testData/codegen/box/fir/JKEnumConstant.kt");
}
@Test
@TestMetadata("javaAnnotationWithDefaultValueForenumArray.kt")
public void testJavaAnnotationWithDefaultValueForenumArray() throws Exception {
runTest("compiler/testData/codegen/box/fir/javaAnnotationWithDefaultValueForenumArray.kt");
}
@Test
@TestMetadata("jvmFieldInLocalClass.kt")
public void testJvmFieldInLocalClass() throws Exception {
@@ -19029,6 +19029,12 @@ public class IrBlackBoxCodegenWithIrInlinerTestGenerated extends AbstractIrBlack
runTest("compiler/testData/codegen/box/fir/JKEnumConstant.kt");
}
@Test
@TestMetadata("javaAnnotationWithDefaultValueForenumArray.kt")
public void testJavaAnnotationWithDefaultValueForenumArray() throws Exception {
runTest("compiler/testData/codegen/box/fir/javaAnnotationWithDefaultValueForenumArray.kt");
}
@Test
@TestMetadata("jvmFieldInLocalClass.kt")
public void testJvmFieldInLocalClass() throws Exception {
@@ -15846,6 +15846,11 @@ public class LightAnalysisModeTestGenerated extends AbstractLightAnalysisModeTes
runTest("compiler/testData/codegen/box/fir/JKEnumConstant.kt");
}
@TestMetadata("javaAnnotationWithDefaultValueForenumArray.kt")
public void testJavaAnnotationWithDefaultValueForenumArray() throws Exception {
runTest("compiler/testData/codegen/box/fir/javaAnnotationWithDefaultValueForenumArray.kt");
}
@TestMetadata("jvmFieldInLocalClass.kt")
public void testJvmFieldInLocalClass() throws Exception {
runTest("compiler/testData/codegen/box/fir/jvmFieldInLocalClass.kt");