diff --git a/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/JvmSymbols.kt b/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/JvmSymbols.kt index 68f30643992..47efb0579ba 100644 --- a/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/JvmSymbols.kt +++ b/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/JvmSymbols.kt @@ -316,6 +316,13 @@ class JvmSymbols( } } + // invoke redirects to get + klass.addFunction("invoke", irBuiltIns.anyNType, Modality.FINAL).apply { + for (i in 0 until n) { + addValueParameter("receiver$i", irBuiltIns.anyNType) + } + } + if (mutable) { klass.addFunction("set", irBuiltIns.unitType, Modality.ABSTRACT).apply { for (i in 0 until n) { diff --git a/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/ir/IrUtils.kt b/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/ir/IrUtils.kt index 0f64b1239c5..3103b791af7 100644 --- a/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/ir/IrUtils.kt +++ b/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/ir/IrUtils.kt @@ -137,4 +137,4 @@ fun JvmBackendContext.createJvmIrBuilder( fun IrDeclaration.isInCurrentModule(): Boolean = - getPackageFragment() is IrFile \ No newline at end of file + getPackageFragment() is IrFile diff --git a/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/lower/PropertyReferenceLowering.kt b/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/lower/PropertyReferenceLowering.kt index 66b9bd3c1a8..c177e42b98d 100644 --- a/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/lower/PropertyReferenceLowering.kt +++ b/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/lower/PropertyReferenceLowering.kt @@ -113,6 +113,19 @@ internal class PropertyReferenceLowering(val context: JvmBackendContext) : Class } } + private fun IrClass.addFakeOverride(method: IrSimpleFunction) = + addFunction { + name = method.name + returnType = method.returnType + visibility = method.visibility + origin = IrDeclarationOrigin.FAKE_OVERRIDE + }.apply { + overriddenSymbols.add(method.symbol) + dispatchReceiverParameter = thisReceiver!!.copyTo(this) + for (parameter in method.valueParameters) + valueParameters.add(parameter.copyTo(this)) + } + private class PropertyReferenceKind( val interfaceSymbol: IrClassSymbol, val reflectedSymbol: IrClassSymbol, @@ -246,6 +259,7 @@ internal class PropertyReferenceLowering(val context: JvmBackendContext) : Class val getSignature = superClass.functions.single { it.name.asString() == "getSignature" } val get = superClass.functions.find { it.name.asString() == "get" } val set = superClass.functions.find { it.name.asString() == "set" } + val invoke = superClass.functions.find { it.name.asString() == "invoke" } referenceClass.addSimpleDelegatingConstructor(superConstructor, context.irBuiltIns, isPrimary = true) referenceClass.addOverride(getName) { irString(expression.descriptor.name.asString()) } @@ -277,6 +291,7 @@ internal class PropertyReferenceLowering(val context: JvmBackendContext) : Class setCallArguments(this, arguments) } } + referenceClass.addFakeOverride(invoke!!) } expression.setter?.owner?.let { setter -> diff --git a/compiler/testData/codegen/boxAgainstJava/sam/propertyReference.kt b/compiler/testData/codegen/boxAgainstJava/sam/propertyReference.kt new file mode 100644 index 00000000000..e61f8a3add8 --- /dev/null +++ b/compiler/testData/codegen/boxAgainstJava/sam/propertyReference.kt @@ -0,0 +1,14 @@ +// FILE: Base.java + +interface Interface { + String call(String t); +} + +// FILE: 1.kt + +val String.property: String + get() = this + +fun box(): String { + return Interface(String::property).call("OK") +} diff --git a/compiler/tests/org/jetbrains/kotlin/codegen/BlackBoxAgainstJavaCodegenTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/codegen/BlackBoxAgainstJavaCodegenTestGenerated.java index 82fa61d23a6..7375eeaebeb 100644 --- a/compiler/tests/org/jetbrains/kotlin/codegen/BlackBoxAgainstJavaCodegenTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/codegen/BlackBoxAgainstJavaCodegenTestGenerated.java @@ -675,6 +675,11 @@ public class BlackBoxAgainstJavaCodegenTestGenerated extends AbstractBlackBoxAga runTest("compiler/testData/codegen/boxAgainstJava/sam/kt4753_2.kt"); } + @TestMetadata("propertyReference.kt") + public void testPropertyReference() throws Exception { + runTest("compiler/testData/codegen/boxAgainstJava/sam/propertyReference.kt"); + } + @TestMetadata("samConstructorGenericSignature.kt") public void testSamConstructorGenericSignature() throws Exception { runTest("compiler/testData/codegen/boxAgainstJava/sam/samConstructorGenericSignature.kt"); diff --git a/compiler/tests/org/jetbrains/kotlin/codegen/ir/IrBlackBoxAgainstJavaCodegenTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/codegen/ir/IrBlackBoxAgainstJavaCodegenTestGenerated.java index 7f3c12e5875..f58efdc63e2 100644 --- a/compiler/tests/org/jetbrains/kotlin/codegen/ir/IrBlackBoxAgainstJavaCodegenTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/codegen/ir/IrBlackBoxAgainstJavaCodegenTestGenerated.java @@ -676,6 +676,11 @@ public class IrBlackBoxAgainstJavaCodegenTestGenerated extends AbstractIrBlackBo runTest("compiler/testData/codegen/boxAgainstJava/sam/kt4753_2.kt"); } + @TestMetadata("propertyReference.kt") + public void testPropertyReference() throws Exception { + runTest("compiler/testData/codegen/boxAgainstJava/sam/propertyReference.kt"); + } + @TestMetadata("samConstructorGenericSignature.kt") public void testSamConstructorGenericSignature() throws Exception { runTest("compiler/testData/codegen/boxAgainstJava/sam/samConstructorGenericSignature.kt");