Psi2ir: fix function type check for fun interface conversion

Parameter of a synthetic SAM adapter always has a function type (not a
subtype). Checking for the subtypes broke the case from KT-46908, where
fun interface is itself a subtype of a function type.

 #KT-46908 Fixed
This commit is contained in:
Alexander Udalov
2021-09-29 16:17:01 +02:00
parent e6a160e115
commit 9857af7823
18 changed files with 173 additions and 2 deletions
@@ -16492,6 +16492,12 @@ public class FirBlackBoxCodegenTestGenerated extends AbstractFirBlackBoxCodegenT
runTest("compiler/testData/codegen/box/funInterface/kt46512_indyFunInterfaceOverCallableReference.kt");
}
@Test
@TestMetadata("kt46908_functionSupertype.kt")
public void testKt46908_functionSupertype() throws Exception {
runTest("compiler/testData/codegen/box/funInterface/kt46908_functionSupertype.kt");
}
@Test
@TestMetadata("multimodule.kt")
public void testMultimodule() throws Exception {
@@ -2089,6 +2089,12 @@ public class Fir2IrTextTestGenerated extends AbstractFir2IrTextTest {
runTest("compiler/testData/ir/irText/expressions/funInterface/castFromAny.kt");
}
@Test
@TestMetadata("functionSupertype.kt")
public void testFunctionSupertype() throws Exception {
runTest("compiler/testData/ir/irText/expressions/funInterface/functionSupertype.kt");
}
@Test
@TestMetadata("partialSam.kt")
public void testPartialSam() throws Exception {
@@ -17,7 +17,7 @@
package org.jetbrains.kotlin.psi2ir.generators
import org.jetbrains.kotlin.builtins.isBuiltinFunctionalType
import org.jetbrains.kotlin.builtins.isFunctionTypeOrSubtype
import org.jetbrains.kotlin.builtins.isFunctionType
import org.jetbrains.kotlin.config.LanguageFeature
import org.jetbrains.kotlin.descriptors.*
import org.jetbrains.kotlin.descriptors.impl.SyntheticFieldDescriptor
@@ -585,7 +585,7 @@ fun StatementGenerator.generateSamConversionForValueArgumentsIfRequired(call: Ca
if (!samConversion.isSamType(underlyingValueParameter.type) &&
!samConversion.isSamType(underlyingValueParameter.original.type)
) continue
if (!originalValueParameters[i].type.isFunctionTypeOrSubtype) continue
if (!originalValueParameters[i].type.isFunctionType) continue
}
val samKotlinType = getSamTypeForValueParameter(underlyingValueParameter)
@@ -0,0 +1,18 @@
// IGNORE_BACKEND: JS, JS_IR, WASM
fun interface Foo : () -> Int
fun id(foo: Foo): Any = foo
fun box(): String {
val p1 = object : Foo {
override fun invoke(): Int = 42
override fun toString(): String = "OK"
}
val p2 = id(p1)
if (p1 !== p2) return "Fail identity equals"
if (p1.toString() != p2.toString()) return "Fail toString"
return p2.toString()
}
@@ -0,0 +1,11 @@
fun interface Foo : Function0<Int> {
}
fun id(foo: Foo): Any {
return foo
}
fun test(foo: Foo) {
id(foo = foo) /*~> Unit */
}
@@ -0,0 +1,31 @@
FILE fqName:<root> fileName:/functionSupertype.kt
CLASS INTERFACE name:Foo modality:ABSTRACT visibility:public [fun] superTypes:[kotlin.Function0<kotlin.Int>]
$this: VALUE_PARAMETER INSTANCE_RECEIVER name:<this> type:<root>.Foo
FUN FAKE_OVERRIDE name:invoke visibility:public modality:ABSTRACT <> ($this:kotlin.Function0<R of kotlin.Function0>) returnType:kotlin.Int [fake_override,operator]
overridden:
public abstract fun invoke (): R of kotlin.Function0 [operator] declared in kotlin.Function0
$this: VALUE_PARAMETER name:<this> type:kotlin.Function0<R of kotlin.Function0>
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 [fake_override,operator] declared in kotlin.Function0
$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 [fake_override] declared in kotlin.Function0
$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 [fake_override] declared in kotlin.Function0
$this: VALUE_PARAMETER name:<this> type:kotlin.Any
FUN name:id visibility:public modality:FINAL <> (foo:<root>.Foo) returnType:kotlin.Any
VALUE_PARAMETER name:foo index:0 type:<root>.Foo
BLOCK_BODY
RETURN type=kotlin.Nothing from='public final fun id (foo: <root>.Foo): kotlin.Any declared in <root>'
GET_VAR 'foo: <root>.Foo declared in <root>.id' type=<root>.Foo origin=null
FUN name:test visibility:public modality:FINAL <> (foo:<root>.Foo) returnType:kotlin.Unit
VALUE_PARAMETER name:foo index:0 type:<root>.Foo
BLOCK_BODY
TYPE_OP type=kotlin.Unit origin=IMPLICIT_COERCION_TO_UNIT typeOperand=kotlin.Unit
CALL 'public final fun id (foo: <root>.Foo): kotlin.Any declared in <root>' type=kotlin.Any origin=null
foo: GET_VAR 'foo: <root>.Foo declared in <root>.test' type=<root>.Foo origin=null
@@ -0,0 +1,9 @@
// IGNORE_BACKEND: JS_IR
fun interface Foo : () -> Int
fun id(foo: Foo): Any = foo
fun test(foo: Foo) {
id(foo)
}
@@ -0,0 +1,11 @@
fun interface Foo : Function0<Int> {
}
fun id(foo: Foo): Any {
return foo
}
fun test(foo: Foo) {
id(foo = foo) /*~> Unit */
}
@@ -0,0 +1,31 @@
FILE fqName:<root> fileName:/functionSupertype.kt
CLASS INTERFACE name:Foo modality:ABSTRACT visibility:public [fun] superTypes:[kotlin.Function0<kotlin.Int>]
$this: VALUE_PARAMETER INSTANCE_RECEIVER name:<this> type:<root>.Foo
FUN FAKE_OVERRIDE name:invoke visibility:public modality:ABSTRACT <> ($this:kotlin.Function0<kotlin.Int>) returnType:kotlin.Int [fake_override,operator]
overridden:
public abstract fun invoke (): R of kotlin.Function0 [operator] declared in kotlin.Function0
$this: VALUE_PARAMETER name:<this> type:kotlin.Function0<kotlin.Int>
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 [fake_override,operator] declared in kotlin.Function0
$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 [fake_override] declared in kotlin.Function0
$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 [fake_override] declared in kotlin.Function0
$this: VALUE_PARAMETER name:<this> type:kotlin.Any
FUN name:id visibility:public modality:FINAL <> (foo:<root>.Foo) returnType:kotlin.Any
VALUE_PARAMETER name:foo index:0 type:<root>.Foo
BLOCK_BODY
RETURN type=kotlin.Nothing from='public final fun id (foo: <root>.Foo): kotlin.Any declared in <root>'
GET_VAR 'foo: <root>.Foo declared in <root>.id' type=<root>.Foo origin=null
FUN name:test visibility:public modality:FINAL <> (foo:<root>.Foo) returnType:kotlin.Unit
VALUE_PARAMETER name:foo index:0 type:<root>.Foo
BLOCK_BODY
TYPE_OP type=kotlin.Unit origin=IMPLICIT_COERCION_TO_UNIT typeOperand=kotlin.Unit
CALL 'public final fun id (foo: <root>.Foo): kotlin.Any declared in <root>' type=kotlin.Any origin=null
foo: GET_VAR 'foo: <root>.Foo declared in <root>.test' type=<root>.Foo origin=null
@@ -16372,6 +16372,12 @@ public class BlackBoxCodegenTestGenerated extends AbstractBlackBoxCodegenTest {
runTest("compiler/testData/codegen/box/funInterface/kt46512_indyFunInterfaceOverCallableReference.kt");
}
@Test
@TestMetadata("kt46908_functionSupertype.kt")
public void testKt46908_functionSupertype() throws Exception {
runTest("compiler/testData/codegen/box/funInterface/kt46908_functionSupertype.kt");
}
@Test
@TestMetadata("multimodule.kt")
public void testMultimodule() throws Exception {
@@ -16492,6 +16492,12 @@ public class IrBlackBoxCodegenTestGenerated extends AbstractIrBlackBoxCodegenTes
runTest("compiler/testData/codegen/box/funInterface/kt46512_indyFunInterfaceOverCallableReference.kt");
}
@Test
@TestMetadata("kt46908_functionSupertype.kt")
public void testKt46908_functionSupertype() throws Exception {
runTest("compiler/testData/codegen/box/funInterface/kt46908_functionSupertype.kt");
}
@Test
@TestMetadata("multimodule.kt")
public void testMultimodule() throws Exception {
@@ -2089,6 +2089,12 @@ public class IrTextTestGenerated extends AbstractIrTextTest {
runTest("compiler/testData/ir/irText/expressions/funInterface/castFromAny.kt");
}
@Test
@TestMetadata("functionSupertype.kt")
public void testFunctionSupertype() throws Exception {
runTest("compiler/testData/ir/irText/expressions/funInterface/functionSupertype.kt");
}
@Test
@TestMetadata("partialSam.kt")
public void testPartialSam() throws Exception {
@@ -13511,6 +13511,11 @@ public class LightAnalysisModeTestGenerated extends AbstractLightAnalysisModeTes
runTest("compiler/testData/codegen/box/funInterface/kt46512_indyFunInterfaceOverCallableReference.kt");
}
@TestMetadata("kt46908_functionSupertype.kt")
public void testKt46908_functionSupertype() throws Exception {
runTest("compiler/testData/codegen/box/funInterface/kt46908_functionSupertype.kt");
}
@TestMetadata("multimodule.kt")
public void testMultimodule() throws Exception {
runTest("compiler/testData/codegen/box/funInterface/multimodule.kt");
@@ -1636,6 +1636,11 @@ public class KlibTextTestCaseGenerated extends AbstractKlibTextTestCase {
runTest("compiler/testData/ir/irText/expressions/funInterface/castFromAny.kt");
}
@TestMetadata("functionSupertype.kt")
public void testFunctionSupertype() throws Exception {
runTest("compiler/testData/ir/irText/expressions/funInterface/functionSupertype.kt");
}
@TestMetadata("partialSam.kt")
public void testPartialSam() throws Exception {
runTest("compiler/testData/ir/irText/expressions/funInterface/partialSam.kt");
@@ -11905,6 +11905,11 @@ public class IrJsCodegenBoxES6TestGenerated extends AbstractIrJsCodegenBoxES6Tes
runTest("compiler/testData/codegen/box/funInterface/kt46512_indyFunInterfaceOverCallableReference.kt");
}
@TestMetadata("kt46908_functionSupertype.kt")
public void testKt46908_functionSupertype() throws Exception {
runTest("compiler/testData/codegen/box/funInterface/kt46908_functionSupertype.kt");
}
@TestMetadata("multimodule.kt")
public void testMultimodule() throws Exception {
runTest("compiler/testData/codegen/box/funInterface/multimodule.kt");
@@ -11311,6 +11311,11 @@ public class IrJsCodegenBoxTestGenerated extends AbstractIrJsCodegenBoxTest {
runTest("compiler/testData/codegen/box/funInterface/kt46512_indyFunInterfaceOverCallableReference.kt");
}
@TestMetadata("kt46908_functionSupertype.kt")
public void testKt46908_functionSupertype() throws Exception {
runTest("compiler/testData/codegen/box/funInterface/kt46908_functionSupertype.kt");
}
@TestMetadata("multimodule.kt")
public void testMultimodule() throws Exception {
runTest("compiler/testData/codegen/box/funInterface/multimodule.kt");
@@ -11276,6 +11276,11 @@ public class JsCodegenBoxTestGenerated extends AbstractJsCodegenBoxTest {
runTest("compiler/testData/codegen/box/funInterface/kt46512_indyFunInterfaceOverCallableReference.kt");
}
@TestMetadata("kt46908_functionSupertype.kt")
public void testKt46908_functionSupertype() throws Exception {
runTest("compiler/testData/codegen/box/funInterface/kt46908_functionSupertype.kt");
}
@TestMetadata("multimodule.kt")
public void testMultimodule() throws Exception {
runTest("compiler/testData/codegen/box/funInterface/multimodule.kt");
@@ -6640,6 +6640,11 @@ public class IrCodegenBoxWasmTestGenerated extends AbstractIrCodegenBoxWasmTest
runTest("compiler/testData/codegen/box/funInterface/kt46512_indyFunInterfaceOverCallableReference.kt");
}
@TestMetadata("kt46908_functionSupertype.kt")
public void testKt46908_functionSupertype() throws Exception {
runTest("compiler/testData/codegen/box/funInterface/kt46908_functionSupertype.kt");
}
@TestMetadata("compiler/testData/codegen/box/funInterface/equality")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)