Files
kotlin-fork/compiler/testData/diagnostics/testsWithStdLib/contracts/smartcasts/nullabilitySmartcastWhenNullability.kt
T
Dmitry Savvinov 4434db4d69 Effects: add diagnostic tests on contracts
- Make AbstractDiagnosticsTest dump function contracts
- Add diagnostics tests on parsing contracts
- Add diagnostics tests on smartcats in presence of functions with
contracts
- Add diagnostics tests on initialization and flow in presence of
in-place called lambdas

==========
Introduction of EffectSystem: 16/18
2017-10-12 11:55:26 +03:00

55 lines
1.2 KiB
Kotlin
Vendored

// !LANGUAGE: +ReturnsEffect
// !DIAGNOSTICS: -INVISIBLE_REFERENCE -INVISIBLE_MEMBER
import kotlin.internal.contracts.*
fun nullWhenNull(x: Int?): Int? {
contract {
returnsNotNull() implies (x != null)
}
return x?.inc()
}
fun testNullWhenNull(x: Int?) {
if (nullWhenNull(x) == null) {
x<!UNSAFE_CALL!>.<!>dec()
}
else {
<!DEBUG_INFO_SMARTCAST!>x<!>.dec()
}
if (nullWhenNull(x) != null) {
<!DEBUG_INFO_SMARTCAST!>x<!>.dec()
}
else {
x<!UNSAFE_CALL!>.<!>dec()
}
x<!UNSAFE_CALL!>.<!>dec()
}
// NB. it is the same function as `nullWhenNull`, but annotations specifies other facet of the function behaviour
fun notNullWhenNotNull (x: Int?): Int? {
contract {
returns(null) implies (x == null)
}
return x?.inc()
}
fun testNotNullWhenNotNull (x: Int?) {
if (notNullWhenNotNull(x) == null) {
<!SENSELESS_COMPARISON!><!DEBUG_INFO_CONSTANT!>x<!> == null<!>
}
else {
x<!UNSAFE_CALL!>.<!>dec()
}
if (notNullWhenNotNull(x) != null) {
x<!UNSAFE_CALL!>.<!>dec()
}
else {
<!SENSELESS_COMPARISON!><!DEBUG_INFO_CONSTANT!>x<!> == null<!>
}
x<!UNSAFE_CALL!>.<!>dec()
}