diff --git a/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/tower/KotlinToResolvedCallTransformer.kt b/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/tower/KotlinToResolvedCallTransformer.kt index 547442b4c47..76bc3ee411f 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/tower/KotlinToResolvedCallTransformer.kt +++ b/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/tower/KotlinToResolvedCallTransformer.kt @@ -566,6 +566,8 @@ sealed class NewAbstractResolvedCall() : ResolvedCall private var nonTrivialUpdatedResultInfo: DataFlowInfo? = null + abstract fun containsOnlyOnlyInputTypesErrors(): Boolean + override fun getCall(): Call = kotlinCall.psiKotlinCall.psiCall override fun getValueArguments(): Map { @@ -710,6 +712,9 @@ class NewResolvedCallImpl( return typeParameters.zip(typeArguments).toMap() } + override fun containsOnlyOnlyInputTypesErrors() = + diagnostics.all { it is KotlinConstraintSystemDiagnostic && it.error is OnlyInputTypesDiagnostic } + override fun getSmartCastDispatchReceiverType(): KotlinType? = smartCastDispatchReceiverType fun updateExtensionReceiverWithSmartCastIfNeeded(smartCastExtensionReceiverType: KotlinType) { diff --git a/compiler/frontend/src/org/jetbrains/kotlin/types/expressions/BasicExpressionTypingVisitor.java b/compiler/frontend/src/org/jetbrains/kotlin/types/expressions/BasicExpressionTypingVisitor.java index e230aca5d63..817a738da66 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/types/expressions/BasicExpressionTypingVisitor.java +++ b/compiler/frontend/src/org/jetbrains/kotlin/types/expressions/BasicExpressionTypingVisitor.java @@ -60,6 +60,7 @@ import org.jetbrains.kotlin.resolve.calls.smartcasts.Nullability; import org.jetbrains.kotlin.resolve.calls.tasks.ExplicitReceiverKind; import org.jetbrains.kotlin.resolve.calls.tasks.ResolutionCandidate; import org.jetbrains.kotlin.resolve.calls.tasks.TracingStrategy; +import org.jetbrains.kotlin.resolve.calls.tower.NewAbstractResolvedCall; import org.jetbrains.kotlin.resolve.calls.util.CallMaker; import org.jetbrains.kotlin.resolve.checkers.UnderscoreChecker; import org.jetbrains.kotlin.resolve.constants.*; @@ -1394,14 +1395,26 @@ public class BasicExpressionTypingVisitor extends ExpressionTypingVisitor { rightTypeInfo = rightTypeInfo.replaceDataFlowInfo(dataFlowInfo); } - if (resolutionResult.isSuccess()) { + if (resolutionResult.isSuccess() || isResolutionSuccessfulWithOnlyInputTypesWarnings(resolutionResult.getResultingCalls(), context)) { return rightTypeInfo.replaceType(components.builtIns.getBooleanType()); - } - else { + } else { return rightTypeInfo.clearType(); } } + private static boolean isResolutionSuccessfulWithOnlyInputTypesWarnings( + @Nullable Collection> allCandidates, + @NotNull ExpressionTypingContext context + ) { + if (allCandidates == null || allCandidates.isEmpty()) return false; + + boolean areAllCandidatesFailedWithOnlyInputTypesError = allCandidates.stream().allMatch((resolvedCall) -> + resolvedCall instanceof NewAbstractResolvedCall && ((NewAbstractResolvedCall) resolvedCall).containsOnlyOnlyInputTypesErrors() + ); + boolean isNonStrictOnlyInputTypesCheckEnabled = context.languageVersionSettings.supportsFeature(LanguageFeature.NonStrictOnlyInputTypesChecks); + + return areAllCandidatesFailedWithOnlyInputTypesError && isNonStrictOnlyInputTypesCheckEnabled; + } private boolean ensureBooleanResult(KtExpression operationSign, Name name, KotlinType resultType, ExpressionTypingContext context) { return ensureBooleanResultWithCustomSubject(operationSign, resultType, "'" + name + "'", context); diff --git a/compiler/testData/codegen/box/inference/builderInference/specialCallsWithCallableReferencesNonStrictOnlyInputTypes.kt b/compiler/testData/codegen/box/inference/builderInference/specialCallsWithCallableReferencesNonStrictOnlyInputTypes.kt new file mode 100644 index 00000000000..5c4b69759fa --- /dev/null +++ b/compiler/testData/codegen/box/inference/builderInference/specialCallsWithCallableReferencesNonStrictOnlyInputTypes.kt @@ -0,0 +1,45 @@ +// 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 + +val bar4: Int + get() = 1 + +var foo4: Float + get() = 1f + set(value) {} + +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 poll81(): Flow { + return flow { + val inv = ::bar2 in setOf(::foo2) + inv + } +} + +fun poll83(): Flow { + return flow { + val inv = ::bar4 in setOf(::foo4) + inv + } +} + +fun box(): String { + poll81() + poll83() + return "OK" +} \ No newline at end of file diff --git a/compiler/testData/diagnostics/tests/inference/builderInference/specialCallsWithCallableReferencesNonStrictOnlyInputTypes.fir.kt b/compiler/testData/diagnostics/tests/inference/builderInference/specialCallsWithCallableReferencesNonStrictOnlyInputTypes.fir.kt new file mode 100644 index 00000000000..1add4c83e66 --- /dev/null +++ b/compiler/testData/diagnostics/tests/inference/builderInference/specialCallsWithCallableReferencesNonStrictOnlyInputTypes.fir.kt @@ -0,0 +1,35 @@ +// 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 bar2(): Int = 1 +fun foo2(): Float = 1f + +val bar4: Int + get() = 1 + +var foo4: Float + get() = 1f + set(value) {} + +interface FlowCollector {} + +fun flow(@BuilderInference block: suspend FlowCollector.() -> Unit) = Flow(block) + +class Flow(private val block: suspend FlowCollector.() -> Unit) + +fun poll81(): Flow { + return flow { + val inv = ::bar2 in setOf(::foo2) + inv() + } +} + +fun poll83(): Flow { + return flow { + val inv = ::bar4 in setOf(::foo4) + inv + } +} diff --git a/compiler/testData/diagnostics/tests/inference/builderInference/specialCallsWithCallableReferencesNonStrictOnlyInputTypes.kt b/compiler/testData/diagnostics/tests/inference/builderInference/specialCallsWithCallableReferencesNonStrictOnlyInputTypes.kt new file mode 100644 index 00000000000..1fc9b4a29dd --- /dev/null +++ b/compiler/testData/diagnostics/tests/inference/builderInference/specialCallsWithCallableReferencesNonStrictOnlyInputTypes.kt @@ -0,0 +1,35 @@ +// 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 bar2(): Int = 1 +fun foo2(): Float = 1f + +val bar4: Int + get() = 1 + +var foo4: Float + get() = 1f + set(value) {} + +interface FlowCollector {} + +fun flow(@BuilderInference block: suspend FlowCollector.() -> Unit) = Flow(block) + +class Flow(private val block: suspend FlowCollector.() -> Unit) + +fun poll81(): Flow { + return flow { + val inv = ::bar2 in setOf(::foo2) + inv() + } +} + +fun poll83(): Flow { + return flow { + val inv = ::bar4 in setOf(::foo4) + inv + } +} diff --git a/compiler/tests-gen/org/jetbrains/kotlin/codegen/BlackBoxCodegenTestGenerated.java b/compiler/tests-gen/org/jetbrains/kotlin/codegen/BlackBoxCodegenTestGenerated.java index 2b85cc4dc4b..a791da0ab32 100644 --- a/compiler/tests-gen/org/jetbrains/kotlin/codegen/BlackBoxCodegenTestGenerated.java +++ b/compiler/tests-gen/org/jetbrains/kotlin/codegen/BlackBoxCodegenTestGenerated.java @@ -13506,6 +13506,11 @@ public class BlackBoxCodegenTestGenerated extends AbstractBlackBoxCodegenTest { runTest("compiler/testData/codegen/box/inference/builderInference/specialCallsWithCallableReferencesErrorType.kt"); } + @TestMetadata("specialCallsWithCallableReferencesNonStrictOnlyInputTypes.kt") + public void testSpecialCallsWithCallableReferencesNonStrictOnlyInputTypes() throws Exception { + runTest("compiler/testData/codegen/box/inference/builderInference/specialCallsWithCallableReferencesNonStrictOnlyInputTypes.kt"); + } + @TestMetadata("substituteStubTypeIntoCR.kt") public void testSubstituteStubTypeIntoCR() throws Exception { runTest("compiler/testData/codegen/box/inference/builderInference/substituteStubTypeIntoCR.kt"); diff --git a/compiler/tests-spec/testData/diagnostics/linked/expressions/when-expression/p-6/neg/3.2.kt b/compiler/tests-spec/testData/diagnostics/linked/expressions/when-expression/p-6/neg/3.2.kt index 4c7af495269..f7867067d90 100644 --- a/compiler/tests-spec/testData/diagnostics/linked/expressions/when-expression/p-6/neg/3.2.kt +++ b/compiler/tests-spec/testData/diagnostics/linked/expressions/when-expression/p-6/neg/3.2.kt @@ -29,8 +29,8 @@ fun case_1(value_1: Int, value_2: EmptyClass, value_3: Int, value_4: Any): Strin */ fun case_2(value_1: Int, value_3: Nothing) { when (value_1) { - in value_3 -> {} - in throw Exception() -> {} - in return -> {} + in value_3 -> {} + in throw Exception() -> {} + in return -> {} } }