FirCallCompleter: unwrap definitely not-null type properly

#KT-50180 Fixed
This commit is contained in:
Mikhail Glukhikh
2021-12-10 17:03:44 +03:00
parent febf336d23
commit df50a8141f
6 changed files with 55 additions and 2 deletions
@@ -5555,6 +5555,12 @@ public class DiagnosisCompilerFirTestdataTestGenerated extends AbstractDiagnosis
runTest("compiler/fir/analysis-tests/testData/resolveWithStdlib/problems/DeepCopyIrTree.kt");
}
@Test
@TestMetadata("definitelyNotNullCast.kt")
public void testDefinitelyNotNullCast() throws Exception {
runTest("compiler/fir/analysis-tests/testData/resolveWithStdlib/problems/definitelyNotNullCast.kt");
}
@Test
@TestMetadata("EnumMapGet.kt")
public void testEnumMapGet() throws Exception {
@@ -0,0 +1,14 @@
FILE: test.kt
public open class Value : R|kotlin/Any| {
public constructor(s: R|kotlin/String|): R|Value| {
super<R|kotlin/Any|>()
}
public final val s: R|kotlin/String| = R|<local>/s|
public get(): R|kotlin/String|
}
public final val generator: R|Generator| = R|/Generator.Generator|()
public get(): R|Generator|
public final val y: R|Value| = (R|/generator|.R|/Generator.createValue|<R|ft<Value, Value?>|>(String(Omega)) as R|Value|)
public get(): R|Value|
@@ -0,0 +1,17 @@
// FILE: Generator.java
import org.jetbrains.annotations.NotNull;
public class Generator {
@NotNull
public <T extends Value> T createValue(@NotNull String content) {
return (T) (new Value(content));
}
}
// FILE: test.kt
open class Value(val s: String)
val generator = Generator()
val y = generator.createValue("Omega") <!USELESS_CAST!>as Value<!>
@@ -5555,6 +5555,12 @@ public class FirDiagnosticTestGenerated extends AbstractFirDiagnosticTest {
runTest("compiler/fir/analysis-tests/testData/resolveWithStdlib/problems/DeepCopyIrTree.kt");
}
@Test
@TestMetadata("definitelyNotNullCast.kt")
public void testDefinitelyNotNullCast() throws Exception {
runTest("compiler/fir/analysis-tests/testData/resolveWithStdlib/problems/definitelyNotNullCast.kt");
}
@Test
@TestMetadata("EnumMapGet.kt")
public void testEnumMapGet() throws Exception {
@@ -5555,6 +5555,12 @@ public class FirDiagnosticsWithLightTreeTestGenerated extends AbstractFirDiagnos
runTest("compiler/fir/analysis-tests/testData/resolveWithStdlib/problems/DeepCopyIrTree.kt");
}
@Test
@TestMetadata("definitelyNotNullCast.kt")
public void testDefinitelyNotNullCast() throws Exception {
runTest("compiler/fir/analysis-tests/testData/resolveWithStdlib/problems/definitelyNotNullCast.kt");
}
@Test
@TestMetadata("EnumMapGet.kt")
public void testEnumMapGet() throws Exception {
@@ -345,14 +345,18 @@ internal fun FirFunction.isFunctionForExpectTypeFromCastFeature(): Boolean {
val returnType = returnTypeRef.coneTypeSafe<ConeKotlinType>() ?: return false
if ((returnType.lowerBoundIfFlexible() as? ConeTypeParameterType)?.lookupTag != typeParameter.symbol.toLookupTag()) return false
if ((returnType.unwrap() as? ConeTypeParameterType)?.lookupTag != typeParameter.symbol.toLookupTag()) return false
fun FirTypeRef.isBadType() =
coneTypeSafe<ConeKotlinType>()
?.contains { (it.lowerBoundIfFlexible() as? ConeTypeParameterType)?.lookupTag == typeParameter.symbol.toLookupTag() } != false
?.contains { (it.unwrap() as? ConeTypeParameterType)?.lookupTag == typeParameter.symbol.toLookupTag() } != false
if (valueParameters.any { it.returnTypeRef.isBadType() } || receiverTypeRef?.isBadType() == true) return false
return true
}
private fun ConeKotlinType.unwrap(): ConeSimpleKotlinType = lowerBoundIfFlexible().let {
if (it is ConeDefinitelyNotNullType) it.original.unwrap() else it
}