FIR: allow no else branch for when on Nothing(?)

This commit is contained in:
Tianyu Geng
2021-07-26 15:02:22 -07:00
committed by teamcityserver
parent e242ad955b
commit f737d8002e
8 changed files with 69 additions and 2 deletions
@@ -32089,6 +32089,12 @@ public class FirOldFrontendDiagnosticsTestGenerated extends AbstractFirDiagnosti
runTest("compiler/testData/diagnostics/tests/when/whenAndLambdaWithExpectedType.kt");
}
@Test
@TestMetadata("whenOnNothing.kt")
public void testWhenOnNothing() throws Exception {
runTest("compiler/testData/diagnostics/tests/when/whenOnNothing.kt");
}
@Test
@TestMetadata("WhenTypeDisjunctions.kt")
public void testWhenTypeDisjunctions() throws Exception {
@@ -32089,6 +32089,12 @@ public class FirOldFrontendDiagnosticsWithLightTreeTestGenerated extends Abstrac
runTest("compiler/testData/diagnostics/tests/when/whenAndLambdaWithExpectedType.kt");
}
@Test
@TestMetadata("whenOnNothing.kt")
public void testWhenOnNothing() throws Exception {
runTest("compiler/testData/diagnostics/tests/when/whenOnNothing.kt");
}
@Test
@TestMetadata("WhenTypeDisjunctions.kt")
public void testWhenTypeDisjunctions() throws Exception {
@@ -10,7 +10,9 @@ import org.jetbrains.kotlin.descriptors.Modality
import org.jetbrains.kotlin.diagnostics.WhenMissingCase
import org.jetbrains.kotlin.fir.FirElement
import org.jetbrains.kotlin.fir.FirSession
import org.jetbrains.kotlin.fir.declarations.*
import org.jetbrains.kotlin.fir.declarations.FirEnumEntry
import org.jetbrains.kotlin.fir.declarations.FirRegularClass
import org.jetbrains.kotlin.fir.declarations.getSealedClassInheritors
import org.jetbrains.kotlin.fir.declarations.utils.collectEnumEntries
import org.jetbrains.kotlin.fir.declarations.utils.modality
import org.jetbrains.kotlin.fir.expressions.*
@@ -33,7 +35,8 @@ class FirWhenExhaustivenessTransformer(private val bodyResolveComponents: BodyRe
private val exhaustivenessCheckers = listOf(
WhenOnBooleanExhaustivenessChecker,
WhenOnEnumExhaustivenessChecker,
WhenOnSealedClassExhaustivenessChecker
WhenOnSealedClassExhaustivenessChecker,
WhenOnNothingExhaustivenessChecker
)
@OptIn(ExperimentalStdlibApi::class)
@@ -389,3 +392,18 @@ private object WhenOnSealedClassExhaustivenessChecker : WhenExhaustivenessChecke
}
}
}
private object WhenOnNothingExhaustivenessChecker : WhenExhaustivenessChecker() {
override fun isApplicable(subjectType: ConeKotlinType, session: FirSession): Boolean {
return subjectType.isNullableNothing || subjectType.isNothing
}
override fun computeMissingCases(
whenExpression: FirWhenExpression,
subjectType: ConeKotlinType,
session: FirSession,
destination: MutableCollection<WhenMissingCase>
) {
// Nothing has no branches. The null case for `Nothing?` is handled by WhenOnNullableExhaustivenessChecker
}
}
@@ -0,0 +1,11 @@
// !DIAGNOSTICS: -UNREACHABLE_CODE
// exhaustive
fun test1(n: Nothing) = when (n) { }
fun test2(n: Nothing?) = when (n) {
null -> {}
}
// not exhaustive
fun test3(n: Nothing?) = <!NO_ELSE_IN_WHEN!>when<!> (n) {
}
@@ -0,0 +1,11 @@
// !DIAGNOSTICS: -UNREACHABLE_CODE
// exhaustive
fun test1(n: Nothing) = when (n) { }
fun test2(n: Nothing?) = <!NO_ELSE_IN_WHEN!>when<!> (<!DEBUG_INFO_CONSTANT!>n<!>) {
null -> {}
}
// not exhaustive
fun test3(n: Nothing?) = <!NO_ELSE_IN_WHEN!>when<!> (<!DEBUG_INFO_CONSTANT!>n<!>) {
}
@@ -0,0 +1,3 @@
package
public fun foo(/*0*/ n: kotlin.Nothing, /*1*/ nn: kotlin.Nothing?): kotlin.Unit
@@ -32185,6 +32185,12 @@ public class DiagnosticTestGenerated extends AbstractDiagnosticTest {
runTest("compiler/testData/diagnostics/tests/when/whenAndLambdaWithExpectedType.kt");
}
@Test
@TestMetadata("whenOnNothing.kt")
public void testWhenOnNothing() throws Exception {
runTest("compiler/testData/diagnostics/tests/when/whenOnNothing.kt");
}
@Test
@TestMetadata("WhenTypeDisjunctions.kt")
public void testWhenTypeDisjunctions() throws Exception {
@@ -32089,6 +32089,12 @@ public class DiagnosisCompilerTestFE10TestdataTestGenerated extends AbstractDiag
runTest("compiler/testData/diagnostics/tests/when/whenAndLambdaWithExpectedType.kt");
}
@Test
@TestMetadata("whenOnNothing.kt")
public void testWhenOnNothing() throws Exception {
runTest("compiler/testData/diagnostics/tests/when/whenOnNothing.kt");
}
@Test
@TestMetadata("WhenTypeDisjunctions.kt")
public void testWhenTypeDisjunctions() throws Exception {