FIR2IR: don't apply SAM conversion for type parameter based types

#KT-58893 Fixed
#KT-58884 Fixed
This commit is contained in:
Mikhail Glukhikh
2023-08-21 19:19:17 +02:00
committed by Space Team
parent a832510487
commit 289dafa331
10 changed files with 94 additions and 4 deletions
@@ -562,7 +562,7 @@ internal class AdapterGenerator(
}
// If the expected type is a built-in functional type, we don't need SAM conversion.
val expectedType = argument.getExpectedType(parameter)
if (expectedType is ConeTypeParameterType || expectedType.isSomeFunctionType(session)) {
if (expectedType.isTypeParameterBased() || expectedType.isSomeFunctionType(session)) {
return false
}
// On the other hand, the actual type should be either a functional type or a subtype of a class that has a contributed `invoke`.
@@ -570,6 +570,15 @@ internal class AdapterGenerator(
return argument.isFunctional(session, scopeSession, expectedFunctionType, ReturnTypeCalculatorForFullBodyResolve.Default)
}
private fun ConeKotlinType.isTypeParameterBased(): Boolean {
return when (this) {
is ConeTypeParameterType -> true
is ConeDefinitelyNotNullType -> original.isTypeParameterBased()
is ConeFlexibleType -> lowerBound.isTypeParameterBased()
else -> false
}
}
internal fun getFunctionTypeForPossibleSamType(parameterType: ConeKotlinType): ConeKotlinType? {
return samResolver.getFunctionTypeForPossibleSamType(parameterType)
}
@@ -18815,6 +18815,12 @@ public class FirLightTreeBlackBoxCodegenTestGenerated extends AbstractFirLightTr
runTest("compiler/testData/codegen/box/fir/manyImplFromOneJavaInterfaceWithDelegation2.kt");
}
@Test
@TestMetadata("Mockito.kt")
public void testMockito() throws Exception {
runTest("compiler/testData/codegen/box/fir/Mockito.kt");
}
@Test
@TestMetadata("NameHighlighter.kt")
public void testNameHighlighter() throws Exception {
@@ -18815,6 +18815,12 @@ public class FirPsiBlackBoxCodegenTestGenerated extends AbstractFirPsiBlackBoxCo
runTest("compiler/testData/codegen/box/fir/manyImplFromOneJavaInterfaceWithDelegation2.kt");
}
@Test
@TestMetadata("Mockito.kt")
public void testMockito() throws Exception {
runTest("compiler/testData/codegen/box/fir/Mockito.kt");
}
@Test
@TestMetadata("NameHighlighter.kt")
public void testNameHighlighter() throws Exception {
+47
View File
@@ -0,0 +1,47 @@
// ISSUE: KT-58893
// TARGET_BACKEND: JVM
// WITH_STDLIB
// IGNORE_INLINER: IR
// FILE: InOrder.java
public class InOrder {
<T> T verify(T mock) {
String s = mock.getClass().toString();
if (!s.equals(TestKt.expected)) throw new IllegalStateException(s);
return mock;
}
}
// FILE: test.kt
inline fun <reified T : Any> mock(s: String = ""): T = MOCK as T
val MOCK = {}
inline fun inOrder(
vararg mocks: Any,
evaluation: InOrder.() -> Unit
) {
inOrder.evaluation()
InOrder().evaluation()
}
val inOrder = object : InOrder() {
override fun <T : Any?> verify(mock: T): T {
val s = mock!!.javaClass.toString()
if (s != expected) throw IllegalStateException(s)
return mock
}
}
lateinit var expected: String
fun box(): String {
val mock = mock<() -> Unit>()
expected = mock.javaClass.toString()
inOrder(mock) {
verify(mock)()
}
return "OK"
}
@@ -104,8 +104,7 @@ FILE fqName:<root> fileName:/samConversionsWithSmartCasts.kt
r: TYPE_OP type=@[FlexibleNullability] java.lang.Runnable? origin=SAM_CONVERSION typeOperand=@[FlexibleNullability] java.lang.Runnable?
CALL 'public open fun id <T> (x: @[FlexibleNullability] T of <root>.J.id?): @[FlexibleNullability] T of <root>.J.id? declared in <root>.J' type=@[FlexibleNullability] kotlin.Function0<kotlin.Unit>? origin=null
<T>: @[FlexibleNullability] kotlin.Function0<kotlin.Unit>?
x: TYPE_OP type=@[FlexibleNullability] kotlin.Function0<kotlin.Unit>? origin=SAM_CONVERSION typeOperand=@[FlexibleNullability] kotlin.Function0<kotlin.Unit>?
GET_VAR 'a: kotlin.Function0<kotlin.Unit> declared in <root>.test8' type=kotlin.Function0<kotlin.Unit> origin=null
x: GET_VAR 'a: kotlin.Function0<kotlin.Unit> declared in <root>.test8' type=kotlin.Function0<kotlin.Unit> origin=null
FUN name:test9 visibility:public modality:FINAL <> () returnType:kotlin.Unit
BLOCK_BODY
CALL 'public open fun run1 (r: @[FlexibleNullability] java.lang.Runnable?): kotlin.Unit declared in <root>.J' type=kotlin.Unit origin=null
@@ -48,7 +48,7 @@ fun test7(a: Function1<Int, Int>) {
}
fun test8(a: Function0<Unit>) {
J().run1(r = id<@FlexibleNullability Function0<Unit>?>(x = a /*-> @FlexibleNullability Function0<Unit>? */) /*-> @FlexibleNullability Runnable? */)
J().run1(r = id<@FlexibleNullability Function0<Unit>?>(x = a) /*-> @FlexibleNullability Runnable? */)
}
fun test9() {
@@ -17909,6 +17909,12 @@ public class BlackBoxCodegenTestGenerated extends AbstractBlackBoxCodegenTest {
runTest("compiler/testData/codegen/box/fir/LookupTags.kt");
}
@Test
@TestMetadata("Mockito.kt")
public void testMockito() throws Exception {
runTest("compiler/testData/codegen/box/fir/Mockito.kt");
}
@Test
@TestMetadata("NameHighlighter.kt")
public void testNameHighlighter() throws Exception {
@@ -18815,6 +18815,12 @@ public class IrBlackBoxCodegenTestGenerated extends AbstractIrBlackBoxCodegenTes
runTest("compiler/testData/codegen/box/fir/manyImplFromOneJavaInterfaceWithDelegation2.kt");
}
@Test
@TestMetadata("Mockito.kt")
public void testMockito() throws Exception {
runTest("compiler/testData/codegen/box/fir/Mockito.kt");
}
@Test
@TestMetadata("NameHighlighter.kt")
public void testNameHighlighter() throws Exception {
@@ -18815,6 +18815,12 @@ public class IrBlackBoxCodegenWithIrInlinerTestGenerated extends AbstractIrBlack
runTest("compiler/testData/codegen/box/fir/manyImplFromOneJavaInterfaceWithDelegation2.kt");
}
@Test
@TestMetadata("Mockito.kt")
public void testMockito() throws Exception {
runTest("compiler/testData/codegen/box/fir/Mockito.kt");
}
@Test
@TestMetadata("NameHighlighter.kt")
public void testNameHighlighter() throws Exception {
@@ -15663,6 +15663,11 @@ public class LightAnalysisModeTestGenerated extends AbstractLightAnalysisModeTes
runTest("compiler/testData/codegen/box/fir/manyImplFromOneJavaInterfaceWithDelegation2.kt");
}
@TestMetadata("Mockito.kt")
public void testMockito() throws Exception {
runTest("compiler/testData/codegen/box/fir/Mockito.kt");
}
@TestMetadata("NameHighlighter.kt")
public void testNameHighlighter() throws Exception {
runTest("compiler/testData/codegen/box/fir/NameHighlighter.kt");