From b56e84d47fd548d9c4c3436ebb5ed7586a40d299 Mon Sep 17 00:00:00 2001 From: Mikhail Glukhikh Date: Tue, 26 Jul 2016 15:50:03 +0300 Subject: [PATCH] Cast never succeeds: special 'Nothing' / 'Nothing?' case (cherry picked from commit 63435b4) --- .../jetbrains/kotlin/types/CastDiagnosticsUtil.kt | 2 ++ .../testData/diagnostics/tests/cast/AsNothing.kt | 14 ++++++++++++++ .../testData/diagnostics/tests/cast/AsNothing.txt | 5 +++++ .../reified/reifiedNothingSubstitution.kt | 4 ++-- .../kotlin/checkers/DiagnosticsTestGenerated.java | 6 ++++++ 5 files changed, 29 insertions(+), 2 deletions(-) create mode 100644 compiler/testData/diagnostics/tests/cast/AsNothing.kt create mode 100644 compiler/testData/diagnostics/tests/cast/AsNothing.txt diff --git a/compiler/frontend/src/org/jetbrains/kotlin/types/CastDiagnosticsUtil.kt b/compiler/frontend/src/org/jetbrains/kotlin/types/CastDiagnosticsUtil.kt index 74a828c1fcb..cc6cc3402db 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/types/CastDiagnosticsUtil.kt +++ b/compiler/frontend/src/org/jetbrains/kotlin/types/CastDiagnosticsUtil.kt @@ -38,6 +38,8 @@ object CastDiagnosticsUtil { rhsType: KotlinType, platformToKotlinClassMap: PlatformToKotlinClassMap): Boolean { if (KotlinBuiltIns.isNullableNothing(lhsType) && !TypeUtils.isNullableType(rhsType)) return false + if (KotlinBuiltIns.isNothing(rhsType)) return false + if (KotlinBuiltIns.isNullableNothing(rhsType)) return TypeUtils.isNullableType(lhsType) if (lhsType.isError) return true if (isRelated(lhsType, rhsType, platformToKotlinClassMap)) return true // This is an oversimplification (which does not render the method incomplete): diff --git a/compiler/testData/diagnostics/tests/cast/AsNothing.kt b/compiler/testData/diagnostics/tests/cast/AsNothing.kt new file mode 100644 index 00000000000..c6753ad3dcc --- /dev/null +++ b/compiler/testData/diagnostics/tests/cast/AsNothing.kt @@ -0,0 +1,14 @@ +// Nothing can be cast to Nothing +fun foo(x: String) { + x as Nothing +} + +fun gav(y: String?) { + y as Nothing +} + +// Only nullable can be cast to Nothing? +fun bar(x: String, y: String?) { + x as Nothing? + y as Nothing? +} \ No newline at end of file diff --git a/compiler/testData/diagnostics/tests/cast/AsNothing.txt b/compiler/testData/diagnostics/tests/cast/AsNothing.txt new file mode 100644 index 00000000000..581c97c9099 --- /dev/null +++ b/compiler/testData/diagnostics/tests/cast/AsNothing.txt @@ -0,0 +1,5 @@ +package + +public fun bar(/*0*/ x: kotlin.String, /*1*/ y: kotlin.String?): kotlin.Unit +public fun foo(/*0*/ x: kotlin.String): kotlin.Unit +public fun gav(/*0*/ y: kotlin.String?): kotlin.Unit diff --git a/compiler/testData/diagnostics/testsWithStdLib/reified/reifiedNothingSubstitution.kt b/compiler/testData/diagnostics/testsWithStdLib/reified/reifiedNothingSubstitution.kt index 1354c9bb4b6..1ef08806039 100644 --- a/compiler/testData/diagnostics/testsWithStdLib/reified/reifiedNothingSubstitution.kt +++ b/compiler/testData/diagnostics/testsWithStdLib/reified/reifiedNothingSubstitution.kt @@ -9,8 +9,8 @@ fun box() { val b = Array<Nothing?>(5) { null!! } val c = foo() { null!! } val d = foo { null!! } - val e = foo { "1" as Nothing } - val e1 = foo { "1" as Nothing? } + val e = foo { "1" as Nothing } + val e1 = foo { "1" as Nothing? } val f = javaClass<Nothing>() } diff --git a/compiler/tests/org/jetbrains/kotlin/checkers/DiagnosticsTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/checkers/DiagnosticsTestGenerated.java index c1b42767a7f..23cd5aa9031 100644 --- a/compiler/tests/org/jetbrains/kotlin/checkers/DiagnosticsTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/checkers/DiagnosticsTestGenerated.java @@ -2421,6 +2421,12 @@ public class DiagnosticsTestGenerated extends AbstractDiagnosticsTest { doTest(fileName); } + @TestMetadata("AsNothing.kt") + public void testAsNothing() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/cast/AsNothing.kt"); + doTest(fileName); + } + @TestMetadata("AsWithOtherParameter.kt") public void testAsWithOtherParameter() throws Exception { String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/cast/AsWithOtherParameter.kt");