From 098520de64f0ffddcaf8237bfd0dd343cc1ceb4d Mon Sep 17 00:00:00 2001 From: Mikhail Glukhikh Date: Tue, 9 Aug 2016 16:41:30 +0300 Subject: [PATCH] CAST_NEVER_SUCCEEDS: do not report when casting nullable to nullable #KT-260 Fixed --- .../src/org/jetbrains/kotlin/types/CastDiagnosticsUtil.kt | 7 +++++-- .../testData/diagnostics/tests/cast/NullableToNullable.kt | 7 +++++++ .../testData/diagnostics/tests/cast/NullableToNullable.txt | 4 ++++ .../kotlin/checkers/DiagnosticsTestGenerated.java | 6 ++++++ 4 files changed, 22 insertions(+), 2 deletions(-) create mode 100644 compiler/testData/diagnostics/tests/cast/NullableToNullable.kt create mode 100644 compiler/testData/diagnostics/tests/cast/NullableToNullable.txt diff --git a/compiler/frontend/src/org/jetbrains/kotlin/types/CastDiagnosticsUtil.kt b/compiler/frontend/src/org/jetbrains/kotlin/types/CastDiagnosticsUtil.kt index cc6cc3402db..64d16cdc682 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/types/CastDiagnosticsUtil.kt +++ b/compiler/frontend/src/org/jetbrains/kotlin/types/CastDiagnosticsUtil.kt @@ -37,9 +37,12 @@ object CastDiagnosticsUtil { lhsType: KotlinType, rhsType: KotlinType, platformToKotlinClassMap: PlatformToKotlinClassMap): Boolean { - if (KotlinBuiltIns.isNullableNothing(lhsType) && !TypeUtils.isNullableType(rhsType)) return false + val rhsNullable = TypeUtils.isNullableType(rhsType) + val lhsNullable = TypeUtils.isNullableType(lhsType) + if (KotlinBuiltIns.isNullableNothing(lhsType) && !rhsNullable) return false if (KotlinBuiltIns.isNothing(rhsType)) return false - if (KotlinBuiltIns.isNullableNothing(rhsType)) return TypeUtils.isNullableType(lhsType) + if (KotlinBuiltIns.isNullableNothing(rhsType)) return lhsNullable + if (lhsNullable && rhsNullable) return true 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/NullableToNullable.kt b/compiler/testData/diagnostics/tests/cast/NullableToNullable.kt new file mode 100644 index 00000000000..67a064bd39f --- /dev/null +++ b/compiler/testData/diagnostics/tests/cast/NullableToNullable.kt @@ -0,0 +1,7 @@ +// From KT-13324: always succeeds +val x = null as String? +// From KT-260: sometimes succeeds +fun foo(a: String?): Int? { + val c = a as? Int? + return c +} \ No newline at end of file diff --git a/compiler/testData/diagnostics/tests/cast/NullableToNullable.txt b/compiler/testData/diagnostics/tests/cast/NullableToNullable.txt new file mode 100644 index 00000000000..91d2e0b0866 --- /dev/null +++ b/compiler/testData/diagnostics/tests/cast/NullableToNullable.txt @@ -0,0 +1,4 @@ +package + +public val x: kotlin.String? = null +public fun foo(/*0*/ a: kotlin.String?): kotlin.Int? diff --git a/compiler/tests/org/jetbrains/kotlin/checkers/DiagnosticsTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/checkers/DiagnosticsTestGenerated.java index 3d8d3ab8eae..d4f1e8bcc95 100644 --- a/compiler/tests/org/jetbrains/kotlin/checkers/DiagnosticsTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/checkers/DiagnosticsTestGenerated.java @@ -2673,6 +2673,12 @@ public class DiagnosticsTestGenerated extends AbstractDiagnosticsTest { doTest(fileName); } + @TestMetadata("NullableToNullable.kt") + public void testNullableToNullable() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/cast/NullableToNullable.kt"); + doTest(fileName); + } + @TestMetadata("WhenErasedDisallowFromAny.kt") public void testWhenErasedDisallowFromAny() throws Exception { String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/cast/WhenErasedDisallowFromAny.kt");