From 1ce96b74332dbca2221223258d9057e0aa4af44b Mon Sep 17 00:00:00 2001 From: "anastasiia.spaseeva" Date: Thu, 27 Feb 2020 14:28:46 +0300 Subject: [PATCH] [FIR] Add tests for KT-37091 , KT-37081 --- .../fir/resolve/testData/resolve/whenElse.kt | 56 ++++++ .../fir/resolve/testData/resolve/whenElse.txt | 104 ++++++++++++ .../testData/resolve/whenExpressionType.kt | 111 ++++++++++++ .../testData/resolve/whenExpressionType.txt | 159 ++++++++++++++++++ .../fir/FirDiagnosticsTestGenerated.java | 10 ++ ...DiagnosticsWithLightTreeTestGenerated.java | 10 ++ 6 files changed, 450 insertions(+) create mode 100644 compiler/fir/resolve/testData/resolve/whenElse.kt create mode 100644 compiler/fir/resolve/testData/resolve/whenElse.txt create mode 100644 compiler/fir/resolve/testData/resolve/whenExpressionType.kt create mode 100644 compiler/fir/resolve/testData/resolve/whenExpressionType.txt diff --git a/compiler/fir/resolve/testData/resolve/whenElse.kt b/compiler/fir/resolve/testData/resolve/whenElse.kt new file mode 100644 index 00000000000..4fcd6982b97 --- /dev/null +++ b/compiler/fir/resolve/testData/resolve/whenElse.kt @@ -0,0 +1,56 @@ + +/* + * UNEXPECTED BEHAVIOUR + * ISSUES: KT-37081 + */ + + +enum class A { + A1, + A2, +} +class B() +class C(val b : B) +fun get(f: Boolean) = if (f) {A.A1} else {""} + +fun case2() { + + val flag: Any = get(false) //string + val l1 = when (flag!!) { // should be NO_ELSE_IN_WHEN + A.A1 -> B() + A.A2 -> B() + } + + val l2 = when (flag) {// should be NO_ELSE_IN_WHEN + A.A1 -> B() + A.A2 -> B() + } +} + +fun case2() { + + val flag: Any = get(true) //A + val l1 = when (flag!!) {// should be NO_ELSE_IN_WHEN + A.A1 -> B() + A.A2 -> B() + } + + val l2 = when (flag) {// should be NO_ELSE_IN_WHEN + A.A1 -> B() + A.A2 -> B() + } +} + +fun case3() { + + val flag = "" //A + val l1 = when (flag!!) {// should be NO_ELSE_IN_WHEN + A.A1 -> B() //should be INCOMPATIBLE_TYPES + A.A2 -> B() //should be INCOMPATIBLE_TYPES + } + + val l2 = when (flag) {// should be NO_ELSE_IN_WHEN + A.A1 -> B() //should be INCOMPATIBLE_TYPES + A.A2 -> B() //should be INCOMPATIBLE_TYPES + } +} \ No newline at end of file diff --git a/compiler/fir/resolve/testData/resolve/whenElse.txt b/compiler/fir/resolve/testData/resolve/whenElse.txt new file mode 100644 index 00000000000..f24bbf8f7f6 --- /dev/null +++ b/compiler/fir/resolve/testData/resolve/whenElse.txt @@ -0,0 +1,104 @@ +FILE: whenElse.kt + public final enum class A : R|kotlin/Enum| { + private constructor(): R|A| { + super|>() + } + + public final static enum entry A1: R|A| + public final static enum entry A2: R|A| + public final static fun values(): R|kotlin/Array| { + } + + public final static fun valueOf(value: R|kotlin/String|): R|A| { + } + + } + public final class B : R|kotlin/Any| { + public constructor(): R|B| { + super() + } + + } + public final class C : R|kotlin/Any| { + public constructor(b: R|B|): R|C| { + super() + } + + public final val b: R|B| = R|/b| + public get(): R|B| + + } + public final fun get(f: R|kotlin/Boolean|): R|kotlin/Comparable| { + ^get when () { + R|/f| -> { + Q|A|.R|/A.A1| + } + else -> { + String() + } + } + + } + public final fun case2(): R|kotlin/Unit| { + lval flag: R|kotlin/Any| = R|/get|(Boolean(false)) + lval l1: R|kotlin/Unit| = when (R|/flag|!!) { + ==($subj$, Q|A|.R|/A.A1|) -> { + R|/B.B|() + } + ==($subj$, Q|A|.R|/A.A2|) -> { + R|/B.B|() + } + } + + lval l2: R|kotlin/Unit| = when (R|/flag|) { + ==($subj$, Q|A|.R|/A.A1|) -> { + R|/B.B|() + } + ==($subj$, Q|A|.R|/A.A2|) -> { + R|/B.B|() + } + } + + } + public final fun case2(): R|kotlin/Unit| { + lval flag: R|kotlin/Any| = R|/get|(Boolean(true)) + lval l1: R|kotlin/Unit| = when (R|/flag|!!) { + ==($subj$, Q|A|.R|/A.A1|) -> { + R|/B.B|() + } + ==($subj$, Q|A|.R|/A.A2|) -> { + R|/B.B|() + } + } + + lval l2: R|kotlin/Unit| = when (R|/flag|) { + ==($subj$, Q|A|.R|/A.A1|) -> { + R|/B.B|() + } + ==($subj$, Q|A|.R|/A.A2|) -> { + R|/B.B|() + } + } + + } + public final fun case3(): R|kotlin/Unit| { + lval flag: R|kotlin/String| = String() + lval l1: R|kotlin/Unit| = when (R|/flag|!!) { + ==($subj$, Q|A|.R|/A.A1|) -> { + R|/B.B|() + } + ==($subj$, Q|A|.R|/A.A2|) -> { + R|/B.B|() + } + } + + lval l2: R|kotlin/Unit| = when (R|/flag|) { + ==($subj$, Q|A|.R|/A.A1|) -> { + R|/B.B|() + } + ==($subj$, Q|A|.R|/A.A2|) -> { + R|/B.B|() + } + } + + } diff --git a/compiler/fir/resolve/testData/resolve/whenExpressionType.kt b/compiler/fir/resolve/testData/resolve/whenExpressionType.kt new file mode 100644 index 00000000000..c8343a057c5 --- /dev/null +++ b/compiler/fir/resolve/testData/resolve/whenExpressionType.kt @@ -0,0 +1,111 @@ +/* + * UNEXPECTED BEHAVIOUR + * ISSUES: KT-37091 + */ + +enum class A() { + A1, A2; +} + +class B() +class C(val b: B) + +// TESTCASE NUMBER: 1 +fun case1() { + val flag = A.A1 + val l0: B = when (flag!!) { + A.A1 -> B() + A.A2 -> B() + } + val x1 = C(l0) //ok (l0 is B) +} + +// TESTCASE NUMBER: 2 +fun case2() { + val flag = A.A1 + val l0: B = when (flag) { + A.A1 -> B() + A.A2 -> B() + } + val x1 = C(l0) //ok (l0 is B) +} + +// TESTCASE NUMBER: 3 +fun case3() { + val flag = A.A1 + + val l1 = when (flag!!) { + A.A1 -> B() + A.A2 -> B() + } + val x1 = C(l1) //INAPPLICABLE_CANDIDATE (l1 is Unit) +} + +// TESTCASE NUMBER: 4 +fun case4() { + val flag = A.A1 + + val l2 = when (flag) { + A.A1 -> B() + A.A2 -> B() + } + val x2 = C(l2) +} + + +// TESTCASE NUMBER: 5 +fun case5() { + val flag: Any = A.A1 + val l1 = when (flag) { + A.A1 -> B() + A.A2 -> B() + else -> B() + } + val x1 = C(l1) +} + + +// TESTCASE NUMBER: 6 +fun case6() { + val flag: Any = A.A1 + val l1 = when (flag!!) { + A.A1 -> B() + A.A2 -> B() + else -> B() + } + val x1 = C(l1) +} + +// TESTCASE NUMBER: 7 +fun case7() { + val flag: Any = A.A1 + val l1: B = when (flag) { + A.A1 -> B() + A.A2 -> B() + else -> B() + } + val x1 = C(l1) +} + + +// TESTCASE NUMBER: 8 +fun case8() { + val flag: Any = A.A1 + val l1: B = when (flag!!) { + A.A1 -> B() + A.A2 -> B() + else -> B() + } + val x1 = C(l1) +} + +// TESTCASE NUMBER: 9 +fun case9() { + val flag: Any = A.A1 + val l1 = when (flag) { + A.A1 -> B() + A.A2 -> B() + } + val x1 = C(l1) +} + diff --git a/compiler/fir/resolve/testData/resolve/whenExpressionType.txt b/compiler/fir/resolve/testData/resolve/whenExpressionType.txt new file mode 100644 index 00000000000..1d8f558fa6a --- /dev/null +++ b/compiler/fir/resolve/testData/resolve/whenExpressionType.txt @@ -0,0 +1,159 @@ +FILE: whenExpressionType.kt + public final enum class A : R|kotlin/Enum| { + private constructor(): R|A| { + super|>() + } + + public final static enum entry A1: R|A| + public final static enum entry A2: R|A| + public final static fun values(): R|kotlin/Array| { + } + + public final static fun valueOf(value: R|kotlin/String|): R|A| { + } + + } + public final class B : R|kotlin/Any| { + public constructor(): R|B| { + super() + } + + } + public final class C : R|kotlin/Any| { + public constructor(b: R|B|): R|C| { + super() + } + + public final val b: R|B| = R|/b| + public get(): R|B| + + } + public final fun case1(): R|kotlin/Unit| { + lval flag: R|A| = Q|A|.R|/A.A1| + lval l0: R|B| = when (R|/flag|!!) { + ==($subj$, Q|A|.R|/A.A1|) -> { + R|/B.B|() + } + ==($subj$, Q|A|.R|/A.A2|) -> { + R|/B.B|() + } + } + + lval x1: R|C| = R|/C.C|(R|/l0|) + } + public final fun case2(): R|kotlin/Unit| { + lval flag: R|A| = Q|A|.R|/A.A1| + lval l0: R|B| = when (R|/flag|) { + ==($subj$, Q|A|.R|/A.A1|) -> { + R|/B.B|() + } + ==($subj$, Q|A|.R|/A.A2|) -> { + R|/B.B|() + } + } + + lval x1: R|C| = R|/C.C|(R|/l0|) + } + public final fun case3(): R|kotlin/Unit| { + lval flag: R|A| = Q|A|.R|/A.A1| + lval l1: R|kotlin/Unit| = when (R|/flag|!!) { + ==($subj$, Q|A|.R|/A.A1|) -> { + R|/B.B|() + } + ==($subj$, Q|A|.R|/A.A2|) -> { + R|/B.B|() + } + } + + lval x1: = #(R|/l1|) + } + public final fun case4(): R|kotlin/Unit| { + lval flag: R|A| = Q|A|.R|/A.A1| + lval l2: R|B| = when (R|/flag|) { + ==($subj$, Q|A|.R|/A.A1|) -> { + R|/B.B|() + } + ==($subj$, Q|A|.R|/A.A2|) -> { + R|/B.B|() + } + } + + lval x2: R|C| = R|/C.C|(R|/l2|) + } + public final fun case5(): R|kotlin/Unit| { + lval flag: R|kotlin/Any| = Q|A|.R|/A.A1| + lval l1: R|B| = when (R|/flag|) { + ==($subj$, Q|A|.R|/A.A1|) -> { + R|/B.B|() + } + ==($subj$, Q|A|.R|/A.A2|) -> { + R|/B.B|() + } + else -> { + R|/B.B|() + } + } + + lval x1: R|C| = R|/C.C|(R|/l1|) + } + public final fun case6(): R|kotlin/Unit| { + lval flag: R|kotlin/Any| = Q|A|.R|/A.A1| + lval l1: R|B| = when (R|/flag|!!) { + ==($subj$, Q|A|.R|/A.A1|) -> { + R|/B.B|() + } + ==($subj$, Q|A|.R|/A.A2|) -> { + R|/B.B|() + } + else -> { + R|/B.B|() + } + } + + lval x1: R|C| = R|/C.C|(R|/l1|) + } + public final fun case7(): R|kotlin/Unit| { + lval flag: R|kotlin/Any| = Q|A|.R|/A.A1| + lval l1: R|B| = when (R|/flag|) { + ==($subj$, Q|A|.R|/A.A1|) -> { + R|/B.B|() + } + ==($subj$, Q|A|.R|/A.A2|) -> { + R|/B.B|() + } + else -> { + R|/B.B|() + } + } + + lval x1: R|C| = R|/C.C|(R|/l1|) + } + public final fun case8(): R|kotlin/Unit| { + lval flag: R|kotlin/Any| = Q|A|.R|/A.A1| + lval l1: R|B| = when (R|/flag|!!) { + ==($subj$, Q|A|.R|/A.A1|) -> { + R|/B.B|() + } + ==($subj$, Q|A|.R|/A.A2|) -> { + R|/B.B|() + } + else -> { + R|/B.B|() + } + } + + lval x1: R|C| = R|/C.C|(R|/l1|) + } + public final fun case9(): R|kotlin/Unit| { + lval flag: R|kotlin/Any| = Q|A|.R|/A.A1| + lval l1: R|B| = when (R|/flag|) { + ==($subj$, Q|A|.R|/A.A1|) -> { + R|/B.B|() + } + ==($subj$, Q|A|.R|/A.A2|) -> { + R|/B.B|() + } + } + + lval x1: R|C| = R|/C.C|(R|/l1|) + } diff --git a/compiler/fir/resolve/tests/org/jetbrains/kotlin/fir/FirDiagnosticsTestGenerated.java b/compiler/fir/resolve/tests/org/jetbrains/kotlin/fir/FirDiagnosticsTestGenerated.java index 29e1d2359bf..b0f92678e93 100644 --- a/compiler/fir/resolve/tests/org/jetbrains/kotlin/fir/FirDiagnosticsTestGenerated.java +++ b/compiler/fir/resolve/tests/org/jetbrains/kotlin/fir/FirDiagnosticsTestGenerated.java @@ -368,6 +368,16 @@ public class FirDiagnosticsTestGenerated extends AbstractFirDiagnosticsTest { runTest("compiler/fir/resolve/testData/resolve/whenAsReceiver.kt"); } + @TestMetadata("whenElse.kt") + public void testWhenElse() throws Exception { + runTest("compiler/fir/resolve/testData/resolve/whenElse.kt"); + } + + @TestMetadata("whenExpressionType.kt") + public void testWhenExpressionType() throws Exception { + runTest("compiler/fir/resolve/testData/resolve/whenExpressionType.kt"); + } + @TestMetadata("whenInference.kt") public void testWhenInference() throws Exception { runTest("compiler/fir/resolve/testData/resolve/whenInference.kt"); diff --git a/compiler/fir/resolve/tests/org/jetbrains/kotlin/fir/FirDiagnosticsWithLightTreeTestGenerated.java b/compiler/fir/resolve/tests/org/jetbrains/kotlin/fir/FirDiagnosticsWithLightTreeTestGenerated.java index 2f073cf5dc7..8ab85b7f9f8 100644 --- a/compiler/fir/resolve/tests/org/jetbrains/kotlin/fir/FirDiagnosticsWithLightTreeTestGenerated.java +++ b/compiler/fir/resolve/tests/org/jetbrains/kotlin/fir/FirDiagnosticsWithLightTreeTestGenerated.java @@ -368,6 +368,16 @@ public class FirDiagnosticsWithLightTreeTestGenerated extends AbstractFirDiagnos runTest("compiler/fir/resolve/testData/resolve/whenAsReceiver.kt"); } + @TestMetadata("whenElse.kt") + public void testWhenElse() throws Exception { + runTest("compiler/fir/resolve/testData/resolve/whenElse.kt"); + } + + @TestMetadata("whenExpressionType.kt") + public void testWhenExpressionType() throws Exception { + runTest("compiler/fir/resolve/testData/resolve/whenExpressionType.kt"); + } + @TestMetadata("whenInference.kt") public void testWhenInference() throws Exception { runTest("compiler/fir/resolve/testData/resolve/whenInference.kt");