diff --git a/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirOldFrontendDiagnosticsTestGenerated.java b/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirOldFrontendDiagnosticsTestGenerated.java index 7ae3f1bf26a..5ae092dd2d3 100644 --- a/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirOldFrontendDiagnosticsTestGenerated.java +++ b/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirOldFrontendDiagnosticsTestGenerated.java @@ -3837,6 +3837,12 @@ public class FirOldFrontendDiagnosticsTestGenerated extends AbstractFirDiagnosti runTest("compiler/testData/diagnostics/tests/cast/kt15161.kt"); } + @Test + @TestMetadata("kt47685.kt") + public void testKt47685() throws Exception { + runTest("compiler/testData/diagnostics/tests/cast/kt47685.kt"); + } + @Test @TestMetadata("kt614.kt") public void testKt614() throws Exception { diff --git a/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirOldFrontendDiagnosticsWithLightTreeTestGenerated.java b/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirOldFrontendDiagnosticsWithLightTreeTestGenerated.java index f1fb0b0d323..4f638967937 100644 --- a/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirOldFrontendDiagnosticsWithLightTreeTestGenerated.java +++ b/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirOldFrontendDiagnosticsWithLightTreeTestGenerated.java @@ -3837,6 +3837,12 @@ public class FirOldFrontendDiagnosticsWithLightTreeTestGenerated extends Abstrac runTest("compiler/testData/diagnostics/tests/cast/kt15161.kt"); } + @Test + @TestMetadata("kt47685.kt") + public void testKt47685() throws Exception { + runTest("compiler/testData/diagnostics/tests/cast/kt47685.kt"); + } + @Test @TestMetadata("kt614.kt") public void testKt614() throws Exception { diff --git a/compiler/frontend/src/org/jetbrains/kotlin/types/CastDiagnosticsUtil.kt b/compiler/frontend/src/org/jetbrains/kotlin/types/CastDiagnosticsUtil.kt index 6d7746aa4b3..c2926616969 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/types/CastDiagnosticsUtil.kt +++ b/compiler/frontend/src/org/jetbrains/kotlin/types/CastDiagnosticsUtil.kt @@ -43,6 +43,10 @@ object CastDiagnosticsUtil { rhsType: KotlinType, platformToKotlinClassMapper: PlatformToKotlinClassMapper ): Boolean { + val typeConstructor = lhsType.constructor + if (typeConstructor is IntersectionTypeConstructor) { + return typeConstructor.supertypes.any { isCastPossible(it, rhsType, platformToKotlinClassMapper) } + } val rhsNullable = TypeUtils.isNullableType(rhsType) val lhsNullable = TypeUtils.isNullableType(lhsType) if (KotlinBuiltIns.isNothing(lhsType)) return true diff --git a/compiler/testData/diagnostics/tests/cast/kt47685.fir.kt b/compiler/testData/diagnostics/tests/cast/kt47685.fir.kt new file mode 100644 index 00000000000..370bc19406e --- /dev/null +++ b/compiler/testData/diagnostics/tests/cast/kt47685.fir.kt @@ -0,0 +1,29 @@ +// KT-47685 +interface KtFunction { + fun foo() {} +} + +abstract class ASTDelegatePsiElement { + fun bar() {} +} + +class KtNamedFunction : ASTDelegatePsiElement(), KtFunction { + fun baz() {} +} +class KtFunctionLiteral : ASTDelegatePsiElement(), KtFunction + +fun test_1(namedFunction: KtNamedFunction, functionLiteral: KtFunctionLiteral, cond: Boolean) { + val function = when (cond) { + true -> namedFunction + false -> functionLiteral + } + // approve that foo has type (ASTDelegatePsiElement & KtFunction) + function.foo() + function.bar() + + if (function is KtNamedFunction) { + function.baz() + } + + val myNamedFunction = function as KtNamedFunction +} diff --git a/compiler/testData/diagnostics/tests/cast/kt47685.kt b/compiler/testData/diagnostics/tests/cast/kt47685.kt new file mode 100644 index 00000000000..33634453727 --- /dev/null +++ b/compiler/testData/diagnostics/tests/cast/kt47685.kt @@ -0,0 +1,29 @@ +// KT-47685 +interface KtFunction { + fun foo() {} +} + +abstract class ASTDelegatePsiElement { + fun bar() {} +} + +class KtNamedFunction : ASTDelegatePsiElement(), KtFunction { + fun baz() {} +} +class KtFunctionLiteral : ASTDelegatePsiElement(), KtFunction + +fun test_1(namedFunction: KtNamedFunction, functionLiteral: KtFunctionLiteral, cond: Boolean) { + val function = when (cond) { + true -> namedFunction + false -> functionLiteral + } + // approve that foo has type (ASTDelegatePsiElement & KtFunction) + function.foo() + function.bar() + + if (function is KtNamedFunction) { + function.baz() + } + + val myNamedFunction = function as KtNamedFunction +} diff --git a/compiler/testData/diagnostics/tests/cast/kt47685.txt b/compiler/testData/diagnostics/tests/cast/kt47685.txt new file mode 100644 index 00000000000..710fe6c0d80 --- /dev/null +++ b/compiler/testData/diagnostics/tests/cast/kt47685.txt @@ -0,0 +1,37 @@ +package + +public fun test_1(/*0*/ namedFunction: KtNamedFunction, /*1*/ functionLiteral: KtFunctionLiteral, /*2*/ cond: kotlin.Boolean): kotlin.Unit + +public abstract class ASTDelegatePsiElement { + public constructor ASTDelegatePsiElement() + public final fun bar(): kotlin.Unit + public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean + public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int + public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String +} + +public interface KtFunction { + public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean + public open fun foo(): kotlin.Unit + public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int + public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String +} + +public final class KtFunctionLiteral : ASTDelegatePsiElement, KtFunction { + public constructor KtFunctionLiteral() + public final override /*1*/ /*fake_override*/ fun bar(): kotlin.Unit + public open override /*2*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean + public open override /*1*/ /*fake_override*/ fun foo(): kotlin.Unit + public open override /*2*/ /*fake_override*/ fun hashCode(): kotlin.Int + public open override /*2*/ /*fake_override*/ fun toString(): kotlin.String +} + +public final class KtNamedFunction : ASTDelegatePsiElement, KtFunction { + public constructor KtNamedFunction() + public final override /*1*/ /*fake_override*/ fun bar(): kotlin.Unit + public final fun baz(): kotlin.Unit + public open override /*2*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean + public open override /*1*/ /*fake_override*/ fun foo(): kotlin.Unit + public open override /*2*/ /*fake_override*/ fun hashCode(): kotlin.Int + public open override /*2*/ /*fake_override*/ fun toString(): kotlin.String +} diff --git a/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/DiagnosticTestGenerated.java b/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/DiagnosticTestGenerated.java index b51151d8835..fa7d16ded93 100644 --- a/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/DiagnosticTestGenerated.java +++ b/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/DiagnosticTestGenerated.java @@ -3843,6 +3843,12 @@ public class DiagnosticTestGenerated extends AbstractDiagnosticTest { runTest("compiler/testData/diagnostics/tests/cast/kt15161.kt"); } + @Test + @TestMetadata("kt47685.kt") + public void testKt47685() throws Exception { + runTest("compiler/testData/diagnostics/tests/cast/kt47685.kt"); + } + @Test @TestMetadata("kt614.kt") public void testKt614() throws Exception { diff --git a/idea/idea-frontend-fir/idea-fir-low-level-api/tests/org/jetbrains/kotlin/idea/fir/low/level/api/diagnostic/compiler/based/DiagnosisCompilerTestFE10TestdataTestGenerated.java b/idea/idea-frontend-fir/idea-fir-low-level-api/tests/org/jetbrains/kotlin/idea/fir/low/level/api/diagnostic/compiler/based/DiagnosisCompilerTestFE10TestdataTestGenerated.java index 4cfec42c36b..1c0718a070c 100644 --- a/idea/idea-frontend-fir/idea-fir-low-level-api/tests/org/jetbrains/kotlin/idea/fir/low/level/api/diagnostic/compiler/based/DiagnosisCompilerTestFE10TestdataTestGenerated.java +++ b/idea/idea-frontend-fir/idea-fir-low-level-api/tests/org/jetbrains/kotlin/idea/fir/low/level/api/diagnostic/compiler/based/DiagnosisCompilerTestFE10TestdataTestGenerated.java @@ -3837,6 +3837,12 @@ public class DiagnosisCompilerTestFE10TestdataTestGenerated extends AbstractDiag runTest("compiler/testData/diagnostics/tests/cast/kt15161.kt"); } + @Test + @TestMetadata("kt47685.kt") + public void testKt47685() throws Exception { + runTest("compiler/testData/diagnostics/tests/cast/kt47685.kt"); + } + @Test @TestMetadata("kt614.kt") public void testKt614() throws Exception {