b29a6e48fb
- 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).
72 lines
1.3 KiB
Kotlin
Vendored
72 lines
1.3 KiB
Kotlin
Vendored
// !LANGUAGE: +AllowContractsForCustomFunctions +UseCallsInPlaceEffect
|
|
|
|
import kotlin.internal.contracts.*
|
|
|
|
inline fun <T> myRun(block: () -> T): T {
|
|
contract {
|
|
callsInPlace(block, InvocationKind.EXACTLY_ONCE)
|
|
}
|
|
return block()
|
|
}
|
|
|
|
fun someComputation(): Int = 42
|
|
|
|
fun tryCatchInlined() {
|
|
val x: Int
|
|
|
|
myRun {
|
|
try {
|
|
x = someComputation()
|
|
x.inc()
|
|
}
|
|
catch (e: java.lang.Exception) {
|
|
// I?
|
|
x.inc()
|
|
}
|
|
}
|
|
|
|
// I?
|
|
x.inc()
|
|
}
|
|
|
|
fun possibleReassignmentInTryCatch() {
|
|
val x: Int
|
|
|
|
myRun {
|
|
x = 42
|
|
try {
|
|
x = someComputation()
|
|
x.inc()
|
|
}
|
|
catch (e: java.lang.Exception) {
|
|
// Possible reassignment
|
|
x = 42
|
|
x.inc()
|
|
}
|
|
// Initialized
|
|
x.inc()
|
|
}
|
|
// Initialized
|
|
x.inc()
|
|
}
|
|
|
|
|
|
|
|
fun tryCatchOuter() {
|
|
var x: Int
|
|
try {
|
|
myRun {
|
|
x = someComputation()
|
|
x.inc()
|
|
}
|
|
}
|
|
catch (e: java.lang.UnsupportedOperationException) {
|
|
myRun { x = 42 }
|
|
}
|
|
catch (e: java.lang.Exception) {
|
|
// do nothing
|
|
}
|
|
// I? because we can leave with last catch-clause which doesn't initialize x
|
|
x.inc()
|
|
}
|