From 6adad1055b7a25506aa87bf56d881b952e3405e5 Mon Sep 17 00:00:00 2001 From: Alexander Udalov Date: Fri, 1 May 2020 18:58:45 +0200 Subject: [PATCH] JVM IR: generate delegates to DefaultImpls for fun interfaces --- .../jetbrains/kotlin/backend/common/ir/IrUtils.kt | 6 +++--- .../lower/AbstractSuspendFunctionsLowering.kt | 2 +- .../common/lower/SingleAbstractMethodLowering.kt | 4 ++-- .../AbstractSuspendFunctionsLowering.kt | 2 +- .../jvm/lower/FunctionReferenceLowering.kt | 15 ++++++++------- .../box/funInterface/funInterfaceInheritance.kt | 1 - .../codegen/box/funInterface/nonAbstractMethod.kt | 2 +- 7 files changed, 16 insertions(+), 16 deletions(-) diff --git a/compiler/ir/backend.common/src/org/jetbrains/kotlin/backend/common/ir/IrUtils.kt b/compiler/ir/backend.common/src/org/jetbrains/kotlin/backend/common/ir/IrUtils.kt index 53ec736a96c..6e8ddf4e3be 100644 --- a/compiler/ir/backend.common/src/org/jetbrains/kotlin/backend/common/ir/IrUtils.kt +++ b/compiler/ir/backend.common/src/org/jetbrains/kotlin/backend/common/ir/IrUtils.kt @@ -485,7 +485,7 @@ val IrFunction.allParameters: List explicitParameters } -fun IrClass.addFakeOverrides(implementedMembers: List = emptyList()) { +fun IrClass.addFakeOverridesViaIncorrectHeuristic(implementedMembers: List = emptyList()) { fun IrDeclaration.toList() = when (this) { is IrSimpleFunction -> listOf(this) is IrProperty -> listOfNotNull(getter, setter) @@ -519,7 +519,7 @@ fun IrClass.addFakeOverrides(implementedMembers: List = emptyL IrDeclarationOrigin.FAKE_OVERRIDE, IrSimpleFunctionSymbolImpl(descriptor), irFunction.name, - Visibilities.INHERITED, + Visibilities.PUBLIC, irFunction.modality, irFunction.returnType, isInline = irFunction.isInline, @@ -531,7 +531,7 @@ fun IrClass.addFakeOverrides(implementedMembers: List = emptyL isOperator = irFunction.isOperator ).apply { descriptor.bind(this) - parent = this@addFakeOverrides + parent = this@addFakeOverridesViaIncorrectHeuristic overriddenSymbols = overriddenFunctions.map { it.symbol } copyParameterDeclarationsFrom(irFunction) copyAttributes(irFunction) diff --git a/compiler/ir/backend.common/src/org/jetbrains/kotlin/backend/common/lower/AbstractSuspendFunctionsLowering.kt b/compiler/ir/backend.common/src/org/jetbrains/kotlin/backend/common/lower/AbstractSuspendFunctionsLowering.kt index 27943bc6557..43e85cb3981 100644 --- a/compiler/ir/backend.common/src/org/jetbrains/kotlin/backend/common/lower/AbstractSuspendFunctionsLowering.kt +++ b/compiler/ir/backend.common/src/org/jetbrains/kotlin/backend/common/lower/AbstractSuspendFunctionsLowering.kt @@ -367,7 +367,7 @@ abstract class AbstractSuspendFunctionsLowering(val co } coroutineClass.superTypes += superTypes - coroutineClass.addFakeOverrides() + coroutineClass.addFakeOverridesViaIncorrectHeuristic() initializeStateMachine(coroutineConstructors, coroutineClassThis) diff --git a/compiler/ir/backend.common/src/org/jetbrains/kotlin/backend/common/lower/SingleAbstractMethodLowering.kt b/compiler/ir/backend.common/src/org/jetbrains/kotlin/backend/common/lower/SingleAbstractMethodLowering.kt index 19a7728a76a..79d4a2c5d98 100644 --- a/compiler/ir/backend.common/src/org/jetbrains/kotlin/backend/common/lower/SingleAbstractMethodLowering.kt +++ b/compiler/ir/backend.common/src/org/jetbrains/kotlin/backend/common/lower/SingleAbstractMethodLowering.kt @@ -8,7 +8,7 @@ package org.jetbrains.kotlin.backend.common.lower import org.jetbrains.kotlin.backend.common.CommonBackendContext import org.jetbrains.kotlin.backend.common.FileLoweringPass import org.jetbrains.kotlin.backend.common.IrElementTransformerVoidWithContext -import org.jetbrains.kotlin.backend.common.ir.addFakeOverrides +import org.jetbrains.kotlin.backend.common.ir.addFakeOverridesViaIncorrectHeuristic import org.jetbrains.kotlin.backend.common.ir.copyTo import org.jetbrains.kotlin.backend.common.ir.createImplicitParameterDeclarationWithWrappedDescriptor import org.jetbrains.kotlin.descriptors.ClassKind @@ -215,7 +215,7 @@ abstract class SingleAbstractMethodLowering(val context: CommonBackendContext) : generateEqualsHashCode(subclass, superType, field) - subclass.addFakeOverrides() + subclass.addFakeOverridesViaIncorrectHeuristic() return subclass } diff --git a/compiler/ir/backend.js/src/org/jetbrains/kotlin/ir/backend/js/lower/coroutines/AbstractSuspendFunctionsLowering.kt b/compiler/ir/backend.js/src/org/jetbrains/kotlin/ir/backend/js/lower/coroutines/AbstractSuspendFunctionsLowering.kt index 032fdbf4e60..94b576aeb50 100644 --- a/compiler/ir/backend.js/src/org/jetbrains/kotlin/ir/backend/js/lower/coroutines/AbstractSuspendFunctionsLowering.kt +++ b/compiler/ir/backend.js/src/org/jetbrains/kotlin/ir/backend/js/lower/coroutines/AbstractSuspendFunctionsLowering.kt @@ -468,7 +468,7 @@ abstract class AbstractSuspendFunctionsLowering(val co coroutineClass.superTypes += coroutineBaseClass.defaultType } - coroutineClass.addFakeOverrides(implementedMembers) + coroutineClass.addFakeOverridesViaIncorrectHeuristic(implementedMembers) // TODO: to meet PIR lower model constructor modification shouldn't be performed here initializeStateMachine(listOf(coroutineConstructor), coroutineClassThis) diff --git a/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/lower/FunctionReferenceLowering.kt b/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/lower/FunctionReferenceLowering.kt index b9083f748ff..dc1c34c0c97 100644 --- a/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/lower/FunctionReferenceLowering.kt +++ b/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/lower/FunctionReferenceLowering.kt @@ -7,10 +7,7 @@ package org.jetbrains.kotlin.backend.jvm.lower import org.jetbrains.kotlin.backend.common.FileLoweringPass import org.jetbrains.kotlin.backend.common.IrElementTransformerVoidWithContext -import org.jetbrains.kotlin.backend.common.ir.copyTo -import org.jetbrains.kotlin.backend.common.ir.createImplicitParameterDeclarationWithWrappedDescriptor -import org.jetbrains.kotlin.backend.common.ir.isSuspend -import org.jetbrains.kotlin.backend.common.ir.moveBodyTo +import org.jetbrains.kotlin.backend.common.ir.* import org.jetbrains.kotlin.backend.common.phaser.makeIrFilePhase import org.jetbrains.kotlin.backend.jvm.JvmBackendContext import org.jetbrains.kotlin.backend.jvm.JvmLoweredDeclarationOrigin @@ -150,10 +147,11 @@ internal class FunctionReferenceLowering(private val context: JvmBackendContext) private val adaptedReferenceOriginalTarget: IrFunction? = adapteeCall?.symbol?.owner private val isAdaptedReference = adaptedReferenceOriginalTarget != null + private val isKotlinFunInterface = + samSuperType != null && samSuperType.getClass()?.origin != IrDeclarationOrigin.IR_EXTERNAL_JAVA_DECLARATION_STUB + private val needToGenerateSamEqualsHashCodeMethods = - samSuperType != null && - samSuperType.getClass()?.origin != IrDeclarationOrigin.IR_EXTERNAL_JAVA_DECLARATION_STUB && - (isAdaptedReference || !isLambda) + isKotlinFunInterface && (isAdaptedReference || !isLambda) private val superType = samSuperType ?: when { @@ -229,6 +227,9 @@ internal class FunctionReferenceLowering(private val context: JvmBackendContext) if (needToGenerateSamEqualsHashCodeMethods) { generateSamEqualsHashCodeMethods(boundReceiverVar) } + if (isKotlinFunInterface) { + functionReferenceClass.addFakeOverridesViaIncorrectHeuristic() + } +functionReferenceClass +irCall(constructor.symbol).apply { diff --git a/compiler/testData/codegen/box/funInterface/funInterfaceInheritance.kt b/compiler/testData/codegen/box/funInterface/funInterfaceInheritance.kt index 007b92fa13b..937e1266cb0 100644 --- a/compiler/testData/codegen/box/funInterface/funInterfaceInheritance.kt +++ b/compiler/testData/codegen/box/funInterface/funInterfaceInheritance.kt @@ -1,5 +1,4 @@ // !LANGUAGE: +NewInference +FunctionalInterfaceConversion +SamConversionPerArgument +SamConversionForKotlinFunctions -// IGNORE_BACKEND: JVM_IR // IGNORE_BACKEND_FIR: JVM_IR // SKIP_DCE_DRIVEN diff --git a/compiler/testData/codegen/box/funInterface/nonAbstractMethod.kt b/compiler/testData/codegen/box/funInterface/nonAbstractMethod.kt index f5a4b5fe509..2b5a188c64a 100644 --- a/compiler/testData/codegen/box/funInterface/nonAbstractMethod.kt +++ b/compiler/testData/codegen/box/funInterface/nonAbstractMethod.kt @@ -1,4 +1,4 @@ -// IGNORE_BACKEND: JVM_IR, JS_IR +// IGNORE_BACKEND: JS_IR // IGNORE_BACKEND_FIR: JVM_IR interface I {