KTIJ-24768 Fix IllegalArgumentException in KtType.isPrimitiveBacked

This commit is contained in:
Pavel Mikhailovskii
2023-03-10 11:03:45 +00:00
committed by Space Team
parent 8d1cfe0017
commit 164cbc9968
16 changed files with 93 additions and 12 deletions
@@ -27,4 +27,9 @@ internal class KtFe10JvmTypeMapper(
val kotlinType = (type as KtFe10Type).fe10Type
return typeMapper.mapType(kotlinType, mode)
}
override fun isPrimitiveBacked(type: KtType): Boolean {
val kotlinType = (type as KtFe10Type).fe10Type
return typeMapper.isPrimitiveBacked(kotlinType)
}
}
@@ -57,6 +57,9 @@ internal class KtFe10JvmTypeMapperContext(private val resolveSession: ResolveSes
return AbstractTypeMapper.mapType(this, type, mode, sw)
}
fun isPrimitiveBacked(type: KotlinType): Boolean =
AbstractTypeMapper.isPrimitiveBacked(this, type)
override fun getClassInternalName(typeConstructor: TypeConstructorMarker): String {
require(typeConstructor is TypeConstructor)
@@ -21,4 +21,8 @@ internal class KtFirJvmTypeMapper(
override fun mapTypeToJvmType(type: KtType, mode: TypeMappingMode): Type {
return analysisSession.useSiteSession.jvmTypeMapper.mapType(type.coneType, mode, sw = null, unresolvedQualifierRemapper = null)
}
override fun isPrimitiveBacked(type: KtType): Boolean {
return analysisSession.useSiteSession.jvmTypeMapper.isPrimitiveBacked(type.coneType)
}
}
@@ -12,6 +12,7 @@ import org.jetbrains.org.objectweb.asm.Type
public abstract class KtJvmTypeMapper : KtAnalysisSessionComponent() {
public abstract fun mapTypeToJvmType(type: KtType, mode: TypeMappingMode): Type
public abstract fun isPrimitiveBacked(type: KtType): Boolean
}
public interface KtJvmTypeMapperMixIn : KtAnalysisSessionMixIn {
@@ -22,4 +23,10 @@ public interface KtJvmTypeMapperMixIn : KtAnalysisSessionMixIn {
*/
public fun KtType.mapTypeToJvmType(mode: TypeMappingMode = TypeMappingMode.DEFAULT): Type =
withValidityAssertion { analysisSession.jvmTypeMapper.mapTypeToJvmType(this, mode) }
/**
* Returns true if the type is backed by a single JVM primitive type
*/
public val KtType.isPrimitiveBacked: Boolean
get() = withValidityAssertion { analysisSession.jvmTypeMapper.isPrimitiveBacked(this) }
}
@@ -15,7 +15,6 @@ import org.jetbrains.kotlin.analysis.api.types.KtTypeNullability
import org.jetbrains.kotlin.builtins.StandardNames
import org.jetbrains.kotlin.builtins.functions.FunctionTypeKind
import org.jetbrains.kotlin.name.ClassId
import org.jetbrains.org.objectweb.asm.Type
public abstract class KtTypeInfoProvider : KtAnalysisSessionComponent() {
public abstract fun isFunctionalInterfaceType(type: KtType): Boolean
@@ -144,16 +143,6 @@ public interface KtTypeInfoProviderMixIn : KtAnalysisSessionMixIn {
return this.classId in DefaultTypeClassIds.PRIMITIVES
}
context(KtJvmTypeMapperMixIn)
public val KtType.isPrimitiveBacked: Boolean
get() = withValidityAssertion {
if (this !is KtNonErrorClassType) return false
return when (this.mapTypeToJvmType().sort) {
Type.BOOLEAN, Type.CHAR, Type.BYTE, Type.SHORT, Type.INT, Type.FLOAT, Type.LONG, Type.DOUBLE -> true
else -> false
}
}
public val KtType.defaultInitializer: String?
get() = withValidityAssertion {
when {
@@ -453,6 +453,12 @@ public class SymbolLightClassesByFqNameForLibraryTestGenerated extends AbstractS
runTest("compiler/testData/asJava/lightClasses/lightClassByFqName/nullabilityAnnotations/Generic.kt");
}
@Test
@TestMetadata("ImplicitArrayWithFlexibleParameterTypes.kt")
public void testImplicitArrayWithFlexibleParameterTypes() throws Exception {
runTest("compiler/testData/asJava/lightClasses/lightClassByFqName/nullabilityAnnotations/ImplicitArrayWithFlexibleParameterTypes.kt");
}
@Test
@TestMetadata("IntOverridesAny.kt")
public void testIntOverridesAny() throws Exception {
@@ -453,6 +453,12 @@ public class SymbolLightClassesEqualityByFqNameForLibraryTestGenerated extends A
runTest("compiler/testData/asJava/lightClasses/lightClassByFqName/nullabilityAnnotations/Generic.kt");
}
@Test
@TestMetadata("ImplicitArrayWithFlexibleParameterTypes.kt")
public void testImplicitArrayWithFlexibleParameterTypes() throws Exception {
runTest("compiler/testData/asJava/lightClasses/lightClassByFqName/nullabilityAnnotations/ImplicitArrayWithFlexibleParameterTypes.kt");
}
@Test
@TestMetadata("IntOverridesAny.kt")
public void testIntOverridesAny() throws Exception {
@@ -453,6 +453,12 @@ public class SymbolLightClassesParentingByFqNameForLibraryTestGenerated extends
runTest("compiler/testData/asJava/lightClasses/lightClassByFqName/nullabilityAnnotations/Generic.kt");
}
@Test
@TestMetadata("ImplicitArrayWithFlexibleParameterTypes.kt")
public void testImplicitArrayWithFlexibleParameterTypes() throws Exception {
runTest("compiler/testData/asJava/lightClasses/lightClassByFqName/nullabilityAnnotations/ImplicitArrayWithFlexibleParameterTypes.kt");
}
@Test
@TestMetadata("IntOverridesAny.kt")
public void testIntOverridesAny() throws Exception {
@@ -595,6 +595,12 @@ public class SymbolLightClassesByFqNameForSourceTestGenerated extends AbstractSy
runTest("compiler/testData/asJava/lightClasses/lightClassByFqName/nullabilityAnnotations/Generic.kt");
}
@Test
@TestMetadata("ImplicitArrayWithFlexibleParameterTypes.kt")
public void testImplicitArrayWithFlexibleParameterTypes() throws Exception {
runTest("compiler/testData/asJava/lightClasses/lightClassByFqName/nullabilityAnnotations/ImplicitArrayWithFlexibleParameterTypes.kt");
}
@Test
@TestMetadata("IntOverridesAny.kt")
public void testIntOverridesAny() throws Exception {
@@ -595,6 +595,12 @@ public class SymbolLightClassesEqualityByFqNameForSourceTestGenerated extends Ab
runTest("compiler/testData/asJava/lightClasses/lightClassByFqName/nullabilityAnnotations/Generic.kt");
}
@Test
@TestMetadata("ImplicitArrayWithFlexibleParameterTypes.kt")
public void testImplicitArrayWithFlexibleParameterTypes() throws Exception {
runTest("compiler/testData/asJava/lightClasses/lightClassByFqName/nullabilityAnnotations/ImplicitArrayWithFlexibleParameterTypes.kt");
}
@Test
@TestMetadata("IntOverridesAny.kt")
public void testIntOverridesAny() throws Exception {
@@ -595,6 +595,12 @@ public class SymbolLightClassesParentingByFqNameForSourceTestGenerated extends A
runTest("compiler/testData/asJava/lightClasses/lightClassByFqName/nullabilityAnnotations/Generic.kt");
}
@Test
@TestMetadata("ImplicitArrayWithFlexibleParameterTypes.kt")
public void testImplicitArrayWithFlexibleParameterTypes() throws Exception {
runTest("compiler/testData/asJava/lightClasses/lightClassByFqName/nullabilityAnnotations/ImplicitArrayWithFlexibleParameterTypes.kt");
}
@Test
@TestMetadata("IntOverridesAny.kt")
public void testIntOverridesAny() throws Exception {
@@ -155,7 +155,6 @@ object AbstractTypeMapper {
typeArgument.isStarProjection() -> Variance.OUT_VARIANCE to nullableAnyType()
else -> typeArgument.getVariance().toVariance() to typeArgument.getType()
}
require(memberType is SimpleTypeMarker)
val arrayElementType: Type
sw?.writeArrayType()
@@ -202,4 +201,14 @@ object AbstractTypeMapper {
TypeVariance.OUT -> Variance.OUT_VARIANCE
TypeVariance.INV -> Variance.INVARIANT
}
fun <Writer : JvmDescriptorTypeWriter<Type>> isPrimitiveBacked(
context: TypeMappingContext<Writer>,
type: KotlinTypeMarker
): Boolean = context.typeContext.isPrimitiveBacked(type)
private fun TypeSystemCommonBackendContext.isPrimitiveBacked(type: KotlinTypeMarker): Boolean =
!type.isNullableType() &&
(type is SimpleTypeMarker && type.isPrimitiveType() ||
type.typeConstructor().getValueClassProperties()?.singleOrNull()?.let { isPrimitiveBacked(it.second) } == true)
}
@@ -65,6 +65,9 @@ class FirJvmTypeMapper(val session: FirSession) : FirSessionComponent {
return AbstractTypeMapper.mapType(context, type, mode, sw)
}
fun isPrimitiveBacked(type: ConeKotlinType): Boolean =
AbstractTypeMapper.isPrimitiveBacked(defaultContext, type)
private val defaultContext = Context { null }
val typeContext: TypeSystemCommonBackendContext
get() = defaultContext.typeContext
@@ -0,0 +1,4 @@
public final class ImplicitArrayWithFlexibleParameterTypesKt /* ImplicitArrayWithFlexibleParameterTypesKt*/ {
@org.jetbrains.annotations.NotNull()
public static final java.lang.Integer[] getArrayOfFlexibleInts();// getArrayOfFlexibleInts()
}
@@ -0,0 +1,16 @@
// ImplicitArrayWithFlexibleParameterTypesKt
// WITH_STDLIB
// FILE: ImplicitArrayWithFlexibleParameterTypes.kt
fun getArrayOfFlexibleInts() /* : Array<Int!> */ = arrayOf(JavaClass.getJavaInt())
// FILE: JavaClass.java
public class JavaClass {
public static Integer getJavaInt() {
return 10;
}
}
// FIR_COMPARISON
@@ -475,6 +475,11 @@ public class CompilerLightClassTestGenerated extends AbstractCompilerLightClassT
runTest("compiler/testData/asJava/lightClasses/lightClassByFqName/nullabilityAnnotations/Generic.kt");
}
@TestMetadata("ImplicitArrayWithFlexibleParameterTypes.kt")
public void testImplicitArrayWithFlexibleParameterTypes() throws Exception {
runTest("compiler/testData/asJava/lightClasses/lightClassByFqName/nullabilityAnnotations/ImplicitArrayWithFlexibleParameterTypes.kt");
}
@TestMetadata("IntOverridesAny.kt")
public void testIntOverridesAny() throws Exception {
runTest("compiler/testData/asJava/lightClasses/lightClassByFqName/nullabilityAnnotations/IntOverridesAny.kt");