NI: Fix type mismatch caused by smartcast

^KT-25434 Fixed
This commit is contained in:
Denis Zharkov
2019-12-25 16:41:17 +03:00
parent ae4397fbfb
commit 7898922066
6 changed files with 102 additions and 3 deletions
@@ -10023,6 +10023,11 @@ public class FirOldFrontendDiagnosticsTestGenerated extends AbstractFirOldFronte
runTest("compiler/testData/diagnostics/tests/inference/starApproximationFlexible.kt");
}
@TestMetadata("tooEagerSmartcast.kt")
public void testTooEagerSmartcast() throws Exception {
runTest("compiler/testData/diagnostics/tests/inference/tooEagerSmartcast.kt");
}
@TestMetadata("tryNumberLowerBoundsBeforeUpperBounds.kt")
public void testTryNumberLowerBoundsBeforeUpperBounds() throws Exception {
runTest("compiler/testData/diagnostics/tests/inference/tryNumberLowerBoundsBeforeUpperBounds.kt");
@@ -0,0 +1,42 @@
// SKIP_TXT
// !DIAGNOSTICS: -UNUSED_VARIABLE
// !LANGUAGE: +NewInference
interface OutBase<out E>
interface OutDerived<out F> : OutBase<F>
fun <X> OutBase<X>.myLast(): X = TODO()
fun <T> foo(x: OutBase<T>) {
if (x is OutDerived<*>) {
val l: T = x.myLast() // required T, found Cap(*). Only in NI
}
}
interface InvBase<E>
interface InvDerived<F> : InvBase<F>
fun <X> InvBase<X>.myLastInv(): X = TODO()
fun <T> fooInv(x: InvBase<T>) {
if (x is InvDerived<*>) {
val l: T = x.<!INAPPLICABLE_CANDIDATE!>myLastInv<!>() // required T, found Cap(*). Only in NI
}
}
interface Base<E>
fun <Q> Base<Q>.foo(): Q = TODO()
fun <Q : CharSequence> Base<Q>.baz(): Q = TODO()
interface Derived<F : CharSequence> : Base<F>
fun Number.num() {}
fun main(b: Base<out Number>) {
b.foo().num()
if (b is Derived<*>) {
b.<!INAPPLICABLE_CANDIDATE!>foo<!>().num()
b.<!INAPPLICABLE_CANDIDATE!>baz<!>().<!UNRESOLVED_REFERENCE!>length<!>
}
}
@@ -0,0 +1,42 @@
// SKIP_TXT
// !DIAGNOSTICS: -UNUSED_VARIABLE
// !LANGUAGE: +NewInference
interface OutBase<out E>
interface OutDerived<out F> : OutBase<F>
fun <X> OutBase<X>.myLast(): X = TODO()
fun <T> foo(x: OutBase<T>) {
if (x is OutDerived<*>) {
val l: T = x.myLast() // required T, found Cap(*). Only in NI
}
}
interface InvBase<E>
interface InvDerived<F> : InvBase<F>
fun <X> InvBase<X>.myLastInv(): X = TODO()
fun <T> fooInv(x: InvBase<T>) {
if (x is InvDerived<*>) {
val l: T = x.myLastInv() // required T, found Cap(*). Only in NI
}
}
interface Base<E>
fun <Q> Base<Q>.foo(): Q = TODO()
fun <Q : CharSequence> Base<Q>.baz(): Q = TODO()
interface Derived<F : CharSequence> : Base<F>
fun Number.num() {}
fun main(b: Base<out Number>) {
b.foo().num()
if (b is Derived<*>) {
b.foo().num()
b.baz().length
}
}
@@ -10030,6 +10030,11 @@ public class DiagnosticsTestGenerated extends AbstractDiagnosticsTest {
runTest("compiler/testData/diagnostics/tests/inference/starApproximationFlexible.kt");
}
@TestMetadata("tooEagerSmartcast.kt")
public void testTooEagerSmartcast() throws Exception {
runTest("compiler/testData/diagnostics/tests/inference/tooEagerSmartcast.kt");
}
@TestMetadata("tryNumberLowerBoundsBeforeUpperBounds.kt")
public void testTryNumberLowerBoundsBeforeUpperBounds() throws Exception {
runTest("compiler/testData/diagnostics/tests/inference/tryNumberLowerBoundsBeforeUpperBounds.kt");
@@ -10025,6 +10025,11 @@ public class DiagnosticsUsingJavacTestGenerated extends AbstractDiagnosticsUsing
runTest("compiler/testData/diagnostics/tests/inference/starApproximationFlexible.kt");
}
@TestMetadata("tooEagerSmartcast.kt")
public void testTooEagerSmartcast() throws Exception {
runTest("compiler/testData/diagnostics/tests/inference/tooEagerSmartcast.kt");
}
@TestMetadata("tryNumberLowerBoundsBeforeUpperBounds.kt")
public void testTryNumberLowerBoundsBeforeUpperBounds() throws Exception {
runTest("compiler/testData/diagnostics/tests/inference/tryNumberLowerBoundsBeforeUpperBounds.kt");
@@ -269,8 +269,6 @@ object AbstractTypeChecker {
1 -> return isSubtypeForSameConstructor(supertypesWithSameConstructor.first().asArgumentList(), superType)
else -> { // at least 2 supertypes with same constructors. Such case is rare
if (supertypesWithSameConstructor.any { isSubtypeForSameConstructor(it.asArgumentList(), superType) }) return true
val newArguments = ArgumentList(superConstructor.parametersCount())
for (index in 0 until superConstructor.parametersCount()) {
val allProjections = supertypesWithSameConstructor.map {
@@ -283,7 +281,9 @@ object AbstractTypeChecker {
newArguments.add(intersection)
}
return isSubtypeForSameConstructor(newArguments, superType)
if (isSubtypeForSameConstructor(newArguments, superType)) return true
return supertypesWithSameConstructor.any { isSubtypeForSameConstructor(it.asArgumentList(), superType) }
}
}
}