JS/RTTI: support unsafe casts
#KT-2670 fixed
This commit is contained in:
committed by
Alexey Andreev
parent
1d2da9729e
commit
3fd387d4a6
@@ -0,0 +1,16 @@
|
||||
package kotlin
|
||||
|
||||
// CHECK_NOT_REFERENCED: Kotlin.isInstanceOf
|
||||
// CHECK_NOT_REFERENCED: Kotlin.isTypeOf
|
||||
// CHECK_NOT_REFERENCED: Kotlin.orNull
|
||||
|
||||
fun failsClassCast(message: String, fn: ()->Unit) {
|
||||
try {
|
||||
fn()
|
||||
}
|
||||
catch (e: ClassCastException) {
|
||||
return
|
||||
}
|
||||
|
||||
throw Exception("Expected ClassCastException to be thrown: message=$message")
|
||||
}
|
||||
@@ -43,17 +43,6 @@ fun test(f: () -> Unit) {
|
||||
}
|
||||
}
|
||||
|
||||
fun fails(f: () -> Unit) {
|
||||
try {
|
||||
f()
|
||||
}
|
||||
catch(e: Exception) {
|
||||
return
|
||||
}
|
||||
|
||||
throw Exception("Expected an exception to be thrown from $f")
|
||||
}
|
||||
|
||||
fun box(): String {
|
||||
val a = A("OK")
|
||||
|
||||
@@ -69,6 +58,7 @@ fun box(): String {
|
||||
|
||||
test { castNullableToNotNullT<A>(a) }
|
||||
fails { castNullableToNotNullT<A>(null) }
|
||||
failsClassCast("castNullableToNotNullT<A>(null)") { castNullableToNotNullT<A>(null) }
|
||||
|
||||
return "OK"
|
||||
}
|
||||
|
||||
@@ -0,0 +1,18 @@
|
||||
package foo
|
||||
|
||||
trait A
|
||||
|
||||
class AImpl: A
|
||||
|
||||
fun test(x: Any?): A = x as A
|
||||
|
||||
fun box(): String {
|
||||
var a: A = AImpl()
|
||||
assertEquals(a, test(a), "a = AImpl()")
|
||||
a = object : A {}
|
||||
assertEquals(a, test(a), "a = object : A{}")
|
||||
failsClassCast("test(null)") { test(null) }
|
||||
failsClassCast("test(object{})") { test(object{}) }
|
||||
|
||||
return "OK"
|
||||
}
|
||||
@@ -1,26 +0,0 @@
|
||||
package foo
|
||||
|
||||
class A
|
||||
|
||||
fun checkCastNullableToNotNull(): Boolean {
|
||||
val a = null
|
||||
try {
|
||||
val s = a as A
|
||||
}
|
||||
catch (e: Exception) {
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
fun checkCastNotNullToNotNull(): Boolean {
|
||||
val a = A()
|
||||
var s = a as A
|
||||
return s == a
|
||||
}
|
||||
|
||||
fun box(): String {
|
||||
if (!checkCastNullableToNotNull()) return "Failed when try cast Nullable to NotNull"
|
||||
if (!checkCastNotNullToNotNull()) return "Failed when try cast NotNull to NotNull"
|
||||
return "OK"
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
package foo
|
||||
|
||||
interface A
|
||||
|
||||
class AImpl : A {}
|
||||
|
||||
fun test(x: Any?): A? = x as A?
|
||||
|
||||
fun box(): String {
|
||||
var a: A? = AImpl()
|
||||
assertEquals(a, test(a), "a = AImpl()")
|
||||
a = object : A {}
|
||||
assertEquals(a, test(a), "a = object : A{}")
|
||||
assertEquals(null, test(null), "test(null)")
|
||||
failsClassCast("test(object{})") { test(object{}) }
|
||||
|
||||
return "OK"
|
||||
}
|
||||
@@ -1,21 +0,0 @@
|
||||
package foo
|
||||
|
||||
class A
|
||||
|
||||
fun <T> Any.cast() = this!! as T
|
||||
|
||||
fun box(): String {
|
||||
val a = null
|
||||
val s = a as A?
|
||||
if (s != null) return "Failed when try cast Nullable with null value to Nullable"
|
||||
|
||||
val b: A? = A()
|
||||
val n = b as A?
|
||||
if (n != b) return "Failed when try cast Nullable with not null value to Nullable"
|
||||
|
||||
val c = A()
|
||||
val m = c as A?
|
||||
if (m != c) return "Failed when try cast NotNull to Nullable"
|
||||
|
||||
return "OK"
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
package foo
|
||||
|
||||
// CHECK_NOT_CALLED: test
|
||||
|
||||
trait A
|
||||
|
||||
class AImpl: A
|
||||
|
||||
inline
|
||||
fun test<reified T>(x: Any?): T = x as T
|
||||
|
||||
fun box(): String {
|
||||
var a: A = AImpl()
|
||||
assertEquals(a, test<A>(a), "a = AImpl()")
|
||||
a = object : A {}
|
||||
assertEquals(a, test<A>(a), "a = object : A{}")
|
||||
failsClassCast("test(null)") { test<A>(null) }
|
||||
failsClassCast("test(object{})") { test<A>(object{}) }
|
||||
|
||||
return "OK"
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
package foo
|
||||
|
||||
interface A
|
||||
|
||||
class AImpl : A {}
|
||||
|
||||
inline
|
||||
fun test<reified T>(x: Any?): T = x as T
|
||||
|
||||
fun box(): String {
|
||||
var a: A? = AImpl()
|
||||
assertEquals(a, test<A?>(a), "a = AImpl()")
|
||||
a = object : A {}
|
||||
assertEquals(a, test<A?>(a), "a = object : A{}")
|
||||
assertEquals(null, test<A?>(null), "test(null)")
|
||||
failsClassCast("test(object{})") { test<A?>(object{}) }
|
||||
|
||||
return "OK"
|
||||
}
|
||||
Reference in New Issue
Block a user