Files
kotlin-fork/compiler/testData/diagnostics/testsWithStdLib/contracts/fromStdlib/fromStandardKt.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

110 lines
1.8 KiB
Kotlin
Vendored

// !LANGUAGE: +ReadDeserializedContracts +UseCallsInPlaceEffect
// !DIAGNOSTICS: -INVISIBLE_REFERENCE -INVISIBLE_MEMBER
fun testRunWithUnitReturn() {
val x: Int
run { x = 42 }
println(x)
}
fun testRunWithReturnValue() {
val x: Int
val y = run {
x = 42
"hello"
}
println(x)
println(y)
}
fun testRunWithCoercionToUnit() {
val <!ASSIGNED_BUT_NEVER_ACCESSED_VARIABLE!>x<!>: Int
run {
x = 42
"hello"
}
}
fun testRunWithReceiver(x: Int) {
val s: String
x.run {
s = this.toString()
}
println(s)
}
fun testWith(x: Int) {
val s: String
with(x) {
s = toString()
}
println(s)
}
fun testApply(x: Int) {
val y: Int
val z: Int = x.apply { y = 42 }
println(y)
println(z)
}
fun testAlso(x: Int) {
val y: Int
x.also { y = it + 1 }
println(y)
}
fun testLet(x: Int) {
val z: Int
val y: String = x.let {
z = 42
(it + 1).toString()
}
println(z)
println(y)
}
fun testTakeIf(x: Int?) {
val y: Int
x.takeIf {
y = 42
it != null
}
println(y)
}
fun testTakeUnless(x: Int?) {
val y: Int
x.takeIf {
y = 42
it != null
}
println(y)
}
fun testRepeatOnVal(x: Int) {
val y: Int
repeat(x) {
// reassignment instead of captured val initalization
<!VAL_REASSIGNMENT!>y<!> = 42
}
println(<!UNINITIALIZED_VARIABLE!>y<!>)
}
fun testRepeatOnVar(x: Int) {
var y: Int
repeat(x) {
// no reassignment reported
y = 42
}
// but here we still unsure if 'y' was initialized
println(<!UNINITIALIZED_VARIABLE!>y<!>)
}
fun testRepeatOnInitializedVar(x: Int) {
var y: Int = 24
repeat(x) {
y = 42
}
println(y)
}