diff --git a/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/tower/KotlinResolutionCallbacksImpl.kt b/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/tower/KotlinResolutionCallbacksImpl.kt index 0cdb9349ef7..36f1fa339b5 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/tower/KotlinResolutionCallbacksImpl.kt +++ b/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/tower/KotlinResolutionCallbacksImpl.kt @@ -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) diff --git a/compiler/testData/diagnostics/tests/inference/coercionToUnit/coercionToUnitForIfAsLastExpressionInLambda.kt b/compiler/testData/diagnostics/tests/inference/coercionToUnit/coercionToUnitForIfAsLastExpressionInLambda.kt new file mode 100644 index 00000000000..b232d44632e --- /dev/null +++ b/compiler/testData/diagnostics/tests/inference/coercionToUnit/coercionToUnitForIfAsLastExpressionInLambda.kt @@ -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 Obj() // correct error, type check against return type of function "foo" + } + + run { + if (true) + return@run + else + if (true) 42 + } + + run { + if (true) + 42 + else + if (true) 42 + } + + run { + if (true) return@run + + if (true) { + Obj() + } else + if (true) return null + } + + return "" +} \ No newline at end of file diff --git a/compiler/testData/diagnostics/tests/inference/coercionToUnit/coercionToUnitForIfAsLastExpressionInLambda.txt b/compiler/testData/diagnostics/tests/inference/coercionToUnit/coercionToUnitForIfAsLastExpressionInLambda.txt new file mode 100644 index 00000000000..8f26e35ee42 --- /dev/null +++ b/compiler/testData/diagnostics/tests/inference/coercionToUnit/coercionToUnitForIfAsLastExpressionInLambda.txt @@ -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 +} diff --git a/compiler/testData/diagnostics/tests/inference/coercionToUnit/nonPropagationOfCoercionToUnitInsideNestedLambda.kt b/compiler/testData/diagnostics/tests/inference/coercionToUnit/nonPropagationOfCoercionToUnitInsideNestedLambda.kt new file mode 100644 index 00000000000..05f01522125 --- /dev/null +++ b/compiler/testData/diagnostics/tests/inference/coercionToUnit/nonPropagationOfCoercionToUnitInsideNestedLambda.kt @@ -0,0 +1,52 @@ +// !LANGUAGE: +NewInference + +class Obj + +fun foo(): String? { + run { + if (true) return@run + + run { + if (true) { + Obj() + } else + 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 + 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 "" +} \ No newline at end of file diff --git a/compiler/testData/diagnostics/tests/inference/coercionToUnit/nonPropagationOfCoercionToUnitInsideNestedLambda.txt b/compiler/testData/diagnostics/tests/inference/coercionToUnit/nonPropagationOfCoercionToUnitInsideNestedLambda.txt new file mode 100644 index 00000000000..8f26e35ee42 --- /dev/null +++ b/compiler/testData/diagnostics/tests/inference/coercionToUnit/nonPropagationOfCoercionToUnitInsideNestedLambda.txt @@ -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 +} diff --git a/compiler/tests/org/jetbrains/kotlin/checkers/DiagnosticsTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/checkers/DiagnosticsTestGenerated.java index 5343ba29ca6..9884395956e 100644 --- a/compiler/tests/org/jetbrains/kotlin/checkers/DiagnosticsTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/checkers/DiagnosticsTestGenerated.java @@ -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") diff --git a/compiler/tests/org/jetbrains/kotlin/checkers/javac/DiagnosticsUsingJavacTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/checkers/javac/DiagnosticsUsingJavacTestGenerated.java index 0974a463117..7a0491b2f29 100644 --- a/compiler/tests/org/jetbrains/kotlin/checkers/javac/DiagnosticsUsingJavacTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/checkers/javac/DiagnosticsUsingJavacTestGenerated.java @@ -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")