FIR: Support exhaustive whens on subjects of intersection type
This commit is contained in:
+12
@@ -31031,6 +31031,18 @@ public class FirOldFrontendDiagnosticsTestGenerated extends AbstractFirDiagnosti
|
||||
runTest("compiler/testData/diagnostics/tests/when/ExhaustiveWithNullabilityCheckElse.kt");
|
||||
}
|
||||
|
||||
@Test
|
||||
@TestMetadata("intersectionExhaustivenessComplex.kt")
|
||||
public void testIntersectionExhaustivenessComplex() throws Exception {
|
||||
runTest("compiler/testData/diagnostics/tests/when/intersectionExhaustivenessComplex.kt");
|
||||
}
|
||||
|
||||
@Test
|
||||
@TestMetadata("intersectionExhaustivenessSimple.kt")
|
||||
public void testIntersectionExhaustivenessSimple() throws Exception {
|
||||
runTest("compiler/testData/diagnostics/tests/when/intersectionExhaustivenessSimple.kt");
|
||||
}
|
||||
|
||||
@Test
|
||||
@TestMetadata("kt10439.kt")
|
||||
public void testKt10439() throws Exception {
|
||||
|
||||
+12
@@ -31031,6 +31031,18 @@ public class FirOldFrontendDiagnosticsWithLightTreeTestGenerated extends Abstrac
|
||||
runTest("compiler/testData/diagnostics/tests/when/ExhaustiveWithNullabilityCheckElse.kt");
|
||||
}
|
||||
|
||||
@Test
|
||||
@TestMetadata("intersectionExhaustivenessComplex.kt")
|
||||
public void testIntersectionExhaustivenessComplex() throws Exception {
|
||||
runTest("compiler/testData/diagnostics/tests/when/intersectionExhaustivenessComplex.kt");
|
||||
}
|
||||
|
||||
@Test
|
||||
@TestMetadata("intersectionExhaustivenessSimple.kt")
|
||||
public void testIntersectionExhaustivenessSimple() throws Exception {
|
||||
runTest("compiler/testData/diagnostics/tests/when/intersectionExhaustivenessSimple.kt");
|
||||
}
|
||||
|
||||
@Test
|
||||
@TestMetadata("kt10439.kt")
|
||||
public void testKt10439() throws Exception {
|
||||
|
||||
+34
-9
@@ -18,13 +18,13 @@ import org.jetbrains.kotlin.fir.resolve.BodyResolveComponents
|
||||
import org.jetbrains.kotlin.fir.resolve.fullyExpandedType
|
||||
import org.jetbrains.kotlin.fir.resolve.symbolProvider
|
||||
import org.jetbrains.kotlin.fir.symbols.AbstractFirBasedSymbol
|
||||
import org.jetbrains.kotlin.name.StandardClassIds
|
||||
import org.jetbrains.kotlin.fir.symbols.impl.FirClassSymbol
|
||||
import org.jetbrains.kotlin.fir.symbols.impl.FirRegularClassSymbol
|
||||
import org.jetbrains.kotlin.fir.symbols.impl.FirVariableSymbol
|
||||
import org.jetbrains.kotlin.fir.types.*
|
||||
import org.jetbrains.kotlin.fir.visitors.FirTransformer
|
||||
import org.jetbrains.kotlin.fir.visitors.FirVisitor
|
||||
import org.jetbrains.kotlin.name.StandardClassIds
|
||||
|
||||
class FirWhenExhaustivenessTransformer(private val bodyResolveComponents: BodyResolveComponents) : FirTransformer<Any?>() {
|
||||
companion object {
|
||||
@@ -60,32 +60,57 @@ class FirWhenExhaustivenessTransformer(private val bodyResolveComponents: BodyRe
|
||||
|
||||
val session = bodyResolveComponents.session
|
||||
val cleanSubjectType = subjectType.fullyExpandedType(session).lowerBoundIfFlexible()
|
||||
val unwrappedIntersectionTypes = (cleanSubjectType as? ConeIntersectionType)?.intersectedTypes ?: listOf(cleanSubjectType)
|
||||
|
||||
val checkers = buildList {
|
||||
exhaustivenessCheckers.filterTo(this) { it.isApplicable(cleanSubjectType, session) }
|
||||
if (isNotEmpty<WhenExhaustivenessChecker>() && cleanSubjectType.isMarkedNullable) {
|
||||
|
||||
var status: ExhaustivenessStatus = ExhaustivenessStatus.NotExhaustive.NO_ELSE_BRANCH
|
||||
|
||||
for (unwrappedSubjectType in unwrappedIntersectionTypes) {
|
||||
val localStatus = computeStatusForNonIntersectionType(unwrappedSubjectType, session, whenExpression)
|
||||
when {
|
||||
localStatus === ExhaustivenessStatus.Exhaustive -> {
|
||||
status = localStatus
|
||||
break
|
||||
}
|
||||
localStatus !== ExhaustivenessStatus.NotExhaustive.NO_ELSE_BRANCH && status === ExhaustivenessStatus.NotExhaustive.NO_ELSE_BRANCH -> {
|
||||
status = localStatus
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
whenExpression.replaceExhaustivenessStatus(status)
|
||||
}
|
||||
|
||||
@OptIn(ExperimentalStdlibApi::class)
|
||||
private fun computeStatusForNonIntersectionType(
|
||||
unwrappedSubjectType: ConeKotlinType,
|
||||
session: FirSession,
|
||||
whenExpression: FirWhenExpression,
|
||||
): ExhaustivenessStatus {
|
||||
val checkers = buildList<WhenExhaustivenessChecker> {
|
||||
exhaustivenessCheckers.filterTo(this) { it.isApplicable(unwrappedSubjectType, session) }
|
||||
if (isNotEmpty() && unwrappedSubjectType.isMarkedNullable) {
|
||||
add(WhenOnNullableExhaustivenessChecker)
|
||||
}
|
||||
}
|
||||
|
||||
if (checkers.isEmpty()) {
|
||||
whenExpression.replaceExhaustivenessStatus(ExhaustivenessStatus.NotExhaustive.NO_ELSE_BRANCH)
|
||||
return
|
||||
return ExhaustivenessStatus.NotExhaustive.NO_ELSE_BRANCH
|
||||
}
|
||||
|
||||
val whenMissingCases = mutableListOf<WhenMissingCase>()
|
||||
for (checker in checkers) {
|
||||
checker.computeMissingCases(whenExpression, cleanSubjectType, session, whenMissingCases)
|
||||
checker.computeMissingCases(whenExpression, unwrappedSubjectType, session, whenMissingCases)
|
||||
}
|
||||
if (whenMissingCases.isEmpty() && whenExpression.branches.isEmpty()) {
|
||||
whenMissingCases.add(WhenMissingCase.Unknown)
|
||||
}
|
||||
|
||||
val status = if (whenMissingCases.isEmpty()) {
|
||||
return if (whenMissingCases.isEmpty()) {
|
||||
ExhaustivenessStatus.Exhaustive
|
||||
} else {
|
||||
ExhaustivenessStatus.NotExhaustive(whenMissingCases)
|
||||
}
|
||||
whenExpression.replaceExhaustivenessStatus(status)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user