Fix regression on smart casts in when on a sealed class
See the comment in PatternMatchingTypingVisitor #KT-27221 Fixed
This commit is contained in:
+7
-1
@@ -410,7 +410,13 @@ class PatternMatchingTypingVisitor internal constructor(facade: ExpressionTyping
|
|||||||
possibleTypesForSubject: Set<KotlinType>
|
possibleTypesForSubject: Set<KotlinType>
|
||||||
) {
|
) {
|
||||||
val subjectExpression = expression.subjectExpression ?: return
|
val subjectExpression = expression.subjectExpression ?: return
|
||||||
for (possibleCastType in possibleTypesForSubject) {
|
// Using "reversed()" here is a kind of hack to fix KT-27221
|
||||||
|
// KT-27221 was a breaking change introduced after DataFlowImpl optimization that changed the order of possible types to reversed,
|
||||||
|
// and it led to the wrong sealed class being chosen.
|
||||||
|
// But it seems that order of collected types shouldn't matter at all
|
||||||
|
// (at least because the order of relevant checks might change the behavior, see KT-27252)
|
||||||
|
// TODO: Read the comment above, wait for resolution in KT-27252 and get rid of "reversed" call here
|
||||||
|
for (possibleCastType in possibleTypesForSubject.reversed()) {
|
||||||
val possibleCastClass = possibleCastType.constructor.declarationDescriptor as? ClassDescriptor ?: continue
|
val possibleCastClass = possibleCastType.constructor.declarationDescriptor as? ClassDescriptor ?: continue
|
||||||
if (possibleCastClass.kind == ClassKind.ENUM_CLASS || possibleCastClass.modality == Modality.SEALED) {
|
if (possibleCastClass.kind == ClassKind.ENUM_CLASS || possibleCastClass.modality == Modality.SEALED) {
|
||||||
if (checkSmartCastToExpectedTypeInSubject(
|
if (checkSmartCastToExpectedTypeInSubject(
|
||||||
|
|||||||
@@ -0,0 +1,28 @@
|
|||||||
|
// !DIAGNOSTICS: -UNUSED_VARIABLE
|
||||||
|
// SKIP_TXT
|
||||||
|
|
||||||
|
sealed class A
|
||||||
|
sealed class B : A()
|
||||||
|
sealed class C : B()
|
||||||
|
object BB : B()
|
||||||
|
object CC : C()
|
||||||
|
|
||||||
|
fun foo(a: A) {
|
||||||
|
if (a is B) {
|
||||||
|
if (a is C) {
|
||||||
|
val t = when (<!DEBUG_INFO_SMARTCAST!>a<!>) {
|
||||||
|
is CC -> "CC"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fun foo2(a: A) {
|
||||||
|
if (a is C) {
|
||||||
|
if (<!USELESS_IS_CHECK!>a is B<!>) {
|
||||||
|
val t = when (<!DEBUG_INFO_SMARTCAST!>a<!>) {
|
||||||
|
is CC -> "CC"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,36 @@
|
|||||||
|
// !DIAGNOSTICS: -UNUSED_VARIABLE
|
||||||
|
// SKIP_TXT
|
||||||
|
|
||||||
|
sealed class A
|
||||||
|
sealed class B : A()
|
||||||
|
sealed class C : B()
|
||||||
|
sealed class D : C()
|
||||||
|
object BB : B()
|
||||||
|
object CC : C()
|
||||||
|
object DD : D()
|
||||||
|
|
||||||
|
fun foo1(a: A) {
|
||||||
|
if (a is B) {
|
||||||
|
if (a is D) {
|
||||||
|
if (<!USELESS_IS_CHECK!>a is C<!>) {
|
||||||
|
val t =
|
||||||
|
when (<!DEBUG_INFO_SMARTCAST!>a<!>) {
|
||||||
|
is DD -> "DD"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fun foo2(a: A) {
|
||||||
|
if (a is B) {
|
||||||
|
if (a is D) {
|
||||||
|
if (<!USELESS_IS_CHECK!>a is C<!>) {
|
||||||
|
val t =
|
||||||
|
when (<!DEBUG_INFO_SMARTCAST!>a<!>) {
|
||||||
|
is DD -> "DD"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,28 @@
|
|||||||
|
// !DIAGNOSTICS: -UNUSED_VARIABLE
|
||||||
|
// SKIP_TXT
|
||||||
|
|
||||||
|
sealed class A
|
||||||
|
sealed class B : A()
|
||||||
|
sealed class C : A()
|
||||||
|
object BB : B()
|
||||||
|
object CC : C()
|
||||||
|
|
||||||
|
fun foo(a: A) {
|
||||||
|
if (a is B) {
|
||||||
|
if (a is C) {
|
||||||
|
val t = when (<!DEBUG_INFO_SMARTCAST!>a<!>) {
|
||||||
|
is CC -> "CC"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fun foo2(a: A) {
|
||||||
|
if (a is C) {
|
||||||
|
if (a is B) {
|
||||||
|
val t = <!NO_ELSE_IN_WHEN!>when<!> (<!DEBUG_INFO_SMARTCAST!>a<!>) {
|
||||||
|
is CC -> "CC"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -19117,6 +19117,21 @@ public class DiagnosticsTestGenerated extends AbstractDiagnosticsTest {
|
|||||||
runTest("compiler/testData/diagnostics/tests/smartCasts/kt2422.kt");
|
runTest("compiler/testData/diagnostics/tests/smartCasts/kt2422.kt");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@TestMetadata("kt27221.kt")
|
||||||
|
public void testKt27221() throws Exception {
|
||||||
|
runTest("compiler/testData/diagnostics/tests/smartCasts/kt27221.kt");
|
||||||
|
}
|
||||||
|
|
||||||
|
@TestMetadata("kt27221_2.kt")
|
||||||
|
public void testKt27221_2() throws Exception {
|
||||||
|
runTest("compiler/testData/diagnostics/tests/smartCasts/kt27221_2.kt");
|
||||||
|
}
|
||||||
|
|
||||||
|
@TestMetadata("kt27221_irrelevantClasses.kt")
|
||||||
|
public void testKt27221_irrelevantClasses() throws Exception {
|
||||||
|
runTest("compiler/testData/diagnostics/tests/smartCasts/kt27221_irrelevantClasses.kt");
|
||||||
|
}
|
||||||
|
|
||||||
@TestMetadata("kt2865.kt")
|
@TestMetadata("kt2865.kt")
|
||||||
public void testKt2865() throws Exception {
|
public void testKt2865() throws Exception {
|
||||||
runTest("compiler/testData/diagnostics/tests/smartCasts/kt2865.kt");
|
runTest("compiler/testData/diagnostics/tests/smartCasts/kt2865.kt");
|
||||||
|
|||||||
Generated
+15
@@ -19117,6 +19117,21 @@ public class DiagnosticsUsingJavacTestGenerated extends AbstractDiagnosticsUsing
|
|||||||
runTest("compiler/testData/diagnostics/tests/smartCasts/kt2422.kt");
|
runTest("compiler/testData/diagnostics/tests/smartCasts/kt2422.kt");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@TestMetadata("kt27221.kt")
|
||||||
|
public void testKt27221() throws Exception {
|
||||||
|
runTest("compiler/testData/diagnostics/tests/smartCasts/kt27221.kt");
|
||||||
|
}
|
||||||
|
|
||||||
|
@TestMetadata("kt27221_2.kt")
|
||||||
|
public void testKt27221_2() throws Exception {
|
||||||
|
runTest("compiler/testData/diagnostics/tests/smartCasts/kt27221_2.kt");
|
||||||
|
}
|
||||||
|
|
||||||
|
@TestMetadata("kt27221_irrelevantClasses.kt")
|
||||||
|
public void testKt27221_irrelevantClasses() throws Exception {
|
||||||
|
runTest("compiler/testData/diagnostics/tests/smartCasts/kt27221_irrelevantClasses.kt");
|
||||||
|
}
|
||||||
|
|
||||||
@TestMetadata("kt2865.kt")
|
@TestMetadata("kt2865.kt")
|
||||||
public void testKt2865() throws Exception {
|
public void testKt2865() throws Exception {
|
||||||
runTest("compiler/testData/diagnostics/tests/smartCasts/kt2865.kt");
|
runTest("compiler/testData/diagnostics/tests/smartCasts/kt2865.kt");
|
||||||
|
|||||||
Reference in New Issue
Block a user