JVM_IR KT-43109 generate internal bridge for custom internal 'toArray'

Also add some tests for internal collection stubs.
This commit is contained in:
Dmitry Petrov
2020-12-04 11:18:22 +03:00
parent 149bcc2d22
commit 3dbe02b7fe
20 changed files with 266 additions and 15 deletions
@@ -5023,6 +5023,16 @@ public class FirBlackBoxCodegenTestGenerated extends AbstractFirBlackBoxCodegenT
runTest("compiler/testData/codegen/box/collections/inheritFromHashtable.kt");
}
@TestMetadata("internalRemove.kt")
public void testInternalRemove() throws Exception {
runTest("compiler/testData/codegen/box/collections/internalRemove.kt");
}
@TestMetadata("internalRemoveFromJava.kt")
public void testInternalRemoveFromJava() throws Exception {
runTest("compiler/testData/codegen/box/collections/internalRemoveFromJava.kt");
}
@TestMetadata("irrelevantImplCharSequence.kt")
public void testIrrelevantImplCharSequence() throws Exception {
runTest("compiler/testData/codegen/box/collections/irrelevantImplCharSequence.kt");
@@ -49,11 +49,11 @@ private class ToArrayLowering(private val context: JvmBackendContext) : ClassLow
it.origin != IrDeclarationOrigin.IR_EXTERNAL_JAVA_DECLARATION_STUB
}?.isCollectionSubClass == true
irClass.findOrCreate(indirectCollectionSubClass, { it.isGenericToArray(context) }) {
irClass.findOrCreate(indirectCollectionSubClass, { it.isGenericToArray(context) }) { bridge ->
irClass.addFunction {
name = Name.identifier("toArray")
origin = JvmLoweredDeclarationOrigin.TO_ARRAY
modality = Modality.OPEN
modality = if (bridge != null) Modality.FINAL else Modality.OPEN
}.apply {
val elementType = addTypeParameter {
name = Name.identifier("T")
@@ -67,19 +67,26 @@ private class ToArrayLowering(private val context: JvmBackendContext) : ClassLow
}
val prototype = addValueParameter("array", returnType, JvmLoweredDeclarationOrigin.TO_ARRAY)
body = context.createIrBuilder(symbol).irBlockBody {
+irReturn(irCall(symbols.genericToArray, symbols.genericToArray.owner.returnType).apply {
putValueArgument(0, irGet(receiver))
putValueArgument(1, irGet(prototype))
})
if (bridge == null) {
+irReturn(irCall(symbols.genericToArray, symbols.genericToArray.owner.returnType).apply {
putValueArgument(0, irGet(receiver))
putValueArgument(1, irGet(prototype))
})
} else {
+irReturn(irCall(bridge.target.symbol, bridge.target.returnType).apply {
dispatchReceiver = irGet(receiver)
putValueArgument(0, irGet(prototype))
})
}
}
}
}
irClass.findOrCreate(indirectCollectionSubClass, IrSimpleFunction::isNonGenericToArray) {
irClass.findOrCreate(indirectCollectionSubClass, IrSimpleFunction::isNonGenericToArray) { bridge ->
irClass.addFunction {
name = Name.identifier("toArray")
origin = JvmLoweredDeclarationOrigin.TO_ARRAY
modality = Modality.OPEN
modality = if (bridge != null) Modality.FINAL else Modality.OPEN
returnType = context.irBuiltIns.arrayClass.typeWith(context.irBuiltIns.anyNType)
}.apply {
val receiver = addDispatchReceiver {
@@ -87,28 +94,49 @@ private class ToArrayLowering(private val context: JvmBackendContext) : ClassLow
origin = JvmLoweredDeclarationOrigin.TO_ARRAY
}
body = context.createIrBuilder(symbol).irBlockBody {
+irReturn(irCall(symbols.nonGenericToArray, symbols.nonGenericToArray.owner.returnType).apply {
putValueArgument(0, irGet(receiver))
})
if (bridge == null) {
+irReturn(irCall(symbols.nonGenericToArray, symbols.nonGenericToArray.owner.returnType).apply {
putValueArgument(0, irGet(receiver))
})
} else {
+irReturn(irCall(bridge.target.symbol, bridge.target.returnType).apply {
dispatchReceiver = irGet(receiver)
})
}
}
}
}
}
private fun IrClass.findOrCreate(indirectSubclass: Boolean, matcher: (IrSimpleFunction) -> Boolean, fallback: () -> IrSimpleFunction) {
private class ToArrayBridge(
val target: IrSimpleFunction
)
private fun IrClass.findOrCreate(
indirectSubclass: Boolean,
matcher: (IrSimpleFunction) -> Boolean,
fallback: (ToArrayBridge?) -> IrSimpleFunction
) {
val existing = functions.find(matcher)
if (existing != null) {
// This is an explicit override of a method defined in `kotlin.collections.AbstractCollection`
// or `java.util.Collection`. From here on, the frontend will check the existence of implementations;
// we just need to match visibility in the former case to the latter.
existing.visibility = DescriptorVisibilities.PUBLIC
if (existing.visibility == DescriptorVisibilities.INTERNAL) {
// If existing `toArray` is internal, create a public bridge method
// to preserve binary compatibility with code generated by the old back-end.
// NB This will preserve existing custom `toArray` signature.
fallback(ToArrayBridge(existing))
} else {
existing.visibility = DescriptorVisibilities.PUBLIC
}
return
}
if (indirectSubclass) {
// There's a Kotlin class up the hierarchy that should already have `toArray`.
return
}
fallback()
fallback(null)
}
}
@@ -0,0 +1,16 @@
class Test<T> : Collection<T> {
override val size: Int get() = TODO()
override fun contains(element: T): Boolean = TODO()
override fun containsAll(elements: Collection<T>): Boolean = TODO()
override fun isEmpty(): Boolean = TODO()
override fun iterator(): Iterator<T> = TODO()
internal fun remove(x: T): Boolean = false
}
fun box(): String {
return if (Test<String>().remove("") == false)
"OK"
else
"Fail"
}
@@ -0,0 +1,34 @@
// TARGET_BACKEND: JVM
// IGNORE_BACKEND: JVM
// ^ KT-43334 AbstractMethodError when calling 'remove' from Java on a Kotlin Collection with custom internal 'remove'
// fixed in JVM_IR
// FILE: internalRemoveFromJava.kt
class Test<T> : Collection<T> {
override val size: Int get() = TODO()
override fun contains(element: T): Boolean = TODO()
override fun containsAll(elements: Collection<T>): Boolean = TODO()
override fun isEmpty(): Boolean = TODO()
override fun iterator(): Iterator<T> = TODO()
internal fun remove(x: T): Boolean = false
}
fun box(): String {
val t = Test<String>()
return if (J.testRemove(t, "") == false)
"OK"
else
"Fail"
}
// FILE: J.java
import java.util.Collection;
public class J {
public static <T> boolean testRemove(Collection<T> c, T x) {
return c.remove(x);
}
}
@@ -0,0 +1,9 @@
class Test<T> : Collection<T> {
override val size: Int get() = TODO()
override fun contains(element: T): Boolean = TODO()
override fun containsAll(elements: Collection<T>): Boolean = TODO()
override fun isEmpty(): Boolean = TODO()
override fun iterator(): Iterator<T> = TODO()
internal fun remove(x: T): Boolean = false
}
@@ -0,0 +1,19 @@
@kotlin.Metadata
public final class Test {
// source: 'collectionWithInternalRemove.kt'
public method <init>(): void
public method add(p0: java.lang.Object): boolean
public method addAll(p0: java.util.Collection): boolean
public method clear(): void
public method contains(p0: java.lang.Object): boolean
public method containsAll(@org.jetbrains.annotations.NotNull p0: java.util.Collection): boolean
public method getSize(): int
public method isEmpty(): boolean
public @org.jetbrains.annotations.NotNull method iterator(): java.util.Iterator
public final method remove$test_module(p0: java.lang.Object): boolean
public method removeAll(p0: java.util.Collection): boolean
public method retainAll(p0: java.util.Collection): boolean
public bridge final method size(): int
public method toArray(): java.lang.Object[]
public method toArray(p0: java.lang.Object[]): java.lang.Object[]
}
@@ -0,0 +1,20 @@
@kotlin.Metadata
public final class Test {
// source: 'collectionWithInternalRemove.kt'
public method <init>(): void
public method add(p0: java.lang.Object): boolean
public method addAll(p0: java.util.Collection): boolean
public method clear(): void
public method contains(p0: java.lang.Object): boolean
public method containsAll(@org.jetbrains.annotations.NotNull p0: java.util.Collection): boolean
public method getSize(): int
public method isEmpty(): boolean
public @org.jetbrains.annotations.NotNull method iterator(): java.util.Iterator
public final method remove$test_module(p0: java.lang.Object): boolean
public bridge final method remove(p0: java.lang.Object): boolean
public method removeAll(p0: java.util.Collection): boolean
public method retainAll(p0: java.util.Collection): boolean
public bridge final method size(): int
public method toArray(): java.lang.Object[]
public method toArray(p0: java.lang.Object[]): java.lang.Object[]
}
@@ -15,7 +15,8 @@ public final class InternalToArray {
public method removeAll(p0: java.util.Collection): boolean
public method retainAll(p0: java.util.Collection): boolean
public bridge final method size(): int
public final @org.jetbrains.annotations.NotNull method toArray(): java.lang.Integer[]
public final @org.jetbrains.annotations.NotNull method toArray$test_module(): java.lang.Integer[]
public final method toArray(): java.lang.Object[]
public method toArray(p0: java.lang.Object[]): java.lang.Object[]
}
@@ -0,0 +1,3 @@
class InternalGenericToArray<T>(d: Collection<T>): Collection<T> by d {
internal fun <T> toArray(arr: Array<T>): Array<T> = null!!
}
@@ -0,0 +1,20 @@
@kotlin.Metadata
public final class InternalGenericToArray {
// source: 'internalGenericToArray.kt'
private synthetic final field $$delegate_0: java.util.Collection
public method <init>(@org.jetbrains.annotations.NotNull p0: java.util.Collection): void
public method add(p0: java.lang.Object): boolean
public method addAll(p0: java.util.Collection): boolean
public method clear(): void
public method contains(p0: java.lang.Object): boolean
public method containsAll(@org.jetbrains.annotations.NotNull p0: java.util.Collection): boolean
public method getSize(): int
public method isEmpty(): boolean
public @org.jetbrains.annotations.NotNull method iterator(): java.util.Iterator
public method remove(p0: java.lang.Object): boolean
public method removeAll(p0: java.util.Collection): boolean
public method retainAll(p0: java.util.Collection): boolean
public bridge final method size(): int
public final @org.jetbrains.annotations.NotNull method toArray$test_module(@org.jetbrains.annotations.NotNull p0: java.lang.Object[]): java.lang.Object[]
public method toArray(): java.lang.Object[]
}
@@ -0,0 +1,21 @@
@kotlin.Metadata
public final class InternalGenericToArray {
// source: 'internalGenericToArray.kt'
private synthetic final field $$delegate_0: java.util.Collection
public method <init>(@org.jetbrains.annotations.NotNull p0: java.util.Collection): void
public method add(p0: java.lang.Object): boolean
public method addAll(p0: java.util.Collection): boolean
public method clear(): void
public method contains(p0: java.lang.Object): boolean
public method containsAll(@org.jetbrains.annotations.NotNull p0: java.util.Collection): boolean
public method getSize(): int
public method isEmpty(): boolean
public @org.jetbrains.annotations.NotNull method iterator(): java.util.Iterator
public method remove(p0: java.lang.Object): boolean
public method removeAll(p0: java.util.Collection): boolean
public method retainAll(p0: java.util.Collection): boolean
public bridge final method size(): int
public final @org.jetbrains.annotations.NotNull method toArray$test_module(@org.jetbrains.annotations.NotNull p0: java.lang.Object[]): java.lang.Object[]
public method toArray(): java.lang.Object[]
public final method toArray(p0: java.lang.Object[]): java.lang.Object[]
}
@@ -5053,6 +5053,16 @@ public class BlackBoxCodegenTestGenerated extends AbstractBlackBoxCodegenTest {
runTest("compiler/testData/codegen/box/collections/inheritFromHashtable.kt");
}
@TestMetadata("internalRemove.kt")
public void testInternalRemove() throws Exception {
runTest("compiler/testData/codegen/box/collections/internalRemove.kt");
}
@TestMetadata("internalRemoveFromJava.kt")
public void testInternalRemoveFromJava() throws Exception {
runTest("compiler/testData/codegen/box/collections/internalRemoveFromJava.kt");
}
@TestMetadata("irrelevantImplCharSequence.kt")
public void testIrrelevantImplCharSequence() throws Exception {
runTest("compiler/testData/codegen/box/collections/irrelevantImplCharSequence.kt");
@@ -344,6 +344,11 @@ public class BytecodeListingTestGenerated extends AbstractBytecodeListingTest {
runTest("compiler/testData/codegen/bytecodeListing/collectionStubs/collectionByDelegationWithFullJdk.kt");
}
@TestMetadata("collectionWithInternalRemove.kt")
public void testCollectionWithInternalRemove() throws Exception {
runTest("compiler/testData/codegen/bytecodeListing/collectionStubs/collectionWithInternalRemove.kt");
}
@TestMetadata("collectionsWithFullJdk.kt")
public void testCollectionsWithFullJdk() throws Exception {
runTest("compiler/testData/codegen/bytecodeListing/collectionStubs/collectionsWithFullJdk.kt");
@@ -629,6 +634,11 @@ public class BytecodeListingTestGenerated extends AbstractBytecodeListingTest {
runTest("compiler/testData/codegen/bytecodeListing/collectionStubs/toArray/customNonGenericToArray.kt");
}
@TestMetadata("internalGenericToArray.kt")
public void testInternalGenericToArray() throws Exception {
runTest("compiler/testData/codegen/bytecodeListing/collectionStubs/toArray/internalGenericToArray.kt");
}
@TestMetadata("noToArrayInJava.kt")
public void testNoToArrayInJava() throws Exception {
runTest("compiler/testData/codegen/bytecodeListing/collectionStubs/toArray/noToArrayInJava.kt");
@@ -5020,6 +5020,11 @@ public class LightAnalysisModeTestGenerated extends AbstractLightAnalysisModeTes
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)
public static class Collections extends AbstractLightAnalysisModeTest {
@TestMetadata("internalRemoveFromJava.kt")
public void ignoreInternalRemoveFromJava() throws Exception {
runTest("compiler/testData/codegen/box/collections/internalRemoveFromJava.kt");
}
private void runTest(String testDataFilePath) throws Exception {
KotlinTestUtils.runTest(this::doTest, TargetBackend.JVM, testDataFilePath);
}
@@ -5053,6 +5058,11 @@ public class LightAnalysisModeTestGenerated extends AbstractLightAnalysisModeTes
runTest("compiler/testData/codegen/box/collections/inheritFromHashtable.kt");
}
@TestMetadata("internalRemove.kt")
public void testInternalRemove() throws Exception {
runTest("compiler/testData/codegen/box/collections/internalRemove.kt");
}
@TestMetadata("irrelevantImplCharSequence.kt")
public void testIrrelevantImplCharSequence() throws Exception {
runTest("compiler/testData/codegen/box/collections/irrelevantImplCharSequence.kt");
@@ -5023,6 +5023,16 @@ public class IrBlackBoxCodegenTestGenerated extends AbstractIrBlackBoxCodegenTes
runTest("compiler/testData/codegen/box/collections/inheritFromHashtable.kt");
}
@TestMetadata("internalRemove.kt")
public void testInternalRemove() throws Exception {
runTest("compiler/testData/codegen/box/collections/internalRemove.kt");
}
@TestMetadata("internalRemoveFromJava.kt")
public void testInternalRemoveFromJava() throws Exception {
runTest("compiler/testData/codegen/box/collections/internalRemoveFromJava.kt");
}
@TestMetadata("irrelevantImplCharSequence.kt")
public void testIrrelevantImplCharSequence() throws Exception {
runTest("compiler/testData/codegen/box/collections/irrelevantImplCharSequence.kt");
@@ -344,6 +344,11 @@ public class IrBytecodeListingTestGenerated extends AbstractIrBytecodeListingTes
runTest("compiler/testData/codegen/bytecodeListing/collectionStubs/collectionByDelegationWithFullJdk.kt");
}
@TestMetadata("collectionWithInternalRemove.kt")
public void testCollectionWithInternalRemove() throws Exception {
runTest("compiler/testData/codegen/bytecodeListing/collectionStubs/collectionWithInternalRemove.kt");
}
@TestMetadata("collectionsWithFullJdk.kt")
public void testCollectionsWithFullJdk() throws Exception {
runTest("compiler/testData/codegen/bytecodeListing/collectionStubs/collectionsWithFullJdk.kt");
@@ -629,6 +634,11 @@ public class IrBytecodeListingTestGenerated extends AbstractIrBytecodeListingTes
runTest("compiler/testData/codegen/bytecodeListing/collectionStubs/toArray/customNonGenericToArray.kt");
}
@TestMetadata("internalGenericToArray.kt")
public void testInternalGenericToArray() throws Exception {
runTest("compiler/testData/codegen/bytecodeListing/collectionStubs/toArray/internalGenericToArray.kt");
}
@TestMetadata("noToArrayInJava.kt")
public void testNoToArrayInJava() throws Exception {
runTest("compiler/testData/codegen/bytecodeListing/collectionStubs/toArray/noToArrayInJava.kt");
@@ -4123,6 +4123,11 @@ public class IrJsCodegenBoxES6TestGenerated extends AbstractIrJsCodegenBoxES6Tes
runTest("compiler/testData/codegen/box/collections/inSetWithSmartCast.kt");
}
@TestMetadata("internalRemove.kt")
public void testInternalRemove() throws Exception {
runTest("compiler/testData/codegen/box/collections/internalRemove.kt");
}
@TestMetadata("kt41123.kt")
public void testKt41123() throws Exception {
runTest("compiler/testData/codegen/box/collections/kt41123.kt");
@@ -4123,6 +4123,11 @@ public class IrJsCodegenBoxTestGenerated extends AbstractIrJsCodegenBoxTest {
runTest("compiler/testData/codegen/box/collections/inSetWithSmartCast.kt");
}
@TestMetadata("internalRemove.kt")
public void testInternalRemove() throws Exception {
runTest("compiler/testData/codegen/box/collections/internalRemove.kt");
}
@TestMetadata("kt41123.kt")
public void testKt41123() throws Exception {
runTest("compiler/testData/codegen/box/collections/kt41123.kt");
@@ -4123,6 +4123,11 @@ public class JsCodegenBoxTestGenerated extends AbstractJsCodegenBoxTest {
runTest("compiler/testData/codegen/box/collections/inSetWithSmartCast.kt");
}
@TestMetadata("internalRemove.kt")
public void testInternalRemove() throws Exception {
runTest("compiler/testData/codegen/box/collections/internalRemove.kt");
}
@TestMetadata("kt41123.kt")
public void testKt41123() throws Exception {
runTest("compiler/testData/codegen/box/collections/kt41123.kt");
@@ -2979,6 +2979,11 @@ public class IrCodegenBoxWasmTestGenerated extends AbstractIrCodegenBoxWasmTest
KotlinTestUtils.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("compiler/testData/codegen/box/collections"), Pattern.compile("^([^_](.+))\\.kt$"), null, TargetBackend.WASM, true);
}
@TestMetadata("internalRemove.kt")
public void testInternalRemove() throws Exception {
runTest("compiler/testData/codegen/box/collections/internalRemove.kt");
}
@TestMetadata("removeClash.kt")
public void testRemoveClash() throws Exception {
runTest("compiler/testData/codegen/box/collections/removeClash.kt");