Allow use reference to reified type parameters in contracts since 1.4

This commit is contained in:
Dmitriy Novozhilov
2019-12-26 13:08:32 +03:00
parent 9c1b68f839
commit 5dfe100ae5
32 changed files with 232 additions and 47 deletions
@@ -0,0 +1,31 @@
// !LANGUAGE: +AllowContractsForCustomFunctions +UseReturnsEffect +AllowReifiedGenericsInContracts
// !USE_EXPERIMENTAL: kotlin.contracts.ExperimentalContracts
// !DIAGNOSTICS: -INVISIBLE_REFERENCE -INVISIBLE_MEMBER -UNUSED_VARIABLE
import kotlin.contracts.*
inline fun <reified T> requireIsInstance(value: Any?) {
contract {
returns() implies (value is T)
}
if (value !is T) {
throw IllegalArgumentException()
}
}
inline fun <reified T> cast(value: Any?): T {
contract {
returns() implies (value is T)
}
return value as T
}
fun test_1(x: Any) {
requireIsInstance<String>(x)
<!DEBUG_INFO_SMARTCAST!>x<!>.length
}
fun test_2(x: Any) {
val s: String = cast(x)
<!DEBUG_INFO_SMARTCAST!>x<!>.length
}