diff --git a/compiler/frontend/src/org/jetbrains/kotlin/types/expressions/DataFlowAnalyzer.java b/compiler/frontend/src/org/jetbrains/kotlin/types/expressions/DataFlowAnalyzer.java index e3778d1a893..7693949b816 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/types/expressions/DataFlowAnalyzer.java +++ b/compiler/frontend/src/org/jetbrains/kotlin/types/expressions/DataFlowAnalyzer.java @@ -286,6 +286,9 @@ public class DataFlowAnalyzer { return expressionType; } } + else if (isIfExpression && context.expectedType != NO_EXPECTED_TYPE) { + return expressionType; + } else { context.trace.report(IMPLICIT_CAST_TO_UNIT_OR_ANY.on(expression, expressionType)); } diff --git a/compiler/testData/diagnostics/tests/controlStructures/kt770.kt351.kt735_StatementType.kt b/compiler/testData/diagnostics/tests/controlStructures/kt770.kt351.kt735_StatementType.kt index f4324f58781..a0c559f9690 100644 --- a/compiler/testData/diagnostics/tests/controlStructures/kt770.kt351.kt735_StatementType.kt +++ b/compiler/testData/diagnostics/tests/controlStructures/kt770.kt351.kt735_StatementType.kt @@ -114,6 +114,39 @@ fun testImplicitCoercion() { }) } +fun fooWithAnyArg(arg: Any) {} +fun fooWithAnyNullableArg(arg: Any?) {} + +fun testCoercionToAny() { + val d = 21 + val x1: Any = if (1>2) 1 else 2.0 + val x2: Any? = if (1>2) 1 else 2.0 + val x3: Any? = if (1>2) 1 else (if (1>2) null else 2.0) + + fooWithAnyArg(if (1>2) 1 else 2.0) + fooWithAnyNullableArg(if (1>2) 1 else 2.0) + fooWithAnyNullableArg(if (1>2) 1 else (if (1>2) null else 2.0)) + + val y1: Any = when(d) { 1 -> 1.0 else -> 2.0 } + val y2: Any? = when(d) { 1 -> 1.0 else -> 2.0 } + val y3: Any? = when(d) { 1 -> 1.0; 2 -> null; else -> 2.0 } + + fooWithAnyArg(when(d) { 1 -> 1.0 else -> 2.0 }) + fooWithAnyNullableArg(when(d) { 1 -> 1.0 else -> 2.0 }) + fooWithAnyNullableArg(when(d) { 1 -> 1.0; 2 -> null; else -> 2.0 }) +} + +fun fooWithAnuNullableResult(s: String?, name: String, optional: Boolean): Any? { + return if (s == null) { + if (!optional) { + throw java.lang.IllegalArgumentException("Parameter '$name' was not found in the request") + } + null + } else { + name + } +} + fun bar(a: Unit) {} fun testStatementInExpressionContext() { diff --git a/compiler/testData/diagnostics/tests/controlStructures/kt770.kt351.kt735_StatementType.txt b/compiler/testData/diagnostics/tests/controlStructures/kt770.kt351.kt735_StatementType.txt index 83ec1b67f96..6ef5d018431 100644 --- a/compiler/testData/diagnostics/tests/controlStructures/kt770.kt351.kt735_StatementType.txt +++ b/compiler/testData/diagnostics/tests/controlStructures/kt770.kt351.kt735_StatementType.txt @@ -6,9 +6,13 @@ package kt770_351_735 { public fun box(): kotlin.Int public fun doSmth(/*0*/ i: kotlin.Int): kotlin.Unit public fun foo(): kotlin.Unit + public fun fooWithAnuNullableResult(/*0*/ s: kotlin.String?, /*1*/ name: kotlin.String, /*2*/ optional: kotlin.Boolean): kotlin.Any? + public fun fooWithAnyArg(/*0*/ arg: kotlin.Any): kotlin.Unit + public fun fooWithAnyNullableArg(/*0*/ arg: kotlin.Any?): kotlin.Unit public fun main(/*0*/ args: kotlin.Array): kotlin.Unit public fun test1(): kotlin.Unit public fun test2(): kotlin.Unit + public fun testCoercionToAny(): kotlin.Unit public fun testCoercionToUnit(): kotlin.Unit public fun testImplicitCoercion(): kotlin.Unit public fun testStatementInExpressionContext(): kotlin.Unit