Fix codegen box tests for language version 1.4

Since API version 1.4, NullPointerException is thrown for casts of null
to any type instead of TypeCastException.
This commit is contained in:
Alexander Udalov
2020-01-20 16:55:53 +01:00
parent 6a90dc2efe
commit f48bdc1fcb
17 changed files with 115 additions and 51 deletions
@@ -12,15 +12,15 @@ fun case1(): Int =
null.castTo<Int?, Int>()
fun box(): String {
failTypeCast { case1(); return "failTypeCast 9" }
failNPE { case1(); return "Fail" }
return "OK"
}
inline fun failTypeCast(s: () -> Unit) {
inline fun failNPE(s: () -> Unit) {
try {
s()
}
catch (e: TypeCastException) {
catch (e: NullPointerException) {
// OK
}
}
@@ -1,5 +1,4 @@
// FILE: 1.kt
// WITH_RUNTIME
package test
class A
@@ -12,34 +11,34 @@ inline fun <reified T> Any?.foo(): T = this as T
import test.*
fun box(): String {
failTypeCast { null.foo<Any>(); return "failTypeCast 1" }
if (null.foo<Any?>() != null) return "failTypeCast 2"
failNPE { null.foo<Any>(); return "Fail 1" }
if (null.foo<Any?>() != null) return "Fail 2"
failTypeCast { null.foo<A>(); return "failTypeCast 3" }
if (null.foo<A?>() != null) return "failTypeCast 4"
failNPE { null.foo<A>(); return "Fail 3" }
if (null.foo<A?>() != null) return "Fail 4"
val a = A()
if (a.foo<Any>() != a) return "failTypeCast 5"
if (a.foo<Any?>() != a) return "failTypeCast 6"
if (a.foo<Any>() != a) return "Fail 5"
if (a.foo<Any?>() != a) return "Fail 6"
if (a.foo<A>() != a) return "failTypeCast 7"
if (a.foo<A?>() != a) return "failTypeCast 8"
if (a.foo<A>() != a) return "Fail 7"
if (a.foo<A?>() != a) return "Fail 8"
val b = B()
failClassCast { b.foo<A>(); return "failTypeCast 9" }
failClassCast { b.foo<A?>(); return "failTypeCast 10" }
failClassCast { b.foo<A>(); return "Fail 9" }
failClassCast { b.foo<A?>(); return "Fail 10" }
return "OK"
}
inline fun failTypeCast(s: () -> Unit) {
inline fun failNPE(s: () -> Unit) {
try {
s()
}
catch (e: TypeCastException) {
catch (e: NullPointerException) {
// OK
}
}
@@ -51,6 +50,6 @@ inline fun failClassCast(s: () -> Unit) {
throw e
}
catch (e: ClassCastException) {
// OK
}
}
@@ -0,0 +1,57 @@
// !API_VERSION: 1.3
// FILE: 1.kt
// WITH_RUNTIME
package test
class A
class B
inline fun <reified T> Any?.foo(): T = this as T
// FILE: 2.kt
import test.*
fun box(): String {
failTypeCast { null.foo<Any>(); return "Fail 1" }
if (null.foo<Any?>() != null) return "Fail 2"
failTypeCast { null.foo<A>(); return "Fail 3" }
if (null.foo<A?>() != null) return "Fail 4"
val a = A()
if (a.foo<Any>() != a) return "Fail 5"
if (a.foo<Any?>() != a) return "Fail 6"
if (a.foo<A>() != a) return "Fail 7"
if (a.foo<A?>() != a) return "Fail 8"
val b = B()
failClassCast { b.foo<A>(); return "Fail 9" }
failClassCast { b.foo<A?>(); return "Fail 10" }
return "OK"
}
inline fun failTypeCast(s: () -> Unit) {
try {
s()
}
catch (e: TypeCastException) {
// OK
}
}
inline fun failClassCast(s: () -> Unit) {
try {
s()
}
catch (e: TypeCastException) {
throw e
}
catch (e: ClassCastException) {
// OK
}
}