[NI] Fix coercion to Unit for lambdas with empty labeled return

This commit is contained in:
Mikhail Zarechenskiy
2019-01-29 15:37:27 +03:00
parent 147d7844bc
commit 6705803d83
7 changed files with 136 additions and 2 deletions
@@ -178,10 +178,10 @@ class KotlinResolutionCallbacksImpl(
}
val lastExpressionArgument = getLastDeparentesizedExpression(psiCallArgument)?.let { lastExpression ->
if (expectedReturnType?.isUnit() == true) return@let null // coercion to Unit
if (expectedReturnType?.isUnit() == true || hasReturnWithoutExpression) return@let null // coercion to Unit
// todo lastExpression can be if without else
val lastExpressionType = if (hasReturnWithoutExpression) null else trace.getType(lastExpression)
val lastExpressionType = trace.getType(lastExpression)
val contextInfo = lambdaInfo.lastExpressionInfo
val lastExpressionTypeInfo = KotlinTypeInfo(lastExpressionType, contextInfo.dataFlowInfoAfter ?: functionTypeInfo.dataFlowInfo)
createCallArgument(lastExpression, lastExpressionTypeInfo, contextInfo.lexicalScope, contextInfo.trace)
@@ -0,0 +1,42 @@
// !LANGUAGE: +NewInference
class Obj
fun foo(): String? {
run {
if (true) return@run
if (true) Obj()
}
run {
if (true) return@run
if (true) return <!TYPE_MISMATCH, TYPE_MISMATCH!>Obj()<!> // correct error, type check against return type of function "foo"
}
run {
if (true)
return@run
else
if (true) <!UNUSED_EXPRESSION!>42<!>
}
run {
if (true)
42
else
<!INVALID_IF_AS_EXPRESSION!>if<!> (true) 42
}
run {
if (true) return@run
if (true) {
Obj()
} else
if (true) return null
}
return ""
}
@@ -0,0 +1,10 @@
package
public fun foo(): kotlin.String?
public final class Obj {
public constructor Obj()
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
}
@@ -0,0 +1,52 @@
// !LANGUAGE: +NewInference
class Obj
fun foo(): String? {
run {
if (true) return@run
run {
if (true) {
Obj()
} else
<!INVALID_IF_AS_EXPRESSION!>if<!> (true) return null // Error, coercion to Unit doesn't propagate inside nested lambdas
}
if (true) {
Obj()
} else
if (true) return null // OK, no error
}
run {
if (true) return@run
run {
if (true) {
Obj()
} else
<!INVALID_IF_AS_EXPRESSION!>if<!> (true) return null // Error, coercion to Unit doesn't propagate inside nested lambdas
}
}
run {
if (true) return@run
run nestedRun@{
if (true) return@nestedRun
if (true) {
Obj()
} else
if (true) return null // OK, additional empty labeled return helps
}
if (true) {
Obj()
} else
if (true) return null // OK, no error
}
return ""
}
@@ -0,0 +1,10 @@
package
public fun foo(): kotlin.String?
public final class Obj {
public constructor Obj()
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
}
@@ -9710,6 +9710,11 @@ public class DiagnosticsTestGenerated extends AbstractDiagnosticsTest {
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("compiler/testData/diagnostics/tests/inference/coercionToUnit"), Pattern.compile("^(.+)\\.kt$"), TargetBackend.ANY, true);
}
@TestMetadata("coercionToUnitForIfAsLastExpressionInLambda.kt")
public void testCoercionToUnitForIfAsLastExpressionInLambda() throws Exception {
runTest("compiler/testData/diagnostics/tests/inference/coercionToUnit/coercionToUnitForIfAsLastExpressionInLambda.kt");
}
@TestMetadata("coercionWithExpectedType.kt")
public void testCoercionWithExpectedType() throws Exception {
runTest("compiler/testData/diagnostics/tests/inference/coercionToUnit/coercionWithExpectedType.kt");
@@ -9739,6 +9744,11 @@ public class DiagnosticsTestGenerated extends AbstractDiagnosticsTest {
public void testNoCoercion() throws Exception {
runTest("compiler/testData/diagnostics/tests/inference/coercionToUnit/noCoercion.kt");
}
@TestMetadata("nonPropagationOfCoercionToUnitInsideNestedLambda.kt")
public void testNonPropagationOfCoercionToUnitInsideNestedLambda() throws Exception {
runTest("compiler/testData/diagnostics/tests/inference/coercionToUnit/nonPropagationOfCoercionToUnitInsideNestedLambda.kt");
}
}
@TestMetadata("compiler/testData/diagnostics/tests/inference/commonSystem")
@@ -9710,6 +9710,11 @@ public class DiagnosticsUsingJavacTestGenerated extends AbstractDiagnosticsUsing
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("compiler/testData/diagnostics/tests/inference/coercionToUnit"), Pattern.compile("^(.+)\\.kt$"), TargetBackend.ANY, true);
}
@TestMetadata("coercionToUnitForIfAsLastExpressionInLambda.kt")
public void testCoercionToUnitForIfAsLastExpressionInLambda() throws Exception {
runTest("compiler/testData/diagnostics/tests/inference/coercionToUnit/coercionToUnitForIfAsLastExpressionInLambda.kt");
}
@TestMetadata("coercionWithExpectedType.kt")
public void testCoercionWithExpectedType() throws Exception {
runTest("compiler/testData/diagnostics/tests/inference/coercionToUnit/coercionWithExpectedType.kt");
@@ -9739,6 +9744,11 @@ public class DiagnosticsUsingJavacTestGenerated extends AbstractDiagnosticsUsing
public void testNoCoercion() throws Exception {
runTest("compiler/testData/diagnostics/tests/inference/coercionToUnit/noCoercion.kt");
}
@TestMetadata("nonPropagationOfCoercionToUnitInsideNestedLambda.kt")
public void testNonPropagationOfCoercionToUnitInsideNestedLambda() throws Exception {
runTest("compiler/testData/diagnostics/tests/inference/coercionToUnit/nonPropagationOfCoercionToUnitInsideNestedLambda.kt");
}
}
@TestMetadata("compiler/testData/diagnostics/tests/inference/commonSystem")