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 f01cde503d4..5d70c0457a9 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 @@ -20596,6 +20596,18 @@ public class FirBlackBoxCodegenTestGenerated extends AbstractFirBlackBoxCodegenT runTest("compiler/testData/codegen/box/invokedynamic/sam/functionRefToJavaInterface/boundMemberRef.kt"); } + @Test + @TestMetadata("boundRefToSuperClassMethod.kt") + public void testBoundRefToSuperClassMethod() throws Exception { + runTest("compiler/testData/codegen/box/invokedynamic/sam/functionRefToJavaInterface/boundRefToSuperClassMethod.kt"); + } + + @Test + @TestMetadata("boundRefToSuperInterfaceMethod.kt") + public void testBoundRefToSuperInterfaceMethod() throws Exception { + runTest("compiler/testData/codegen/box/invokedynamic/sam/functionRefToJavaInterface/boundRefToSuperInterfaceMethod.kt"); + } + @Test @TestMetadata("constructorRef.kt") public void testConstructorRef() throws Exception { diff --git a/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/lower/TypeOperatorLowering.kt b/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/lower/TypeOperatorLowering.kt index 51034e352a0..945d42a7583 100644 --- a/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/lower/TypeOperatorLowering.kt +++ b/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/lower/TypeOperatorLowering.kt @@ -29,8 +29,7 @@ import org.jetbrains.kotlin.ir.symbols.IrFunctionSymbol import org.jetbrains.kotlin.ir.symbols.IrSimpleFunctionSymbol import org.jetbrains.kotlin.ir.symbols.IrTypeParameterSymbol import org.jetbrains.kotlin.ir.types.* -import org.jetbrains.kotlin.ir.util.dump -import org.jetbrains.kotlin.ir.util.render +import org.jetbrains.kotlin.ir.util.* import org.jetbrains.kotlin.ir.visitors.IrElementVisitorVoid import org.jetbrains.kotlin.ir.visitors.acceptChildrenVoid import org.jetbrains.kotlin.ir.visitors.acceptVoid @@ -294,7 +293,8 @@ private class TypeOperatorLowering(private val context: JvmBackendContext) : Fil when (targetFun) { is IrSimpleFunction -> { if (refDispatchReceiver != null) { - addValueParameter("p${syntheticParameterIndex++}", targetFun.dispatchReceiverParameter!!.type) + // Fake overrides may have inexact dispatch receiver type. + addValueParameter("p${syntheticParameterIndex++}", targetFun.parentAsClass.defaultType) dynamicCallArguments.add(refDispatchReceiver) } if (refExtensionReceiver != null) { diff --git a/compiler/testData/codegen/box/invokedynamic/sam/functionRefToJavaInterface/boundRefToSuperClassMethod.kt b/compiler/testData/codegen/box/invokedynamic/sam/functionRefToJavaInterface/boundRefToSuperClassMethod.kt new file mode 100644 index 00000000000..7b8cf582c54 --- /dev/null +++ b/compiler/testData/codegen/box/invokedynamic/sam/functionRefToJavaInterface/boundRefToSuperClassMethod.kt @@ -0,0 +1,36 @@ +// TARGET_BACKEND: JVM +// JVM_TARGET: 1.8 +// SAM_CONVERSIONS: INDY +// WITH_RUNTIME +// FULL_JDK + +// FILE: boundRefToSuperClassMethod.kt + +class Impl(val set1: Set, val set2: Set) : JDerived() { + override fun is1(x: String) = x in set1 + override fun is2(x: String) = x in set2 +} + +fun cmp(d: JDerived) = + Comparator.comparing(d::is1) + .thenComparing(d::is2) + +fun box(): String { + val cmp = cmp(Impl(setOf("a", "c"), setOf("c", "d"))) + val list = listOf("e", "d", "c", "b", "a").sortedWith(cmp) + if (list != listOf("e", "b", "d", "a", "c")) + return "Failed: ${list.toString()}" + return "OK" +} + +// FILE: JBase.java + +public abstract class JBase { + public abstract boolean is1(String x); +} + +// FILE: JDerived.java + +public abstract class JDerived extends JBase { + public abstract boolean is2(String x); +} diff --git a/compiler/testData/codegen/box/invokedynamic/sam/functionRefToJavaInterface/boundRefToSuperInterfaceMethod.kt b/compiler/testData/codegen/box/invokedynamic/sam/functionRefToJavaInterface/boundRefToSuperInterfaceMethod.kt new file mode 100644 index 00000000000..f5122da281d --- /dev/null +++ b/compiler/testData/codegen/box/invokedynamic/sam/functionRefToJavaInterface/boundRefToSuperInterfaceMethod.kt @@ -0,0 +1,36 @@ +// TARGET_BACKEND: JVM +// JVM_TARGET: 1.8 +// SAM_CONVERSIONS: INDY +// WITH_RUNTIME +// FULL_JDK + +// FILE: boundRefToSuperInterfaceMethod.kt + +class Impl(val set1: Set, val set2: Set) : JDerived { + override fun is1(x: String) = x in set1 + override fun is2(x: String) = x in set2 +} + +fun cmp(d: JDerived) = + Comparator.comparing(d::is1) + .thenComparing(d::is2) + +fun box(): String { + val cmp = cmp(Impl(setOf("a", "c"), setOf("c", "d"))) + val list = listOf("e", "d", "c", "b", "a").sortedWith(cmp) + if (list != listOf("e", "b", "d", "a", "c")) + return "Failed: ${list.toString()}" + return "OK" +} + +// FILE: JBase.java + +public interface JBase { + boolean is1(String x); +} + +// FILE: JDerived.java + +public interface JDerived extends JBase { + boolean is2(String x); +} 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 f12b5c51277..85887f4307e 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 @@ -20584,6 +20584,18 @@ public class BlackBoxCodegenTestGenerated extends AbstractBlackBoxCodegenTest { runTest("compiler/testData/codegen/box/invokedynamic/sam/functionRefToJavaInterface/boundMemberRef.kt"); } + @Test + @TestMetadata("boundRefToSuperClassMethod.kt") + public void testBoundRefToSuperClassMethod() throws Exception { + runTest("compiler/testData/codegen/box/invokedynamic/sam/functionRefToJavaInterface/boundRefToSuperClassMethod.kt"); + } + + @Test + @TestMetadata("boundRefToSuperInterfaceMethod.kt") + public void testBoundRefToSuperInterfaceMethod() throws Exception { + runTest("compiler/testData/codegen/box/invokedynamic/sam/functionRefToJavaInterface/boundRefToSuperInterfaceMethod.kt"); + } + @Test @TestMetadata("constructorRef.kt") public void testConstructorRef() 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 92198057c4e..5e71719233e 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 @@ -20596,6 +20596,18 @@ public class IrBlackBoxCodegenTestGenerated extends AbstractIrBlackBoxCodegenTes runTest("compiler/testData/codegen/box/invokedynamic/sam/functionRefToJavaInterface/boundMemberRef.kt"); } + @Test + @TestMetadata("boundRefToSuperClassMethod.kt") + public void testBoundRefToSuperClassMethod() throws Exception { + runTest("compiler/testData/codegen/box/invokedynamic/sam/functionRefToJavaInterface/boundRefToSuperClassMethod.kt"); + } + + @Test + @TestMetadata("boundRefToSuperInterfaceMethod.kt") + public void testBoundRefToSuperInterfaceMethod() throws Exception { + runTest("compiler/testData/codegen/box/invokedynamic/sam/functionRefToJavaInterface/boundRefToSuperInterfaceMethod.kt"); + } + @Test @TestMetadata("constructorRef.kt") public void testConstructorRef() 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 af85264c098..f7a1ac97ae0 100644 --- a/compiler/tests-gen/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java +++ b/compiler/tests-gen/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java @@ -17209,6 +17209,16 @@ public class LightAnalysisModeTestGenerated extends AbstractLightAnalysisModeTes runTest("compiler/testData/codegen/box/invokedynamic/sam/functionRefToJavaInterface/boundMemberRef.kt"); } + @TestMetadata("boundRefToSuperClassMethod.kt") + public void testBoundRefToSuperClassMethod() throws Exception { + runTest("compiler/testData/codegen/box/invokedynamic/sam/functionRefToJavaInterface/boundRefToSuperClassMethod.kt"); + } + + @TestMetadata("boundRefToSuperInterfaceMethod.kt") + public void testBoundRefToSuperInterfaceMethod() throws Exception { + runTest("compiler/testData/codegen/box/invokedynamic/sam/functionRefToJavaInterface/boundRefToSuperInterfaceMethod.kt"); + } + @TestMetadata("constructorRef.kt") public void testConstructorRef() throws Exception { runTest("compiler/testData/codegen/box/invokedynamic/sam/functionRefToJavaInterface/constructorRef.kt");