diff --git a/compiler/testData/diagnostics/tests/when/withSubjectVariable/reassignmentToWhenSubjectVariable.kt b/compiler/testData/diagnostics/tests/when/withSubjectVariable/reassignmentToWhenSubjectVariable.kt index 4ede6e1db62..4a71925c5ee 100644 --- a/compiler/testData/diagnostics/tests/when/withSubjectVariable/reassignmentToWhenSubjectVariable.kt +++ b/compiler/testData/diagnostics/tests/when/withSubjectVariable/reassignmentToWhenSubjectVariable.kt @@ -3,7 +3,7 @@ fun foo(): Any = 42 -fun test(x: Any) { +fun test1(x: Any) { when (val y = foo()) { is String -> y = "" } diff --git a/compiler/testData/diagnostics/tests/when/withSubjectVariable/reassignmentToWhenSubjectVariable.txt b/compiler/testData/diagnostics/tests/when/withSubjectVariable/reassignmentToWhenSubjectVariable.txt index 7227d2c6116..45aca6d2b0c 100644 --- a/compiler/testData/diagnostics/tests/when/withSubjectVariable/reassignmentToWhenSubjectVariable.txt +++ b/compiler/testData/diagnostics/tests/when/withSubjectVariable/reassignmentToWhenSubjectVariable.txt @@ -1,4 +1,4 @@ package public fun foo(): kotlin.Any -public fun test(/*0*/ x: kotlin.Any): kotlin.Unit +public fun test1(/*0*/ x: kotlin.Any): kotlin.Unit diff --git a/compiler/testData/diagnostics/tests/when/withSubjectVariable/smartCastOnValueBoundToSubjectVariable.kt b/compiler/testData/diagnostics/tests/when/withSubjectVariable/smartCastOnValueBoundToSubjectVariable.kt index 8845e45cbff..72e74508fb9 100644 --- a/compiler/testData/diagnostics/tests/when/withSubjectVariable/smartCastOnValueBoundToSubjectVariable.kt +++ b/compiler/testData/diagnostics/tests/when/withSubjectVariable/smartCastOnValueBoundToSubjectVariable.kt @@ -1,9 +1,48 @@ // !LANGUAGE: +VariableDeclarationInWhenSubject -fun foo(s1: String, s2: String) = s1 + s2 +fun foo(s1: Int, s2: Int) = s1 + s2 -fun test(x: Any?) = - when (val y = x?.toString()) { - null -> "null" - else -> foo(x.toString(), y) - } \ No newline at end of file +fun test1(x: String?) = + when (val y = x?.length) { + null -> 0 + else -> foo(x.length, y) + } + +fun test2(x: String?) { + when (val y = run { x!! }) { + "foo" -> x.length + "bar" -> y.length + } +} + +fun test3(x: String?, y: String?) { + when (val z = x ?: y!!) { + "foo" -> x.length + "bar" -> y.length + "baz" -> z.length + } +} + +fun id(x: T): T = x + +fun test4(x: String?) { + when (val y = id(x!!)) { + "foo" -> x.length + "bar" -> y.length + } +} + +class Inv(val data: T) + +fun test5(x: Inv) { + when (val y = x.data) { + is String -> y.length // should be ok + null -> x.data.length // should be error + } +} + +fun test6(x: Inv) { + when (val y = x.data) { + is String -> x.data.length // should be ok + } +} \ No newline at end of file diff --git a/compiler/testData/diagnostics/tests/when/withSubjectVariable/smartCastOnValueBoundToSubjectVariable.txt b/compiler/testData/diagnostics/tests/when/withSubjectVariable/smartCastOnValueBoundToSubjectVariable.txt index 8be3c40533b..b8725daaca5 100644 --- a/compiler/testData/diagnostics/tests/when/withSubjectVariable/smartCastOnValueBoundToSubjectVariable.txt +++ b/compiler/testData/diagnostics/tests/when/withSubjectVariable/smartCastOnValueBoundToSubjectVariable.txt @@ -1,4 +1,18 @@ package -public fun foo(/*0*/ s1: kotlin.String, /*1*/ s2: kotlin.String): kotlin.String -public fun test(/*0*/ x: kotlin.Any?): kotlin.String +public fun foo(/*0*/ s1: kotlin.Int, /*1*/ s2: kotlin.Int): kotlin.Int +public fun id(/*0*/ x: T): T +public fun test1(/*0*/ x: kotlin.String?): kotlin.Int +public fun test2(/*0*/ x: kotlin.String?): kotlin.Unit +public fun test3(/*0*/ x: kotlin.String?, /*1*/ y: kotlin.String?): kotlin.Unit +public fun test4(/*0*/ x: kotlin.String?): kotlin.Unit +public fun test5(/*0*/ x: Inv): kotlin.Unit +public fun test6(/*0*/ x: Inv): kotlin.Unit + +public final class Inv { + public constructor Inv(/*0*/ data: T) + public final val data: T + 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 +}