JVM IR: process type arguments for function references in DefaultImpls

Local functions for lambdas declared in DefaultImpls are now also
getting additional type parameters from the containing class (see
4dd794c2d2), so function references to them should be adapted similarly
to normal calls.

This change fixes
`box/coroutines/featureIntersection/interfaceMethodWithBodyGeneric.kt`.
This commit is contained in:
Alexander Udalov
2020-02-05 18:08:55 +01:00
parent 763cb6dd6f
commit 5d766eace4
4 changed files with 26 additions and 54 deletions
@@ -244,18 +244,22 @@ fun createDelegatingCallWithPlaceholderTypeArguments(existingCall: IrCall, redir
valueArgumentsCount = redirectTarget.valueParameters.size, valueArgumentsCount = redirectTarget.valueParameters.size,
origin = existingCall.origin origin = existingCall.origin
).apply { ).apply {
copyValueArgumentsFrom( copyFromWithPlaceholderTypeArguments(existingCall, irBuiltIns)
existingCall, }
existingCall.symbol.owner,
this.symbol.owner, fun IrFunctionAccessExpression.copyFromWithPlaceholderTypeArguments(existingCall: IrFunctionAccessExpression, irBuiltIns: IrBuiltIns) {
receiversAsArguments = true, copyValueArgumentsFrom(
argumentsAsReceivers = false existingCall,
) existingCall.symbol.owner,
var offset = 0 this.symbol.owner,
existingCall.symbol.owner.parentAsClass.typeParameters.forEach { _ -> receiversAsArguments = true,
putTypeArgument(offset++, createPlaceholderAnyNType(irBuiltIns)) argumentsAsReceivers = false,
} )
for (i in 0 until (existingCall.typeArgumentsCount)) { var offset = 0
putTypeArgument(i + offset, existingCall.getTypeArgument(i)) existingCall.symbol.owner.parentAsClass.typeParameters.forEach { _ ->
} putTypeArgument(offset++, createPlaceholderAnyNType(irBuiltIns))
} }
for (i in 0 until existingCall.typeArgumentsCount) {
putTypeArgument(i + offset, existingCall.getTypeArgument(i))
}
}
@@ -11,6 +11,7 @@ import org.jetbrains.kotlin.backend.common.ir.moveBodyTo
import org.jetbrains.kotlin.backend.jvm.JvmBackendContext import org.jetbrains.kotlin.backend.jvm.JvmBackendContext
import org.jetbrains.kotlin.backend.jvm.JvmLoweredDeclarationOrigin import org.jetbrains.kotlin.backend.jvm.JvmLoweredDeclarationOrigin
import org.jetbrains.kotlin.backend.jvm.codegen.isJvmInterface import org.jetbrains.kotlin.backend.jvm.codegen.isJvmInterface
import org.jetbrains.kotlin.backend.jvm.ir.copyFromWithPlaceholderTypeArguments
import org.jetbrains.kotlin.backend.jvm.ir.createDelegatingCallWithPlaceholderTypeArguments import org.jetbrains.kotlin.backend.jvm.ir.createDelegatingCallWithPlaceholderTypeArguments
import org.jetbrains.kotlin.backend.jvm.ir.createPlaceholderAnyNType import org.jetbrains.kotlin.backend.jvm.ir.createPlaceholderAnyNType
import org.jetbrains.kotlin.backend.jvm.ir.hasJvmDefault import org.jetbrains.kotlin.backend.jvm.ir.hasJvmDefault
@@ -264,15 +265,10 @@ internal class InterfaceLowering(val context: JvmBackendContext) : IrElementTran
if (newFunction != null) { if (newFunction != null) {
with(expression) { with(expression) {
IrFunctionReferenceImpl( IrFunctionReferenceImpl(
startOffset, startOffset, endOffset, type, newFunction.symbol, newFunction.typeParameters.size, newFunction.valueParameters.size,
endOffset, expression.reflectionTarget, origin
type,
newFunction.symbol,
typeArgumentsCount,
expression.reflectionTarget,
origin
).apply { ).apply {
copyTypeAndValueArgumentsFrom(expression, receiversAsArguments = true) copyFromWithPlaceholderTypeArguments(expression, context.irBuiltIns)
copyAttributes(expression) copyAttributes(expression)
} }
} }
@@ -20,8 +20,7 @@ import org.jetbrains.kotlin.ir.symbols.*
interface IrCallableReference : IrMemberAccessExpression interface IrCallableReference : IrMemberAccessExpression
interface IrFunctionReference : IrCallableReference { interface IrFunctionReference : IrCallableReference, IrFunctionAccessExpression {
override val symbol: IrFunctionSymbol
val reflectionTarget: IrFunctionSymbol? val reflectionTarget: IrFunctionSymbol?
} }
@@ -43,4 +42,4 @@ interface IrLocalDelegatedPropertyReference : IrCallableReference {
val delegate: IrVariableSymbol val delegate: IrVariableSymbol
val getter: IrSimpleFunctionSymbol val getter: IrSimpleFunctionSymbol
val setter: IrSimpleFunctionSymbol? val setter: IrSimpleFunctionSymbol?
} }
@@ -18,7 +18,6 @@ import org.jetbrains.kotlin.ir.expressions.impl.IrConstructorCallImpl
import org.jetbrains.kotlin.ir.expressions.impl.IrTypeOperatorCallImpl import org.jetbrains.kotlin.ir.expressions.impl.IrTypeOperatorCallImpl
import org.jetbrains.kotlin.ir.symbols.* import org.jetbrains.kotlin.ir.symbols.*
import org.jetbrains.kotlin.ir.types.* import org.jetbrains.kotlin.ir.types.*
import org.jetbrains.kotlin.ir.types.impl.originalKotlinType
import org.jetbrains.kotlin.name.FqName import org.jetbrains.kotlin.name.FqName
import org.jetbrains.kotlin.name.SpecialNames import org.jetbrains.kotlin.name.SpecialNames
import org.jetbrains.kotlin.psi.psiUtil.endOffset import org.jetbrains.kotlin.psi.psiUtil.endOffset
@@ -555,35 +554,9 @@ fun IrFunctionAccessExpression.copyTypeAndValueArgumentsFrom(
src: IrFunctionAccessExpression, src: IrFunctionAccessExpression,
receiversAsArguments: Boolean = false, receiversAsArguments: Boolean = false,
argumentsAsReceivers: Boolean = false argumentsAsReceivers: Boolean = false
) = copyTypeAndValueArgumentsFrom(
src,
src.symbol.owner,
symbol.owner,
receiversAsArguments,
argumentsAsReceivers
)
fun IrFunctionReference.copyTypeAndValueArgumentsFrom(
src: IrFunctionReference,
receiversAsArguments: Boolean = false,
argumentsAsReceivers: Boolean = false
) = copyTypeAndValueArgumentsFrom(
src,
src.symbol.owner,
symbol.owner,
receiversAsArguments,
argumentsAsReceivers
)
private fun IrMemberAccessExpression.copyTypeAndValueArgumentsFrom(
src: IrMemberAccessExpression,
srcFunction: IrFunction,
destFunction: IrFunction,
receiversAsArguments: Boolean = false,
argumentsAsReceivers: Boolean = false
) { ) {
copyTypeArgumentsFrom(src) copyTypeArgumentsFrom(src)
copyValueArgumentsFrom(src, srcFunction, destFunction, receiversAsArguments, argumentsAsReceivers) copyValueArgumentsFrom(src, src.symbol.owner, symbol.owner, receiversAsArguments, argumentsAsReceivers)
} }
fun IrMemberAccessExpression.copyValueArgumentsFrom( fun IrMemberAccessExpression.copyValueArgumentsFrom(