Change throw AssertionError to error() that throws IllegalStateException

as ISE is more appropriate exception for validation.

Improve SerializerClassLowering.runPluginSafe so precise IrClass
where plugin have failed can be reported for non-fatal errors.

#KT-55296 Fixed
This commit is contained in:
Leonid Startsev
2022-12-07 15:32:26 +01:00
committed by Space Team
parent 7ee877f9aa
commit c011f0c374
5 changed files with 18 additions and 13 deletions
@@ -87,7 +87,7 @@ abstract class BaseIrGenerator(private val currentClass: IrClass, final override
{ serializerSymbol -> { serializerSymbol ->
val kotlinType = (serializerSymbol.owner.superTypes.find(IrType::isKSerializer) as? IrSimpleType)?.arguments?.firstOrNull()?.typeOrNull val kotlinType = (serializerSymbol.owner.superTypes.find(IrType::isKSerializer) as? IrSimpleType)?.arguments?.firstOrNull()?.typeOrNull
val classSymbol = kotlinType?.classOrNull val classSymbol = kotlinType?.classOrNull
?: throw AssertionError("Argument for ${SerializationAnnotations.additionalSerializersFqName} does not implement KSerializer or does not provide serializer for concrete type") ?: error("Argument for ${SerializationAnnotations.additionalSerializersFqName} does not implement KSerializer or does not provide serializer for concrete type")
classSymbol to kotlinType.isNullable() classSymbol to kotlinType.isNullable()
}, },
{ it } { it }
@@ -372,7 +372,7 @@ abstract class BaseIrGenerator(private val currentClass: IrClass, final override
cacheableSerializers.firstOrNull { it != null } ?: return null cacheableSerializers.firstOrNull { it != null } ?: return null
val kSerializerClass = compilerContext.kSerializerClass val kSerializerClass = compilerContext.kSerializerClass
?: throw AssertionError("Serializer class '$KSERIALIZER_NAME_FQ' not found. Check that the kotlinx.serialization runtime is connected correctly") ?: error("Serializer class '$KSERIALIZER_NAME_FQ' not found. Check that the kotlinx.serialization runtime is connected correctly")
val kSerializerType = kSerializerClass.typeWith(compilerContext.irBuiltIns.anyType) val kSerializerType = kSerializerClass.typeWith(compilerContext.irBuiltIns.anyType)
val arrayType = compilerContext.irBuiltIns.arrayClass.typeWith(kSerializerType) val arrayType = compilerContext.irBuiltIns.arrayClass.typeWith(kSerializerType)
@@ -170,7 +170,7 @@ interface IrBuilderWithPluginContext {
val irBlockBody = irBlockBody(startOffset, endOffset, blockBuilder) val irBlockBody = irBlockBody(startOffset, endOffset, blockBuilder)
irBlockBody.statements.dropLast(1).forEach { +it } irBlockBody.statements.dropLast(1).forEach { +it }
return irBlockBody.statements.last() as? IrExpression return irBlockBody.statements.last() as? IrExpression
?: throw AssertionError("Last statement in property initializer builder is not an a expression") ?: error("Last statement in property initializer builder is not an a expression")
} }
fun IrBuilderWithScope.irInvoke( fun IrBuilderWithScope.irInvoke(
@@ -122,7 +122,7 @@ fun IrClass.serialName(): String {
fun IrClass.findEnumValuesMethod() = this.functions.singleOrNull { f -> fun IrClass.findEnumValuesMethod() = this.functions.singleOrNull { f ->
f.name == Name.identifier("values") && f.valueParameters.isEmpty() && f.extensionReceiverParameter == null && f.dispatchReceiverParameter == null f.name == Name.identifier("values") && f.valueParameters.isEmpty() && f.extensionReceiverParameter == null && f.dispatchReceiverParameter == null
} ?: throw AssertionError("Enum class does not have single .values() function") } ?: error("Enum class does not have single .values() function")
internal fun IrClass.enumEntries(): List<IrEnumEntry> { internal fun IrClass.enumEntries(): List<IrEnumEntry> {
check(this.kind == ClassKind.ENUM_CLASS) check(this.kind == ClassKind.ENUM_CLASS)
@@ -401,7 +401,7 @@ class SerializableIrGenerator(
hasSerializableOrMetaAnnotationWithoutArgs() && !isInternalSerializable && !hasCompanionObjectAsSerializer && kind != ClassKind.ENUM_CLASS && !isSealedSerializableInterface hasSerializableOrMetaAnnotationWithoutArgs() && !isInternalSerializable && !hasCompanionObjectAsSerializer && kind != ClassKind.ENUM_CLASS && !isSealedSerializableInterface
} }
if (serializableAnnotationIsUseless) if (serializableAnnotationIsUseless)
throw AssertionError( error(
"@Serializable annotation on $irClass would be ignored because it is impossible to serialize it automatically. " + "@Serializable annotation on $irClass would be ignored because it is impossible to serialize it automatically. " +
"Provide serializer manually via e.g. companion object" "Provide serializer manually via e.g. companion object"
) )
@@ -106,13 +106,16 @@ class SerializationPluginContext(baseContext: IrPluginContext, val metadataPlugi
private inline fun IrClass.runPluginSafe(block: () -> Unit) { private inline fun IrClass.runPluginSafe(block: () -> Unit) {
try { try {
block() block()
} catch (e: Exception) { } catch (e: Throwable) {
throw CompilationException( throw when (e) {
"kotlinx.serialization compiler plugin internal error: unable to transform declaration, see cause", is VirtualMachineError, is ThreadDeath -> e
this.fileParent, else -> CompilationException(
this, "kotlinx.serialization compiler plugin internal error: unable to transform declaration, see cause",
e this.fileParent,
) this,
e
)
}
} }
} }
@@ -163,7 +166,9 @@ open class SerializationLoweringExtension @JvmOverloads constructor(
private var intrinsicsState = SerializationIntrinsicsState.NORMAL private var intrinsicsState = SerializationIntrinsicsState.NORMAL
constructor(metadataPlugin: SerializationDescriptorSerializerPlugin, intrinsicsState: SerializationIntrinsicsState) : this(metadataPlugin) { constructor(metadataPlugin: SerializationDescriptorSerializerPlugin, intrinsicsState: SerializationIntrinsicsState) : this(
metadataPlugin
) {
this.intrinsicsState = intrinsicsState this.intrinsicsState = intrinsicsState
} }