PSI2IR: SAM conversion in varargs

This commit is contained in:
Dmitry Petrov
2020-06-02 19:00:02 +03:00
parent 16f175612e
commit 5efbe6ae15
9 changed files with 125 additions and 11 deletions
@@ -1595,6 +1595,11 @@ public class Fir2IrTextTestGenerated extends AbstractFir2IrTextTest {
runTest("compiler/testData/ir/irText/expressions/funInterface/samConversionInVarargs.kt"); runTest("compiler/testData/ir/irText/expressions/funInterface/samConversionInVarargs.kt");
} }
@TestMetadata("samConversionInVarargsMixed.kt")
public void testSamConversionInVarargsMixed() throws Exception {
runTest("compiler/testData/ir/irText/expressions/funInterface/samConversionInVarargsMixed.kt");
}
@TestMetadata("samConversionOnCallableReference.kt") @TestMetadata("samConversionOnCallableReference.kt")
public void testSamConversionOnCallableReference() throws Exception { public void testSamConversionOnCallableReference() throws Exception {
runTest("compiler/testData/ir/irText/expressions/funInterface/samConversionOnCallableReference.kt"); runTest("compiler/testData/ir/irText/expressions/funInterface/samConversionOnCallableReference.kt");
@@ -23,7 +23,10 @@ import org.jetbrains.kotlin.descriptors.*
import org.jetbrains.kotlin.descriptors.impl.SyntheticFieldDescriptor import org.jetbrains.kotlin.descriptors.impl.SyntheticFieldDescriptor
import org.jetbrains.kotlin.descriptors.impl.TypeAliasConstructorDescriptor import org.jetbrains.kotlin.descriptors.impl.TypeAliasConstructorDescriptor
import org.jetbrains.kotlin.incremental.components.NoLookupLocation import org.jetbrains.kotlin.incremental.components.NoLookupLocation
import org.jetbrains.kotlin.ir.builders.* import org.jetbrains.kotlin.ir.builders.irBlock
import org.jetbrains.kotlin.ir.builders.irBlockBody
import org.jetbrains.kotlin.ir.builders.irGet
import org.jetbrains.kotlin.ir.builders.irTemporary
import org.jetbrains.kotlin.ir.declarations.IrDeclarationOrigin import org.jetbrains.kotlin.ir.declarations.IrDeclarationOrigin
import org.jetbrains.kotlin.ir.declarations.IrSimpleFunction import org.jetbrains.kotlin.ir.declarations.IrSimpleFunction
import org.jetbrains.kotlin.ir.declarations.impl.IrFunctionImpl import org.jetbrains.kotlin.ir.declarations.impl.IrFunctionImpl
@@ -619,10 +622,11 @@ fun StatementGenerator.generateSamConversionForValueArgumentsIfRequired(call: Ca
for (i in underlyingValueParameters.indices) { for (i in underlyingValueParameters.indices) {
val underlyingValueParameter = underlyingValueParameters[i] val underlyingValueParameter = underlyingValueParameters[i]
val expectedSamConversionTypesForVararg = ArrayList<KotlinType?>()
if (expectSamConvertedArgumentToBeAvailableInResolvedCall && resolvedCall is NewResolvedCallImpl<*>) { if (expectSamConvertedArgumentToBeAvailableInResolvedCall && resolvedCall is NewResolvedCallImpl<*>) {
// TODO support SAM conversion of varargs val arguments = resolvedCall.valueArguments[originalValueParameters[i]]?.arguments ?: continue
val argument = resolvedCall.valueArguments[originalValueParameters[i]]?.arguments?.singleOrNull() ?: continue arguments.mapTo(expectedSamConversionTypesForVararg) { resolvedCall.getExpectedTypeForSamConvertedArgument(it) }
resolvedCall.getExpectedTypeForSamConvertedArgument(argument) ?: continue if (expectedSamConversionTypesForVararg.all { it == null }) continue
} else { } else {
// When the method is `f(T)` with `T` = a SAM type, the substituted type is a SAM while the original is not; // When the method is `f(T)` with `T` = a SAM type, the substituted type is a SAM while the original is not;
// when the method is `f(X<T>)` with `T` = `out V` where `X` is a SAM type, the substituted type is `Nothing` // when the method is `f(X<T>)` with `T` = `out V` where `X` is a SAM type, the substituted type is `Nothing`
@@ -678,11 +682,16 @@ fun StatementGenerator.generateSamConversionForValueArgumentsIfRequired(call: Ca
substitutedVarargType.toIrType(), substitutedVarargType.toIrType(),
irSamType irSamType
).apply { ).apply {
originalArgument.elements.mapTo(elements) { originalArgument.elements.mapIndexedTo(elements) { index, element ->
if (it is IrExpression) if (element is IrExpression) {
samConvertScalarExpression(it) val samType = expectedSamConversionTypesForVararg[index]
else if (samType != null)
samConvertScalarExpression(element)
else
element
} else {
throw AssertionError("Unsupported: spread vararg element with SAM conversion") throw AssertionError("Unsupported: spread vararg element with SAM conversion")
}
} }
} }
} }
@@ -1,4 +1,6 @@
// !LANGUAGE: +NewInference +FunctionalInterfaceConversion +SamConversionPerArgument // !LANGUAGE: +NewInference +FunctionalInterfaceConversion +SamConversionPerArgument
// IGNORE_BACKEND_FIR: JVM_IR
fun interface MyRunnable { fun interface MyRunnable {
fun run() fun run()
} }
+1
View File
@@ -1,4 +1,5 @@
// TARGET_BACKEND: JVM // TARGET_BACKEND: JVM
// IGNORE_BACKEND_FIR: JVM_IR
// FILE: kt31908.kt // FILE: kt31908.kt
fun box(): String { fun box(): String {
var result = "failed" var result = "failed"
@@ -35,21 +35,21 @@ FILE fqName:<root> fileName:/samConversionInVarargs.kt
BLOCK_BODY BLOCK_BODY
CALL 'public final fun useVararg (vararg foos: <root>.IFoo): kotlin.Unit declared in <root>' type=kotlin.Unit origin=null CALL 'public final fun useVararg (vararg foos: <root>.IFoo): kotlin.Unit declared in <root>' type=kotlin.Unit origin=null
foos: VARARG type=kotlin.Array<out <root>.IFoo> varargElementType=<root>.IFoo foos: VARARG type=kotlin.Array<out <root>.IFoo> varargElementType=<root>.IFoo
TYPE_OP type=<root>.IFoo origin=IMPLICIT_CAST typeOperand=<root>.IFoo TYPE_OP type=<root>.IFoo origin=SAM_CONVERSION typeOperand=<root>.IFoo
FUN_EXPR type=kotlin.Function1<@[ParameterName(name = 'i')] kotlin.Int, kotlin.Unit> origin=LAMBDA FUN_EXPR type=kotlin.Function1<@[ParameterName(name = 'i')] kotlin.Int, kotlin.Unit> origin=LAMBDA
FUN LOCAL_FUNCTION_FOR_LAMBDA name:<anonymous> visibility:local modality:FINAL <> (it:kotlin.Int) returnType:kotlin.Unit FUN LOCAL_FUNCTION_FOR_LAMBDA name:<anonymous> visibility:local modality:FINAL <> (it:kotlin.Int) returnType:kotlin.Unit
VALUE_PARAMETER name:it index:0 type:kotlin.Int VALUE_PARAMETER name:it index:0 type:kotlin.Int
BLOCK_BODY BLOCK_BODY
RETURN type=kotlin.Nothing from='local final fun <anonymous> (it: kotlin.Int): kotlin.Unit declared in <root>.testSeveralLambdas' RETURN type=kotlin.Nothing from='local final fun <anonymous> (it: kotlin.Int): kotlin.Unit declared in <root>.testSeveralLambdas'
GET_OBJECT 'CLASS IR_EXTERNAL_DECLARATION_STUB OBJECT name:Unit modality:FINAL visibility:public superTypes:[kotlin.Any]' type=kotlin.Unit GET_OBJECT 'CLASS IR_EXTERNAL_DECLARATION_STUB OBJECT name:Unit modality:FINAL visibility:public superTypes:[kotlin.Any]' type=kotlin.Unit
TYPE_OP type=<root>.IFoo origin=IMPLICIT_CAST typeOperand=<root>.IFoo TYPE_OP type=<root>.IFoo origin=SAM_CONVERSION typeOperand=<root>.IFoo
FUN_EXPR type=kotlin.Function1<@[ParameterName(name = 'i')] kotlin.Int, kotlin.Unit> origin=LAMBDA FUN_EXPR type=kotlin.Function1<@[ParameterName(name = 'i')] kotlin.Int, kotlin.Unit> origin=LAMBDA
FUN LOCAL_FUNCTION_FOR_LAMBDA name:<anonymous> visibility:local modality:FINAL <> (it:kotlin.Int) returnType:kotlin.Unit FUN LOCAL_FUNCTION_FOR_LAMBDA name:<anonymous> visibility:local modality:FINAL <> (it:kotlin.Int) returnType:kotlin.Unit
VALUE_PARAMETER name:it index:0 type:kotlin.Int VALUE_PARAMETER name:it index:0 type:kotlin.Int
BLOCK_BODY BLOCK_BODY
RETURN type=kotlin.Nothing from='local final fun <anonymous> (it: kotlin.Int): kotlin.Unit declared in <root>.testSeveralLambdas' RETURN type=kotlin.Nothing from='local final fun <anonymous> (it: kotlin.Int): kotlin.Unit declared in <root>.testSeveralLambdas'
GET_OBJECT 'CLASS IR_EXTERNAL_DECLARATION_STUB OBJECT name:Unit modality:FINAL visibility:public superTypes:[kotlin.Any]' type=kotlin.Unit GET_OBJECT 'CLASS IR_EXTERNAL_DECLARATION_STUB OBJECT name:Unit modality:FINAL visibility:public superTypes:[kotlin.Any]' type=kotlin.Unit
TYPE_OP type=<root>.IFoo origin=IMPLICIT_CAST typeOperand=<root>.IFoo TYPE_OP type=<root>.IFoo origin=SAM_CONVERSION typeOperand=<root>.IFoo
FUN_EXPR type=kotlin.Function1<@[ParameterName(name = 'i')] kotlin.Int, kotlin.Unit> origin=LAMBDA FUN_EXPR type=kotlin.Function1<@[ParameterName(name = 'i')] kotlin.Int, kotlin.Unit> origin=LAMBDA
FUN LOCAL_FUNCTION_FOR_LAMBDA name:<anonymous> visibility:local modality:FINAL <> (it:kotlin.Int) returnType:kotlin.Unit FUN LOCAL_FUNCTION_FOR_LAMBDA name:<anonymous> visibility:local modality:FINAL <> (it:kotlin.Int) returnType:kotlin.Unit
VALUE_PARAMETER name:it index:0 type:kotlin.Int VALUE_PARAMETER name:it index:0 type:kotlin.Int
@@ -0,0 +1,39 @@
FILE fqName:<root> fileName:/samConversionInVarargsMixed.kt
CLASS INTERFACE name:MyRunnable modality:ABSTRACT visibility:public superTypes:[kotlin.Any]
$this: VALUE_PARAMETER INSTANCE_RECEIVER name:<this> type:<root>.MyRunnable
FUN name:run visibility:public modality:ABSTRACT <> ($this:<root>.MyRunnable) returnType:kotlin.Unit
$this: VALUE_PARAMETER name:<this> type:<root>.MyRunnable
FUN FAKE_OVERRIDE name:equals visibility:public modality:OPEN <> ($this:kotlin.Any, other:kotlin.Any?) returnType:kotlin.Boolean [fake_override,operator]
overridden:
public open fun equals (other: kotlin.Any?): kotlin.Boolean [operator] declared in kotlin.Any
$this: VALUE_PARAMETER name:<this> type:kotlin.Any
VALUE_PARAMETER name:other index:0 type:kotlin.Any?
FUN FAKE_OVERRIDE name:hashCode visibility:public modality:OPEN <> ($this:kotlin.Any) returnType:kotlin.Int [fake_override]
overridden:
public open fun hashCode (): kotlin.Int declared in kotlin.Any
$this: VALUE_PARAMETER name:<this> type:kotlin.Any
FUN FAKE_OVERRIDE name:toString visibility:public modality:OPEN <> ($this:kotlin.Any) returnType:kotlin.String [fake_override]
overridden:
public open fun toString (): kotlin.String declared in kotlin.Any
$this: VALUE_PARAMETER name:<this> type:kotlin.Any
FUN name:test visibility:public modality:FINAL <> (a:kotlin.Any, r:<root>.MyRunnable) returnType:kotlin.Unit
VALUE_PARAMETER name:a index:0 type:kotlin.Any
VALUE_PARAMETER name:r index:1 type:<root>.MyRunnable
BLOCK_BODY
WHEN type=kotlin.Unit origin=IF
BRANCH
if: TYPE_OP type=kotlin.Boolean origin=INSTANCEOF typeOperand=<root>.MyRunnable
GET_VAR 'a: kotlin.Any declared in <root>.test' type=kotlin.Any origin=null
then: CALL 'public final fun foo (vararg rs: <root>.MyRunnable): kotlin.Unit declared in <root>' type=kotlin.Unit origin=null
rs: VARARG type=kotlin.Array<out <root>.MyRunnable> varargElementType=<root>.MyRunnable
FUN_EXPR type=kotlin.Function0<kotlin.Unit> origin=LAMBDA
FUN LOCAL_FUNCTION_FOR_LAMBDA name:<anonymous> visibility:local modality:FINAL <> () returnType:kotlin.Unit
BLOCK_BODY
RETURN type=kotlin.Nothing from='local final fun <anonymous> (): kotlin.Unit declared in <root>.test'
GET_OBJECT 'CLASS IR_EXTERNAL_DECLARATION_STUB OBJECT name:Unit modality:FINAL visibility:public superTypes:[kotlin.Any]' type=kotlin.Unit
GET_VAR 'r: <root>.MyRunnable declared in <root>.test' type=<root>.MyRunnable origin=null
TYPE_OP type=<root>.MyRunnable origin=IMPLICIT_CAST typeOperand=<root>.MyRunnable
GET_VAR 'a: kotlin.Any declared in <root>.test' type=kotlin.Any origin=null
FUN name:foo visibility:public modality:FINAL <> (rs:kotlin.Array<out <root>.MyRunnable>) returnType:kotlin.Unit
VALUE_PARAMETER name:rs index:0 type:kotlin.Array<out <root>.MyRunnable> varargElementType:<root>.MyRunnable [vararg]
BLOCK_BODY
@@ -0,0 +1,12 @@
// !LANGUAGE: +NewInference +FunctionalInterfaceConversion +SamConversionPerArgument
fun interface MyRunnable {
fun run()
}
fun test(a: Any, r: MyRunnable) {
if (a is MyRunnable) {
foo({}, r, a)
}
}
fun foo(vararg rs: MyRunnable) {}
@@ -0,0 +1,41 @@
FILE fqName:<root> fileName:/samConversionInVarargsMixed.kt
CLASS INTERFACE name:MyRunnable modality:ABSTRACT visibility:public [fun] superTypes:[kotlin.Any]
$this: VALUE_PARAMETER INSTANCE_RECEIVER name:<this> type:<root>.MyRunnable
FUN name:run visibility:public modality:ABSTRACT <> ($this:<root>.MyRunnable) returnType:kotlin.Unit
$this: VALUE_PARAMETER name:<this> type:<root>.MyRunnable
FUN FAKE_OVERRIDE name:equals visibility:public modality:OPEN <> ($this:kotlin.Any, other:kotlin.Any?) returnType:kotlin.Boolean [fake_override,operator]
overridden:
public open fun equals (other: kotlin.Any?): kotlin.Boolean [operator] declared in kotlin.Any
$this: VALUE_PARAMETER name:<this> type:kotlin.Any
VALUE_PARAMETER name:other index:0 type:kotlin.Any?
FUN FAKE_OVERRIDE name:hashCode visibility:public modality:OPEN <> ($this:kotlin.Any) returnType:kotlin.Int [fake_override]
overridden:
public open fun hashCode (): kotlin.Int declared in kotlin.Any
$this: VALUE_PARAMETER name:<this> type:kotlin.Any
FUN FAKE_OVERRIDE name:toString visibility:public modality:OPEN <> ($this:kotlin.Any) returnType:kotlin.String [fake_override]
overridden:
public open fun toString (): kotlin.String declared in kotlin.Any
$this: VALUE_PARAMETER name:<this> type:kotlin.Any
FUN name:test visibility:public modality:FINAL <> (a:kotlin.Any, r:<root>.MyRunnable) returnType:kotlin.Unit
VALUE_PARAMETER name:a index:0 type:kotlin.Any
VALUE_PARAMETER name:r index:1 type:<root>.MyRunnable
BLOCK_BODY
WHEN type=kotlin.Unit origin=IF
BRANCH
if: TYPE_OP type=kotlin.Boolean origin=INSTANCEOF typeOperand=<root>.MyRunnable
GET_VAR 'a: kotlin.Any declared in <root>.test' type=kotlin.Any origin=null
then: BLOCK type=kotlin.Unit origin=null
CALL 'public final fun foo (vararg rs: <root>.MyRunnable): kotlin.Unit declared in <root>' type=kotlin.Unit origin=null
rs: VARARG type=kotlin.Array<out <root>.MyRunnable> varargElementType=<root>.MyRunnable
TYPE_OP type=<root>.MyRunnable origin=SAM_CONVERSION typeOperand=<root>.MyRunnable
FUN_EXPR type=kotlin.Function0<kotlin.Unit> origin=LAMBDA
FUN LOCAL_FUNCTION_FOR_LAMBDA name:<anonymous> visibility:local modality:FINAL <> () returnType:kotlin.Unit
BLOCK_BODY
RETURN type=kotlin.Nothing from='local final fun <anonymous> (): kotlin.Unit declared in <root>.test'
GET_OBJECT 'CLASS IR_EXTERNAL_DECLARATION_STUB OBJECT name:Unit modality:FINAL visibility:public superTypes:[kotlin.Any]' type=kotlin.Unit
GET_VAR 'r: <root>.MyRunnable declared in <root>.test' type=<root>.MyRunnable origin=null
TYPE_OP type=<root>.MyRunnable origin=IMPLICIT_CAST typeOperand=<root>.MyRunnable
GET_VAR 'a: kotlin.Any declared in <root>.test' type=kotlin.Any origin=null
FUN name:foo visibility:public modality:FINAL <> (rs:kotlin.Array<out <root>.MyRunnable>) returnType:kotlin.Unit
VALUE_PARAMETER name:rs index:0 type:kotlin.Array<out <root>.MyRunnable> varargElementType:<root>.MyRunnable [vararg]
BLOCK_BODY
@@ -1594,6 +1594,11 @@ public class IrTextTestCaseGenerated extends AbstractIrTextTestCase {
runTest("compiler/testData/ir/irText/expressions/funInterface/samConversionInVarargs.kt"); runTest("compiler/testData/ir/irText/expressions/funInterface/samConversionInVarargs.kt");
} }
@TestMetadata("samConversionInVarargsMixed.kt")
public void testSamConversionInVarargsMixed() throws Exception {
runTest("compiler/testData/ir/irText/expressions/funInterface/samConversionInVarargsMixed.kt");
}
@TestMetadata("samConversionOnCallableReference.kt") @TestMetadata("samConversionOnCallableReference.kt")
public void testSamConversionOnCallableReference() throws Exception { public void testSamConversionOnCallableReference() throws Exception {
runTest("compiler/testData/ir/irText/expressions/funInterface/samConversionOnCallableReference.kt"); runTest("compiler/testData/ir/irText/expressions/funInterface/samConversionOnCallableReference.kt");