Fix CAST_NEVER_SUCCEEDS when casting Nothing to some type
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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() <!USELESS_CAST!>as Any<!>
|
||||
TODO() <!USELESS_CAST!>as Any?<!>
|
||||
TODO() <!USELESS_CAST!>as OpenClass<!>
|
||||
TODO() <!USELESS_CAST!>as FinalClass<!>
|
||||
TODO() <!USELESS_CAST!>as AbstractClass<!>
|
||||
TODO() <!USELESS_CAST!>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() <!USELESS_CAST!>as Any<!>
|
||||
fun b() = TODO() <!USELESS_CAST!>as Any?<!>
|
||||
fun c() = TODO() <!USELESS_CAST!>as OpenClass<!>
|
||||
fun d() = TODO() <!USELESS_CAST!>as FinalClass<!>
|
||||
fun e() = TODO() <!USELESS_CAST!>as AbstractClass<!>
|
||||
fun f() = TODO() <!USELESS_CAST!>as Interface<!>
|
||||
@@ -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
|
||||
}
|
||||
@@ -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");
|
||||
|
||||
+6
@@ -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");
|
||||
|
||||
Reference in New Issue
Block a user