Files
kotlin-fork/compiler/testData/diagnostics/tests/controlFlowAnalysis/fieldInitialization.fir.kt
T
Tianyu Geng fc8d0e3ee0 FIR checkers: report VAL_REASSIGNMENT for assignment operators
Currently VAL_REASSIGNMENT are only reported on direct assignments.
Reassignments in the form of, for example, `+=` are reported as
`VARIABLE_EXPECTED`, which differs from FE1.0.
2021-04-02 13:33:52 +03:00

153 lines
2.9 KiB
Kotlin
Vendored

// !LANGUAGE: +AllowContractsForCustomFunctions +UseCallsInPlaceEffect
// !USE_EXPERIMENTAL: kotlin.internal.ContractsDsl
import kotlin.contracts.*
@kotlin.contracts.ExperimentalContracts
inline fun inlineMe(block: () -> Unit) {
contract {
callsInPlace(block, InvocationKind.EXACTLY_ONCE)
}
block()
}
@kotlin.contracts.ExperimentalContracts
inline fun crossinlineMe(crossinline block: () -> Unit) {
contract {
callsInPlace(block, InvocationKind.EXACTLY_ONCE)
}
block()
}
@Suppress("NOTHING_TO_INLINE")
@kotlin.contracts.ExperimentalContracts
inline fun noinlineMe(noinline block: () -> Unit) {
contract {
callsInPlace(block, InvocationKind.EXACTLY_ONCE)
}
block()
}
@kotlin.contracts.ExperimentalContracts
fun notinline(block: () -> Unit) {
contract {
callsInPlace(block, InvocationKind.EXACTLY_ONCE)
}
block()
}
@kotlin.contracts.ExperimentalContracts
class Test {
val a: String
val b: String
val c: String
val d: String
init {
inlineMe {
a = "allowed"
}
crossinlineMe {
b = "not allowed"
}
noinlineMe {
c = "not allowed"
}
notinline {
d = "not allowed"
}
}
}
@kotlin.contracts.ExperimentalContracts
class Test1 {
val a: String = ""
val b: String = ""
val c: String = ""
val d: String = ""
init {
inlineMe {
<!VAL_REASSIGNMENT!>a<!> += "allowed"
}
crossinlineMe {
<!VAL_REASSIGNMENT!>b<!> += "not allowed"
}
noinlineMe {
<!VAL_REASSIGNMENT!>c<!> += "not allowed"
}
notinline {
<!VAL_REASSIGNMENT!>d<!> += "not allowed"
}
}
}
@kotlin.contracts.ExperimentalContracts
class Test2 {
val a: String = ""
val b: String = ""
val c: String = ""
val d: String = ""
init {
var blackhole = ""
inlineMe {
blackhole += a
}
crossinlineMe {
blackhole += b
}
noinlineMe {
blackhole += c
}
notinline {
blackhole += d
}
}
}
@kotlin.contracts.ExperimentalContracts
class Test4 {
val a: String = ""
val b: String = ""
val c: String = ""
val d: String = ""
init {
var blackhole: String
inlineMe {
blackhole = a
}
crossinlineMe {
blackhole = b
}
noinlineMe {
blackhole = c
}
notinline {
blackhole = d
}
}
}
@kotlin.contracts.ExperimentalContracts
class Test5 {
val a: String
val b: String
val c: String
val d: String
val aInit = inlineMe {
a = "OK"
}
val bInit = crossinlineMe {
b = "OK"
}
val cInit = noinlineMe {
c = "OK"
}
val dInit = notinline {
d = "OK"
}
}