Files
kotlin-fork/compiler/testData/diagnostics/testsWithStdLib/contracts/smartcasts/multieffect/implicitIff.kt
T
Dmitry Savvinov b29a6e48fb Refactor language features, which control effect system
- Introduce new language feature 'ReadDeserializedContracts', which
allows to deserialize contracts from metadata.

- Introduce new language feature 'AllowContractsForCustomFunctions',
which allows reading contracts from sources.

- Use new features instead of combination 'CallsInPlaceEffect ||
ReturnsEffect'

- Rename 'CallsInPlaceEffect' -> 'UseCallsInPlaceEffect',
'ReturnsEffect' -> 'UseReturnsEffect'. As names suggest, they control
if it is allowed to use corresponding effect in analysis.

We have to introduce separate 'ReadDeserializedContracts' to enable
contracts only in some modules of the project, because libraries are
read with project-wide settings (see KT-20692).
2018-01-26 11:30:44 +03:00

86 lines
1.8 KiB
Kotlin
Vendored

// !LANGUAGE: +AllowContractsForCustomFunctions +UseReturnsEffect
// !DIAGNOSTICS: -INVISIBLE_REFERENCE -INVISIBLE_MEMBER
import kotlin.internal.contracts.*
fun onlyTrue(b: Boolean): Boolean {
contract {
returns(true) implies (b)
}
return b
}
fun onlyFalse(b: Boolean): Boolean {
contract {
returns(false) implies (!b)
}
return b
}
fun trueAndFalse(b: Boolean): Boolean {
contract {
returns(true) implies (b)
returns(false) implies (!b)
}
return b
}
// ==== actual tests ====
fun useOnlyTrueInTrueBranch(x: Any?) {
if (onlyTrue(x is String)) {
<!DEBUG_INFO_SMARTCAST!>x<!>.length
}
else {
x.<!UNRESOLVED_REFERENCE!>length<!>
}
}
fun useOnlyTrueInFalseBranch(x: Any?) {
if (onlyTrue(x !is String)) {
x.<!UNRESOLVED_REFERENCE!>length<!>
}
else {
// No smartcast here, we don't know that condition is false here
x.<!UNRESOLVED_REFERENCE!>length<!>
}
}
fun useOnlyFalseInTrueBranch(x: Any?) {
if (onlyFalse(x is String)) {
// No smartcast here, we don't know that condition is true here
x.<!UNRESOLVED_REFERENCE!>length<!>
}
else {
x.<!UNRESOLVED_REFERENCE!>length<!>
}
}
fun useOnlyFalseInFalseBranch(x: Any?) {
if (onlyFalse(x !is String)) {
x.<!UNRESOLVED_REFERENCE!>length<!>
}
else {
<!DEBUG_INFO_SMARTCAST!>x<!>.length
}
}
fun useTrueAndFalseInTrueBranch(x: Any?) {
if (trueAndFalse(x is String)) {
<!DEBUG_INFO_SMARTCAST!>x<!>.length
}
else {
x.<!UNRESOLVED_REFERENCE!>length<!>
}
}
fun useTrueAndFalseInFalseBranch(x: Any?) {
if (trueAndFalse(x !is String)) {
x.<!UNRESOLVED_REFERENCE!>length<!>
}
else {
<!DEBUG_INFO_SMARTCAST!>x<!>.length
}
}