Kapt+JVM_IR: do not generate SuspendFunction supertypes

#KT-54380 Fixed
This commit is contained in:
Alexander Udalov
2023-02-02 00:10:48 +01:00
committed by Space Team
parent 93e92cbf74
commit 94f1c579df
7 changed files with 31 additions and 3 deletions
@@ -110,7 +110,7 @@ class ClassCodegen private constructor(
private val classOrigin = irClass.descriptorOrigin
private val visitor = state.factory.newVisitor(classOrigin, type, irClass.fileParent.loadSourceFilesInfo()).apply {
val signature = typeMapper.mapClassSignature(irClass, type)
val signature = typeMapper.mapClassSignature(irClass, type, context.state.classBuilderMode.generateBodies)
// Ensure that the backend only produces class names that would be valid in the frontend for JVM.
if (context.state.classBuilderMode.generateBodies && signature.hasInvalidName()) {
throw IllegalStateException("Generating class with invalid name '${type.className}': ${irClass.dump()}")
@@ -218,7 +218,7 @@ private val KOTLIN_MARKER_INTERFACES: Map<FqName, String> = run {
kotlinMarkerInterfaces
}
internal fun IrTypeMapper.mapClassSignature(irClass: IrClass, type: Type): JvmClassSignature {
internal fun IrTypeMapper.mapClassSignature(irClass: IrClass, type: Type, generateBodies: Boolean): JvmClassSignature {
val sw = BothSignatureWriter(BothSignatureWriter.Mode.CLASS)
writeFormalTypeParameters(irClass.typeParameters, sw)
@@ -234,7 +234,11 @@ internal fun IrTypeMapper.mapClassSignature(irClass: IrClass, type: Type): JvmCl
sw.writeSuperclassEnd()
val kotlinMarkerInterfaces = LinkedHashSet<String>()
if (irClass.superTypes.any { it.isSuspendFunction() || it.isKSuspendFunction() }) {
if (generateBodies && irClass.superTypes.any { it.isSuspendFunction() || it.isKSuspendFunction() }) {
// Do not generate this class in the kapt3 mode (generateBodies=false), because kapt3 transforms supertypes correctly in the
// "correctErrorTypes" mode only when the number of supertypes between PSI and bytecode is equal. Otherwise it tries to "correct"
// the FunctionN type and fails, because that type doesn't need an import in the Kotlin source (kotlin.FunctionN), but needs one
// in the Java source (kotlin.jvm.functions.FunctionN), and kapt3 doesn't perform any Kotlin->Java name lookup.
kotlinMarkerInterfaces.add("kotlin/coroutines/jvm/internal/SuspendFunction")
}