diff --git a/compiler/fir/checkers/src/org/jetbrains/kotlin/fir/analysis/checkers/FirCastDiagnosticsHelpers.kt b/compiler/fir/checkers/src/org/jetbrains/kotlin/fir/analysis/checkers/FirCastDiagnosticsHelpers.kt index 84d334d5dc9..6ae1614e146 100644 --- a/compiler/fir/checkers/src/org/jetbrains/kotlin/fir/analysis/checkers/FirCastDiagnosticsHelpers.kt +++ b/compiler/fir/checkers/src/org/jetbrains/kotlin/fir/analysis/checkers/FirCastDiagnosticsHelpers.kt @@ -128,7 +128,7 @@ fun isCastErased(supertype: ConeKotlinType, subtype: ConeKotlinType, context: Ch } // cast between T and T? is always OK - if (supertype.isMarkedNullable || subtype.isMarkedNullable) { + if ((supertype !is ConeErrorType && supertype.isMarkedNullable) || (subtype !is ConeErrorType && subtype.isMarkedNullable)) { return isCastErased( supertype.withNullability(ConeNullability.NOT_NULL, typeContext), subtype.withNullability(ConeNullability.NOT_NULL, typeContext), diff --git a/compiler/testData/diagnostics/tests/cast/bare/ToErrorType.fir.kt b/compiler/testData/diagnostics/tests/cast/bare/ToErrorType.fir.kt index 65f47e1f487..9daf96c0f54 100644 --- a/compiler/testData/diagnostics/tests/cast/bare/ToErrorType.fir.kt +++ b/compiler/testData/diagnostics/tests/cast/bare/ToErrorType.fir.kt @@ -3,4 +3,9 @@ class P fun foo(p: P): Any { val v = p as G return v -} \ No newline at end of file +} + +fun bar(p: P): Any { + val v = p as G? + return v +} diff --git a/compiler/testData/diagnostics/tests/cast/bare/ToErrorType.kt b/compiler/testData/diagnostics/tests/cast/bare/ToErrorType.kt index 84bd38b1368..6e01a0bf309 100644 --- a/compiler/testData/diagnostics/tests/cast/bare/ToErrorType.kt +++ b/compiler/testData/diagnostics/tests/cast/bare/ToErrorType.kt @@ -3,4 +3,9 @@ class P fun foo(p: P): Any { val v = p as G return v -} \ No newline at end of file +} + +fun bar(p: P): Any { + val v = p as G? + return v +} diff --git a/compiler/testData/diagnostics/tests/cast/bare/ToErrorType.txt b/compiler/testData/diagnostics/tests/cast/bare/ToErrorType.txt index a6f23f42a68..0d5fa8fb132 100644 --- a/compiler/testData/diagnostics/tests/cast/bare/ToErrorType.txt +++ b/compiler/testData/diagnostics/tests/cast/bare/ToErrorType.txt @@ -1,5 +1,6 @@ package +public fun bar(/*0*/ p: P): kotlin.Any public fun foo(/*0*/ p: P): kotlin.Any public final class P {