JVM IR: fix noarg plugin for sealed class with existing noarg ctor

#KT-48111 Fixed
This commit is contained in:
Alexander Udalov
2021-08-17 18:24:51 +02:00
parent d3de0109ca
commit b1bce6a29e
4 changed files with 31 additions and 5 deletions

View File

@@ -50,10 +50,7 @@ private class NoArgIrTransformer(
override fun visitClass(declaration: IrClass) {
super.visitClass(declaration)
if (declaration.kind == ClassKind.CLASS &&
declaration.isAnnotatedWithNoarg() &&
declaration.constructors.none { it.isZeroParameterConstructor() }
) {
if (needsNoargConstructor(declaration)) {
declaration.declarations.add(getOrGenerateNoArgConstructor(declaration))
}
}
@@ -66,7 +63,7 @@ private class NoArgIrTransformer(
?: context.irBuiltIns.anyClass.owner
val superConstructor =
if (superClass.isAnnotatedWithNoarg())
if (needsNoargConstructor(superClass))
getOrGenerateNoArgConstructor(superClass)
else superClass.constructors.singleOrNull { it.isZeroParameterConstructor() }
?: error("No noarg super constructor for ${klass.render()}:\n" + superClass.constructors.joinToString("\n") { it.render() })
@@ -90,6 +87,11 @@ private class NoArgIrTransformer(
}
}
private fun needsNoargConstructor(declaration: IrClass): Boolean =
declaration.kind == ClassKind.CLASS &&
declaration.isAnnotatedWithNoarg() &&
declaration.constructors.none { it.isZeroParameterConstructor() }
private fun IrClass.isAnnotatedWithNoarg(): Boolean =
toIrBasedDescriptor().hasSpecialAnnotation(null)

View File

@@ -65,6 +65,11 @@ public class BlackBoxCodegenTestForNoArgGenerated extends AbstractBlackBoxCodege
runTest("plugins/noarg/noarg-cli/testData/box/nestedClass.kt");
}
@TestMetadata("sealedClassWithExistingNoargCtor.kt")
public void testSealedClassWithExistingNoargCtor() throws Exception {
runTest("plugins/noarg/noarg-cli/testData/box/sealedClassWithExistingNoargCtor.kt");
}
@TestMetadata("simple.kt")
public void testSimple() throws Exception {
runTest("plugins/noarg/noarg-cli/testData/box/simple.kt");

View File

@@ -65,6 +65,11 @@ public class IrBlackBoxCodegenTestForNoArgGenerated extends AbstractIrBlackBoxCo
runTest("plugins/noarg/noarg-cli/testData/box/nestedClass.kt");
}
@TestMetadata("sealedClassWithExistingNoargCtor.kt")
public void testSealedClassWithExistingNoargCtor() throws Exception {
runTest("plugins/noarg/noarg-cli/testData/box/sealedClassWithExistingNoargCtor.kt");
}
@TestMetadata("simple.kt")
public void testSimple() throws Exception {
runTest("plugins/noarg/noarg-cli/testData/box/simple.kt");

View File

@@ -0,0 +1,14 @@
// WITH_RUNTIME
annotation class NoArg
@NoArg
sealed class MappedSuperClass
@NoArg
class ConcreteClass(val x: String) : MappedSuperClass()
fun box(): String {
ConcreteClass::class.java.getConstructor().newInstance()
return "OK"
}