Cast never succeeds: special 'Nothing' / 'Nothing?' case

(cherry picked from commit 63435b4)
This commit is contained in:
Mikhail Glukhikh
2016-07-26 15:50:03 +03:00
committed by Mikhail Glukhikh
parent bf4231d65a
commit b56e84d47f
5 changed files with 29 additions and 2 deletions
@@ -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):
+14
View File
@@ -0,0 +1,14 @@
// Nothing can be cast to Nothing
fun foo(x: String) {
x <!CAST_NEVER_SUCCEEDS!>as<!> Nothing
}
fun gav(y: String?) {
y <!CAST_NEVER_SUCCEEDS!>as<!> Nothing
}
// Only nullable can be cast to Nothing?
fun bar(x: String, y: String?) {
x <!CAST_NEVER_SUCCEEDS!>as<!> Nothing?
y as Nothing?
}
@@ -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
@@ -9,8 +9,8 @@ fun box() {
val b = <!UNSUPPORTED!>Array<!><<!REIFIED_TYPE_FORBIDDEN_SUBSTITUTION!>Nothing?<!>>(5) { null!! }
val c = <!REIFIED_TYPE_FORBIDDEN_SUBSTITUTION!>foo<!>() { null!! }
val d = foo<Any> { null!! }
val e = <!REIFIED_TYPE_FORBIDDEN_SUBSTITUTION!>foo<!> { "1" as Nothing }
val e1 = <!REIFIED_TYPE_FORBIDDEN_SUBSTITUTION!>foo<!> { "1" as Nothing? }
val e = <!REIFIED_TYPE_FORBIDDEN_SUBSTITUTION!>foo<!> { "1" <!CAST_NEVER_SUCCEEDS!>as<!> Nothing }
val e1 = <!REIFIED_TYPE_FORBIDDEN_SUBSTITUTION!>foo<!> { "1" <!CAST_NEVER_SUCCEEDS!>as<!> Nothing? }
val f = javaClass<<!REIFIED_TYPE_FORBIDDEN_SUBSTITUTION!>Nothing<!>>()
}
@@ -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");