diff --git a/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirOldFrontendDiagnosticsTestGenerated.java b/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirOldFrontendDiagnosticsTestGenerated.java index 1c51433c2f5..6281b9375b9 100644 --- a/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirOldFrontendDiagnosticsTestGenerated.java +++ b/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirOldFrontendDiagnosticsTestGenerated.java @@ -31921,6 +31921,12 @@ public class FirOldFrontendDiagnosticsTestGenerated extends AbstractFirDiagnosti runTest("compiler/testData/diagnostics/tests/when/kt4434.kt"); } + @Test + @TestMetadata("kt47922.kt") + public void testKt47922() throws Exception { + runTest("compiler/testData/diagnostics/tests/when/kt47922.kt"); + } + @Test @TestMetadata("kt9929.kt") public void testKt9929() throws Exception { diff --git a/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirOldFrontendDiagnosticsWithLightTreeTestGenerated.java b/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirOldFrontendDiagnosticsWithLightTreeTestGenerated.java index 9b2ba8cb5b9..864df50edbd 100644 --- a/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirOldFrontendDiagnosticsWithLightTreeTestGenerated.java +++ b/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirOldFrontendDiagnosticsWithLightTreeTestGenerated.java @@ -31921,6 +31921,12 @@ public class FirOldFrontendDiagnosticsWithLightTreeTestGenerated extends Abstrac runTest("compiler/testData/diagnostics/tests/when/kt4434.kt"); } + @Test + @TestMetadata("kt47922.kt") + public void testKt47922() throws Exception { + runTest("compiler/testData/diagnostics/tests/when/kt47922.kt"); + } + @Test @TestMetadata("kt9929.kt") public void testKt9929() throws Exception { 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 6e22a61515b..2daa406b02d 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/types/expressions/DataFlowAnalyzer.java +++ b/compiler/frontend/src/org/jetbrains/kotlin/types/expressions/DataFlowAnalyzer.java @@ -305,8 +305,11 @@ public class DataFlowAnalyzer { } if (expression instanceof KtWhenExpression) { - // No need in additional check because type mismatch is already reported for entries - return expressionType; + KtWhenExpression whenExpression = (KtWhenExpression) expression; + if (!whenExpression.getEntries().isEmpty()) { + // No need in additional check because type mismatch is already reported for entries + return expressionType; + } } SmartCastResult castResult = checkPossibleCast(expressionType, expression, c); diff --git a/compiler/testData/diagnostics/tests/sealed/WhenOnEmptySealed.fir.kt b/compiler/testData/diagnostics/tests/sealed/WhenOnEmptySealed.fir.kt new file mode 100644 index 00000000000..ea45861d7cd --- /dev/null +++ b/compiler/testData/diagnostics/tests/sealed/WhenOnEmptySealed.fir.kt @@ -0,0 +1,11 @@ +// !DIAGNOSTICS: -UNUSED_EXPRESSION +sealed class Sealed { + +} + +fun foo(s: Sealed): Int { + return when(s) { + // We do not return anything, so else branch must be here + } +} + diff --git a/compiler/testData/diagnostics/tests/sealed/WhenOnEmptySealed.kt b/compiler/testData/diagnostics/tests/sealed/WhenOnEmptySealed.kt index 05304d09f67..036c84fc8cb 100644 --- a/compiler/testData/diagnostics/tests/sealed/WhenOnEmptySealed.kt +++ b/compiler/testData/diagnostics/tests/sealed/WhenOnEmptySealed.kt @@ -1,12 +1,11 @@ -// FIR_IDENTICAL // !DIAGNOSTICS: -UNUSED_EXPRESSION sealed class Sealed { } fun foo(s: Sealed): Int { - return when(s) { + return when(s) { // We do not return anything, so else branch must be here - } + } } diff --git a/compiler/testData/diagnostics/tests/when/kt47922.fir.kt b/compiler/testData/diagnostics/tests/when/kt47922.fir.kt new file mode 100644 index 00000000000..1d39f987a62 --- /dev/null +++ b/compiler/testData/diagnostics/tests/when/kt47922.fir.kt @@ -0,0 +1,21 @@ +// ISSUE: KT-47922 + +package whencase.castissue + +sealed class SealedBase { + object Complete : SealedBase() +} + +abstract class NonSealedBase { + object Complete : NonSealedBase() +} + +sealed class ToState + +val sealedTest: SealedBase.() -> ToState? = { + when(this) {} +} + +val nonSealedTest: NonSealedBase.() -> ToState? = { + when(this) {} +} diff --git a/compiler/testData/diagnostics/tests/when/kt47922.kt b/compiler/testData/diagnostics/tests/when/kt47922.kt new file mode 100644 index 00000000000..a919bf47920 --- /dev/null +++ b/compiler/testData/diagnostics/tests/when/kt47922.kt @@ -0,0 +1,21 @@ +// ISSUE: KT-47922 + +package whencase.castissue + +sealed class SealedBase { + object Complete : SealedBase() +} + +abstract class NonSealedBase { + object Complete : NonSealedBase() +} + +sealed class ToState + +val sealedTest: SealedBase.() -> ToState? = { + when(this) {} +} + +val nonSealedTest: NonSealedBase.() -> ToState? = { + when(this) {} +} diff --git a/compiler/testData/diagnostics/tests/when/kt47922.txt b/compiler/testData/diagnostics/tests/when/kt47922.txt new file mode 100644 index 00000000000..c3114744221 --- /dev/null +++ b/compiler/testData/diagnostics/tests/when/kt47922.txt @@ -0,0 +1,44 @@ +package + +package whencase { + + package whencase.castissue { + public val nonSealedTest: whencase.castissue.NonSealedBase.() -> whencase.castissue.ToState? + public val sealedTest: whencase.castissue.SealedBase.() -> whencase.castissue.ToState? + + public abstract class NonSealedBase { + public constructor NonSealedBase() + 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 + + public object Complete : whencase.castissue.NonSealedBase { + private constructor Complete() + 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 + } + } + + public sealed class SealedBase { + protected constructor SealedBase() + 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 + + public object Complete : whencase.castissue.SealedBase { + private constructor Complete() + 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 + } + } + + public sealed class ToState { + protected constructor ToState() + 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-common-new/tests-gen/org/jetbrains/kotlin/test/runners/DiagnosticTestGenerated.java b/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/DiagnosticTestGenerated.java index 0c0e4383c60..696cdec20c4 100644 --- a/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/DiagnosticTestGenerated.java +++ b/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/DiagnosticTestGenerated.java @@ -32017,6 +32017,12 @@ public class DiagnosticTestGenerated extends AbstractDiagnosticTest { runTest("compiler/testData/diagnostics/tests/when/kt4434.kt"); } + @Test + @TestMetadata("kt47922.kt") + public void testKt47922() throws Exception { + runTest("compiler/testData/diagnostics/tests/when/kt47922.kt"); + } + @Test @TestMetadata("kt9929.kt") public void testKt9929() throws Exception { diff --git a/compiler/tests-spec/testData/diagnostics/linked/expressions/when-expression/exhaustive-when-expressions/p-2/neg/1.1.fir.kt b/compiler/tests-spec/testData/diagnostics/linked/expressions/when-expression/exhaustive-when-expressions/p-2/neg/1.1.fir.kt new file mode 100644 index 00000000000..ca615536179 --- /dev/null +++ b/compiler/tests-spec/testData/diagnostics/linked/expressions/when-expression/exhaustive-when-expressions/p-2/neg/1.1.fir.kt @@ -0,0 +1,25 @@ +// SKIP_TXT + +/* + * KOTLIN DIAGNOSTICS SPEC TEST (NEGATIVE) + * + * SPEC VERSION: 0.1-100 + * MAIN LINK: expressions, when-expression, exhaustive-when-expressions -> paragraph 2 -> sentence 1 + * NUMBER: 1 + * DESCRIPTION: Non-exhaustive when, without bound value, without else branch. + */ + +// TESTCASE NUMBER: 1 +fun case_1(value_1: Int): String = when { + value_1 == 1 -> "" + value_1 == 2 -> "" + value_1 == 3 -> "" +} + +// TESTCASE NUMBER: 2 +fun case_2(value_1: Int): String = when { + value_1 == 1 -> "" +} + +// TESTCASE NUMBER: 3 +fun case_3(): Int = when {} diff --git a/compiler/tests-spec/testData/diagnostics/linked/expressions/when-expression/exhaustive-when-expressions/p-2/neg/1.1.kt b/compiler/tests-spec/testData/diagnostics/linked/expressions/when-expression/exhaustive-when-expressions/p-2/neg/1.1.kt index 62f00d0c2c2..c4277d5c861 100644 --- a/compiler/tests-spec/testData/diagnostics/linked/expressions/when-expression/exhaustive-when-expressions/p-2/neg/1.1.kt +++ b/compiler/tests-spec/testData/diagnostics/linked/expressions/when-expression/exhaustive-when-expressions/p-2/neg/1.1.kt @@ -1,4 +1,3 @@ -// FIR_IDENTICAL // SKIP_TXT /* @@ -23,4 +22,4 @@ fun case_2(value_1: Int): String = when { } // TESTCASE NUMBER: 3 -fun case_3(): Int = when {} +fun case_3(): Int = when {} diff --git a/compiler/tests-spec/testData/diagnostics/linked/expressions/when-expression/exhaustive-when-expressions/p-2/neg/1.2.fir.kt b/compiler/tests-spec/testData/diagnostics/linked/expressions/when-expression/exhaustive-when-expressions/p-2/neg/1.2.fir.kt new file mode 100644 index 00000000000..6df980a3982 --- /dev/null +++ b/compiler/tests-spec/testData/diagnostics/linked/expressions/when-expression/exhaustive-when-expressions/p-2/neg/1.2.fir.kt @@ -0,0 +1,26 @@ +// !DIAGNOSTICS: -UNUSED_EXPRESSION +// SKIP_TXT + +/* + * KOTLIN DIAGNOSTICS SPEC TEST (NEGATIVE) + * + * SPEC VERSION: 0.1-100 + * MAIN LINK: expressions, when-expression, exhaustive-when-expressions -> paragraph 2 -> sentence 1 + * NUMBER: 2 + * DESCRIPTION: Non-exhaustive when, with bound value, without else branch. + */ + +// TESTCASE NUMBER: 1 +fun case_1(value_1: Int): String = when (value_1) { + 1 -> "" + 2 -> "" + 3 -> "" +} + +// TESTCASE NUMBER: 2 +fun case_2(value_1: Int): String = when (value_1) { + 1 -> "" +} + +// TESTCASE NUMBER: 3 +fun case_3(value_1: Int): Int = when (value_1) {} diff --git a/compiler/tests-spec/testData/diagnostics/linked/expressions/when-expression/exhaustive-when-expressions/p-2/neg/1.2.kt b/compiler/tests-spec/testData/diagnostics/linked/expressions/when-expression/exhaustive-when-expressions/p-2/neg/1.2.kt index 2621f903616..a7101f99523 100644 --- a/compiler/tests-spec/testData/diagnostics/linked/expressions/when-expression/exhaustive-when-expressions/p-2/neg/1.2.kt +++ b/compiler/tests-spec/testData/diagnostics/linked/expressions/when-expression/exhaustive-when-expressions/p-2/neg/1.2.kt @@ -1,4 +1,3 @@ -// FIR_IDENTICAL // !DIAGNOSTICS: -UNUSED_EXPRESSION // SKIP_TXT @@ -24,4 +23,4 @@ fun case_2(value_1: Int): String = when (value_1) { } // TESTCASE NUMBER: 3 -fun case_3(value_1: Int): Int = when (value_1) {} +fun case_3(value_1: Int): Int = when (value_1) {} diff --git a/compiler/tests-spec/testData/diagnostics/linked/expressions/when-expression/exhaustive-when-expressions/p-2/neg/11.1.fir.kt b/compiler/tests-spec/testData/diagnostics/linked/expressions/when-expression/exhaustive-when-expressions/p-2/neg/11.1.fir.kt new file mode 100644 index 00000000000..ac8c01f4382 --- /dev/null +++ b/compiler/tests-spec/testData/diagnostics/linked/expressions/when-expression/exhaustive-when-expressions/p-2/neg/11.1.fir.kt @@ -0,0 +1,35 @@ +// LANGUAGE: +ProhibitSimplificationOfNonTrivialConstBooleanExpressions +// !DIAGNOSTICS: -UNUSED_EXPRESSION +// SKIP_TXT + +/* + * KOTLIN DIAGNOSTICS SPEC TEST (NEGATIVE) + * + * SPEC VERSION: 0.1-435 + * MAIN LINK: expressions, when-expression, exhaustive-when-expressions -> paragraph 2 -> sentence 11 + * NUMBER: 1 + * DESCRIPTION: Non-exhaustive when using nullable boolean values. + */ + + +// TESTCASE NUMBER: 1 +fun case_1(value_1: Boolean?): String = when(value_1) { + true -> "" + false -> "" +} + +// TESTCASE NUMBER: 2 +fun case_2(value_1: Boolean?): String = when(value_1) { + true -> "" + null -> "" +} + +// TESTCASE NUMBER: 3 +fun case_3(value_1: Boolean?): Int = when(value_1) { } + +// TESTCASE NUMBER: 4 +fun case_4(value_1: Boolean?): String = when (value_1) { + true && false && ((true || false)) || true && !!!false && !!!true -> "" + true && false && ((true || false)) || true && !!!false -> "" + null -> "" +} diff --git a/compiler/tests-spec/testData/diagnostics/linked/expressions/when-expression/exhaustive-when-expressions/p-2/neg/11.1.kt b/compiler/tests-spec/testData/diagnostics/linked/expressions/when-expression/exhaustive-when-expressions/p-2/neg/11.1.kt index b7266707c0d..a83702fb404 100644 --- a/compiler/tests-spec/testData/diagnostics/linked/expressions/when-expression/exhaustive-when-expressions/p-2/neg/11.1.kt +++ b/compiler/tests-spec/testData/diagnostics/linked/expressions/when-expression/exhaustive-when-expressions/p-2/neg/11.1.kt @@ -1,4 +1,3 @@ -// FIR_IDENTICAL // LANGUAGE: +ProhibitSimplificationOfNonTrivialConstBooleanExpressions // !DIAGNOSTICS: -UNUSED_EXPRESSION // SKIP_TXT @@ -26,7 +25,7 @@ fun case_2(value_1: Boolean?): String = when(value_1) { } // TESTCASE NUMBER: 3 -fun case_3(value_1: Boolean?): Int = when(value_1) { } +fun case_3(value_1: Boolean?): Int = when(value_1) { } // TESTCASE NUMBER: 4 fun case_4(value_1: Boolean?): String = when (value_1) { diff --git a/compiler/tests-spec/testData/diagnostics/linked/expressions/when-expression/exhaustive-when-expressions/p-2/neg/11.2.fir.kt b/compiler/tests-spec/testData/diagnostics/linked/expressions/when-expression/exhaustive-when-expressions/p-2/neg/11.2.fir.kt new file mode 100644 index 00000000000..014eec0ea2a --- /dev/null +++ b/compiler/tests-spec/testData/diagnostics/linked/expressions/when-expression/exhaustive-when-expressions/p-2/neg/11.2.fir.kt @@ -0,0 +1,81 @@ +// !DIAGNOSTICS: -UNUSED_EXPRESSION +// SKIP_TXT + +/* + * KOTLIN DIAGNOSTICS SPEC TEST (NEGATIVE) + * + * SPEC VERSION: 0.1-435 + * MAIN LINK: expressions, when-expression, exhaustive-when-expressions -> paragraph 2 -> sentence 11 + * NUMBER: 2 + * DESCRIPTION: Non-exhaustive when using subclasses of the nullable sealed class. + * HELPERS: sealedClasses + */ + +// TESTCASE NUMBER: 1 +fun case_1(value_1: SealedClass?): String = when(value_1) { + is SealedChild1 -> "" + is SealedChild2 -> "" + is SealedChild3 -> "" +} + +// TESTCASE NUMBER: 2 +fun case_2(value_1: SealedClassMixed?): String = when(value_1) { + is SealedMixedChild1 -> "" + is SealedMixedChild2 -> "" + SealedMixedChildObject1 -> "" + null -> "" +} + +// TESTCASE NUMBER: 3 +fun case_3(value_1: SealedClassMixed?): String = when(value_1) { + null, is SealedMixedChild1, is SealedMixedChild2, SealedMixedChildObject1 -> "" +} + +// TESTCASE NUMBER: 4 +fun case_4(value_1: SealedClassMixed?): String = when(value_1) { + is SealedMixedChild1 -> "" + is SealedMixedChild2 -> "" + is SealedMixedChild3 -> "" + SealedMixedChildObject1 -> "" + SealedMixedChildObject2 -> "" + SealedMixedChildObject3 -> "" +} + +// TESTCASE NUMBER: 5 +fun case_5(value_1: SealedClassMixed?): String = when(value_1) { + is SealedMixedChild1 -> "" + is SealedMixedChild2 -> "" + is SealedMixedChild3 -> "" +} + +// TESTCASE NUMBER: 6 +fun case_6(value_1: SealedClassMixed?): Int = when(value_1) {} + +// TESTCASE NUMBER: 7 +fun case_7(value_1: SealedClassMixed?): String = when(value_1) { + is SealedMixedChild1 -> "" + is SealedMixedChild2-> "" + is SealedMixedChild3 -> "" + null -> "" +} + +// TESTCASE NUMBER: 8 +fun case_8(value_1: SealedClassMixed?): String = when(value_1) { + SealedMixedChildObject1 -> "" +} + +/* + * TESTCASE NUMBER: 9 + * DISCUSSION: maybe make exhaustive without else? + */ +fun case_9(value_1: Any?): String = when (value_1) { + is Any -> "" + null -> "" +} + +/* + * TESTCASE NUMBER: 10 + * DISCUSSION + * ISSUES: KT-26044 + */ +fun case_10(value: SealedClassEmpty): String = when (value) {} diff --git a/compiler/tests-spec/testData/diagnostics/linked/expressions/when-expression/exhaustive-when-expressions/p-2/neg/11.2.kt b/compiler/tests-spec/testData/diagnostics/linked/expressions/when-expression/exhaustive-when-expressions/p-2/neg/11.2.kt index 270201e6b75..db4c7bd0add 100644 --- a/compiler/tests-spec/testData/diagnostics/linked/expressions/when-expression/exhaustive-when-expressions/p-2/neg/11.2.kt +++ b/compiler/tests-spec/testData/diagnostics/linked/expressions/when-expression/exhaustive-when-expressions/p-2/neg/11.2.kt @@ -1,4 +1,3 @@ -// FIR_IDENTICAL // !DIAGNOSTICS: -UNUSED_EXPRESSION // SKIP_TXT @@ -50,7 +49,7 @@ fun case_5(value_1: SealedClassMixed?): String = when(valu } // TESTCASE NUMBER: 6 -fun case_6(value_1: SealedClassMixed?): Int = when(value_1) {} +fun case_6(value_1: SealedClassMixed?): Int = when(value_1) {} // TESTCASE NUMBER: 7 fun case_7(value_1: SealedClassMixed?): String = when(value_1) { @@ -79,4 +78,4 @@ fun case_9(value_1: Any?): String = when (value_1) { * DISCUSSION * ISSUES: KT-26044 */ -fun case_10(value: SealedClassEmpty): String = when (value) {} +fun case_10(value: SealedClassEmpty): String = when (value) {} diff --git a/compiler/tests-spec/testData/diagnostics/linked/expressions/when-expression/exhaustive-when-expressions/p-2/neg/11.3.fir.kt b/compiler/tests-spec/testData/diagnostics/linked/expressions/when-expression/exhaustive-when-expressions/p-2/neg/11.3.fir.kt new file mode 100644 index 00000000000..1a7f76a7371 --- /dev/null +++ b/compiler/tests-spec/testData/diagnostics/linked/expressions/when-expression/exhaustive-when-expressions/p-2/neg/11.3.fir.kt @@ -0,0 +1,46 @@ +// !DIAGNOSTICS: -UNUSED_EXPRESSION +// SKIP_TXT + +/* + * KOTLIN DIAGNOSTICS SPEC TEST (NEGATIVE) + * + * SPEC VERSION: 0.1-435 + * MAIN LINK: expressions, when-expression, exhaustive-when-expressions -> paragraph 2 -> sentence 11 + * NUMBER: 3 + * DESCRIPTION: Non-exhaustive when using nullable enum values. + * HELPERS: enumClasses + */ + +// TESTCASE NUMBER: 1 +fun case_1(value_1: EnumClass?): String = when(value_1) { + EnumClass.EAST -> "" + EnumClass.SOUTH -> "" + EnumClass.NORTH -> "" + EnumClass.WEST -> "" +} + +// TESTCASE NUMBER: 2 +fun case_2(value_1: EnumClass?): String = when(value_1) { + EnumClass.EAST -> "" + EnumClass.SOUTH -> "" + EnumClass.NORTH -> "" + null -> "" +} + +// TESTCASE NUMBER: 3 +fun case_3(value_1: EnumClass?): String = when(value_1) { + EnumClass.EAST, null, EnumClass.SOUTH, EnumClass.NORTH -> "" +} + +// TESTCASE NUMBER: 4 +fun case_4(value_1: EnumClassSingle): Int = when(value_1) {} + +// TESTCASE NUMBER: 5 +fun case_5(value_1: EnumClassSingle?): String = when(value_1) { + EnumClassSingle.EVERYTHING -> "" +} + +// TESTCASE NUMBER: 6 +fun case_6(value_1: EnumClassSingle?): String = when(value_1) { + null -> "" +} diff --git a/compiler/tests-spec/testData/diagnostics/linked/expressions/when-expression/exhaustive-when-expressions/p-2/neg/11.3.kt b/compiler/tests-spec/testData/diagnostics/linked/expressions/when-expression/exhaustive-when-expressions/p-2/neg/11.3.kt index 69b5d521070..680a7abc1b6 100644 --- a/compiler/tests-spec/testData/diagnostics/linked/expressions/when-expression/exhaustive-when-expressions/p-2/neg/11.3.kt +++ b/compiler/tests-spec/testData/diagnostics/linked/expressions/when-expression/exhaustive-when-expressions/p-2/neg/11.3.kt @@ -1,4 +1,3 @@ -// FIR_IDENTICAL // !DIAGNOSTICS: -UNUSED_EXPRESSION // SKIP_TXT @@ -34,7 +33,7 @@ fun case_3(value_1: EnumClass?): String = when(value_1) { } // TESTCASE NUMBER: 4 -fun case_4(value_1: EnumClassSingle): Int = when(value_1) {} +fun case_4(value_1: EnumClassSingle): Int = when(value_1) {} // TESTCASE NUMBER: 5 fun case_5(value_1: EnumClassSingle?): String = when(value_1) { diff --git a/compiler/tests-spec/testData/diagnostics/linked/expressions/when-expression/exhaustive-when-expressions/p-2/neg/3.1.fir.kt b/compiler/tests-spec/testData/diagnostics/linked/expressions/when-expression/exhaustive-when-expressions/p-2/neg/3.1.fir.kt new file mode 100644 index 00000000000..d9d21146aa9 --- /dev/null +++ b/compiler/tests-spec/testData/diagnostics/linked/expressions/when-expression/exhaustive-when-expressions/p-2/neg/3.1.fir.kt @@ -0,0 +1,53 @@ +// LANGUAGE: +ProhibitSimplificationOfNonTrivialConstBooleanExpressions +// !DIAGNOSTICS: -UNUSED_EXPRESSION +// SKIP_TXT + +/* + * KOTLIN DIAGNOSTICS SPEC TEST (NEGATIVE) + * + * SPEC VERSION: 0.1-100 + * MAIN LINK: expressions, when-expression, exhaustive-when-expressions -> paragraph 2 -> sentence 3 + * NUMBER: 1 + * DESCRIPTION: Non-exhaustive when using boolean values. + */ + +// TESTCASE NUMBER: 1 +fun case_1(value_1: Boolean): String = when(value_1) { + true -> "" +} + +// TESTCASE NUMBER: 2 +fun case_2(value_1: Boolean): String = when(value_1) { + false -> "" +} + +// TESTCASE NUMBER: 3 +fun case_3(value_1: Boolean): Int = when(value_1) { } + +// TESTCASE NUMBER: 4 +fun case_4(value_1: Boolean): String = when { + value_1 == true -> "" + value_1 == false -> "" +} + +/* + * TESTCASE NUMBER: 5 + * DISCUSSION: maybe use const propagation here? + * ISSUES: KT-25265 + */ +fun case_5(value_1: Boolean): String { + val trueValue = true + val falseValue = false + + return when (value_1) { + trueValue -> "" + falseValue -> "" + } +} + + +// TESTCASE NUMBER: 6 +fun case_6(value_1: Boolean): String = when (value_1) { + true && false && ((true || false)) || true && !!!false && !!!true -> "" + true && false && ((true || false)) || true && !!!false -> "" +} diff --git a/compiler/tests-spec/testData/diagnostics/linked/expressions/when-expression/exhaustive-when-expressions/p-2/neg/3.1.kt b/compiler/tests-spec/testData/diagnostics/linked/expressions/when-expression/exhaustive-when-expressions/p-2/neg/3.1.kt index bf4cc306d2c..fd5d99bde08 100644 --- a/compiler/tests-spec/testData/diagnostics/linked/expressions/when-expression/exhaustive-when-expressions/p-2/neg/3.1.kt +++ b/compiler/tests-spec/testData/diagnostics/linked/expressions/when-expression/exhaustive-when-expressions/p-2/neg/3.1.kt @@ -1,4 +1,3 @@ -// FIR_IDENTICAL // LANGUAGE: +ProhibitSimplificationOfNonTrivialConstBooleanExpressions // !DIAGNOSTICS: -UNUSED_EXPRESSION // SKIP_TXT @@ -23,7 +22,7 @@ fun case_2(value_1: Boolean): String = when(value_1) { } // TESTCASE NUMBER: 3 -fun case_3(value_1: Boolean): Int = when(value_1) { } +fun case_3(value_1: Boolean): Int = when(value_1) { } // TESTCASE NUMBER: 4 fun case_4(value_1: Boolean): String = when { diff --git a/compiler/tests-spec/testData/diagnostics/linked/expressions/when-expression/exhaustive-when-expressions/p-2/neg/9.1.fir.kt b/compiler/tests-spec/testData/diagnostics/linked/expressions/when-expression/exhaustive-when-expressions/p-2/neg/9.1.fir.kt new file mode 100644 index 00000000000..b40e79ef1b0 --- /dev/null +++ b/compiler/tests-spec/testData/diagnostics/linked/expressions/when-expression/exhaustive-when-expressions/p-2/neg/9.1.fir.kt @@ -0,0 +1,83 @@ +// !DIAGNOSTICS: -UNUSED_EXPRESSION +// SKIP_TXT + +/* + * KOTLIN DIAGNOSTICS SPEC TEST (NEGATIVE) + * + * SPEC VERSION: 0.1-435 + * MAIN LINK: expressions, when-expression, exhaustive-when-expressions -> paragraph 2 -> sentence 9 + * NUMBER: 1 + * DESCRIPTION: Non-exhaustive when using subclasses of the sealed class. + * HELPERS: sealedClasses + */ + +// TESTCASE NUMBER: 1 +fun case_1(value_1: SealedClass): String = when(value_1) { + is SealedChild1 -> "" + is SealedChild2 -> "" +} + +// TESTCASE NUMBER: 2 +fun case_2(value_1: SealedClass): String = when(value_1) { + is SealedChild1, is SealedChild2 -> "" +} + +// TESTCASE NUMBER: 3 +fun case_3(value_1: SealedClassMixed): String = when(value_1) { + is SealedMixedChild1 -> "" + is SealedMixedChild2 -> "" + SealedMixedChildObject1 -> "" +} + +// TESTCASE NUMBER: 4 +fun case_4(value_1: SealedClassMixed): String = when(value_1) { + SealedMixedChildObject1, is SealedMixedChild2, is SealedMixedChild1 -> "" +} + +// TESTCASE NUMBER: 5 +fun case_5(value_1: SealedClassMixed): String = when(value_1) { + is SealedMixedChild1 -> "" + is SealedMixedChild2 -> "" + is SealedMixedChild3 -> "" +} + +// TESTCASE NUMBER: 6 +fun case_6(value_1: SealedClassMixed): Int = when(value_1) { } + +// TESTCASE NUMBER: 7 +fun case_7(value_1: SealedClassSingleWithObject): Int = when(value_1) { } + +// TESTCASE NUMBER: 8 +fun case_8(value_1: SealedClassEmpty): String = when (value_1) { } + +// TESTCASE NUMBER: 9 +fun case_9(value_1: Number): String = when (value_1) { + is Byte -> "" + is Double -> "" + is Float -> "" + is Int -> "" + is Long -> "" + is Short -> "" +} + +/* + * TESTCASE NUMBER: 10 + * DISCUSSION: maybe make exhaustive without else? + */ +fun case_10(value_1: Any): String = when (value_1) { + is Any -> "" +} + +// TESTCASE NUMBER: 11 +fun case_11(value_1: SealedClass): String = when { + value_1 is SealedChild1 -> "" + value_1 is SealedChild2 -> "" + value_1 is SealedChild3 -> "" +} + +// TESTCASE NUMBER: 12 +fun case_12(value_1: SealedClassMixed): String = when(value_1) { + is SealedMixedChild1 -> "" + is SealedMixedChild2 -> "" + is SealedMixedChild3 -> "" +} diff --git a/compiler/tests-spec/testData/diagnostics/linked/expressions/when-expression/exhaustive-when-expressions/p-2/neg/9.1.kt b/compiler/tests-spec/testData/diagnostics/linked/expressions/when-expression/exhaustive-when-expressions/p-2/neg/9.1.kt index adfb86ec3cd..35ea467d79d 100644 --- a/compiler/tests-spec/testData/diagnostics/linked/expressions/when-expression/exhaustive-when-expressions/p-2/neg/9.1.kt +++ b/compiler/tests-spec/testData/diagnostics/linked/expressions/when-expression/exhaustive-when-expressions/p-2/neg/9.1.kt @@ -1,4 +1,3 @@ -// FIR_IDENTICAL // !DIAGNOSTICS: -UNUSED_EXPRESSION // SKIP_TXT @@ -43,13 +42,13 @@ fun case_5(value_1: SealedClassMixed): String = when(value } // TESTCASE NUMBER: 6 -fun case_6(value_1: SealedClassMixed): Int = when(value_1) { } +fun case_6(value_1: SealedClassMixed): Int = when(value_1) { } // TESTCASE NUMBER: 7 -fun case_7(value_1: SealedClassSingleWithObject): Int = when(value_1) { } +fun case_7(value_1: SealedClassSingleWithObject): Int = when(value_1) { } // TESTCASE NUMBER: 8 -fun case_8(value_1: SealedClassEmpty): String = when (value_1) { } +fun case_8(value_1: SealedClassEmpty): String = when (value_1) { } // TESTCASE NUMBER: 9 fun case_9(value_1: Number): String = when (value_1) { diff --git a/idea/idea-frontend-fir/idea-fir-low-level-api/tests/org/jetbrains/kotlin/idea/fir/low/level/api/diagnostic/compiler/based/DiagnosisCompilerTestFE10TestdataTestGenerated.java b/idea/idea-frontend-fir/idea-fir-low-level-api/tests/org/jetbrains/kotlin/idea/fir/low/level/api/diagnostic/compiler/based/DiagnosisCompilerTestFE10TestdataTestGenerated.java index a2cecdcdd3b..e0bbda5c650 100644 --- a/idea/idea-frontend-fir/idea-fir-low-level-api/tests/org/jetbrains/kotlin/idea/fir/low/level/api/diagnostic/compiler/based/DiagnosisCompilerTestFE10TestdataTestGenerated.java +++ b/idea/idea-frontend-fir/idea-fir-low-level-api/tests/org/jetbrains/kotlin/idea/fir/low/level/api/diagnostic/compiler/based/DiagnosisCompilerTestFE10TestdataTestGenerated.java @@ -31921,6 +31921,12 @@ public class DiagnosisCompilerTestFE10TestdataTestGenerated extends AbstractDiag runTest("compiler/testData/diagnostics/tests/when/kt4434.kt"); } + @Test + @TestMetadata("kt47922.kt") + public void testKt47922() throws Exception { + runTest("compiler/testData/diagnostics/tests/when/kt47922.kt"); + } + @Test @TestMetadata("kt9929.kt") public void testKt9929() throws Exception {