FIR: approximate intersections in public position properly

#KT-48166 Fixed
This commit is contained in:
Mikhail Glukhikh
2021-08-18 10:58:25 +03:00
committed by TeamCityServer
parent 7b0e5927cb
commit 19ab0ab2f8
7 changed files with 38 additions and 4 deletions
@@ -0,0 +1,6 @@
FILE: ParentOfType.kt
public final fun <T : R|kotlin/Number|> R|kotlin/Any|.parentOfTypes(vararg classes: R|kotlin/Array<out kotlin/reflect/KClass<out T>>|): R|T?| {
throw R|java/lang/IllegalStateException.IllegalStateException|()
}
public final val some: R|kotlin/Number?| = String(123).R|/parentOfTypes|<R|it(kotlin/Number & kotlin/Comparable<*>)|>(vararg(<getClass>(Q|kotlin/Int|), <getClass>(Q|kotlin/Double|)))
public get(): R|kotlin/Number?|
@@ -0,0 +1,7 @@
import kotlin.reflect.KClass
fun <T : Number> Any.parentOfTypes(vararg classes: KClass<out T>): T? {
throw IllegalStateException()
}
val some = "123".parentOfTypes(Int::class, Double::class)
@@ -5051,6 +5051,12 @@ public class FirDiagnosticTestGenerated extends AbstractFirDiagnosticTest {
runTest("compiler/fir/analysis-tests/testData/resolveWithStdlib/intellij/IntersectionWithJavaString.kt");
}
@Test
@TestMetadata("ParentOfType.kt")
public void testParentOfType() throws Exception {
runTest("compiler/fir/analysis-tests/testData/resolveWithStdlib/intellij/ParentOfType.kt");
}
@Test
@TestMetadata("PersistentStateComponent.kt")
public void testPersistentStateComponent() throws Exception {
@@ -5051,6 +5051,12 @@ public class FirDiagnosticsWithLightTreeTestGenerated extends AbstractFirDiagnos
runTest("compiler/fir/analysis-tests/testData/resolveWithStdlib/intellij/IntersectionWithJavaString.kt");
}
@Test
@TestMetadata("ParentOfType.kt")
public void testParentOfType() throws Exception {
runTest("compiler/fir/analysis-tests/testData/resolveWithStdlib/intellij/ParentOfType.kt");
}
@Test
@TestMetadata("PersistentStateComponent.kt")
public void testPersistentStateComponent() throws Exception {
@@ -278,7 +278,7 @@ fun ConeIntersectionType.withAlternative(alternativeType: ConeKotlinType): ConeI
}
fun ConeIntersectionType.mapTypes(func: (ConeKotlinType) -> ConeKotlinType): ConeIntersectionType {
return ConeIntersectionType(intersectedTypes.map(func))
return ConeIntersectionType(intersectedTypes.map(func), alternativeType?.let(func))
}
class ConeStubType(val variable: ConeTypeVariable, override val nullability: ConeNullability) : StubTypeMarker, ConeSimpleKotlinType() {
@@ -283,12 +283,15 @@ fun FirTypeRef.withReplacedConeType(
fun FirTypeRef.approximated(
typeApproximator: ConeTypeApproximator,
toSuper: Boolean,
conf: TypeApproximatorConfiguration = TypeApproximatorConfiguration.PublicDeclaration
): FirTypeRef {
val alternativeType = (coneType as? ConeIntersectionType)?.alternativeType ?: coneType
if (alternativeType !== coneType && !alternativeType.requiresApproximationInPublicPosition()) {
return withReplacedConeType(alternativeType)
}
val approximatedType = if (toSuper)
typeApproximator.approximateToSuperType(coneType, conf)
typeApproximator.approximateToSuperType(alternativeType, TypeApproximatorConfiguration.PublicDeclaration)
else
typeApproximator.approximateToSubType(coneType, conf)
typeApproximator.approximateToSubType(alternativeType, TypeApproximatorConfiguration.PublicDeclaration)
return withReplacedConeType(approximatedType)
}
@@ -5051,6 +5051,12 @@ public class DiagnosisCompilerFirTestdataTestGenerated extends AbstractDiagnosis
runTest("compiler/fir/analysis-tests/testData/resolveWithStdlib/intellij/IntersectionWithJavaString.kt");
}
@Test
@TestMetadata("ParentOfType.kt")
public void testParentOfType() throws Exception {
runTest("compiler/fir/analysis-tests/testData/resolveWithStdlib/intellij/ParentOfType.kt");
}
@Test
@TestMetadata("PersistentStateComponent.kt")
public void testPersistentStateComponent() throws Exception {