From 7a7672b0de589249b8080db8af215d5846554858 Mon Sep 17 00:00:00 2001 From: Dmitriy Novozhilov Date: Thu, 9 Sep 2021 15:19:18 +0300 Subject: [PATCH] [FE 1.0] Fix reporting of non exhaustive when statement for whens with subject ^KT-48653 Fixed --- ...irOldFrontendDiagnosticsTestGenerated.java | 12 +++++++++ ...DiagnosticsWithLightTreeTestGenerated.java | 12 +++++++++ .../cfg/ControlFlowInformationProviderImpl.kt | 5 +--- .../diagnostics/tests/when/kt48653_after.kt | 22 ++++++++++++++++ .../diagnostics/tests/when/kt48653_after.txt | 26 +++++++++++++++++++ .../diagnostics/tests/when/kt48653_before.kt | 22 ++++++++++++++++ .../diagnostics/tests/when/kt48653_before.txt | 26 +++++++++++++++++++ .../test/runners/DiagnosticTestGenerated.java | 12 +++++++++ ...CompilerTestFE10TestdataTestGenerated.java | 12 +++++++++ 9 files changed, 145 insertions(+), 4 deletions(-) create mode 100644 compiler/testData/diagnostics/tests/when/kt48653_after.kt create mode 100644 compiler/testData/diagnostics/tests/when/kt48653_after.txt create mode 100644 compiler/testData/diagnostics/tests/when/kt48653_before.kt create mode 100644 compiler/testData/diagnostics/tests/when/kt48653_before.txt 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 1649f24385e..bbd19ff63c5 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 @@ -32111,6 +32111,18 @@ public class FirOldFrontendDiagnosticsTestGenerated extends AbstractFirDiagnosti runTest("compiler/testData/diagnostics/tests/when/kt47922.kt"); } + @Test + @TestMetadata("kt48653_after.kt") + public void testKt48653_after() throws Exception { + runTest("compiler/testData/diagnostics/tests/when/kt48653_after.kt"); + } + + @Test + @TestMetadata("kt48653_before.kt") + public void testKt48653_before() throws Exception { + runTest("compiler/testData/diagnostics/tests/when/kt48653_before.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 a8560113e41..853c182fca3 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 @@ -32111,6 +32111,18 @@ public class FirOldFrontendDiagnosticsWithLightTreeTestGenerated extends Abstrac runTest("compiler/testData/diagnostics/tests/when/kt47922.kt"); } + @Test + @TestMetadata("kt48653_after.kt") + public void testKt48653_after() throws Exception { + runTest("compiler/testData/diagnostics/tests/when/kt48653_after.kt"); + } + + @Test + @TestMetadata("kt48653_before.kt") + public void testKt48653_before() throws Exception { + runTest("compiler/testData/diagnostics/tests/when/kt48653_before.kt"); + } + @Test @TestMetadata("kt9929.kt") public void testKt9929() throws Exception { diff --git a/compiler/frontend/cfg/src/org/jetbrains/kotlin/cfg/ControlFlowInformationProviderImpl.kt b/compiler/frontend/cfg/src/org/jetbrains/kotlin/cfg/ControlFlowInformationProviderImpl.kt index fa05a324898..a4830fce4fd 100644 --- a/compiler/frontend/cfg/src/org/jetbrains/kotlin/cfg/ControlFlowInformationProviderImpl.kt +++ b/compiler/frontend/cfg/src/org/jetbrains/kotlin/cfg/ControlFlowInformationProviderImpl.kt @@ -1002,7 +1002,7 @@ class ControlFlowInformationProviderImpl private constructor( trace.report(EXPECT_TYPE_IN_WHEN_WITHOUT_ELSE.on(element, it.typeOfDeclaration)) } } else if (subjectExpression != null) { - val subjectType = trace.getType(subjectExpression) + val subjectType = WhenChecker.whenSubjectType(element, trace.bindingContext) if (elseEntry != null) { if (missingCases.isEmpty() && subjectType != null && !subjectType.isFlexible()) { val subjectClass = subjectType.constructor.declarationDescriptor as? ClassDescriptor @@ -1016,9 +1016,6 @@ class ControlFlowInformationProviderImpl private constructor( } } continue - } - if (!usedAsExpression) { - } if (!usedAsExpression) { if (languageVersionSettings.supportsFeature(LanguageFeature.WarnAboutNonExhaustiveWhenOnAlgebraicTypes)) { diff --git a/compiler/testData/diagnostics/tests/when/kt48653_after.kt b/compiler/testData/diagnostics/tests/when/kt48653_after.kt new file mode 100644 index 00000000000..ccce194c1e5 --- /dev/null +++ b/compiler/testData/diagnostics/tests/when/kt48653_after.kt @@ -0,0 +1,22 @@ +// FIR_IDENTICAL +// LANGUAGE: +ProhibitNonExhaustiveWhenOnAlgebraicTypes +// ISSUE: KT-48653 + +sealed class Sealed { + object A : Sealed() + object B : Sealed() +} +fun functionReturningSealed(): Sealed = null!! + +fun test_1() { + when (val result = functionReturningSealed()) { + is Sealed.A -> {} + } +} + +fun test_2() { + val result2 = functionReturningSealed() + when (result2) { + is Sealed.A -> {} + } +} diff --git a/compiler/testData/diagnostics/tests/when/kt48653_after.txt b/compiler/testData/diagnostics/tests/when/kt48653_after.txt new file mode 100644 index 00000000000..902ad6ae80d --- /dev/null +++ b/compiler/testData/diagnostics/tests/when/kt48653_after.txt @@ -0,0 +1,26 @@ +package + +public fun functionReturningSealed(): Sealed +public fun test_1(): kotlin.Unit +public fun test_2(): kotlin.Unit + +public sealed class Sealed { + protected constructor Sealed() + 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 A : Sealed { + private constructor A() + 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 B : Sealed { + private constructor B() + 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/when/kt48653_before.kt b/compiler/testData/diagnostics/tests/when/kt48653_before.kt new file mode 100644 index 00000000000..b298075c4ab --- /dev/null +++ b/compiler/testData/diagnostics/tests/when/kt48653_before.kt @@ -0,0 +1,22 @@ +// FIR_IDENTICAL +// LANGUAGE: -ProhibitNonExhaustiveWhenOnAlgebraicTypes +// ISSUE: KT-48653 + +sealed class Sealed { + object A : Sealed() + object B : Sealed() +} +fun functionReturningSealed(): Sealed = null!! + +fun test_1() { + when (val result = functionReturningSealed()) { + is Sealed.A -> {} + } +} + +fun test_2() { + val result2 = functionReturningSealed() + when (result2) { + is Sealed.A -> {} + } +} diff --git a/compiler/testData/diagnostics/tests/when/kt48653_before.txt b/compiler/testData/diagnostics/tests/when/kt48653_before.txt new file mode 100644 index 00000000000..902ad6ae80d --- /dev/null +++ b/compiler/testData/diagnostics/tests/when/kt48653_before.txt @@ -0,0 +1,26 @@ +package + +public fun functionReturningSealed(): Sealed +public fun test_1(): kotlin.Unit +public fun test_2(): kotlin.Unit + +public sealed class Sealed { + protected constructor Sealed() + 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 A : Sealed { + private constructor A() + 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 B : Sealed { + private constructor B() + 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 027e2642316..b943bb7dc9c 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 @@ -32207,6 +32207,18 @@ public class DiagnosticTestGenerated extends AbstractDiagnosticTest { runTest("compiler/testData/diagnostics/tests/when/kt47922.kt"); } + @Test + @TestMetadata("kt48653_after.kt") + public void testKt48653_after() throws Exception { + runTest("compiler/testData/diagnostics/tests/when/kt48653_after.kt"); + } + + @Test + @TestMetadata("kt48653_before.kt") + public void testKt48653_before() throws Exception { + runTest("compiler/testData/diagnostics/tests/when/kt48653_before.kt"); + } + @Test @TestMetadata("kt9929.kt") public void testKt9929() throws Exception { 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 205581b6988..378ced7ee62 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 @@ -32111,6 +32111,18 @@ public class DiagnosisCompilerTestFE10TestdataTestGenerated extends AbstractDiag runTest("compiler/testData/diagnostics/tests/when/kt47922.kt"); } + @Test + @TestMetadata("kt48653_after.kt") + public void testKt48653_after() throws Exception { + runTest("compiler/testData/diagnostics/tests/when/kt48653_after.kt"); + } + + @Test + @TestMetadata("kt48653_before.kt") + public void testKt48653_before() throws Exception { + runTest("compiler/testData/diagnostics/tests/when/kt48653_before.kt"); + } + @Test @TestMetadata("kt9929.kt") public void testKt9929() throws Exception {