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)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+2
-2
@@ -10,7 +10,7 @@ object CC : C()
|
||||
fun foo(a: A) {
|
||||
if (a is B) {
|
||||
if (a is C) {
|
||||
val t = <!NO_ELSE_IN_WHEN!>when<!> (a) {
|
||||
val t = when (a) {
|
||||
is CC -> "CC"
|
||||
}
|
||||
}
|
||||
@@ -20,7 +20,7 @@ fun foo(a: A) {
|
||||
fun foo2(a: A) {
|
||||
if (a is C) {
|
||||
if (a is B) {
|
||||
val t = <!NO_ELSE_IN_WHEN!>when<!> (a) {
|
||||
val t = when (a) {
|
||||
is CC -> "CC"
|
||||
}
|
||||
}
|
||||
|
||||
+42
@@ -0,0 +1,42 @@
|
||||
// !CHECK_TYPE
|
||||
|
||||
sealed class A {
|
||||
class A1 : A()
|
||||
class A2 : A()
|
||||
}
|
||||
|
||||
sealed class B {
|
||||
class B1 : B()
|
||||
class B2 : B()
|
||||
}
|
||||
|
||||
fun foo(a: A) {
|
||||
if (a !is B) return
|
||||
|
||||
when (a) {
|
||||
is A.A1 -> ""
|
||||
is A.A2 -> "v"
|
||||
}.length
|
||||
|
||||
when (a) {
|
||||
is A.A1 -> ""
|
||||
is A.A2 -> "v"
|
||||
}.length // OK
|
||||
|
||||
when (a) {
|
||||
is A.A1 -> ""
|
||||
is A.A2 -> "v"
|
||||
is B.B1 -> "..." // should be warning: unreachable code
|
||||
}.length // OK
|
||||
|
||||
when (a) {
|
||||
is A.A1 -> ""
|
||||
is B.B1 -> "..."
|
||||
is A.A2 -> "v"
|
||||
}.length // OK
|
||||
|
||||
<!NO_ELSE_IN_WHEN!>when<!> (a) {
|
||||
is A.A1 -> ""
|
||||
is B.B1 -> "..."
|
||||
}.<!UNRESOLVED_REFERENCE!>length<!>
|
||||
}
|
||||
+42
@@ -0,0 +1,42 @@
|
||||
// !CHECK_TYPE
|
||||
|
||||
sealed class A {
|
||||
class A1 : A()
|
||||
class A2 : A()
|
||||
}
|
||||
|
||||
sealed class B {
|
||||
class B1 : B()
|
||||
class B2 : B()
|
||||
}
|
||||
|
||||
fun foo(a: A) {
|
||||
if (a !is B) return
|
||||
|
||||
<!NO_ELSE_IN_WHEN!>when<!> (<!DEBUG_INFO_SMARTCAST!>a<!>) {
|
||||
is A.A1 -> ""
|
||||
is A.A2 -> "v"
|
||||
}.length
|
||||
|
||||
<!NO_ELSE_IN_WHEN!>when<!> (<!DEBUG_INFO_SMARTCAST!>a<!>) {
|
||||
is A.A1 -> ""
|
||||
is A.A2 -> "v"
|
||||
}.length // OK
|
||||
|
||||
<!NO_ELSE_IN_WHEN!>when<!> (<!DEBUG_INFO_SMARTCAST!>a<!>) {
|
||||
is A.A1 -> ""
|
||||
is A.A2 -> "v"
|
||||
is <!INCOMPATIBLE_TYPES!>B.B1<!> -> "..." // should be warning: unreachable code
|
||||
}.length // OK
|
||||
|
||||
<!NO_ELSE_IN_WHEN!>when<!> (<!DEBUG_INFO_SMARTCAST!>a<!>) {
|
||||
is A.A1 -> ""
|
||||
is <!INCOMPATIBLE_TYPES!>B.B1<!> -> "..."
|
||||
is A.A2 -> "v"
|
||||
}.length // OK
|
||||
|
||||
<!NO_ELSE_IN_WHEN!>when<!> (<!DEBUG_INFO_SMARTCAST!>a<!>) {
|
||||
is A.A1 -> ""
|
||||
is <!INCOMPATIBLE_TYPES!>B.B1<!> -> "..."
|
||||
}.length
|
||||
}
|
||||
+45
@@ -0,0 +1,45 @@
|
||||
package
|
||||
|
||||
public fun foo(/*0*/ a: A): kotlin.Unit
|
||||
|
||||
public sealed class A {
|
||||
protected 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 final class A1 : A {
|
||||
public constructor A1()
|
||||
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 final class A2 : A {
|
||||
public constructor A2()
|
||||
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 B {
|
||||
protected 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
|
||||
|
||||
public final class B1 : B {
|
||||
public constructor B1()
|
||||
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 final class B2 : B {
|
||||
public constructor B2()
|
||||
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
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
// SKIP_TXT
|
||||
// FIR_IDENTICAL
|
||||
// !CHECK_TYPE
|
||||
|
||||
sealed class KtClassifierSymbol
|
||||
|
||||
interface KtNamedSymbol
|
||||
|
||||
abstract class KtTypeParameterSymbol : KtClassifierSymbol() {}
|
||||
|
||||
sealed class KtClassLikeSymbol : KtClassifierSymbol() {}
|
||||
|
||||
fun foo(symbol: KtClassifierSymbol) {
|
||||
if (symbol !is KtNamedSymbol) return
|
||||
val x = when (symbol) {
|
||||
is KtClassLikeSymbol -> "1"
|
||||
is KtTypeParameterSymbol -> "2"
|
||||
}
|
||||
|
||||
x checkType { _<String>() }
|
||||
}
|
||||
+30
@@ -0,0 +1,30 @@
|
||||
package
|
||||
|
||||
public fun foo(/*0*/ symbol: KtClassifierSymbol): kotlin.Unit
|
||||
|
||||
public sealed class KtClassLikeSymbol : KtClassifierSymbol {
|
||||
protected constructor KtClassLikeSymbol()
|
||||
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 KtClassifierSymbol {
|
||||
protected constructor KtClassifierSymbol()
|
||||
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 interface KtNamedSymbol {
|
||||
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 abstract class KtTypeParameterSymbol : KtClassifierSymbol {
|
||||
public constructor KtTypeParameterSymbol()
|
||||
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
|
||||
}
|
||||
Generated
+12
@@ -31127,6 +31127,18 @@ public class DiagnosticTestGenerated extends AbstractDiagnosticTest {
|
||||
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 {
|
||||
|
||||
+10
@@ -27161,6 +27161,16 @@ public class DiagnosisCompilerTestFE10TestdataTestGenerated extends AbstractDiag
|
||||
runTest("compiler/testData/diagnostics/tests/when/ExhaustiveWithNullabilityCheckElse.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("intersectionExhaustivenessComplex.kt")
|
||||
public void testIntersectionExhaustivenessComplex() throws Exception {
|
||||
runTest("compiler/testData/diagnostics/tests/when/intersectionExhaustivenessComplex.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("intersectionExhaustivenessSimple.kt")
|
||||
public void testIntersectionExhaustivenessSimple() throws Exception {
|
||||
runTest("compiler/testData/diagnostics/tests/when/intersectionExhaustivenessSimple.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("kt10439.kt")
|
||||
public void testKt10439() throws Exception {
|
||||
runTest("compiler/testData/diagnostics/tests/when/kt10439.kt");
|
||||
|
||||
Reference in New Issue
Block a user