Kapt+JVM_IR: do not generate SuspendFunction supertypes
#KT-54380 Fixed
This commit is contained in:
committed by
Space Team
parent
93e92cbf74
commit
94f1c579df
+1
-1
@@ -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()}")
|
||||
|
||||
+6
-2
@@ -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")
|
||||
}
|
||||
|
||||
|
||||
+6
@@ -539,6 +539,12 @@ class ClassFileToSourceStubConverter(val kaptContext: KaptContextForStubGenerati
|
||||
val sameSuperClassCount = (superClass == null) == (defaultSuperTypes.superClass == null)
|
||||
val sameSuperInterfaceCount = superInterfaces.size == defaultSuperTypes.interfaces.size
|
||||
|
||||
// Note: if the number of supertypes is different, it might mean either that one of them is unresolved, or that backend generated
|
||||
// additional supertypes which were not present in the PSI.
|
||||
// In the former case, the subsequent code behaves as expected, trying to recover the types from the PSI.
|
||||
// In the latter case, ideally we shouldn't do anything, but most of the time invoking error type correction is harmless because
|
||||
// it will be a no-op. However, it might lead to problems for non-trivial types such as `kotlin.FunctionN` which are mapped to
|
||||
// `kotlin.jvm.functions.FunctionN`, because the Java source requires a new import, unlike the Kotlin source.
|
||||
if (sameSuperClassCount && sameSuperInterfaceCount) {
|
||||
return defaultSuperTypes
|
||||
}
|
||||
|
||||
@@ -0,0 +1,3 @@
|
||||
// CORRECT_ERROR_TYPES
|
||||
|
||||
interface SomeInterface : suspend (List<String>, Boolean) -> List<Int>
|
||||
@@ -0,0 +1,3 @@
|
||||
@kotlin.Metadata()
|
||||
public abstract interface SomeInterface extends kotlin.jvm.functions.Function3<java.util.List<? extends java.lang.String>, java.lang.Boolean, kotlin.coroutines.Continuation<? super java.util.List<? extends java.lang.Integer>>, java.lang.Object> {
|
||||
}
|
||||
+6
@@ -619,6 +619,12 @@ public class ClassFileToSourceStubConverterTestGenerated extends AbstractClassFi
|
||||
runTest("plugins/kapt3/kapt3-compiler/testData/converter/suspendErrorTypes.kt");
|
||||
}
|
||||
|
||||
@Test
|
||||
@TestMetadata("suspendFunctionSupertype.kt")
|
||||
public void testSuspendFunctionSupertype() throws Exception {
|
||||
runTest("plugins/kapt3/kapt3-compiler/testData/converter/suspendFunctionSupertype.kt");
|
||||
}
|
||||
|
||||
@Test
|
||||
@TestMetadata("topLevel.kt")
|
||||
public void testTopLevel() throws Exception {
|
||||
|
||||
+6
@@ -619,6 +619,12 @@ public class IrClassFileToSourceStubConverterTestGenerated extends AbstractIrCla
|
||||
runTest("plugins/kapt3/kapt3-compiler/testData/converter/suspendErrorTypes.kt");
|
||||
}
|
||||
|
||||
@Test
|
||||
@TestMetadata("suspendFunctionSupertype.kt")
|
||||
public void testSuspendFunctionSupertype() throws Exception {
|
||||
runTest("plugins/kapt3/kapt3-compiler/testData/converter/suspendFunctionSupertype.kt");
|
||||
}
|
||||
|
||||
@Test
|
||||
@TestMetadata("topLevel.kt")
|
||||
public void testTopLevel() throws Exception {
|
||||
|
||||
Reference in New Issue
Block a user