NI. Coerce return type of lambda function descriptor to Unit. #KT-30242 Fixed

This commit is contained in:
Dmitriy Novozhilov
2019-03-06 11:34:47 +03:00
parent 77c98bef4d
commit cbf1d773f7
5 changed files with 115 additions and 5 deletions
@@ -31,13 +31,12 @@ import org.jetbrains.kotlin.resolve.calls.tasks.ExplicitReceiverKind
import org.jetbrains.kotlin.resolve.calls.tasks.TracingStrategyImpl
import org.jetbrains.kotlin.resolve.calls.util.CallMaker
import org.jetbrains.kotlin.resolve.deprecation.DeprecationResolver
import org.jetbrains.kotlin.types.IndexedParametersSubstitution
import org.jetbrains.kotlin.types.TypeUtils
import org.jetbrains.kotlin.types.UnwrappedType
import org.jetbrains.kotlin.types.Variance
import org.jetbrains.kotlin.types.*
import org.jetbrains.kotlin.types.expressions.DoubleColonExpressionResolver
import org.jetbrains.kotlin.types.expressions.ExpressionTypingServices
import org.jetbrains.kotlin.types.typeUtil.asTypeProjection
import org.jetbrains.kotlin.types.typeUtil.isUnit
import org.jetbrains.kotlin.utils.addToStdlib.safeAs
class ResolvedAtomCompleter(
private val resultSubstitutor: NewTypeSubstitutor,
@@ -108,8 +107,27 @@ class ResolvedAtomCompleter(
return resolvedCall
}
private val ResolvedLambdaAtom.isCoercedToUnit: Boolean
get() {
val returnTypes =
resultArguments.mapNotNull {
val type = it.safeAs<SimpleKotlinCallArgument>()?.receiver?.receiverValue?.type ?: return@mapNotNull null
val unwrappedType = when (type) {
is WrappedType -> type.unwrap()
is UnwrappedType -> type
}
resultSubstitutor.safeSubstitute(unwrappedType)
}
val commonReturnType = CommonSupertypes.commonSupertype(returnTypes)
return commonReturnType.isUnit()
}
private fun completeLambda(lambda: ResolvedLambdaAtom) {
val returnType = resultSubstitutor.safeSubstitute(lambda.returnType)
val returnType = if (lambda.isCoercedToUnit) {
builtIns.unitType
} else {
resultSubstitutor.safeSubstitute(lambda.returnType)
}
updateTraceForLambda(lambda, topLevelTrace, returnType)
@@ -0,0 +1,64 @@
// !WITH_NEW_INFERENCE
// !DIAGNOSTICS: -UNUSED_PARAMETER
// ISSUE: KT-30242
class A
fun println(s: String = "") {}
fun foo(f: () -> Any) {}
fun test1(b: Boolean) {
foo {
if (b) {
println("meh")
}
}
}
fun test2(b: Boolean) {
foo {
when {
b -> println("meh")
}
}
}
fun test3(b: Boolean) {
foo {
<!INVALID_IF_AS_EXPRESSION!>if<!> (b) {
return@foo A()
}
}
}
fun test4(b: Boolean) {
foo {
if (b) {
return@foo println("meh")
}
if (b) {
println()
}
}
}
fun bar(block: () -> String) {}
fun test_5(b: Boolean) {
bar {
<!NI;TYPE_MISMATCH, TYPE_MISMATCH!>if (b) {
println("meh")
}<!>
}
}
fun test_6(b: Boolean) {
foo {
if (b) {
return@foo Unit
}
if (b) {}
}
}
@@ -0,0 +1,18 @@
package
public fun bar(/*0*/ block: () -> kotlin.String): kotlin.Unit
public fun foo(/*0*/ f: () -> kotlin.Any): kotlin.Unit
public fun println(/*0*/ s: kotlin.String = ...): kotlin.Unit
public fun test1(/*0*/ b: kotlin.Boolean): kotlin.Unit
public fun test2(/*0*/ b: kotlin.Boolean): kotlin.Unit
public fun test3(/*0*/ b: kotlin.Boolean): kotlin.Unit
public fun test4(/*0*/ b: kotlin.Boolean): kotlin.Unit
public fun test_5(/*0*/ b: kotlin.Boolean): kotlin.Unit
public fun test_6(/*0*/ b: kotlin.Boolean): kotlin.Unit
public final class A {
public constructor A()
public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
}
@@ -9805,6 +9805,11 @@ public class DiagnosticsTestGenerated extends AbstractDiagnosticsTest {
runTest("compiler/testData/diagnostics/tests/inference/coercionToUnit/indirectCoercionWithExpectedType.kt");
}
@TestMetadata("kt30242.kt")
public void testKt30242() throws Exception {
runTest("compiler/testData/diagnostics/tests/inference/coercionToUnit/kt30242.kt");
}
@TestMetadata("noCoercion.kt")
public void testNoCoercion() throws Exception {
runTest("compiler/testData/diagnostics/tests/inference/coercionToUnit/noCoercion.kt");
@@ -9800,6 +9800,11 @@ public class DiagnosticsUsingJavacTestGenerated extends AbstractDiagnosticsUsing
runTest("compiler/testData/diagnostics/tests/inference/coercionToUnit/indirectCoercionWithExpectedType.kt");
}
@TestMetadata("kt30242.kt")
public void testKt30242() throws Exception {
runTest("compiler/testData/diagnostics/tests/inference/coercionToUnit/kt30242.kt");
}
@TestMetadata("noCoercion.kt")
public void testNoCoercion() throws Exception {
runTest("compiler/testData/diagnostics/tests/inference/coercionToUnit/noCoercion.kt");