[FIR-TEST] Add new testdata generated after changes in previous commit

This commit is contained in:
Dmitriy Novozhilov
2019-12-11 16:16:22 +03:00
parent e9c02a1cca
commit 2536fa0cd5
4578 changed files with 104067 additions and 1 deletions
@@ -0,0 +1,51 @@
// !LANGUAGE: +AllowContractsForCustomFunctions +UseCallsInPlaceEffect
// !USE_EXPERIMENTAL: kotlin.contracts.ExperimentalContracts
// !DIAGNOSTICS: -INVISIBLE_REFERENCE -INVISIBLE_MEMBER
import kotlin.contracts.*
inline fun <T> myRun(block: () -> T): T {
contract {
<!INAPPLICABLE_CANDIDATE!>callsInPlace<!>(block, InvocationKind.EXACTLY_ONCE)
}
return block()
}
fun getBoolean(): Boolean = false
fun test() {
val x: Int
if (getBoolean())
myRun {
while (getBoolean()) {
do {
myRun {
if (getBoolean()) {
x = 42 // No reassignment because of break
}
else {
x = 43 // No reassignment because of break
}
}
break
} while (getBoolean())
// Loop executed exectly once, initializing x
myRun { x.inc() }
myRun { x = 42 }
break
}
// x is I?D here because loop could've been execited
// VAL_REASSIGNMENT isn't reported because of repeating diagnostic
x = 42
// x is ID now
}
else
myRun {
x = 42
}
// x is ID because both branches are ID
x.inc()
}
@@ -0,0 +1,18 @@
// !LANGUAGE: +AllowContractsForCustomFunctions +UseCallsInPlaceEffect
// !USE_EXPERIMENTAL: kotlin.contracts.ExperimentalContracts
// !DIAGNOSTICS: -INVISIBLE_REFERENCE -INVISIBLE_MEMBER
import kotlin.contracts.*
inline fun <T> myRun(block: () -> T): T {
contract {
<!INAPPLICABLE_CANDIDATE!>callsInPlace<!>(block, InvocationKind.EXACTLY_ONCE)
}
return block()
}
fun functionWithExpressionBody(x: Int): Boolean = myRun {
if (x == 0) return true
if (x == 1) return false
return functionWithExpressionBody(x - 2)
}
@@ -0,0 +1,20 @@
// !LANGUAGE: +AllowContractsForCustomFunctions +UseCallsInPlaceEffect
// !USE_EXPERIMENTAL: kotlin.contracts.ExperimentalContracts
// !DIAGNOSTICS: -INVISIBLE_REFERENCE -INVISIBLE_MEMBER
import kotlin.contracts.*
fun <T> myRun(block: () -> T): T {
contract {
<!INAPPLICABLE_CANDIDATE!>callsInPlace<!>(block, InvocationKind.EXACTLY_ONCE)
}
return block()
}
fun functionWithSideEffects(x: Int): Int = x + 1 // ...and some other useful side-effects
fun log(s: String) = Unit // some logging or println or whatever returning Unit
fun implicitCastWithIf(s: String) {
myRun { if (s == "") functionWithSideEffects(42) else log(s) }
}
@@ -0,0 +1,17 @@
// !LANGUAGE: +AllowContractsForCustomFunctions +UseCallsInPlaceEffect
// !USE_EXPERIMENTAL: kotlin.contracts.ExperimentalContracts
// !DIAGNOSTICS: -INVISIBLE_REFERENCE -INVISIBLE_MEMBER
import kotlin.contracts.*
inline fun myRun(block: () -> Unit): Unit {
contract {
<!INAPPLICABLE_CANDIDATE!>callsInPlace<!>(block, InvocationKind.EXACTLY_ONCE)
}
block()
}
fun test() {
myRun { throw java.lang.<!UNRESOLVED_REFERENCE!>IllegalArgumentException<!>() }
val x: Int = 42
}
@@ -0,0 +1,23 @@
// !LANGUAGE: +AllowContractsForCustomFunctions +UseCallsInPlaceEffect
// !USE_EXPERIMENTAL: kotlin.contracts.ExperimentalContracts
// !DIAGNOSTICS: -INVISIBLE_REFERENCE -INVISIBLE_MEMBER
import kotlin.contracts.*
inline fun myRun(block: () -> Unit): Unit {
contract {
<!INAPPLICABLE_CANDIDATE!>callsInPlace<!>(block, InvocationKind.EXACTLY_ONCE)
}
return block()
}
inline fun unknownRun(block: () -> Unit) { block() }
fun foo() {
val x: Int
myRun {
unknownRun { println("shouldn't change anything") }
x = 42
}
println(x)
}
@@ -0,0 +1,86 @@
// !LANGUAGE: +AllowContractsForCustomFunctions +UseCallsInPlaceEffect
// !USE_EXPERIMENTAL: kotlin.contracts.ExperimentalContracts
// !DIAGNOSTICS: -INVISIBLE_REFERENCE -INVISIBLE_MEMBER
import kotlin.contracts.*
inline fun <T> myRun(block: () -> T): T {
contract {
<!INAPPLICABLE_CANDIDATE!>callsInPlace<!>(block, InvocationKind.EXACTLY_ONCE)
}
return block()
}
inline fun <T, R> T.myLet(block: (T) -> R): R {
contract {
<!INAPPLICABLE_CANDIDATE!>callsInPlace<!>(block, InvocationKind.EXACTLY_ONCE)
}
return block(this)
}
inline fun unknownRun(block: () -> Unit) = block()
fun getBool(): Boolean = false
fun threeLevelsReturnNoInitialization(x: Int?): Int? {
// Inner always jumps to outer
// And middle always calls inner
// So, in fact, middle never finished normally
// Hence 'y = 54' in middle is unreachable, and middle doesn't performs definite initialization
// Hence, outer doesn't performs definite initialization
val y: Int
myRun outer@ {
myRun middle@ {
x.myLet inner@ {
if (it == null) {
y = 42
return@outer Unit
}
else {
return@outer Unit
}
}
}
// Possible to report unreachable here
y = 54
}
return y.inc()
}
fun threeLevelsReturnWithInitialization(x: Int?): Int? {
val y: Int
myRun outer@ {
myRun middle@ {
x.myLet inner@ {
if (it == null) {
y = 42
return@outer Unit
}
else {
y = 34
return@outer Unit
}
}
}
}
return y.inc()
}
fun threeLevelsReturnWithUnknown(x: Int?): Int? {
val y: Int
myRun outer@ {
unknownRun middle@ {
x.myLet inner@ {
if (it == null) {
y = 42
return@outer Unit
}
else {
y = 34
return@outer Unit
}
}
}
}
return y.inc()
}
@@ -0,0 +1,98 @@
// !DIAGNOSTICS: -UNUSED_PARAMETER -INVISIBLE_MEMBER -INVISIBLE_REFERENCE
// !USE_EXPERIMENTAL: kotlin.contracts.ExperimentalContracts
// !LANGUAGE: +AllowContractsForCustomFunctions +UseCallsInPlaceEffect
import kotlin.contracts.*
inline fun <T> myRun(block: () -> T): T {
contract {
<!INAPPLICABLE_CANDIDATE!>callsInPlace<!>(block, InvocationKind.EXACTLY_ONCE)
}
return block()
}
fun innerComputation(): Int = 42
fun outerComputation(): Int = 52
fun log() = Unit
fun outerFinallyInitializes() {
val x: Int
try {
myRun {
try {
x = innerComputation()
} catch (e: java.lang.Exception) {
log()
}
// possible reassignment if innerComputation finished
x = 42
// x is ID here
}
// Definite reassignment here, cause can get here only if myRun finished
// Not reported because of repeating diagnostic
x = outerComputation()
} catch (e: java.lang.Exception) {
// can catch exception thrown by the inner, so x can be not initialized
x.inc()
log()
} finally {
// Possible reassignment (e.g. if everything finished)
// Not reported because of repeating diagnostic
x = 42
}
// Properly initialized
x.inc()
}
fun innerFinallyInitializes() {
val x: Int
try {
myRun {
try {
innerComputation()
} catch (e: java.lang.Exception) {
log()
} finally {
x = 42
}
}
// Properly initialized
x.inc()
} catch (e: java.lang.Exception) {
log()
}
// Still can be unitialized because we don't know what can happen in try-block
// (e.g., OutOfMemory exception could've happened even before myRun was executed)
x.inc()
}
fun innerFinallyInitializesOuterRethrows() {
val x: Int
try {
myRun {
try {
innerComputation()
} catch (e: java.lang.Exception) {
log()
} finally {
x = 42
}
}
// Properly initialized
x.inc()
} catch (e: java.lang.Exception) {
log()
throw e
}
// Guaranteed to be initialized because all catch-clauses are rethrowing
x.inc()
}
@@ -0,0 +1,47 @@
// !LANGUAGE: +AllowContractsForCustomFunctions +UseCallsInPlaceEffect
// !USE_EXPERIMENTAL: kotlin.contracts.ExperimentalContracts
// !DIAGNOSTICS: -INVISIBLE_REFERENCE -INVISIBLE_MEMBER
import kotlin.contracts.*
inline fun <T> myRun(block: () -> T): T {
contract {
<!INAPPLICABLE_CANDIDATE!>callsInPlace<!>(block, InvocationKind.EXACTLY_ONCE)
}
return block()
}
fun innerComputation(): Int = 42
fun outerComputation(): Int = 52
fun innerTryCatchInitializes() {
val x: Int
try {
myRun {
try {
x = innerComputation()
x.inc()
}
catch (e: java.lang.Exception) {
// Potential reassignment because x.inc() could threw
x = 42
x.inc()
}
}
// Can get here only when inlined lambda exited properly, i.e. x is initialized
x.inc()
outerComputation()
} catch (e: java.lang.Exception) {
// Can get here if innerComputation() threw an exception that wasn't catched by the inner catch (x is not initialized)
// OR if outerComputation() threw an exception (x is initialized because we reach outer computation only when inner finished ok)
// So, x=I? here
x.inc()
// Potential reasignment
x = 42
}
// Here x=I because outer try-catch either exited normally (x=I) or catched exception (x=I, with reassingment, though)
x.inc()
}
@@ -0,0 +1,18 @@
// !LANGUAGE: +AllowContractsForCustomFunctions +UseCallsInPlaceEffect
// !USE_EXPERIMENTAL: kotlin.contracts.ExperimentalContracts
// !DIAGNOSTICS: -INVISIBLE_REFERENCE -INVISIBLE_MEMBER
import kotlin.contracts.*
inline fun <T, R> T.myLet(block: (T) -> R): R {
contract {
<!INAPPLICABLE_CANDIDATE!>callsInPlace<!>(block, InvocationKind.EXACTLY_ONCE)
}
return block(this)
}
fun nonLocalReturnWithElvis(x: Int?): Int? {
x?.myLet { return 42 }
return x?.inc()
}
@@ -0,0 +1,34 @@
// !LANGUAGE: +AllowContractsForCustomFunctions +UseCallsInPlaceEffect
// !USE_EXPERIMENTAL: kotlin.contracts.ExperimentalContracts
// !DIAGNOSTICS: -INVISIBLE_REFERENCE -INVISIBLE_MEMBER
import kotlin.contracts.*
inline fun myRun(block: () -> Unit): Unit {
contract {
<!INAPPLICABLE_CANDIDATE!>callsInPlace<!>(block, InvocationKind.EXACTLY_ONCE)
}
return block()
}
fun getBool(): Boolean = false
fun withLabeledReturn() {
val y: Int
val x = myRun outer@ {
myRun { return@outer Unit }
y = 42
}
println(y)
println(x)
}
fun withLabeledReturn2(y: Int) {
myRun outer@ {
myRun { return@outer Unit }
println(y)
}
println(y)
}
@@ -0,0 +1,23 @@
// !USE_EXPERIMENTAL: kotlin.contracts.ExperimentalContracts
// !DIAGNOSTICS: -INVISIBLE_REFERENCE -INVISIBLE_MEMBER
import kotlin.contracts.*
inline fun <T> Any?.myRun(block: () -> T): T {
contract {
<!INAPPLICABLE_CANDIDATE!>callsInPlace<!>(block, InvocationKind.EXACTLY_ONCE)
}
return block()
}
fun bad(): String {
val x: String? = null
x?.myRun { return "" }
}
fun ok(): String {
val x: String? = null
x?.run { return "non-null" } ?: return "null"
}
@@ -0,0 +1,79 @@
// !LANGUAGE: +AllowContractsForCustomFunctions +UseCallsInPlaceEffect
// !USE_EXPERIMENTAL: kotlin.contracts.ExperimentalContracts
// !DIAGNOSTICS: -INVISIBLE_REFERENCE -INVISIBLE_MEMBER
import kotlin.contracts.*
inline fun <T> myRun(block: () -> T): T {
contract {
<!INAPPLICABLE_CANDIDATE!>callsInPlace<!>(block, InvocationKind.EXACTLY_ONCE)
}
return block()
}
fun exitOnlyThroughLocalReturns(b: Boolean) {
var x: Int
var s: String
myRun {
if (b) {
x = 42
return@myRun
}
if (!b) {
s = "hello"
x = 42
return@myRun
}
else {
s = "world"
x = 239
}
}
x.inc()
s.length
}
fun exitOnlyThroughNonLocalReturns(b: Boolean?) {
var x: Int
var s: String
myRun {
if (b == null) {
x = 42
return
}
if (b.not()) {
x = 54
}
if (x == 42) {
return
}
else {
x = 42
s = "hello"
return
}
}
x.inc()
s.length
}
fun nonLocalReturnAndOrdinaryExit(b: Boolean) {
var x: Int
var s: String
myRun {
if (b) {
x = 42
return
}
x = 54
s = "hello"
}
x.inc()
s.length
}
@@ -0,0 +1,48 @@
// !LANGUAGE: +AllowContractsForCustomFunctions +UseCallsInPlaceEffect
// !USE_EXPERIMENTAL: kotlin.contracts.ExperimentalContracts
// !DIAGNOSTICS: -INVISIBLE_REFERENCE -INVISIBLE_MEMBER
import kotlin.contracts.*
inline fun myRun(block: () -> Unit) {
contract {
<!INAPPLICABLE_CANDIDATE!>callsInPlace<!>(block, InvocationKind.EXACTLY_ONCE)
}
block()
}
inline fun <T> unknownRun(block: () -> T): T = block()
fun throwIfNotCalled() {
val x: Int
myRun outer@ {
unknownRun {
myRun {
x = 42
return@outer
}
}
throw java.lang.<!UNRESOLVED_REFERENCE!>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 {
x = 42
return@outer
}
}
throw java.lang.<!UNRESOLVED_REFERENCE!>IllegalArgumentException<!>()
}
} catch (ignored: java.lang.IllegalArgumentException) { }
// x *isn't* initialized here!
println(x)
}
@@ -0,0 +1,58 @@
// !LANGUAGE: +AllowContractsForCustomFunctions +UseCallsInPlaceEffect
// !USE_EXPERIMENTAL: kotlin.contracts.ExperimentalContracts
// !DIAGNOSTICS: -INVISIBLE_REFERENCE -INVISIBLE_MEMBER
import kotlin.contracts.*
fun <T> myRun(block: () -> T): T {
contract {
<!INAPPLICABLE_CANDIDATE!>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) {
x.inc()
}
}
x = 42
x.inc()
}
fun possibleReassignmentInTryCatch() {
val x: Int
myRun {
try {
x = someComputation()
x.inc()
}
catch (e: java.lang.Exception) {
x = 42
x.inc()
}
x.inc()
}
x.inc()
}
fun tryCatchOuter() {
val x: Int
try {
myRun { x = someComputation() }
x.inc()
}
catch (e: java.lang.Exception) {
x.inc()
}
}
@@ -0,0 +1,34 @@
// !LANGUAGE: +AllowContractsForCustomFunctions +UseCallsInPlaceEffect
// !USE_EXPERIMENTAL: kotlin.contracts.ExperimentalContracts
// !DIAGNOSTICS: -INVISIBLE_REFERENCE -INVISIBLE_MEMBER -UNUSED_PARAMETER
import kotlin.contracts.*
inline fun <T> myRun(block: () -> T): T {
contract {
<!INAPPLICABLE_CANDIDATE!>callsInPlace<!>(block, InvocationKind.EXACTLY_ONCE)
}
return block()
}
fun someComputation(): Int = 42
fun report(x: Int) = Unit
fun innerTryCatchFinally() {
val x: Int
myRun {
try {
x = someComputation()
report(x)
} catch (e: java.lang.Exception) {
x = 42
report(x)
} finally {
x = 0
}
}
x.inc()
}
@@ -0,0 +1,19 @@
// !LANGUAGE: +AllowContractsForCustomFunctions +UseCallsInPlaceEffect
// !USE_EXPERIMENTAL: kotlin.contracts.ExperimentalContracts
// !DIAGNOSTICS: -INVISIBLE_REFERENCE -INVISIBLE_MEMBER
import kotlin.contracts.*
fun <T> myRun(block: () -> T): T {
contract {
<!INAPPLICABLE_CANDIDATE!>callsInPlace<!>(block, InvocationKind.EXACTLY_ONCE)
}
return block()
}
fun foo(x: Int): Int = x + 1
fun typeMismatchInLambda(y: String): Int {
val x = myRun { <!INAPPLICABLE_CANDIDATE!>foo<!>(y) }
return x
}
@@ -0,0 +1,17 @@
// !LANGUAGE: +AllowContractsForCustomFunctions +UseCallsInPlaceEffect
// !USE_EXPERIMENTAL: kotlin.contracts.ExperimentalContracts
// !DIAGNOSTICS: -INVISIBLE_REFERENCE -INVISIBLE_MEMBER
import kotlin.contracts.*
inline fun <T> myRun(block: () -> T): T {
contract {
<!INAPPLICABLE_CANDIDATE!>callsInPlace<!>(block, InvocationKind.EXACTLY_ONCE)
}
return block()
}
fun throwInLambda(): Int {
val x = myRun { throw java.lang.<!UNRESOLVED_REFERENCE!>IllegalArgumentException<!>(); 42 }
return x
}
@@ -0,0 +1,75 @@
// !LANGUAGE: +AllowContractsForCustomFunctions +UseCallsInPlaceEffect
// !USE_EXPERIMENTAL: kotlin.contracts.ExperimentalContracts
// !DIAGNOSTICS: -INVISIBLE_REFERENCE -INVISIBLE_MEMBER
import kotlin.contracts.*
fun <T> runTwice(block: () -> T): T {
contract {
<!INAPPLICABLE_CANDIDATE!>callsInPlace<!>(block, InvocationKind.AT_LEAST_ONCE)
}
block()
return block();
};
fun <T> runOnce(block: () -> T): T {
contract {
<!INAPPLICABLE_CANDIDATE!>callsInPlace<!>(block, InvocationKind.EXACTLY_ONCE)
}
return block();
};
fun valueReassignment() {
val x: Int
x.inc()
runTwice { x = 42 }
x.inc()
}
fun shadowing() {
val x: Int
runTwice { val x: Int; x = 42; x.inc() }
x.inc()
}
fun branchingFlow(a: Any?) {
val x: Int
x.inc()
if (a is String) {
runTwice { x = 42 }
}
else {
x = 43
}
x.inc()
}
fun branchingFlowWithMissingBranches(a: Any?) {
val x: Int
if (a is String) {
runTwice { x = 42 }
}
x.inc()
}
fun repeatingFlow(n: Int) {
val x: Int
x.inc()
for (i in 1..n) {
runTwice { x = 42 }
}
x.inc()
}
fun repeatingFlow2(n: Int) {
val x: Int
for (i in 1..n) {
runTwice { x = 42 }
}
x.inc()
}
@@ -0,0 +1,31 @@
// !LANGUAGE: +AllowContractsForCustomFunctions +UseCallsInPlaceEffect
// !USE_EXPERIMENTAL: kotlin.contracts.ExperimentalContracts
// !DIAGNOSTICS: -INVISIBLE_REFERENCE -INVISIBLE_MEMBER
import kotlin.contracts.*
fun <T> runTwice(block: () -> T): T {
contract {
<!INAPPLICABLE_CANDIDATE!>callsInPlace<!>(block, InvocationKind.AT_LEAST_ONCE)
}
block()
return block();
};
fun testInitialization() {
var x: Int
x.inc()
runTwice { x = 42 }
x.inc()
x = 43
x.inc()
}
fun repeatingFlow(n: Int) {
var x: Int
for (i in 1..n) {
runTwice { x = 42 }
}
x.inc()
}
@@ -0,0 +1,29 @@
// !LANGUAGE: +AllowContractsForCustomFunctions +UseCallsInPlaceEffect
// !USE_EXPERIMENTAL: kotlin.contracts.ExperimentalContracts
// !DIAGNOSTICS: -INVISIBLE_REFERENCE -INVISIBLE_MEMBER
import kotlin.contracts.*
fun <T> runTwice(block: () -> T): T {
contract {
<!INAPPLICABLE_CANDIDATE!>callsInPlace<!>(block, InvocationKind.AT_LEAST_ONCE)
}
block()
return block();
};
fun <T> funWithUnknownInvocations(block: () -> T) = block()
fun indefiniteFlow() {
var x: Int
funWithUnknownInvocations { runTwice { x = 42 } }
x.inc()
}
fun shadowing() {
var x: Int
runTwice { val x: Int; x = 42; x.inc() }
x.inc()
}
@@ -0,0 +1,92 @@
// !LANGUAGE: +AllowContractsForCustomFunctions +UseCallsInPlaceEffect
// !USE_EXPERIMENTAL: kotlin.contracts.ExperimentalContracts
// !DIAGNOSTICS: -INVISIBLE_REFERENCE -INVISIBLE_MEMBER
import kotlin.contracts.*
fun <T> myRun(block: () -> T): T {
contract {
<!INAPPLICABLE_CANDIDATE!>callsInPlace<!>(block, InvocationKind.EXACTLY_ONCE)
}
return block()
}
fun initialization() {
val x: Int
myRun {
x = 42
42
}
x.inc()
}
fun shadowing() {
val x = 42
myRun {
val x = 43
x.inc()
}
x.inc()
}
fun nestedDefiniteAssignment() {
val x: Int
myRun {
val y = "Hello"
myRun {
x = 42
}
y.length
}
x.inc()
}
fun deeplyNestedDefiniteAssignment() {
val x: Int
myRun {
val y: String
myRun {
val z: String
myRun {
z = "Hello"
y = "World"
x = 42
}
z.length
}
y.length
}
x.inc()
}
fun branchingFlow(a: Any?) {
val x: Int
if (a is String) {
myRun { x = 42 }
}
else {
myRun { x = 43 }
}
x.inc()
}
fun returningValue() {
val x: Int
val hello = myRun { x = 42; "hello" }
x.inc()
hello.length
}
fun unknownRun(block: () -> Unit) = block()
class DefiniteInitializationInInitSection {
val x: Int
val y: Int
init {
myRun { x = 42 }
unknownRun { y = 239 }
}
}
@@ -0,0 +1,28 @@
// !LANGUAGE: +AllowContractsForCustomFunctions +UseCallsInPlaceEffect
// !USE_EXPERIMENTAL: kotlin.contracts.ExperimentalContracts
// !DIAGNOSTICS: -INVISIBLE_REFERENCE -INVISIBLE_MEMBER
import kotlin.contracts.*
fun <T> myRun(block: () -> T): T {
contract {
<!INAPPLICABLE_CANDIDATE!>callsInPlace<!>(block, InvocationKind.EXACTLY_ONCE)
}
return block()
}
fun reassignmentInUsualFlow() {
val x: Int
myRun { x = 42 }
x = 43
x.inc()
}
fun reassignment() {
val x = 42
myRun {
x = 43
}
x.inc()
}
@@ -0,0 +1,68 @@
// !LANGUAGE: +AllowContractsForCustomFunctions +UseCallsInPlaceEffect
// !USE_EXPERIMENTAL: kotlin.contracts.ExperimentalContracts
// !DIAGNOSTICS: -INVISIBLE_REFERENCE -INVISIBLE_MEMBER
import kotlin.contracts.*
fun <T> myRun(block: () -> T): T {
contract {
<!INAPPLICABLE_CANDIDATE!>callsInPlace<!>(block, InvocationKind.EXACTLY_ONCE)
}
return block()
}
fun myRepeat(n: Int, action: () -> Unit) {
contract {
<!INAPPLICABLE_CANDIDATE!>callsInPlace<!>(action)
}
for (i in 1..n) action()
}
fun branchingIndetermineFlow(a: Any?) {
val x: Int
if (a is String) {
myRepeat(a.length) {
// Val reassignment because we know that repeat's lambda called in-place
myRun { x = 42 }
}
}
else {
myRun { x = 43 }
}
x.inc()
}
fun nonAnonymousLambdas() {
val x: Int
val initializer = { x = 42 }
myRun(initializer)
x.inc()
}
fun multipleAssignments() {
val x: Int
myRepeat(42) {
// Val reassignment because we know that repeat's lambda called in-place
myRun { x = 42 }
}
x.inc()
}
fun funWithUnknownInvocations(block: () -> Unit) = block()
fun nestedIndefiniteAssignment() {
val x: Int
// Captured val initialization reported, because we don't know anything about funWithUnknownInvocations
funWithUnknownInvocations { myRun { x = 42 } }
x.inc()
}
class InitializationForbiddenInNonInitSection {
val x: Int
fun setup() {
myRun { x = 42 }
}
}
@@ -0,0 +1,42 @@
// !LANGUAGE: +AllowContractsForCustomFunctions +UseCallsInPlaceEffect
// !USE_EXPERIMENTAL: kotlin.contracts.ExperimentalContracts
// !DIAGNOSTICS: -INVISIBLE_REFERENCE -INVISIBLE_MEMBER
import kotlin.contracts.*
fun <T> myRun(block: () -> T): T {
contract {
<!INAPPLICABLE_CANDIDATE!>callsInPlace<!>(block, InvocationKind.EXACTLY_ONCE)
}
return block()
}
fun definiteVarInitialization() {
var x: Int
myRun { x = 42 }
x.inc()
}
fun definiteVarReassignment() {
var x: Int
myRun { x = 42 }
x.inc()
myRun { x = 43 }
x.inc()
x = 44
x.inc()
}
fun nestedVarInitialization() {
var x: Int
myRun { myRun { myRun { x = 42 } } }
x.inc()
myRun { myRun { myRun { x = 42 } } }
}
fun notAnExpression() {
var x: Int = 0
myRun { if (true) x = 42 }
x.inc()
}
@@ -0,0 +1,51 @@
// !LANGUAGE: +AllowContractsForCustomFunctions +UseCallsInPlaceEffect
// !USE_EXPERIMENTAL: kotlin.contracts.ExperimentalContracts
// !DIAGNOSTICS: -INVISIBLE_REFERENCE -INVISIBLE_MEMBER
import kotlin.contracts.*
fun <T> myRun(block: () -> T): T {
contract {
<!INAPPLICABLE_CANDIDATE!>callsInPlace<!>(block, InvocationKind.EXACTLY_ONCE)
}
return block()
}
fun indefiniteVarReassignment(n: Int) {
var x: Int
repeat(n) {
myRun { x = 42 }
}
x.inc()
}
fun nonAnonymousLambdas() {
// Named lambdas are not inlined, even in theory it could be done for some simple cases as this one
var x: Int
val initializer = { x = 42 }
myRun(initializer)
x.inc()
}
fun branchingIndetermineFlow(a: Any) {
var x: Int
if (a is String) {
repeat(a.length) {
myRun { x = 42 }
}
}
else {
myRun { x = 43 }
}
x.inc()
}
fun funWithUnknownInvocations(block: () -> Unit) = block()
fun nestedIndefiniteAssignment() {
val x: Int
funWithUnknownInvocations { myRun { x = 42 } }
x.inc()
}
@@ -0,0 +1,28 @@
// !LANGUAGE: +AllowContractsForCustomFunctions +UseCallsInPlaceEffect
// !USE_EXPERIMENTAL: kotlin.contracts.ExperimentalContracts
// !DIAGNOSTICS: -INVISIBLE_REFERENCE -INVISIBLE_MEMBER
import kotlin.contracts.*
fun <T, R> T.myLet(block: (T) -> R): R {
contract {
<!INAPPLICABLE_CANDIDATE!>callsInPlace<!>(block, InvocationKind.EXACTLY_ONCE)
}
return block(this)
}
fun initializationWithReceiver(y: String) {
val x: Int
y.myLet { x = 42 }
x.inc()
}
fun initializationWithSafeCall(y: String?) {
val x: Int
y?.myLet { x = 42 }
x.inc()
}
fun sanityCheck(x: Int, y: String): Int {
y.let { return x }
}
@@ -0,0 +1,18 @@
// !LANGUAGE: +AllowContractsForCustomFunctions +UseCallsInPlaceEffect
// !USE_EXPERIMENTAL: kotlin.contracts.ExperimentalContracts
// !DIAGNOSTICS: -INVISIBLE_REFERENCE -INVISIBLE_MEMBER
import kotlin.contracts.*
fun <T> inPlace(block: () -> T): T {
contract {
<!INAPPLICABLE_CANDIDATE!>callsInPlace<!>(block)
}
return block()
}
fun reassignmentAndNoInitializaiton() {
val x: Int
inPlace { x = 42 }
x.inc()
}