Cast never succeeds: special 'Nothing' / 'Nothing?' case
(cherry picked from commit 63435b4)
This commit is contained in:
committed by
Mikhail Glukhikh
parent
bf4231d65a
commit
b56e84d47f
@@ -38,6 +38,8 @@ object CastDiagnosticsUtil {
|
|||||||
rhsType: KotlinType,
|
rhsType: KotlinType,
|
||||||
platformToKotlinClassMap: PlatformToKotlinClassMap): Boolean {
|
platformToKotlinClassMap: PlatformToKotlinClassMap): Boolean {
|
||||||
if (KotlinBuiltIns.isNullableNothing(lhsType) && !TypeUtils.isNullableType(rhsType)) return false
|
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 (lhsType.isError) return true
|
||||||
if (isRelated(lhsType, rhsType, platformToKotlinClassMap)) return true
|
if (isRelated(lhsType, rhsType, platformToKotlinClassMap)) return true
|
||||||
// This is an oversimplification (which does not render the method incomplete):
|
// This is an oversimplification (which does not render the method incomplete):
|
||||||
|
|||||||
@@ -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
|
||||||
+2
-2
@@ -9,8 +9,8 @@ fun box() {
|
|||||||
val b = <!UNSUPPORTED!>Array<!><<!REIFIED_TYPE_FORBIDDEN_SUBSTITUTION!>Nothing?<!>>(5) { null!! }
|
val b = <!UNSUPPORTED!>Array<!><<!REIFIED_TYPE_FORBIDDEN_SUBSTITUTION!>Nothing?<!>>(5) { null!! }
|
||||||
val c = <!REIFIED_TYPE_FORBIDDEN_SUBSTITUTION!>foo<!>() { null!! }
|
val c = <!REIFIED_TYPE_FORBIDDEN_SUBSTITUTION!>foo<!>() { null!! }
|
||||||
val d = foo<Any> { null!! }
|
val d = foo<Any> { null!! }
|
||||||
val e = <!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" as Nothing? }
|
val e1 = <!REIFIED_TYPE_FORBIDDEN_SUBSTITUTION!>foo<!> { "1" <!CAST_NEVER_SUCCEEDS!>as<!> Nothing? }
|
||||||
|
|
||||||
val f = javaClass<<!REIFIED_TYPE_FORBIDDEN_SUBSTITUTION!>Nothing<!>>()
|
val f = javaClass<<!REIFIED_TYPE_FORBIDDEN_SUBSTITUTION!>Nothing<!>>()
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2421,6 +2421,12 @@ public class DiagnosticsTestGenerated extends AbstractDiagnosticsTest {
|
|||||||
doTest(fileName);
|
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")
|
@TestMetadata("AsWithOtherParameter.kt")
|
||||||
public void testAsWithOtherParameter() throws Exception {
|
public void testAsWithOtherParameter() throws Exception {
|
||||||
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/cast/AsWithOtherParameter.kt");
|
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/cast/AsWithOtherParameter.kt");
|
||||||
|
|||||||
Reference in New Issue
Block a user