Files
kotlin-fork/compiler/testData/diagnostics/testsWithStdLib/contracts/controlflow/flowInlining/throwIfNotCalled.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

47 lines
1.2 KiB
Kotlin
Vendored

// !LANGUAGE: +AllowContractsForCustomFunctions +UseCallsInPlaceEffect
// !DIAGNOSTICS: -INVISIBLE_REFERENCE -INVISIBLE_MEMBER
import kotlin.internal.contracts.*
inline fun myRun(block: () -> Unit) {
contract {
callsInPlace(block, InvocationKind.EXACTLY_ONCE)
}
block()
}
inline fun <T> unknownRun(block: () -> T): T = block()
fun throwIfNotCalled() {
val x: Int
myRun outer@ {
unknownRun {
myRun {
<!CAPTURED_VAL_INITIALIZATION!>x<!> = 42
return@outer
}
}
throw java.lang.IllegalArgumentException()
}
// x *is* initialized here, because if myRun was never called -> exception
// were thrown and control flow wouldn't be here
println(x)
}
fun catchThrowIfNotCalled() {
val x: Int
try {
myRun outer@ {
unknownRun {
myRun {
<!CAPTURED_VAL_INITIALIZATION!>x<!> = 42
return@outer
}
}
throw java.lang.IllegalArgumentException()
}
} catch (ignored: java.lang.IllegalArgumentException) { }
// x *isn't* initialized here!
println(<!UNINITIALIZED_VARIABLE!>x<!>)
}