diff --git a/compiler/frontend/src/org/jetbrains/kotlin/types/CastDiagnosticsUtil.kt b/compiler/frontend/src/org/jetbrains/kotlin/types/CastDiagnosticsUtil.kt index b75ff7ad55a..faa4e85e65c 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/types/CastDiagnosticsUtil.kt +++ b/compiler/frontend/src/org/jetbrains/kotlin/types/CastDiagnosticsUtil.kt @@ -44,6 +44,7 @@ object CastDiagnosticsUtil { ): Boolean { val rhsNullable = TypeUtils.isNullableType(rhsType) val lhsNullable = TypeUtils.isNullableType(lhsType) + if (KotlinBuiltIns.isNothing(lhsType)) return true if (KotlinBuiltIns.isNullableNothing(lhsType) && !rhsNullable) return false if (KotlinBuiltIns.isNothing(rhsType)) return false if (KotlinBuiltIns.isNullableNothing(rhsType)) return lhsNullable diff --git a/compiler/testData/diagnostics/tests/cast/nothingAs.kt b/compiler/testData/diagnostics/tests/cast/nothingAs.kt new file mode 100644 index 00000000000..404a0678045 --- /dev/null +++ b/compiler/testData/diagnostics/tests/cast/nothingAs.kt @@ -0,0 +1,31 @@ +// !DIAGNOSTICS: -UNREACHABLE_CODE + +fun TODO(): Nothing = throw java.lang.IllegalStateException() + +open class OpenClass +class FinalClass : OpenClass() +abstract class AbstractClass +interface Interface + +fun test() { + TODO() as Any + TODO() as Any? + TODO() as OpenClass + TODO() as FinalClass + TODO() as AbstractClass + TODO() as Interface + + val a = TODO() as Any + val b = TODO() as Any? + val c = TODO() as OpenClass + val d = TODO() as FinalClass + val e = TODO() as AbstractClass + val f = TODO() as Interface +} + +fun a() = TODO() as Any +fun b() = TODO() as Any? +fun c() = TODO() as OpenClass +fun d() = TODO() as FinalClass +fun e() = TODO() as AbstractClass +fun f() = TODO() as Interface diff --git a/compiler/testData/diagnostics/tests/cast/nothingAs.txt b/compiler/testData/diagnostics/tests/cast/nothingAs.txt new file mode 100644 index 00000000000..c792b1a3bca --- /dev/null +++ b/compiler/testData/diagnostics/tests/cast/nothingAs.txt @@ -0,0 +1,37 @@ +package + +public fun TODO(): kotlin.Nothing +public fun a(): kotlin.Any +public fun b(): kotlin.Any? +public fun c(): OpenClass +public fun d(): FinalClass +public fun e(): AbstractClass +public fun f(): Interface +public fun test(): kotlin.Unit + +public abstract class AbstractClass { + public constructor AbstractClass() + 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 final class FinalClass : OpenClass { + public constructor FinalClass() + 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 Interface { + 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 open class OpenClass { + public constructor OpenClass() + 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 +} diff --git a/compiler/tests/org/jetbrains/kotlin/checkers/DiagnosticsTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/checkers/DiagnosticsTestGenerated.java index 39fb5ef84ef..1ace858be82 100644 --- a/compiler/tests/org/jetbrains/kotlin/checkers/DiagnosticsTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/checkers/DiagnosticsTestGenerated.java @@ -3058,6 +3058,12 @@ public class DiagnosticsTestGenerated extends AbstractDiagnosticsTest { doTest(fileName); } + @TestMetadata("nothingAs.kt") + public void testNothingAs() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/cast/nothingAs.kt"); + doTest(fileName); + } + @TestMetadata("NullableToNullable.kt") public void testNullableToNullable() throws Exception { String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/cast/NullableToNullable.kt"); diff --git a/compiler/tests/org/jetbrains/kotlin/checkers/javac/DiagnosticsUsingJavacTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/checkers/javac/DiagnosticsUsingJavacTestGenerated.java index d2c8ec6aace..e0eb59408e5 100644 --- a/compiler/tests/org/jetbrains/kotlin/checkers/javac/DiagnosticsUsingJavacTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/checkers/javac/DiagnosticsUsingJavacTestGenerated.java @@ -3058,6 +3058,12 @@ public class DiagnosticsUsingJavacTestGenerated extends AbstractDiagnosticsUsing doTest(fileName); } + @TestMetadata("nothingAs.kt") + public void testNothingAs() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/cast/nothingAs.kt"); + doTest(fileName); + } + @TestMetadata("NullableToNullable.kt") public void testNullableToNullable() throws Exception { String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/cast/NullableToNullable.kt");