diff --git a/compiler/fir/analysis-tests/testData/resolveWithStdlib/intellij/ParentOfType.fir.txt b/compiler/fir/analysis-tests/testData/resolveWithStdlib/intellij/ParentOfType.fir.txt new file mode 100644 index 00000000000..3e340870c1f --- /dev/null +++ b/compiler/fir/analysis-tests/testData/resolveWithStdlib/intellij/ParentOfType.fir.txt @@ -0,0 +1,6 @@ +FILE: ParentOfType.kt + public final fun R|kotlin/Any|.parentOfTypes(vararg classes: R|kotlin/Array>|): R|T?| { + throw R|java/lang/IllegalStateException.IllegalStateException|() + } + public final val some: R|kotlin/Number?| = String(123).R|/parentOfTypes|)|>(vararg((Q|kotlin/Int|), (Q|kotlin/Double|))) + public get(): R|kotlin/Number?| diff --git a/compiler/fir/analysis-tests/testData/resolveWithStdlib/intellij/ParentOfType.kt b/compiler/fir/analysis-tests/testData/resolveWithStdlib/intellij/ParentOfType.kt new file mode 100644 index 00000000000..44b5084bd0f --- /dev/null +++ b/compiler/fir/analysis-tests/testData/resolveWithStdlib/intellij/ParentOfType.kt @@ -0,0 +1,7 @@ +import kotlin.reflect.KClass + +fun Any.parentOfTypes(vararg classes: KClass): T? { + throw IllegalStateException() +} + +val some = "123".parentOfTypes(Int::class, Double::class) diff --git a/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirDiagnosticTestGenerated.java b/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirDiagnosticTestGenerated.java index 548741234b6..2400e4913d2 100644 --- a/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirDiagnosticTestGenerated.java +++ b/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirDiagnosticTestGenerated.java @@ -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 { diff --git a/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirDiagnosticsWithLightTreeTestGenerated.java b/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirDiagnosticsWithLightTreeTestGenerated.java index b8fe06e5496..d71de277631 100644 --- a/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirDiagnosticsWithLightTreeTestGenerated.java +++ b/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirDiagnosticsWithLightTreeTestGenerated.java @@ -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 { diff --git a/compiler/fir/cones/src/org/jetbrains/kotlin/fir/types/ConeTypes.kt b/compiler/fir/cones/src/org/jetbrains/kotlin/fir/types/ConeTypes.kt index 07c9ac3d639..1e73b610464 100644 --- a/compiler/fir/cones/src/org/jetbrains/kotlin/fir/types/ConeTypes.kt +++ b/compiler/fir/cones/src/org/jetbrains/kotlin/fir/types/ConeTypes.kt @@ -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() { diff --git a/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/types/TypeUtils.kt b/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/types/TypeUtils.kt index 68ae4f236e8..77926f50f67 100644 --- a/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/types/TypeUtils.kt +++ b/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/types/TypeUtils.kt @@ -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) } diff --git a/idea/idea-frontend-fir/idea-fir-low-level-api/tests/org/jetbrains/kotlin/idea/fir/low/level/api/diagnostic/compiler/based/DiagnosisCompilerFirTestdataTestGenerated.java b/idea/idea-frontend-fir/idea-fir-low-level-api/tests/org/jetbrains/kotlin/idea/fir/low/level/api/diagnostic/compiler/based/DiagnosisCompilerFirTestdataTestGenerated.java index f22e79c1ba0..3a756892d05 100644 --- a/idea/idea-frontend-fir/idea-fir-low-level-api/tests/org/jetbrains/kotlin/idea/fir/low/level/api/diagnostic/compiler/based/DiagnosisCompilerFirTestdataTestGenerated.java +++ b/idea/idea-frontend-fir/idea-fir-low-level-api/tests/org/jetbrains/kotlin/idea/fir/low/level/api/diagnostic/compiler/based/DiagnosisCompilerFirTestdataTestGenerated.java @@ -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 {