diff --git a/compiler/fir/fir2ir/tests-gen/org/jetbrains/kotlin/test/runners/codegen/FirBlackBoxCodegenTestGenerated.java b/compiler/fir/fir2ir/tests-gen/org/jetbrains/kotlin/test/runners/codegen/FirBlackBoxCodegenTestGenerated.java index c72cf8b5f35..7298918b111 100644 --- a/compiler/fir/fir2ir/tests-gen/org/jetbrains/kotlin/test/runners/codegen/FirBlackBoxCodegenTestGenerated.java +++ b/compiler/fir/fir2ir/tests-gen/org/jetbrains/kotlin/test/runners/codegen/FirBlackBoxCodegenTestGenerated.java @@ -6004,6 +6004,12 @@ public class FirBlackBoxCodegenTestGenerated extends AbstractFirBlackBoxCodegenT runTest("compiler/testData/codegen/box/collections/inSetWithSmartCast.kt"); } + @Test + @TestMetadata("inheritFromAbstractMutableListInt.kt") + public void testInheritFromAbstractMutableListInt() throws Exception { + runTest("compiler/testData/codegen/box/collections/inheritFromAbstractMutableListInt.kt"); + } + @Test @TestMetadata("inheritFromHashtable.kt") public void testInheritFromHashtable() throws Exception { diff --git a/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/lower/BridgeLowering.kt b/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/lower/BridgeLowering.kt index 31806050ced..bca8f12e450 100644 --- a/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/lower/BridgeLowering.kt +++ b/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/lower/BridgeLowering.kt @@ -205,22 +205,25 @@ internal class BridgeLowering(val context: JvmBackendContext) : FileLoweringPass // can contain a static replacement for a function 'remove', which forces value parameter boxing // in order to avoid signature clash with 'remove(int)' method in 'java.util.List'. // We should rewrite this static replacement as well ('remove' function itself is handled during special bridge processing). - for (irFunction in declaration.functions) { - val originalFunction = context.inlineClassReplacements.originalFunctionForStaticReplacement[irFunction] - ?: continue - if (context.methodSignatureMapper.shouldBoxSingleValueParameterForSpecialCaseOfRemove(originalFunction)) { - val oldValueParameter1 = irFunction.valueParameters[1] - val newValueParameter1 = oldValueParameter1.copyTo(irFunction, type = oldValueParameter1.type.makeNullable()) - irFunction.valueParameters = listOf(irFunction.valueParameters[0], newValueParameter1) - irFunction.body?.transform(VariableRemapper(mapOf(oldValueParameter1 to newValueParameter1)), null) - break - } + val remove = declaration.functions.find { + val original = context.inlineClassReplacements.originalFunctionForStaticReplacement[it] + original != null && context.methodSignatureMapper.shouldBoxSingleValueParameterForSpecialCaseOfRemove(original) + } + if (remove != null) { + makeLastParameterNullable(remove) } } return super.visitClass(declaration) } + private fun makeLastParameterNullable(irFunction: IrSimpleFunction) { + val oldValueParameter = irFunction.valueParameters.last() + val newValueParameter = oldValueParameter.copyTo(irFunction, type = oldValueParameter.type.makeNullable()) + irFunction.valueParameters = irFunction.valueParameters.dropLast(1) + newValueParameter + irFunction.body?.transform(VariableRemapper(mapOf(oldValueParameter to newValueParameter)), null) + } + private fun generateBridges() { for (member in potentialBridgeTargets) { val parent = member.parentAsClass @@ -479,6 +482,10 @@ internal class BridgeLowering(val context: JvmBackendContext) : FileLoweringPass if (specialBridge.isOverriding) { overriddenSymbols = listOf(specialBridge.overridden.symbol) } + + if (context.methodSignatureMapper.shouldBoxSingleValueParameterForSpecialCaseOfRemove(this)) { + makeLastParameterNullable(this) + } } private fun IrSimpleFunction.rewriteSpecialMethodBody( diff --git a/compiler/testData/codegen/box/collections/inheritFromAbstractMutableListInt.kt b/compiler/testData/codegen/box/collections/inheritFromAbstractMutableListInt.kt new file mode 100644 index 00000000000..d590f6490c2 --- /dev/null +++ b/compiler/testData/codegen/box/collections/inheritFromAbstractMutableListInt.kt @@ -0,0 +1,18 @@ +// WITH_RUNTIME +// IGNORE_BACKEND: WASM + +abstract class A : AbstractMutableList() + +class B : A() { + override fun iterator(): MutableIterator = mutableListOf(0).iterator() + override val size = 0 + override fun add(index: Int, element: Int) {} + override fun get(index: Int) = index + override fun removeAt(index: Int) = index + override fun set(index: Int, element: Int) = index +} + +fun box(): String { + val b = B() + return if (b.remove(0)) "OK" else "Fail" +} diff --git a/compiler/testData/codegen/bytecodeListing/collectionStubs/inheritingFromAbstractMutableList.kt b/compiler/testData/codegen/bytecodeListing/collectionStubs/inheritingFromAbstractMutableList.kt index 23ef37cb5fa..52fc3fd633d 100644 --- a/compiler/testData/codegen/bytecodeListing/collectionStubs/inheritingFromAbstractMutableList.kt +++ b/compiler/testData/codegen/bytecodeListing/collectionStubs/inheritingFromAbstractMutableList.kt @@ -1,3 +1,5 @@ // WITH_RUNTIME abstract class AMListD : AbstractMutableList() + +abstract class AMListI : AbstractMutableList() diff --git a/compiler/testData/codegen/bytecodeListing/collectionStubs/inheritingFromAbstractMutableList.txt b/compiler/testData/codegen/bytecodeListing/collectionStubs/inheritingFromAbstractMutableList.txt index 5d105afc626..cc52b04ac2e 100644 --- a/compiler/testData/codegen/bytecodeListing/collectionStubs/inheritingFromAbstractMutableList.txt +++ b/compiler/testData/codegen/bytecodeListing/collectionStubs/inheritingFromAbstractMutableList.txt @@ -13,3 +13,19 @@ public abstract class AMListD { public bridge final method remove(p0: java.lang.Object): boolean public synthetic bridge method removeAt(p0: int): java.lang.Object } + +@kotlin.Metadata +public abstract class AMListI { + // source: 'inheritingFromAbstractMutableList.kt' + public method (): void + public bridge method contains(p0: int): boolean + public bridge final method contains(p0: java.lang.Object): boolean + public bridge method indexOf(p0: int): int + public bridge final method indexOf(p0: java.lang.Object): int + public bridge method lastIndexOf(p0: int): int + public bridge final method lastIndexOf(p0: java.lang.Object): int + public bridge final method remove(p0: int): int + public bridge method remove(p0: java.lang.Integer): boolean + public bridge final method remove(p0: java.lang.Object): boolean + public synthetic bridge method removeAt(p0: int): java.lang.Object +} diff --git a/compiler/testData/codegen/bytecodeListing/collectionStubs/inheritingFromAbstractMutableList_ir.txt b/compiler/testData/codegen/bytecodeListing/collectionStubs/inheritingFromAbstractMutableList_ir.txt index c51fc23b2d5..a1222ad681c 100644 --- a/compiler/testData/codegen/bytecodeListing/collectionStubs/inheritingFromAbstractMutableList_ir.txt +++ b/compiler/testData/codegen/bytecodeListing/collectionStubs/inheritingFromAbstractMutableList_ir.txt @@ -14,3 +14,20 @@ public abstract class AMListD { public abstract method removeAt(p0: int): java.lang.Double public synthetic bridge method removeAt(p0: int): java.lang.Object } + +@kotlin.Metadata +public abstract class AMListI { + // source: 'inheritingFromAbstractMutableList.kt' + public method (): void + public bridge method contains(p0: int): boolean + public bridge final method contains(p0: java.lang.Object): boolean + public bridge method indexOf(p0: int): int + public bridge final method indexOf(p0: java.lang.Object): int + public bridge method lastIndexOf(p0: int): int + public bridge final method lastIndexOf(p0: java.lang.Object): int + public bridge final method remove(p0: int): int + public bridge method remove(p0: java.lang.Integer): boolean + public bridge final method remove(p0: java.lang.Object): boolean + public abstract method removeAt(p0: int): java.lang.Integer + public synthetic bridge method removeAt(p0: int): java.lang.Object +} diff --git a/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/codegen/BlackBoxCodegenTestGenerated.java b/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/codegen/BlackBoxCodegenTestGenerated.java index 1624a6288d2..a11ab7b8ab9 100644 --- a/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/codegen/BlackBoxCodegenTestGenerated.java +++ b/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/codegen/BlackBoxCodegenTestGenerated.java @@ -6004,6 +6004,12 @@ public class BlackBoxCodegenTestGenerated extends AbstractBlackBoxCodegenTest { runTest("compiler/testData/codegen/box/collections/inSetWithSmartCast.kt"); } + @Test + @TestMetadata("inheritFromAbstractMutableListInt.kt") + public void testInheritFromAbstractMutableListInt() throws Exception { + runTest("compiler/testData/codegen/box/collections/inheritFromAbstractMutableListInt.kt"); + } + @Test @TestMetadata("inheritFromHashtable.kt") public void testInheritFromHashtable() throws Exception { diff --git a/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/codegen/IrBlackBoxCodegenTestGenerated.java b/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/codegen/IrBlackBoxCodegenTestGenerated.java index 45e257e5f43..ca0eead1316 100644 --- a/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/codegen/IrBlackBoxCodegenTestGenerated.java +++ b/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/codegen/IrBlackBoxCodegenTestGenerated.java @@ -6004,6 +6004,12 @@ public class IrBlackBoxCodegenTestGenerated extends AbstractIrBlackBoxCodegenTes runTest("compiler/testData/codegen/box/collections/inSetWithSmartCast.kt"); } + @Test + @TestMetadata("inheritFromAbstractMutableListInt.kt") + public void testInheritFromAbstractMutableListInt() throws Exception { + runTest("compiler/testData/codegen/box/collections/inheritFromAbstractMutableListInt.kt"); + } + @Test @TestMetadata("inheritFromHashtable.kt") public void testInheritFromHashtable() throws Exception { diff --git a/compiler/tests-gen/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java b/compiler/tests-gen/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java index d30d3dca426..c38286ec237 100644 --- a/compiler/tests-gen/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java +++ b/compiler/tests-gen/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java @@ -5246,6 +5246,11 @@ public class LightAnalysisModeTestGenerated extends AbstractLightAnalysisModeTes runTest("compiler/testData/codegen/box/collections/inSetWithSmartCast.kt"); } + @TestMetadata("inheritFromAbstractMutableListInt.kt") + public void testInheritFromAbstractMutableListInt() throws Exception { + runTest("compiler/testData/codegen/box/collections/inheritFromAbstractMutableListInt.kt"); + } + @TestMetadata("inheritFromHashtable.kt") public void testInheritFromHashtable() throws Exception { runTest("compiler/testData/codegen/box/collections/inheritFromHashtable.kt"); diff --git a/js/js.tests/tests-gen/org/jetbrains/kotlin/js/test/es6/semantics/IrJsCodegenBoxES6TestGenerated.java b/js/js.tests/tests-gen/org/jetbrains/kotlin/js/test/es6/semantics/IrJsCodegenBoxES6TestGenerated.java index 90e6b463654..31980105650 100644 --- a/js/js.tests/tests-gen/org/jetbrains/kotlin/js/test/es6/semantics/IrJsCodegenBoxES6TestGenerated.java +++ b/js/js.tests/tests-gen/org/jetbrains/kotlin/js/test/es6/semantics/IrJsCodegenBoxES6TestGenerated.java @@ -4171,6 +4171,11 @@ public class IrJsCodegenBoxES6TestGenerated extends AbstractIrJsCodegenBoxES6Tes runTest("compiler/testData/codegen/box/collections/inSetWithSmartCast.kt"); } + @TestMetadata("inheritFromAbstractMutableListInt.kt") + public void testInheritFromAbstractMutableListInt() throws Exception { + runTest("compiler/testData/codegen/box/collections/inheritFromAbstractMutableListInt.kt"); + } + @TestMetadata("internalRemove.kt") public void testInternalRemove() throws Exception { runTest("compiler/testData/codegen/box/collections/internalRemove.kt"); diff --git a/js/js.tests/tests-gen/org/jetbrains/kotlin/js/test/ir/semantics/IrJsCodegenBoxTestGenerated.java b/js/js.tests/tests-gen/org/jetbrains/kotlin/js/test/ir/semantics/IrJsCodegenBoxTestGenerated.java index 5c70594e3c5..211f0e6f5b9 100644 --- a/js/js.tests/tests-gen/org/jetbrains/kotlin/js/test/ir/semantics/IrJsCodegenBoxTestGenerated.java +++ b/js/js.tests/tests-gen/org/jetbrains/kotlin/js/test/ir/semantics/IrJsCodegenBoxTestGenerated.java @@ -4171,6 +4171,11 @@ public class IrJsCodegenBoxTestGenerated extends AbstractIrJsCodegenBoxTest { runTest("compiler/testData/codegen/box/collections/inSetWithSmartCast.kt"); } + @TestMetadata("inheritFromAbstractMutableListInt.kt") + public void testInheritFromAbstractMutableListInt() throws Exception { + runTest("compiler/testData/codegen/box/collections/inheritFromAbstractMutableListInt.kt"); + } + @TestMetadata("internalRemove.kt") public void testInternalRemove() throws Exception { runTest("compiler/testData/codegen/box/collections/internalRemove.kt"); diff --git a/js/js.tests/tests-gen/org/jetbrains/kotlin/js/test/semantics/JsCodegenBoxTestGenerated.java b/js/js.tests/tests-gen/org/jetbrains/kotlin/js/test/semantics/JsCodegenBoxTestGenerated.java index d878b61273d..76aacddeb11 100644 --- a/js/js.tests/tests-gen/org/jetbrains/kotlin/js/test/semantics/JsCodegenBoxTestGenerated.java +++ b/js/js.tests/tests-gen/org/jetbrains/kotlin/js/test/semantics/JsCodegenBoxTestGenerated.java @@ -4171,6 +4171,11 @@ public class JsCodegenBoxTestGenerated extends AbstractJsCodegenBoxTest { runTest("compiler/testData/codegen/box/collections/inSetWithSmartCast.kt"); } + @TestMetadata("inheritFromAbstractMutableListInt.kt") + public void testInheritFromAbstractMutableListInt() throws Exception { + runTest("compiler/testData/codegen/box/collections/inheritFromAbstractMutableListInt.kt"); + } + @TestMetadata("internalRemove.kt") public void testInternalRemove() throws Exception { runTest("compiler/testData/codegen/box/collections/internalRemove.kt"); diff --git a/js/js.tests/tests-gen/org/jetbrains/kotlin/js/test/wasm/semantics/IrCodegenBoxWasmTestGenerated.java b/js/js.tests/tests-gen/org/jetbrains/kotlin/js/test/wasm/semantics/IrCodegenBoxWasmTestGenerated.java index c987aa08263..03b20b33eca 100644 --- a/js/js.tests/tests-gen/org/jetbrains/kotlin/js/test/wasm/semantics/IrCodegenBoxWasmTestGenerated.java +++ b/js/js.tests/tests-gen/org/jetbrains/kotlin/js/test/wasm/semantics/IrCodegenBoxWasmTestGenerated.java @@ -2992,6 +2992,11 @@ public class IrCodegenBoxWasmTestGenerated extends AbstractIrCodegenBoxWasmTest KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("compiler/testData/codegen/box/collections"), Pattern.compile("^([^_](.+))\\.kt$"), null, TargetBackend.WASM, true); } + @TestMetadata("inheritFromAbstractMutableListInt.kt") + public void testInheritFromAbstractMutableListInt() throws Exception { + runTest("compiler/testData/codegen/box/collections/inheritFromAbstractMutableListInt.kt"); + } + @TestMetadata("internalRemove.kt") public void testInternalRemove() throws Exception { runTest("compiler/testData/codegen/box/collections/internalRemove.kt");