[Test] Add diagnostic tests for smartcast in K2

List of issues:
KT-28806, KT-7186, KT-22997, KT-1982, KT-22996, KT-54443, KT-37308, KT-37115, KT-4113, KT-25747, KT-24779
This commit is contained in:
Aleksandra.Arsenteva
2023-11-28 16:50:14 +03:00
committed by Space Team
parent 48072c822b
commit c6b32200df
27 changed files with 3802 additions and 0 deletions
@@ -0,0 +1,622 @@
// ISSUE: KT-28806
// WITH_STDLIB
// DIAGNOSTICS: -ERROR_SUPPRESSION, -DEBUG_INFO_SMARTCAST
import kotlin.contracts.*
@Suppress("OPT_IN_USAGE_ERROR", "OPT_IN_USAGE_FUTURE_ERROR")
fun atLeastOnce(block: () -> Unit) {
contract {
callsInPlace(block, InvocationKind.AT_LEAST_ONCE)
}
block()
}
@Suppress("OPT_IN_USAGE_ERROR", "OPT_IN_USAGE_FUTURE_ERROR")
fun exactlyOnce(block: () -> Unit) {
contract {
callsInPlace(block, InvocationKind.EXACTLY_ONCE)
}
block()
}
fun runWithoutContract(block: () -> Unit): Unit = block()
fun <R> materialize(): R = null!!
fun test1() {
var x: Any? = materialize()
require(x is String)
runWithoutContract {
<!SMARTCAST_IMPOSSIBLE!>x<!>.length
x = 10
}
}
fun test2() {
var x: Any? = materialize()
require(x is String)
exactlyOnce {
x.length
x = 10
}
}
fun test3() {
var x: Any? = materialize()
require(x is String)
atLeastOnce {
x.<!UNRESOLVED_REFERENCE!>length<!>
x = 10
}
}
fun test4() {
var x: Any? = materialize()
require(x is String)
runWithoutContract {
x = ""
<!SMARTCAST_IMPOSSIBLE!>x<!>.length
}
}
fun test5() {
var x: Any? = materialize()
require(x is String)
exactlyOnce {
x = ""
x.length
}
}
fun test6() {
var x: Any? = materialize()
require(x is String)
atLeastOnce {
x = ""
x.length
}
}
fun test7() {
var x: Any? = materialize()
require(x is String)
runWithoutContract {
<!SMARTCAST_IMPOSSIBLE!>x<!>.length
runWithoutContract {
<!SMARTCAST_IMPOSSIBLE!>x<!>.length
}
x = 10
}
}
fun test8() {
var x: Any? = materialize()
require(x is String)
exactlyOnce {
x.length
exactlyOnce {
<!SMARTCAST_IMPOSSIBLE!>x<!>.length
}
x = 10
}
}
fun test9() {
var x: Any? = materialize()
require(x is String)
atLeastOnce {
x.<!UNRESOLVED_REFERENCE!>length<!>
atLeastOnce {
x.<!UNRESOLVED_REFERENCE!>length<!>
}
x = 10
}
}
fun test10() {
var x: Any? = materialize()
runWithoutContract {
val b = x is String
if (b) {
x.length
}
}
}
fun test11(x: Any) {
exactlyOnce {
val b = x is String
if (b) {
x.length
}
}
}
fun test12(x: Any) {
atLeastOnce {
val b = x is String
if (b) {
x.length
}
}
}
fun test13(x: Any) {
if (x is String) {
runWithoutContract {
x.length
}
}
}
fun test14(x: Any) {
if (x is String) {
exactlyOnce {
x.length
}
}
}
fun test15(x: Any) {
if (x is String) {
atLeastOnce {
x.length
}
}
}
fun test16(x: Any) {
atLeastOnce {
when (x) {
is String -> x.length
}
x.<!UNRESOLVED_REFERENCE!>length<!>
}
}
fun test17(x: Any) {
runWithoutContract {
when (x) {
is String -> x.length
}
x.<!UNRESOLVED_REFERENCE!>length<!>
}
}
fun test18(x: Any) {
exactlyOnce {
when (x) {
is String -> x.length
}
x.<!UNRESOLVED_REFERENCE!>length<!>
}
}
fun test19(x: Any) {
when (x) {
is String -> atLeastOnce {
x.length
}
}
}
fun test20(x: Any) {
when (x) {
is String -> runWithoutContract {
x.length
}
}
}
fun test21(x: Any) {
when (x) {
is String -> exactlyOnce {
x.length
}
}
}
fun test22(x: Any) {
val state: Boolean = x is String
runWithoutContract {
if (state) {
x.length
}
}
}
fun test23(x: Any) {
val state: Boolean = x is String
exactlyOnce {
if (state) {
x.length
}
}
}
fun test24(x: Any) {
val state: Boolean = x is String
atLeastOnce {
if (state) {
x.length
}
}
}
fun test25() {
var x: Any? = materialize()
require(x is String)
runWithoutContract {
<!SMARTCAST_IMPOSSIBLE!>x<!>.length
}
x = ""
}
fun test26() {
var x: Any? = materialize()
require(x is String)
exactlyOnce {
x.length
}
x = ""
}
fun test27() {
var x: Any? = materialize()
require(x is String)
atLeastOnce {
x.length
}
x = ""
}
fun test28(x: Any) {
runWithoutContract {
require(x is String)
x.length
}
runWithoutContract {
x.<!UNRESOLVED_REFERENCE!>length<!>
}
}
fun test29(x: Any) {
exactlyOnce {
require(x is String)
x.length
}
exactlyOnce {
x.<!UNRESOLVED_REFERENCE!>length<!>
}
}
fun test30(x: Any) {
atLeastOnce {
require(x is String)
x.length
}
atLeastOnce {
x.<!UNRESOLVED_REFERENCE!>length<!>
}
}
fun test31(x: Any) {
runWithoutContract {
require(x is String)
}
x.<!UNRESOLVED_REFERENCE!>length<!>
}
fun test32(x: Any) {
exactlyOnce {
require(x is String)
}
x.<!UNRESOLVED_REFERENCE!>length<!>
}
fun test33(x: Any) {
atLeastOnce {
require(x is String)
}
x.<!UNRESOLVED_REFERENCE!>length<!>
}
fun test34() {
var x: Any? = materialize()
runWithoutContract {
require(x is String)
x = 10
}
runWithoutContract {
x.<!UNRESOLVED_REFERENCE!>length<!>
}
}
fun test35() {
var x: Any? = materialize()
exactlyOnce {
require(x is String)
x = 10
}
exactlyOnce {
x.<!UNRESOLVED_REFERENCE!>length<!>
}
}
fun test36() {
var x: Any? = materialize()
atLeastOnce {
require(x is String)
x = 10
}
atLeastOnce {
x.<!UNRESOLVED_REFERENCE!>length<!>
}
}
fun test37() {
var x: Any? = materialize()
require(x is String)
fun local() {
atLeastOnce {
<!SMARTCAST_IMPOSSIBLE!>x<!>.length
}
x = ""
}
}
fun test38() {
var x: Any? = materialize()
require(x is String)
fun local() {
exactlyOnce {
<!SMARTCAST_IMPOSSIBLE!>x<!>.length
}
x = ""
}
}
fun test39() {
var x: Any? = materialize()
require(x is String)
fun local() {
runWithoutContract {
<!SMARTCAST_IMPOSSIBLE!>x<!>.length
}
x = ""
}
}
fun test40() {
var x: Any? = materialize()
require(x is String)
fun local() {
atLeastOnce {
x.length
}
}
}
fun test41() {
var x: Any? = materialize()
require(x is String)
fun local() {
exactlyOnce {
x.length
}
}
}
fun test42() {
var x: Any? = materialize()
require(x is String)
fun local() {
runWithoutContract {
x.length
}
}
}
fun test43() {
runWithoutContract {
var x: Any? = materialize()
require(x is String)
x.length
x = 10
}
}
fun test44() {
atLeastOnce {
var x: Any? = materialize()
require(x is String)
x.length
x = 10
}
}
fun test45() {
exactlyOnce {
var x: Any? = materialize()
require(x is String)
x.length
x = 10
}
}
fun test46() {
var x: Any = materialize()
exactlyOnce {
while (x is String) {
x.length
x = 10
}
}
}
fun test47() {
var x: Any = materialize()
atLeastOnce {
while (x is String) {
x.length
x = 10
}
}
}
fun test48() {
var x: Any = materialize()
runWithoutContract {
while (x is String) {
x.length
x = 10
}
}
}
fun test49() {
var x: Any = materialize()
while (x is String) {
exactlyOnce {
x.length
x = 10
}
}
}
fun test50() {
var x: Any = materialize()
while (x is String) {
atLeastOnce {
x.<!UNRESOLVED_REFERENCE!>length<!>
x = 10
}
}
}
fun test51() {
var x: Any = materialize()
while (x is String) {
runWithoutContract {
<!SMARTCAST_IMPOSSIBLE!>x<!>.length
x = 10
}
}
}
fun test52() {
var x: Any = materialize()
exactlyOnce {
do {
x.<!UNRESOLVED_REFERENCE!>length<!>
} while (x is String)
}
}
fun test53() {
var x: Any = materialize()
atLeastOnce {
do {
x.<!UNRESOLVED_REFERENCE!>length<!>
} while (x is String)
}
}
fun test54() {
var x: Any = materialize()
runWithoutContract {
do {
x.<!UNRESOLVED_REFERENCE!>length<!>
} while (x is String)
}
}
fun test55() {
var x: Any = materialize()
runWithoutContract {
for (i in 1..3) {
require(x is String)
x.length
x = 10
}
}
}
fun test56() {
var x: Any = materialize()
atLeastOnce {
for (i in 1..3) {
require(x is String)
x.length
x = 10
}
}
}
fun test57() {
var x: Any = materialize()
exactlyOnce {
for (i in 1..3) {
require(x is String)
x.length
x = 10
}
}
}
fun test58() {
var x: Any = materialize()
for (i in 1..3) {
require(x is String)
runWithoutContract {
<!SMARTCAST_IMPOSSIBLE!>x<!>.length
x = 10
}
}
}
fun test59() {
var x: Any = materialize()
for (i in 1..3) {
require(x is String)
atLeastOnce {
x.<!UNRESOLVED_REFERENCE!>length<!>
x = 10
}
}
}
fun test60() {
var x: Any = materialize()
for (i in 1..3) {
require(x is String)
exactlyOnce {
x.length
x = 10
}
}
}
fun test61() {
var x: String? = materialize()
runWithoutContract {
x?.length ?: -1
x = null
}
}
fun test62() {
var x: String? = materialize()
atLeastOnce {
x?.length ?: -1
x = null
}
}
fun test63() {
var x: String? = materialize()
exactlyOnce {
x?.length ?: -1
x = null
}
}
@@ -0,0 +1,622 @@
// ISSUE: KT-28806
// WITH_STDLIB
// DIAGNOSTICS: -ERROR_SUPPRESSION, -DEBUG_INFO_SMARTCAST
import kotlin.contracts.*
@Suppress("OPT_IN_USAGE_ERROR", "OPT_IN_USAGE_FUTURE_ERROR")
fun atLeastOnce(block: () -> Unit) {
contract {
callsInPlace(block, InvocationKind.AT_LEAST_ONCE)
}
block()
}
@Suppress("OPT_IN_USAGE_ERROR", "OPT_IN_USAGE_FUTURE_ERROR")
fun exactlyOnce(block: () -> Unit) {
contract {
callsInPlace(block, InvocationKind.EXACTLY_ONCE)
}
block()
}
fun runWithoutContract(block: () -> Unit): Unit = block()
fun <R> materialize(): R = null!!
fun test1() {
var x: Any? = materialize()
require(x is String)
runWithoutContract {
<!SMARTCAST_IMPOSSIBLE!>x<!>.length
x = 10
}
}
fun test2() {
var x: Any? = materialize()
require(x is String)
exactlyOnce {
<!SMARTCAST_IMPOSSIBLE!>x<!>.length
x = 10
}
}
fun test3() {
var x: Any? = materialize()
require(x is String)
atLeastOnce {
<!SMARTCAST_IMPOSSIBLE!>x<!>.length
x = 10
}
}
fun test4() {
var x: Any? = materialize()
require(x is String)
runWithoutContract {
x = ""
<!SMARTCAST_IMPOSSIBLE!>x<!>.length
}
}
fun test5() {
var x: Any? = materialize()
require(x is String)
exactlyOnce {
x = ""
<!SMARTCAST_IMPOSSIBLE!>x<!>.length
}
}
fun test6() {
var x: Any? = materialize()
require(x is String)
atLeastOnce {
x = ""
<!SMARTCAST_IMPOSSIBLE!>x<!>.length
}
}
fun test7() {
var x: Any? = materialize()
require(x is String)
runWithoutContract {
<!SMARTCAST_IMPOSSIBLE!>x<!>.length
runWithoutContract {
<!SMARTCAST_IMPOSSIBLE!>x<!>.length
}
x = 10
}
}
fun test8() {
var x: Any? = materialize()
require(x is String)
exactlyOnce {
<!SMARTCAST_IMPOSSIBLE!>x<!>.length
exactlyOnce {
<!SMARTCAST_IMPOSSIBLE!>x<!>.length
}
x = 10
}
}
fun test9() {
var x: Any? = materialize()
require(x is String)
atLeastOnce {
<!SMARTCAST_IMPOSSIBLE!>x<!>.length
atLeastOnce {
<!SMARTCAST_IMPOSSIBLE!>x<!>.length
}
x = 10
}
}
fun test10() {
var x: Any? = materialize()
runWithoutContract {
val b = x is String
if (b) {
x.<!UNRESOLVED_REFERENCE!>length<!>
}
}
}
fun test11(x: Any) {
exactlyOnce {
val b = x is String
if (b) {
x.<!UNRESOLVED_REFERENCE!>length<!>
}
}
}
fun test12(x: Any) {
atLeastOnce {
val b = x is String
if (b) {
x.<!UNRESOLVED_REFERENCE!>length<!>
}
}
}
fun test13(x: Any) {
if (x is String) {
runWithoutContract {
x.length
}
}
}
fun test14(x: Any) {
if (x is String) {
exactlyOnce {
x.length
}
}
}
fun test15(x: Any) {
if (x is String) {
atLeastOnce {
x.length
}
}
}
fun test16(x: Any) {
atLeastOnce {
when (x) {
is String -> x.length
}
x.<!UNRESOLVED_REFERENCE!>length<!>
}
}
fun test17(x: Any) {
runWithoutContract {
when (x) {
is String -> x.length
}
x.<!UNRESOLVED_REFERENCE!>length<!>
}
}
fun test18(x: Any) {
exactlyOnce {
when (x) {
is String -> x.length
}
x.<!UNRESOLVED_REFERENCE!>length<!>
}
}
fun test19(x: Any) {
when (x) {
is String -> atLeastOnce {
x.length
}
}
}
fun test20(x: Any) {
when (x) {
is String -> runWithoutContract {
x.length
}
}
}
fun test21(x: Any) {
when (x) {
is String -> exactlyOnce {
x.length
}
}
}
fun test22(x: Any) {
val state: Boolean = x is String
runWithoutContract {
if (state) {
x.<!UNRESOLVED_REFERENCE!>length<!>
}
}
}
fun test23(x: Any) {
val state: Boolean = x is String
exactlyOnce {
if (state) {
x.<!UNRESOLVED_REFERENCE!>length<!>
}
}
}
fun test24(x: Any) {
val state: Boolean = x is String
atLeastOnce {
if (state) {
x.<!UNRESOLVED_REFERENCE!>length<!>
}
}
}
fun test25() {
var x: Any? = materialize()
require(x is String)
runWithoutContract {
<!SMARTCAST_IMPOSSIBLE!>x<!>.length
}
x = ""
}
fun test26() {
var x: Any? = materialize()
require(x is String)
exactlyOnce {
<!SMARTCAST_IMPOSSIBLE!>x<!>.length
}
x = ""
}
fun test27() {
var x: Any? = materialize()
require(x is String)
atLeastOnce {
<!SMARTCAST_IMPOSSIBLE!>x<!>.length
}
x = ""
}
fun test28(x: Any) {
runWithoutContract {
require(x is String)
x.length
}
runWithoutContract {
x.<!UNRESOLVED_REFERENCE!>length<!>
}
}
fun test29(x: Any) {
exactlyOnce {
require(x is String)
x.length
}
exactlyOnce {
x.<!UNRESOLVED_REFERENCE!>length<!>
}
}
fun test30(x: Any) {
atLeastOnce {
require(x is String)
x.length
}
atLeastOnce {
x.<!UNRESOLVED_REFERENCE!>length<!>
}
}
fun test31(x: Any) {
runWithoutContract {
require(x is String)
}
x.<!UNRESOLVED_REFERENCE!>length<!>
}
fun test32(x: Any) {
exactlyOnce {
require(x is String)
}
x.<!UNRESOLVED_REFERENCE!>length<!>
}
fun test33(x: Any) {
atLeastOnce {
require(x is String)
}
x.<!UNRESOLVED_REFERENCE!>length<!>
}
fun test34() {
var x: Any? = materialize()
runWithoutContract {
require(x is String)
x = 10
}
runWithoutContract {
x.<!UNRESOLVED_REFERENCE!>length<!>
}
}
fun test35() {
var x: Any? = materialize()
exactlyOnce {
require(x is String)
x = 10
}
exactlyOnce {
x.<!UNRESOLVED_REFERENCE!>length<!>
}
}
fun test36() {
var x: Any? = materialize()
atLeastOnce {
require(x is String)
x = 10
}
atLeastOnce {
x.<!UNRESOLVED_REFERENCE!>length<!>
}
}
fun test37() {
var x: Any? = materialize()
require(x is String)
fun local() {
atLeastOnce {
<!SMARTCAST_IMPOSSIBLE!>x<!>.length
}
x = ""
}
}
fun test38() {
var x: Any? = materialize()
require(x is String)
fun local() {
exactlyOnce {
<!SMARTCAST_IMPOSSIBLE!>x<!>.length
}
x = ""
}
}
fun test39() {
var x: Any? = materialize()
require(x is String)
fun local() {
runWithoutContract {
<!SMARTCAST_IMPOSSIBLE!>x<!>.length
}
x = ""
}
}
fun test40() {
var x: Any? = materialize()
require(x is String)
fun local() {
atLeastOnce {
x.length
}
}
}
fun test41() {
var x: Any? = materialize()
require(x is String)
fun local() {
exactlyOnce {
x.length
}
}
}
fun test42() {
var x: Any? = materialize()
require(x is String)
fun local() {
runWithoutContract {
x.length
}
}
}
fun test43() {
runWithoutContract {
var x: Any? = materialize()
require(x is String)
x.length
x = 10
}
}
fun test44() {
atLeastOnce {
var x: Any? = materialize()
require(x is String)
x.length
x = 10
}
}
fun test45() {
exactlyOnce {
var x: Any? = materialize()
require(x is String)
x.length
x = 10
}
}
fun test46() {
var x: Any = materialize()
exactlyOnce {
while (x is String) {
<!SMARTCAST_IMPOSSIBLE!>x<!>.length
x = 10
}
}
}
fun test47() {
var x: Any = materialize()
atLeastOnce {
while (x is String) {
<!SMARTCAST_IMPOSSIBLE!>x<!>.length
x = 10
}
}
}
fun test48() {
var x: Any = materialize()
runWithoutContract {
while (x is String) {
<!SMARTCAST_IMPOSSIBLE!>x<!>.length
x = 10
}
}
}
fun test49() {
var x: Any = materialize()
while (x is String) {
exactlyOnce {
<!SMARTCAST_IMPOSSIBLE!>x<!>.length
x = 10
}
}
}
fun test50() {
var x: Any = materialize()
while (x is String) {
atLeastOnce {
<!SMARTCAST_IMPOSSIBLE!>x<!>.length
x = 10
}
}
}
fun test51() {
var x: Any = materialize()
while (x is String) {
runWithoutContract {
<!SMARTCAST_IMPOSSIBLE!>x<!>.length
x = 10
}
}
}
fun test52() {
var x: Any = materialize()
exactlyOnce {
do {
x.<!UNRESOLVED_REFERENCE!>length<!>
} while (x is String)
}
}
fun test53() {
var x: Any = materialize()
atLeastOnce {
do {
x.<!UNRESOLVED_REFERENCE!>length<!>
} while (x is String)
}
}
fun test54() {
var x: Any = materialize()
runWithoutContract {
do {
x.<!UNRESOLVED_REFERENCE!>length<!>
} while (x is String)
}
}
fun test55() {
var x: Any = materialize()
runWithoutContract {
for (i in 1..3) {
require(x is String)
<!SMARTCAST_IMPOSSIBLE!>x<!>.length
x = 10
}
}
}
fun test56() {
var x: Any = materialize()
atLeastOnce {
for (i in 1..3) {
require(x is String)
<!SMARTCAST_IMPOSSIBLE!>x<!>.length
x = 10
}
}
}
fun test57() {
var x: Any = materialize()
exactlyOnce {
for (i in 1..3) {
require(x is String)
<!SMARTCAST_IMPOSSIBLE!>x<!>.length
x = 10
}
}
}
fun test58() {
var x: Any = materialize()
for (i in 1..3) {
require(x is String)
runWithoutContract {
<!SMARTCAST_IMPOSSIBLE!>x<!>.length
x = 10
}
}
}
fun test59() {
var x: Any = materialize()
for (i in 1..3) {
require(x is String)
atLeastOnce {
<!SMARTCAST_IMPOSSIBLE!>x<!>.length
x = 10
}
}
}
fun test60() {
var x: Any = materialize()
for (i in 1..3) {
require(x is String)
exactlyOnce {
<!SMARTCAST_IMPOSSIBLE!>x<!>.length
x = 10
}
}
}
fun test61() {
var x: String? = materialize()
runWithoutContract {
x?.length ?: -1
x = null
}
}
fun test62() {
var x: String? = materialize()
atLeastOnce {
x?.length ?: -1
x = null
}
}
fun test63() {
var x: String? = materialize()
exactlyOnce {
x?.length ?: -1
x = null
}
}
@@ -0,0 +1,378 @@
// ISSUE: KT-28806
// WITH_STDLIB
// DIAGNOSTICS: -ERROR_SUPPRESSION, -DEBUG_INFO_SMARTCAST
import kotlin.contracts.*
@Suppress("OPT_IN_USAGE_ERROR", "OPT_IN_USAGE_FUTURE_ERROR")
fun atLeastOnce(block: () -> Unit) {
contract {
callsInPlace(block, InvocationKind.AT_LEAST_ONCE)
}
block()
}
@Suppress("OPT_IN_USAGE_ERROR", "OPT_IN_USAGE_FUTURE_ERROR")
fun exactlyOnce(block: () -> Unit) {
contract {
callsInPlace(block, InvocationKind.EXACTLY_ONCE)
}
block()
}
fun runWithoutContract(block: () -> Unit): Unit = block()
fun <R> materialize(): R = null!!
fun test1(x: Any) {
var y = x
require(y is String)
runWithoutContract {
y = 10
y = x
x.length
<!SMARTCAST_IMPOSSIBLE!>y<!>.length
}
}
fun test2(x: Any) {
var y = x
require(y is String)
exactlyOnce {
y = 10
y = x
x.length
y.length
}
}
fun test3(x: Any) {
var y = x
require(y is String)
atLeastOnce {
y = 10
y = x
x.length
y.length
}
}
fun test4(x: Any) {
var y: Any
require(x is String)
runWithoutContract {
y = 10
y = x
x.length
<!SMARTCAST_IMPOSSIBLE!>y<!>.length
}
}
fun test5(x: Any) {
var y: Any
require(x is String)
exactlyOnce {
y = 10
y = x
x.length
y.length
}
}
fun test6(x: Any) {
var y: Any
require(x is String)
atLeastOnce {
y = 10
y = x
x.length
y.length
}
}
fun test7(x: Any) {
var y: Any?
val b = x is String
if (b) {
y = x
exactlyOnce {
x.length
y.length
}
}
}
fun test8(x: Any) {
var y: Any?
val b = x is String
if (b) {
y = x
atLeastOnce {
x.length
y.length
}
}
}
fun test9(x: Any) {
var y: Any?
val b = x is String
if (b) {
y = x
runWithoutContract {
x.length
y.length
}
}
}
fun test10(x: Any) {
var y = x
runWithoutContract {
require(y is String)
x.length
y.length
}
}
fun test11(x: Any) {
var y = x
atLeastOnce {
require(y is String)
x.length
y.length
}
}
fun test12(x: Any) {
var y = x
exactlyOnce {
require(y is String)
x.length
y.length
}
}
fun test13() {
var x: Any? = materialize()
var y = x
atLeastOnce {
require(y is String)
x = 10
y = x
y.<!UNRESOLVED_REFERENCE!>length<!>
y.inc()
}
}
fun test14() {
var x: Any? = materialize()
var y = x
exactlyOnce {
require(y is String)
x = 10
y = x
y.<!UNRESOLVED_REFERENCE!>length<!>
y.inc()
}
}
fun test15() {
var x: Any? = materialize()
var y = x
runWithoutContract {
require(y is String)
x = 10
y = x
y.<!UNRESOLVED_REFERENCE!>length<!>
y.<!UNRESOLVED_REFERENCE!>inc<!>()
}
}
fun test16(x: Any) {
var y: Any
runWithoutContract {
require(x is String)
y = x
<!SMARTCAST_IMPOSSIBLE!>y<!>.length
}
}
fun test17(x: Any) {
var y: Any
exactlyOnce {
require(x is String)
y = x
y.length
}
}
fun test18(x: Any) {
var y: Any
atLeastOnce {
require(x is String)
y = x
y.length
}
}
fun test19(x: Any?) {
var y: Any
atLeastOnce {
if (x != null) {
y = x
} else {
y = <!ASSIGNMENT_TYPE_MISMATCH!>x<!>
}
require(x is String)
y.length
}
}
fun test20(x: Any?) {
var y: Any
exactlyOnce {
if (x != null) {
y = x
} else {
y = <!ASSIGNMENT_TYPE_MISMATCH!>x<!>
}
require(x is String)
y.length
}
}
fun test21(x: Any?) {
var y: Any
runWithoutContract {
if (x != null) {
y = x
} else {
y = <!ASSIGNMENT_TYPE_MISMATCH!>x<!>
}
require(x is String)
<!SMARTCAST_IMPOSSIBLE!>y<!>.length
}
}
fun test22(x: Any) {
var y: Any = materialize()
runWithoutContract {
require(x is String)
y = x
}
y.<!UNRESOLVED_REFERENCE!>length<!>
}
fun test23(x: Any) {
var y: Any
exactlyOnce {
require(x is String)
y = x
}
y.<!UNRESOLVED_REFERENCE!>length<!>
}
fun test24(x: Any) {
var y: Any
atLeastOnce {
require(x is String)
y = x
}
y.<!UNRESOLVED_REFERENCE!>length<!>
}
fun test25(x: Any) {
var y = x
exactlyOnce {
while (x is String) {
y.length
}
}
}
fun test26(x: Any) {
var y = x
atLeastOnce {
while (x is String) {
y.length
}
}
}
fun test27(x: Any) {
var y = x
runWithoutContract {
while (x is String) {
y.length
}
}
}
fun test28() {
var x: Any? = materialize()
var y = x
exactlyOnce {
while (x is String) {
y.<!UNRESOLVED_REFERENCE!>length<!>
x = 10
}
}
}
fun test29() {
var x: Any? = materialize()
var y = x
atLeastOnce {
while (x is String) {
y.<!UNRESOLVED_REFERENCE!>length<!>
x = 10
}
}
}
fun test30() {
var x: Any? = materialize()
var y = x
runWithoutContract {
while (x is String) {
y.<!UNRESOLVED_REFERENCE!>length<!>
x = 10
}
}
}
fun test31() {
var x: Any? = materialize()
var y = x
exactlyOnce {
while (x is String) {
y.length
x.length
}
x = 10
}
}
fun test32() {
var x: Any? = materialize()
var y = x
atLeastOnce {
while (x is String) {
y.<!UNRESOLVED_REFERENCE!>length<!>
x.length
}
x = 10
}
}
fun test33() {
var x: Any? = materialize()
var y = x
runWithoutContract {
while (x is String) {
y.length
<!SMARTCAST_IMPOSSIBLE!>x<!>.length
}
x = 10
}
}
@@ -0,0 +1,378 @@
// ISSUE: KT-28806
// WITH_STDLIB
// DIAGNOSTICS: -ERROR_SUPPRESSION, -DEBUG_INFO_SMARTCAST
import kotlin.contracts.*
@Suppress("OPT_IN_USAGE_ERROR", "OPT_IN_USAGE_FUTURE_ERROR")
fun atLeastOnce(block: () -> Unit) {
contract {
callsInPlace(block, InvocationKind.AT_LEAST_ONCE)
}
block()
}
@Suppress("OPT_IN_USAGE_ERROR", "OPT_IN_USAGE_FUTURE_ERROR")
fun exactlyOnce(block: () -> Unit) {
contract {
callsInPlace(block, InvocationKind.EXACTLY_ONCE)
}
block()
}
fun runWithoutContract(block: () -> Unit): Unit = block()
fun <R> materialize(): R = null!!
fun test1(x: Any) {
var y = x
require(y is String)
runWithoutContract {
y = 10
y = x
x.<!UNRESOLVED_REFERENCE!>length<!>
y.<!UNRESOLVED_REFERENCE!>length<!>
}
}
fun test2(x: Any) {
var y = x
require(y is String)
exactlyOnce {
y = 10
y = x
x.<!UNRESOLVED_REFERENCE!>length<!>
y.<!UNRESOLVED_REFERENCE!>length<!>
}
}
fun test3(x: Any) {
var y = x
require(y is String)
atLeastOnce {
y = 10
y = x
x.<!UNRESOLVED_REFERENCE!>length<!>
y.<!UNRESOLVED_REFERENCE!>length<!>
}
}
fun test4(x: Any) {
var y: Any
require(x is String)
runWithoutContract {
y = 10
y = x
x.length
<!SMARTCAST_IMPOSSIBLE!>y<!>.length
}
}
fun test5(x: Any) {
var y: Any
require(x is String)
exactlyOnce {
y = 10
y = x
x.length
<!SMARTCAST_IMPOSSIBLE!>y<!>.length
}
}
fun test6(x: Any) {
var y: Any
require(x is String)
atLeastOnce {
y = 10
y = x
x.length
<!SMARTCAST_IMPOSSIBLE!>y<!>.length
}
}
fun test7(x: Any) {
var y: Any?
val b = x is String
if (b) {
y = x
exactlyOnce {
x.<!UNRESOLVED_REFERENCE!>length<!>
y.<!UNRESOLVED_REFERENCE!>length<!>
}
}
}
fun test8(x: Any) {
var y: Any?
val b = x is String
if (b) {
y = x
atLeastOnce {
x.<!UNRESOLVED_REFERENCE!>length<!>
y.<!UNRESOLVED_REFERENCE!>length<!>
}
}
}
fun test9(x: Any) {
var y: Any?
val b = x is String
if (b) {
y = x
runWithoutContract {
x.<!UNRESOLVED_REFERENCE!>length<!>
y.<!UNRESOLVED_REFERENCE!>length<!>
}
}
}
fun test10(x: Any) {
var y = x
runWithoutContract {
require(y is String)
x.<!UNRESOLVED_REFERENCE!>length<!>
y.length
}
}
fun test11(x: Any) {
var y = x
atLeastOnce {
require(y is String)
x.<!UNRESOLVED_REFERENCE!>length<!>
y.length
}
}
fun test12(x: Any) {
var y = x
exactlyOnce {
require(y is String)
x.<!UNRESOLVED_REFERENCE!>length<!>
y.length
}
}
fun test13() {
var x: Any? = materialize()
var y = x
atLeastOnce {
require(y is String)
x = 10
y = x
y.<!UNRESOLVED_REFERENCE!>length<!>
y.<!UNRESOLVED_REFERENCE_WRONG_RECEIVER!>inc<!>()
}
}
fun test14() {
var x: Any? = materialize()
var y = x
exactlyOnce {
require(y is String)
x = 10
y = x
y.<!UNRESOLVED_REFERENCE!>length<!>
y.<!UNRESOLVED_REFERENCE_WRONG_RECEIVER!>inc<!>()
}
}
fun test15() {
var x: Any? = materialize()
var y = x
runWithoutContract {
require(y is String)
x = 10
y = x
y.<!UNRESOLVED_REFERENCE!>length<!>
y.<!UNRESOLVED_REFERENCE_WRONG_RECEIVER!>inc<!>()
}
}
fun test16(x: Any) {
var y: Any
runWithoutContract {
require(x is String)
y = x
<!SMARTCAST_IMPOSSIBLE!>y<!>.length
}
}
fun test17(x: Any) {
var y: Any
exactlyOnce {
require(x is String)
y = x
<!SMARTCAST_IMPOSSIBLE!>y<!>.length
}
}
fun test18(x: Any) {
var y: Any
atLeastOnce {
require(x is String)
y = x
<!SMARTCAST_IMPOSSIBLE!>y<!>.length
}
}
fun test19(x: Any?) {
var y: Any
atLeastOnce {
if (x != null) {
y = x
} else {
y = <!DEBUG_INFO_CONSTANT, TYPE_MISMATCH!>x<!>
}
require(x is String)
y.<!UNRESOLVED_REFERENCE!>length<!>
}
}
fun test20(x: Any?) {
var y: Any
exactlyOnce {
if (x != null) {
y = x
} else {
y = <!DEBUG_INFO_CONSTANT, TYPE_MISMATCH!>x<!>
}
require(x is String)
y.<!UNRESOLVED_REFERENCE!>length<!>
}
}
fun test21(x: Any?) {
var y: Any
runWithoutContract {
if (x != null) {
y = x
} else {
y = <!DEBUG_INFO_CONSTANT, TYPE_MISMATCH!>x<!>
}
require(x is String)
y.<!UNRESOLVED_REFERENCE!>length<!>
}
}
fun test22(x: Any) {
var y: Any = materialize()
runWithoutContract {
require(x is String)
y = x
}
y.<!UNRESOLVED_REFERENCE!>length<!>
}
fun test23(x: Any) {
var y: Any
exactlyOnce {
require(x is String)
y = x
}
y.<!UNRESOLVED_REFERENCE!>length<!>
}
fun test24(x: Any) {
var y: Any
atLeastOnce {
require(x is String)
y = x
}
y.<!UNRESOLVED_REFERENCE!>length<!>
}
fun test25(x: Any) {
var y = x
exactlyOnce {
while (x is String) {
y.<!UNRESOLVED_REFERENCE!>length<!>
}
}
}
fun test26(x: Any) {
var y = x
atLeastOnce {
while (x is String) {
y.<!UNRESOLVED_REFERENCE!>length<!>
}
}
}
fun test27(x: Any) {
var y = x
runWithoutContract {
while (x is String) {
y.<!UNRESOLVED_REFERENCE!>length<!>
}
}
}
fun test28() {
var x: Any? = materialize()
var y = x
exactlyOnce {
while (x is String) {
y.<!UNRESOLVED_REFERENCE!>length<!>
x = 10
}
}
}
fun test29() {
var x: Any? = materialize()
var y = x
atLeastOnce {
while (x is String) {
y.<!UNRESOLVED_REFERENCE!>length<!>
x = 10
}
}
}
fun test30() {
var x: Any? = materialize()
var y = x
runWithoutContract {
while (x is String) {
y.<!UNRESOLVED_REFERENCE!>length<!>
x = 10
}
}
}
fun test31() {
var x: Any? = materialize()
var y = x
exactlyOnce {
while (x is String) {
y.<!UNRESOLVED_REFERENCE!>length<!>
<!SMARTCAST_IMPOSSIBLE!>x<!>.length
}
x = 10
}
}
fun test32() {
var x: Any? = materialize()
var y = x
atLeastOnce {
while (x is String) {
y.<!UNRESOLVED_REFERENCE!>length<!>
<!SMARTCAST_IMPOSSIBLE!>x<!>.length
}
x = 10
}
}
fun test33() {
var x: Any? = materialize()
var y = x
runWithoutContract {
while (x is String) {
y.<!UNRESOLVED_REFERENCE!>length<!>
<!SMARTCAST_IMPOSSIBLE!>x<!>.length
}
x = 10
}
}
@@ -0,0 +1,82 @@
// ISSUE: KT-28806
// FIR_IDENTICAL
// WITH_STDLIB
// DIAGNOSTICS: -ERROR_SUPPRESSION
import kotlin.contracts.*
import kotlin.reflect.KProperty
@Suppress("OPT_IN_USAGE_ERROR", "OPT_IN_USAGE_FUTURE_ERROR")
fun atLeastOnce(block: () -> Unit) {
contract {
callsInPlace(block, InvocationKind.AT_LEAST_ONCE)
}
block()
}
@Suppress("OPT_IN_USAGE_ERROR", "OPT_IN_USAGE_FUTURE_ERROR")
fun exactlyOnce(block: () -> Unit) {
contract {
callsInPlace(block, InvocationKind.EXACTLY_ONCE)
}
block()
}
fun runWithoutContract(block: () -> Unit): Unit = block()
class Delegate {
operator fun getValue(thisRef: Any?, property: KProperty<*>): Any {
TODO()
}
operator fun setValue(thisRef: Any?, property: KProperty<*>, value: Any) {
TODO()
}
}
fun test1() {
var x: Any by Delegate()
require(x is String)
runWithoutContract {
<!SMARTCAST_IMPOSSIBLE!>x<!>.length
}
}
fun test2() {
var x: Any by Delegate()
require(x is String)
atLeastOnce {
<!SMARTCAST_IMPOSSIBLE!>x<!>.length
}
}
fun test3() {
var x: Any by Delegate()
require(x is String)
exactlyOnce {
<!SMARTCAST_IMPOSSIBLE!>x<!>.length
}
}
fun test4() {
val x: Any by Delegate()
require(x is String)
runWithoutContract {
<!SMARTCAST_IMPOSSIBLE!>x<!>.length
}
}
fun test5() {
val x: Any by Delegate()
require(x is String)
atLeastOnce {
<!SMARTCAST_IMPOSSIBLE!>x<!>.length
}
}
fun test6() {
val x: Any by Delegate()
require(x is String)
exactlyOnce {
<!SMARTCAST_IMPOSSIBLE!>x<!>.length
}
}
@@ -0,0 +1,58 @@
// ISSUE: KT-28806
// FIR_IDENTICAL
// WITH_STDLIB
// DIAGNOSTICS: -ERROR_SUPPRESSION, -DEBUG_INFO_SMARTCAST
import kotlin.contracts.*
@Suppress("OPT_IN_USAGE_ERROR", "OPT_IN_USAGE_FUTURE_ERROR")
fun atLeastOnce(block: () -> Unit): Boolean {
contract {
callsInPlace(block, InvocationKind.AT_LEAST_ONCE)
}
block()
return true
}
@Suppress("OPT_IN_USAGE_ERROR", "OPT_IN_USAGE_FUTURE_ERROR")
fun exactlyOnce(block: () -> Unit): Boolean {
contract {
callsInPlace(block, InvocationKind.EXACTLY_ONCE)
}
block()
return true
}
fun runWithoutContract(block: () -> Unit): Boolean {
block()
return true
}
fun test1(x: Any) {
if (x !is String || atLeastOnce { x.length }) return
}
fun test2(x: Any) {
if (x !is String || exactlyOnce { x.length }) return
}
fun test3(x: Any) {
if (x !is String || runWithoutContract { x.length }) return
}
fun test4(x: Any) {
if (x is String && atLeastOnce { x.length }) {
x.length
}
}
fun test5(x: Any) {
if (x is String && exactlyOnce { x.length }) {
x.length
}
}
fun test6(x: Any) {
if (x is String && runWithoutContract { x.length }) {
x.length
}
}
@@ -0,0 +1,55 @@
// ISSUE: KT-37308
// WITH_STDLIB
// DIAGNOSTICS: -ERROR_SUPPRESSION
import kotlin.contracts.*
@OptIn(ExperimentalContracts::class)
fun CharSequence?.valueIsNotNull(): Boolean {
contract {
returns(true) implies (this@valueIsNotNull != null)
}
return this != null
}
@OptIn(ExperimentalContracts::class)
fun CharSequence?.valueIsNull(): Boolean {
contract {
returns(false) implies (this@valueIsNull != null)
}
return this == null
}
class A {
val b: String? = ""
val e: C? = C()
}
class C {
val d: String? = ""
}
fun test1(a: A?) {
if (!a?.b.valueIsNull()) {
a.b.length
}
}
fun test2(a: A?) {
require(!a?.b.valueIsNull())
a.b.length
}
fun test3(a: A?) {
if(a?.b.valueIsNotNull()){
a.b.length
}
}
fun test4(a :A?) {
require(a?.b.valueIsNotNull())
a.b.length
}
fun test5(a :A?) {
require(a?.e?.d.valueIsNotNull())
a.e.d.length
}
@@ -0,0 +1,55 @@
// ISSUE: KT-37308
// WITH_STDLIB
// DIAGNOSTICS: -ERROR_SUPPRESSION
import kotlin.contracts.*
@OptIn(ExperimentalContracts::class)
fun CharSequence?.valueIsNotNull(): Boolean {
contract {
returns(true) implies (this@valueIsNotNull != null)
}
return this != null
}
@OptIn(ExperimentalContracts::class)
fun CharSequence?.valueIsNull(): Boolean {
contract {
returns(false) implies (this@valueIsNull != null)
}
return this == null
}
class A {
val b: String? = ""
val e: C? = C()
}
class C {
val d: String? = ""
}
fun test1(a: A?) {
if (!a?.b.valueIsNull()) {
a<!UNSAFE_CALL!>.<!>b<!UNSAFE_CALL!>.<!>length
}
}
fun test2(a: A?) {
require(!a?.b.valueIsNull())
a<!UNSAFE_CALL!>.<!>b<!UNSAFE_CALL!>.<!>length
}
fun test3(a: A?) {
if(a?.b.valueIsNotNull()){
a<!UNSAFE_CALL!>.<!>b<!UNSAFE_CALL!>.<!>length
}
}
fun test4(a :A?) {
require(a?.b.valueIsNotNull())
a<!UNSAFE_CALL!>.<!>b<!UNSAFE_CALL!>.<!>length
}
fun test5(a :A?) {
require(a?.e?.d.valueIsNotNull())
a<!UNSAFE_CALL!>.<!>e<!UNSAFE_CALL!>.<!>d<!UNSAFE_CALL!>.<!>length
}