diff --git a/compiler/frontend/src/org/jetbrains/kotlin/types/expressions/ExpressionTypingServices.java b/compiler/frontend/src/org/jetbrains/kotlin/types/expressions/ExpressionTypingServices.java index e47863a21e8..907a1a3cca8 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/types/expressions/ExpressionTypingServices.java +++ b/compiler/frontend/src/org/jetbrains/kotlin/types/expressions/ExpressionTypingServices.java @@ -22,6 +22,7 @@ import org.jetbrains.kotlin.resolve.*; import org.jetbrains.kotlin.resolve.calls.components.InferenceSession; import org.jetbrains.kotlin.resolve.calls.context.ContextDependency; import org.jetbrains.kotlin.resolve.calls.context.ResolutionContext; +import org.jetbrains.kotlin.resolve.calls.inference.CoroutineInferenceSession; import org.jetbrains.kotlin.resolve.calls.smartcasts.DataFlowInfo; import org.jetbrains.kotlin.resolve.calls.smartcasts.DataFlowValue; import org.jetbrains.kotlin.resolve.calls.tower.KotlinResolutionCallbacksImpl; @@ -423,7 +424,8 @@ public class ExpressionTypingServices { @NotNull KtExpression statementExpression, @NotNull ExpressionTypingContext context ) { - if (!context.languageVersionSettings.supportsFeature(LanguageFeature.NewInference)) return null; + if (!context.languageVersionSettings.supportsFeature(LanguageFeature.NewInference) || context.inferenceSession instanceof CoroutineInferenceSession) + return null; KtFunctionLiteral functionLiteral = PsiUtilsKt.getNonStrictParentOfType(statementExpression, KtFunctionLiteral.class); if (functionLiteral != null) { KotlinResolutionCallbacksImpl.LambdaInfo info = diff --git a/compiler/testData/codegen/box/inference/builderInference/specialCallsWithCallableReferencesDontCareTypeInBlockExpression.kt b/compiler/testData/codegen/box/inference/builderInference/specialCallsWithCallableReferencesDontCareTypeInBlockExpression.kt new file mode 100644 index 00000000000..dea02002d19 --- /dev/null +++ b/compiler/testData/codegen/box/inference/builderInference/specialCallsWithCallableReferencesDontCareTypeInBlockExpression.kt @@ -0,0 +1,54 @@ +// WITH_RUNTIME + +import kotlin.experimental.ExperimentalTypeInference + +fun FlowCollector.bar(): K = null as K +fun FlowCollector.foo(): K = null as K + +fun bar2(): Int = 1 +fun foo2(): Float = 1f + +fun materialize() = null as T + +interface FlowCollector {} + +@Suppress("EXPERIMENTAL_API_USAGE_ERROR") +fun flow(@BuilderInference block: suspend FlowCollector.() -> Unit) = Flow(block) + +class Flow(private val block: suspend FlowCollector.() -> Unit) + +fun poll1(flag: Boolean): Flow { + return flow { + val inv = if (flag) { ::bar2 } else { ::foo2 } + inv() + } +} + +fun poll11(flag: Boolean): Flow { + return flow { + val inv = if (flag) { ::bar2 } else { ::foo2 } + inv() + } +} + +fun poll41(): Flow { + return flow { + val inv = try { ::bar2 } finally { ::foo2 } + inv() + } +} + +fun poll51(): Flow { + return flow { + val inv = try { ::bar2 } catch (e: Exception) { ::foo2 } finally { ::foo2 } + inv() + } +} + +fun box(): String { + poll1(true) + poll11(true) + poll41() + poll51() + return "OK" +} \ No newline at end of file diff --git a/compiler/testData/diagnostics/tests/inference/builderInference/specialCallsWithCallableReferencesDontCareTypeInBlockExression.fir.kt b/compiler/testData/diagnostics/tests/inference/builderInference/specialCallsWithCallableReferencesDontCareTypeInBlockExression.fir.kt new file mode 100644 index 00000000000..5b1aac97a4e --- /dev/null +++ b/compiler/testData/diagnostics/tests/inference/builderInference/specialCallsWithCallableReferencesDontCareTypeInBlockExression.fir.kt @@ -0,0 +1,189 @@ +// WITH_RUNTIME +// SKIP_TXT +// !DIAGNOSTICS: -CAST_NEVER_SUCCEEDS -UNCHECKED_CAST -UNUSED_PARAMETER -UNUSED_VARIABLE -EXPERIMENTAL_API_USAGE_ERROR -UNUSED_EXPRESSION + +import kotlin.experimental.ExperimentalTypeInference + +fun FlowCollector.bar(): K = null as K +fun FlowCollector.foo(): K = null as K + +fun K.bar3(): K = null as K +fun K.foo3(): K = null as K + +fun bar2(): Int = 1 +fun foo2(): Float = 1f + +val bar4: Int + get() = 1 + +var foo4: Float + get() = 1f + set(value) {} + +val FlowCollector.bar5: K get() = null as K +val FlowCollector.foo5: K get() = null as K + +class Foo6 + +class Foo7 +fun foo7() = null as Foo7 + +interface FlowCollector {} + +fun flow(@BuilderInference block: suspend FlowCollector.() -> Unit) = Flow(block) + +class Flow(private val block: suspend FlowCollector.() -> Unit) + +fun poll1(flag: Boolean): Flow { + return flow { + val inv = if (flag) { ::bar2 } else { ::foo2 } + inv() + } +} + +fun poll11(flag: Boolean): Flow { + return flow { + val inv = if (flag) { ::bar2 } else { ::foo2 } + inv() + } +} + +fun poll12(flag: Boolean): Flow { + return flow { + val inv = if (flag) { ::bar3 } else { ::foo3 } + inv() + } +} + +fun poll13(flag: Boolean): Flow { + return flow { + val inv = if (flag) { ::bar2 } else { ::foo3 } + inv() + } +} + +fun poll14(flag: Boolean): Flow { + return flow { + val inv = if (flag) { ::bar4 } else { ::foo4 } + inv() + } +} + +fun poll15(flag: Boolean): Flow { + return flow { + val inv = if (flag) { ::bar5 } else { ::foo5 } + inv() + } +} + +fun poll16(flag: Boolean): Flow { + return flow { + val inv = if (flag) { ::Foo6 } else { ::Foo6 } + inv() + } +} + +fun poll17(flag: Boolean): Flow { + return flow { + val inv = if (flag) { foo7() } else { ::Foo7 } + inv + } +} + +fun poll4(): Flow { + return flow { + val inv = try { ::bar } finally { ::foo } + inv() + } +} + +fun poll41(): Flow { + return flow { + val inv = try { ::bar2 } finally { ::foo2 } + inv() + } +} + +fun poll42(): Flow { + return flow { + val inv = try { ::bar3 } finally { ::foo3 } + inv() + } +} + +fun poll43(): Flow { + return flow { + val inv = try { ::bar4 } finally { ::foo4 } + inv() + } +} + +fun poll44(): Flow { + return flow { + val inv = try { ::bar5 } finally { ::foo5 } + inv() + } +} + +fun poll45(): Flow { + return flow { + val inv = try { ::Foo6 } finally { ::Foo6 } + inv() + } +} + +fun poll46(): Flow { + return flow { + val inv = try { foo7() } finally { ::Foo7 } + inv + } +} + +fun poll5(): Flow { + return flow { + val inv = try { ::bar } catch (e: Exception) { ::foo } finally { ::foo } + inv() + } +} + +fun poll51(): Flow { + return flow { + val inv = try { ::bar2 } catch (e: Exception) { ::foo2 } finally { ::foo2 } + inv() + } +} + +fun poll52(): Flow { + return flow { + val inv = try { ::bar3 } catch (e: Exception) { ::foo3 } finally { ::foo3 } + inv() + } +} + +fun poll53(): Flow { + return flow { + val inv = try { ::bar4 } catch (e: Exception) { ::foo4 } finally { ::foo4 } + inv() + } +} + +fun poll54(): Flow { + return flow { + val inv = try { ::bar5 } catch (e: Exception) { ::foo5 } finally { ::foo5 } + inv() + } +} + +fun poll55(): Flow { + return flow { + val inv = try { ::Foo6 } catch (e: Exception) { ::Foo6 } finally { ::Foo6 } + inv() + } +} + +fun poll56(): Flow { + return flow { + val inv = try { ::Foo7 } catch (e: Exception) { foo7() } finally { foo7() } + inv + } +} diff --git a/compiler/testData/diagnostics/tests/inference/builderInference/specialCallsWithCallableReferencesDontCareTypeInBlockExression.kt b/compiler/testData/diagnostics/tests/inference/builderInference/specialCallsWithCallableReferencesDontCareTypeInBlockExression.kt new file mode 100644 index 00000000000..e87e69c10a7 --- /dev/null +++ b/compiler/testData/diagnostics/tests/inference/builderInference/specialCallsWithCallableReferencesDontCareTypeInBlockExression.kt @@ -0,0 +1,189 @@ +// WITH_RUNTIME +// SKIP_TXT +// !DIAGNOSTICS: -CAST_NEVER_SUCCEEDS -UNCHECKED_CAST -UNUSED_PARAMETER -UNUSED_VARIABLE -EXPERIMENTAL_API_USAGE_ERROR -UNUSED_EXPRESSION + +import kotlin.experimental.ExperimentalTypeInference + +fun FlowCollector.bar(): K = null as K +fun FlowCollector.foo(): K = null as K + +fun K.bar3(): K = null as K +fun K.foo3(): K = null as K + +fun bar2(): Int = 1 +fun foo2(): Float = 1f + +val bar4: Int + get() = 1 + +var foo4: Float + get() = 1f + set(value) {} + +val FlowCollector.bar5: K get() = null as K +val FlowCollector.foo5: K get() = null as K + +class Foo6 + +class Foo7 +fun foo7() = null as Foo7 + +interface FlowCollector {} + +fun flow(@BuilderInference block: suspend FlowCollector.() -> Unit) = Flow(block) + +class Flow(private val block: suspend FlowCollector.() -> Unit) + +fun poll1(flag: Boolean): Flow { + return flow { + val inv = if (flag) { ::bar2 } else { ::foo2 } + inv() + } +} + +fun poll11(flag: Boolean): Flow { + return flow { + val inv = if (flag) { ::bar2 } else { ::foo2 } + inv() + } +} + +fun poll12(flag: Boolean): Flow { + return flow { + val inv = if (flag) { ::bar3 } else { ::foo3 } + inv() + } +} + +fun poll13(flag: Boolean): Flow { + return flow { + val inv = if (flag) { ::bar2 } else { ::foo3 } + inv() + } +} + +fun poll14(flag: Boolean): Flow { + return flow { + val inv = if (flag) { ::bar4 } else { ::foo4 } + inv() + } +} + +fun poll15(flag: Boolean): Flow { + return flow { + val inv = if (flag) { ::bar5 } else { ::foo5 } + inv() + } +} + +fun poll16(flag: Boolean): Flow { + return flow { + val inv = if (flag) { ::Foo6 } else { ::Foo6 } + inv() + } +} + +fun poll17(flag: Boolean): Flow { + return flow { + val inv = if (flag) { foo7() } else { ::Foo7 } + inv + } +} + +fun poll4(): Flow { + return flow { + val inv = try { ::bar } finally { ::foo } + inv() + } +} + +fun poll41(): Flow { + return flow { + val inv = try { ::bar2 } finally { ::foo2 } + inv() + } +} + +fun poll42(): Flow { + return flow { + val inv = try { ::bar3 } finally { ::foo3 } + inv() + } +} + +fun poll43(): Flow { + return flow { + val inv = try { ::bar4 } finally { ::foo4 } + inv() + } +} + +fun poll44(): Flow { + return flow { + val inv = try { ::bar5 } finally { ::foo5 } + inv() + } +} + +fun poll45(): Flow { + return flow { + val inv = try { ::Foo6 } finally { ::Foo6 } + inv() + } +} + +fun poll46(): Flow { + return flow { + val inv = try { foo7() } finally { ::Foo7 } + inv + } +} + +fun poll5(): Flow { + return flow { + val inv = try { ::bar } catch (e: Exception) { ::foo } finally { ::foo } + inv() + } +} + +fun poll51(): Flow { + return flow { + val inv = try { ::bar2 } catch (e: Exception) { ::foo2 } finally { ::foo2 } + inv() + } +} + +fun poll52(): Flow { + return flow { + val inv = try { ::bar3 } catch (e: Exception) { ::foo3 } finally { ::foo3 } + inv() + } +} + +fun poll53(): Flow { + return flow { + val inv = try { ::bar4 } catch (e: Exception) { ::foo4 } finally { ::foo4 } + inv() + } +} + +fun poll54(): Flow { + return flow { + val inv = try { ::bar5 } catch (e: Exception) { ::foo5 } finally { ::foo5 } + inv() + } +} + +fun poll55(): Flow { + return flow { + val inv = try { ::Foo6 } catch (e: Exception) { ::Foo6 } finally { ::Foo6 } + inv() + } +} + +fun poll56(): Flow { + return flow { + val inv = try { ::Foo7 } catch (e: Exception) { foo7() } finally { foo7() } + inv + } +} diff --git a/compiler/tests-gen/org/jetbrains/kotlin/codegen/BlackBoxCodegenTestGenerated.java b/compiler/tests-gen/org/jetbrains/kotlin/codegen/BlackBoxCodegenTestGenerated.java index a791da0ab32..67f51d3591b 100644 --- a/compiler/tests-gen/org/jetbrains/kotlin/codegen/BlackBoxCodegenTestGenerated.java +++ b/compiler/tests-gen/org/jetbrains/kotlin/codegen/BlackBoxCodegenTestGenerated.java @@ -13501,6 +13501,11 @@ public class BlackBoxCodegenTestGenerated extends AbstractBlackBoxCodegenTest { runTest("compiler/testData/codegen/box/inference/builderInference/lackOfNullCheckOnNullableInsideBuild.kt"); } + @TestMetadata("specialCallsWithCallableReferencesDontCareTypeInBlockExpression.kt") + public void testSpecialCallsWithCallableReferencesDontCareTypeInBlockExpression() throws Exception { + runTest("compiler/testData/codegen/box/inference/builderInference/specialCallsWithCallableReferencesDontCareTypeInBlockExpression.kt"); + } + @TestMetadata("specialCallsWithCallableReferencesErrorType.kt") public void testSpecialCallsWithCallableReferencesErrorType() throws Exception { runTest("compiler/testData/codegen/box/inference/builderInference/specialCallsWithCallableReferencesErrorType.kt");