mirror of
https://github.com/jlengrand/kotlin.git
synced 2026-03-10 00:21:28 +00:00
JVM IR: fix noarg plugin for sealed class with existing noarg ctor
#KT-48111 Fixed
This commit is contained in:
@@ -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)
|
||||
|
||||
|
||||
@@ -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");
|
||||
|
||||
@@ -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");
|
||||
|
||||
14
plugins/noarg/noarg-cli/testData/box/sealedClassWithExistingNoargCtor.kt
vendored
Normal file
14
plugins/noarg/noarg-cli/testData/box/sealedClassWithExistingNoargCtor.kt
vendored
Normal 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"
|
||||
}
|
||||
Reference in New Issue
Block a user