Add spec tests for contracts

This commit is contained in:
victor.petukhov
2018-08-28 19:09:27 +03:00
parent ecf8b88c4c
commit 8d91e5998d
144 changed files with 10906 additions and 0 deletions
@@ -0,0 +1,78 @@
// !LANGUAGE: +AllowContractsForCustomFunctions +UseCallsInPlaceEffect
// !USE_EXPERIMENTAL: kotlin.contracts.ExperimentalContracts
// !WITH_CONTRACT_FUNCTIONS
// SKIP_TXT
/*
KOTLIN DIAGNOSTICS NOT LINKED SPEC TEST (NEGATIVE)
SECTION: contracts
CATEGORY: analysis, controlFlow, initialization
NUMBER: 1
DESCRIPTION: val/var reassignment and/or uninitialized variable usages based on CallsInPlace effect with wrong invocation kind
*/
fun case_1() {
val value_1: Int
funWithAtLeastOnceCallsInPlace { <!VAL_REASSIGNMENT!>value_1<!> = 10 }
value_1.inc()
}
fun case_2() {
val value_1: Int
funWithAtMostOnceCallsInPlace { value_1 = 10 }
<!UNINITIALIZED_VARIABLE!>value_1<!>.inc()
}
fun case_3() {
val value_1: Int
funWithUnknownCallsInPlace { <!VAL_REASSIGNMENT!>value_1<!> = 10 }
<!UNINITIALIZED_VARIABLE!>value_1<!>.inc()
}
fun case_4() {
var value_1: Int
var value_2: Int
funWithAtMostOnceCallsInPlace { value_1 = 10 }
funWithUnknownCallsInPlace { value_2 = 10 }
<!UNINITIALIZED_VARIABLE!>value_1<!>.dec()
<!UNINITIALIZED_VARIABLE!>value_2<!>.div(10)
}
class case_5 {
<!MUST_BE_INITIALIZED_OR_BE_ABSTRACT!>val value_1: Int<!>
<!MUST_BE_INITIALIZED_OR_BE_ABSTRACT!>val value_2: Int<!>
val value_3: Int
<!MUST_BE_INITIALIZED_OR_BE_ABSTRACT!>var value_4: Int<!>
<!MUST_BE_INITIALIZED_OR_BE_ABSTRACT!>var value_5: Int<!>
init {
funWithAtMostOnceCallsInPlace { value_1 = 1 }
funWithUnknownCallsInPlace { <!VAL_REASSIGNMENT!>value_2<!> = 1 }
funWithAtLeastOnceCallsInPlace { <!VAL_REASSIGNMENT!>value_3<!> = 1 }
funWithAtMostOnceCallsInPlace { value_4 = 2 }
funWithUnknownCallsInPlace { value_5 = 3 }
}
}
fun case_6() {
val value_1: Int
for (i in 0..1)
funWithExactlyOnceCallsInPlace { <!VAL_REASSIGNMENT!>value_1<!> = 10 }
<!UNINITIALIZED_VARIABLE!>value_1<!>.dec()
}
fun case_7() {
var value_1: Int
var i = 0
while (i < 10) {
funWithExactlyOnceCallsInPlace { value_1 = 10 }
i++
}
<!UNINITIALIZED_VARIABLE!>value_1<!>.dec()
}
fun case_8() {
var value_1: Int
if (true) funWithAtLeastOnceCallsInPlace { value_1 = 10 }
<!UNINITIALIZED_VARIABLE!>value_1<!>.dec()
}
@@ -0,0 +1,84 @@
// !LANGUAGE: +AllowContractsForCustomFunctions +UseCallsInPlaceEffect
// !USE_EXPERIMENTAL: kotlin.contracts.ExperimentalContracts
// !WITH_CONTRACT_FUNCTIONS
// SKIP_TXT
/*
KOTLIN DIAGNOSTICS NOT LINKED SPEC TEST (NEGATIVE)
SECTION: contracts
CATEGORY: analysis, controlFlow, initialization
NUMBER: 2
DESCRIPTION: val/var reassignment and/or uninitialized variable usages based on nested CallsInPlace effects with wrong invocation kind
*/
fun case_1() {
val value_1: Int
funWithAtLeastOnceCallsInPlace {
funWithAtMostOnceCallsInPlace {
<!VAL_REASSIGNMENT!>value_1<!> = 1
funWithExactlyOnceCallsInPlace {
value_1.inc()
}
}
funWithExactlyOnceCallsInPlace {
<!UNINITIALIZED_VARIABLE!>value_1<!>.inc()
}
value_1.inc()
}
value_1.inc()
}
fun case_2() {
val value_1: Int
funWithAtMostOnceCallsInPlace {
funWithAtMostOnceCallsInPlace {
value_1 = 1
}
funWithAtLeastOnceCallsInPlace {
<!UNINITIALIZED_VARIABLE!>value_1<!>.inc()
}
funWithUnknownCallsInPlace {
value_1.inc()
}
value_1.inc()
}
value_1.inc()
}
fun case_3() {
var value_1: Int
funWithAtLeastOnceCallsInPlace {
funWithAtMostOnceCallsInPlace {
value_1 = 1
funWithExactlyOnceCallsInPlace {
value_1.inc()
}
}
funWithExactlyOnceCallsInPlace {
<!UNINITIALIZED_VARIABLE!>value_1<!>.inc()
}
value_1.inc()
}
value_1.inc()
}
fun case_4() {
var value_1: Int
funWithAtLeastOnceCallsInPlace {
funWithUnknownCallsInPlace {
value_1 = 1
}
funWithAtLeastOnceCallsInPlace {
<!UNINITIALIZED_VARIABLE!>value_1<!>.inc()
}
funWithUnknownCallsInPlace {
value_1.inc()
}
funWithExactlyOnceCallsInPlace {
value_1.inc()
}
value_1.inc()
}
value_1.inc()
}
@@ -0,0 +1,176 @@
// !LANGUAGE: +AllowContractsForCustomFunctions +UseCallsInPlaceEffect
// !USE_EXPERIMENTAL: kotlin.contracts.ExperimentalContracts
// !WITH_CONTRACT_FUNCTIONS
// !WITH_ENUM_CLASSES
// SKIP_TXT
/*
KOTLIN DIAGNOSTICS NOT LINKED SPEC TEST (NEGATIVE)
SECTION: contracts
CATEGORY: analysis, controlFlow, initialization
NUMBER: 3
DESCRIPTION: val/var reassignment and/or uninitialized variable usages with compelx control flow inside/outside lambda of contract function with CallsInPlace effect
*/
fun case_1(value_1: _EnumClass?) {
val value_2: Int
<!NON_EXHAUSTIVE_WHEN!>when<!> (value_1) {
_EnumClass.NORTH -> funWithExactlyOnceCallsInPlace { value_2 = 1 }
_EnumClass.SOUTH -> funWithExactlyOnceCallsInPlace { value_2 = 2 }
_EnumClass.EAST -> funWithExactlyOnceCallsInPlace { value_2 = 4 }
null -> funWithExactlyOnceCallsInPlace { value_2 = 5 }
}
<!UNINITIALIZED_VARIABLE!>value_2<!>.inc()
}
fun case_2(value_1: Any?) {
val value_2: Int
funWithAtMostOnceCallsInPlace {
if (value_1 is String) {
value_2 = 0
} else if (value_1 == null) {
value_2 = 1
} else {
funWithAtMostOnceCallsInPlace { value_2 = 2 }
}
<!UNINITIALIZED_VARIABLE!>value_2<!>.dec()
}
value_2.dec()
}
class case_3(value_1: Any?) {
<!MUST_BE_INITIALIZED_OR_BE_ABSTRACT!>var value_2: Int<!>
init {
if (value_1 is String) {
funWithUnknownCallsInPlace { value_2 = 0 }
<!UNINITIALIZED_VARIABLE!>value_2<!>.div(10)
} else if (value_1 == null) {
funWithAtLeastOnceCallsInPlace { value_2 = 1 }
value_2.div(10)
} else {
value_2 = 2
}
<!UNINITIALIZED_VARIABLE!>value_2<!>.div(10)
}
}
fun case_4(value_1: _EnumClassSingle?) {
var value_2: Int
funWithAtMostOnceCallsInPlace {
when (value_1) {
_EnumClassSingle.EVERYTHING -> {
funWithExactlyOnceCallsInPlace { value_2 = 1 }
++value_2
}
null -> {
funWithUnknownCallsInPlace { value_2 = 2 }
}
}
<!UNINITIALIZED_VARIABLE!>value_2<!>.minus(5)
}
value_2.minus(5)
}
fun case_5() {
var value_2: Int
try {
funWithAtLeastOnceCallsInPlace { value_2 = 10 }
} catch (e: Exception) {
funWithAtMostOnceCallsInPlace { value_2 = 1 }
}
<!UNINITIALIZED_VARIABLE!>value_2<!>++
}
fun case_6() {
var value_2: Int
try {
funWithAtLeastOnceCallsInPlace { value_2 = 10 }
} catch (e: Exception) {
throw Exception()
} finally {
println(<!UNINITIALIZED_VARIABLE!>value_2<!>.inc())
}
value_2++
}
fun case_7() {
var value_1: Int
try {
funWithAtLeastOnceCallsInPlace { value_1 = 10 }
} catch (e: Exception) {
try {
funWithAtLeastOnceCallsInPlace { value_1 = 10 }
} catch (e: Exception) {
funWithAtMostOnceCallsInPlace { value_1 = 10 }
}
}
println(<!UNINITIALIZED_VARIABLE!>value_1<!>.inc())
}
fun case_8() {
val x: Int
funWithExactlyOnceCallsInPlace outer@ {
funWithAtMostOnceCallsInPlace {
funWithExactlyOnceCallsInPlace {
x = 42
return@outer
}
}
throw Exception()
}
println(x.inc())
}
fun case_9() {
val x: Int
funWithExactlyOnceCallsInPlace outer@ {
funWithAtMostOnceCallsInPlace {
funWithUnknownCallsInPlace {
<!VAL_REASSIGNMENT!>x<!> = 42
}
return@outer
}
throw Exception()
}
println(<!UNINITIALIZED_VARIABLE!>x<!>.inc())
}
fun case_10() {
val x: Int
funWithExactlyOnceCallsInPlace outer@ {
funWithAtMostOnceCallsInPlace {
x = 42
return@outer
}
}
println(<!UNINITIALIZED_VARIABLE!>x<!>.inc())
}
fun case_11() {
var x: Int
funWithAtLeastOnceCallsInPlace outer@ {
funWithAtMostOnceCallsInPlace {
x = 41
return@outer
}
funWithUnknownCallsInPlace {
x = 42
return@outer
}
return@outer
}
println(<!UNINITIALIZED_VARIABLE!>x<!>.inc())
}
@@ -0,0 +1,91 @@
// !LANGUAGE: +AllowContractsForCustomFunctions +UseCallsInPlaceEffect
// !USE_EXPERIMENTAL: kotlin.contracts.ExperimentalContracts
// !WITH_CONTRACT_FUNCTIONS
// SKIP_TXT
/*
KOTLIN DIAGNOSTICS NOT LINKED SPEC TEST (NEGATIVE)
SECTION: contracts
CATEGORY: analysis, controlFlow, initialization
NUMBER: 4
DESCRIPTION: CallsInPlace contract functions with name shadowing and wrong invocation kind
*/
fun case_1() {
val value_1: Int
funWithExactlyOnceCallsInPlace {
val <!NAME_SHADOWING!>value_1<!> = 10
value_1.inc()
}
<!UNINITIALIZED_VARIABLE!>value_1<!>.inc()
}
fun case_2() {
val value_1: Int
funWithExactlyOnceCallsInPlace {
val <!NAME_SHADOWING!>value_1<!>: Int
funWithExactlyOnceCallsInPlace {
value_1 = 10
}
funWithAtLeastOnceCallsInPlace {
value_1.inc()
}
value_1.inc()
}
<!UNINITIALIZED_VARIABLE!>value_1<!>.inc()
}
fun case_3() {
val value_1: Int
funWithAtLeastOnceCallsInPlace {
val <!NAME_SHADOWING!>value_1<!>: Int
funWithAtMostOnceCallsInPlace {
value_1 = 10
}
funWithAtMostOnceCallsInPlace {
<!UNINITIALIZED_VARIABLE!>value_1<!>.inc()
}
value_1.inc()
}
funWithAtMostOnceCallsInPlace {
value_1 = 10
}
<!UNINITIALIZED_VARIABLE!>value_1<!>.inc()
}
fun case_6() {
var value_1: Int
funWithAtLeastOnceCallsInPlace {
val <!NAME_SHADOWING!>value_1<!>: Int
funWithExactlyOnceCallsInPlace {
value_1 = 10
}
funWithAtMostOnceCallsInPlace {
value_1.inc()
}
value_1.inc()
}
funWithAtMostOnceCallsInPlace {
value_1 = 1
}
<!UNINITIALIZED_VARIABLE!>value_1<!>.dec()
}
fun case_7() {
val value_1: Int
funWithUnknownCallsInPlace {
var <!NAME_SHADOWING!>value_1<!>: Int
funWithAtLeastOnceCallsInPlace {
value_1 = 10
}
funWithUnknownCallsInPlace {
value_1.inc()
}
value_1.inc()
}
funWithUnknownCallsInPlace {
<!VAL_REASSIGNMENT!>value_1<!> = 1
}
<!UNINITIALIZED_VARIABLE!>value_1<!>.dec()
}
@@ -0,0 +1,48 @@
// !LANGUAGE: +AllowContractsForCustomFunctions +UseCallsInPlaceEffect
// !USE_EXPERIMENTAL: kotlin.contracts.ExperimentalContracts
// !WITH_CONTRACT_FUNCTIONS
// SKIP_TXT
/*
KOTLIN DIAGNOSTICS NOT LINKED SPEC TEST (NEGATIVE)
SECTION: contracts
CATEGORY: analysis, controlFlow, initialization
NUMBER: 5
DESCRIPTION: CallsInPlace contract functions with invalid lambda passing to function parameter.
*/
fun case_1() {
val value_1: Int
funWithExactlyOnceCallsInPlace({ <!CAPTURED_VAL_INITIALIZATION!>value_1<!> = 10 })
<!UNINITIALIZED_VARIABLE!>value_1<!>.inc()
}
fun case_2() {
var value_1: Int
val l = { value_1 = 10 }
funWithAtLeastOnceCallsInPlace(l)
<!UNINITIALIZED_VARIABLE!>value_1<!>.inc()
}
fun case_3() {
var value_1: Int
val l = fun () { value_1 = 10 }
funWithAtLeastOnceCallsInPlace(l)
<!UNINITIALIZED_VARIABLE!>value_1<!>.inc()
}
fun case_4() {
var value_1: Int
funWithAtLeastOnceCallsInPlace(fun () { value_1 = 10 })
<!UNINITIALIZED_VARIABLE!>value_1<!>.inc()
}
fun case_5() {
val value_1: Int
val o = object {
fun l() { <!CAPTURED_VAL_INITIALIZATION!>value_1<!> = 10 }
}
funWithExactlyOnceCallsInPlace(o::l)
<!UNINITIALIZED_VARIABLE!>value_1<!>.inc()
}
@@ -0,0 +1,27 @@
// !LANGUAGE: +AllowContractsForCustomFunctions +UseCallsInPlaceEffect
// !USE_EXPERIMENTAL: kotlin.contracts.ExperimentalContracts
// !WITH_CONTRACT_FUNCTIONS
// SKIP_TXT
/*
KOTLIN DIAGNOSTICS NOT LINKED SPEC TEST (NEGATIVE)
SECTION: contracts
CATEGORY: analysis, controlFlow, initialization
NUMBER: 6
DESCRIPTION: Check the lack of CallsInPlace effect on the lambda in the parentheses.
UNEXPECTED BEHAVIOUR
ISSUES: KT-26229
*/
fun case_1() {
val value_1: Int
funWithExactlyOnceCallsInPlace({ <!CAPTURED_VAL_INITIALIZATION!>value_1<!> = 11 })
<!UNINITIALIZED_VARIABLE!>value_1<!>.inc()
}
fun case_2() {
var value_1: Int
funWithAtLeastOnceCallsInPlace({ value_1 = 11 })
<!UNINITIALIZED_VARIABLE!>value_1<!>.inc()
}
@@ -0,0 +1,39 @@
// !LANGUAGE: +AllowContractsForCustomFunctions +UseCallsInPlaceEffect
// !USE_EXPERIMENTAL: kotlin.contracts.ExperimentalContracts
// !WITH_CONTRACT_FUNCTIONS
// SKIP_TXT
/*
KOTLIN DIAGNOSTICS NOT LINKED SPEC TEST (POSITIVE)
SECTION: contracts
CATEGORY: analysis, controlFlow, initialization
NUMBER: 1
DESCRIPTION: val/var assignments using contract functions with CallsInPlace effect
*/
fun case_1() {
val value_1: Int
funWithExactlyOnceCallsInPlace { value_1 = 10 }
value_1.inc()
}
fun case_2() {
var value_1: Int
var value_2: Int
funWithExactlyOnceCallsInPlace { value_1 = 10 }
funWithAtLeastOnceCallsInPlace { value_2 = 10 }
value_1.dec()
value_2.div(10)
}
class case_3 {
val value_1: Int
var value_2: Int
var value_3: Int
init {
funWithExactlyOnceCallsInPlace { value_1 = 1 }
funWithExactlyOnceCallsInPlace { value_2 = 2 }
funWithAtLeastOnceCallsInPlace { value_3 = 3 }
}
}
@@ -0,0 +1,143 @@
// !LANGUAGE: +AllowContractsForCustomFunctions +UseCallsInPlaceEffect
// !USE_EXPERIMENTAL: kotlin.contracts.ExperimentalContracts
// !WITH_CONTRACT_FUNCTIONS
// SKIP_TXT
/*
KOTLIN DIAGNOSTICS NOT LINKED SPEC TEST (POSITIVE)
SECTION: contracts
CATEGORY: analysis, controlFlow, initialization
NUMBER: 2
DESCRIPTION: Nested val/var assignments using contract functions with CallsInPlace effect
*/
fun case_1() {
val value_1: Int
funWithExactlyOnceCallsInPlace {
funWithExactlyOnceCallsInPlace {
value_1 = 1
funWithExactlyOnceCallsInPlace {
value_1.inc()
}
}
funWithExactlyOnceCallsInPlace {
value_1.inc()
}
value_1.inc()
}
value_1.inc()
}
fun case_2() {
val value_1: Int
funWithAtMostOnceCallsInPlace {
funWithExactlyOnceCallsInPlace {
value_1 = 1
}
funWithAtLeastOnceCallsInPlace {
value_1.inc()
}
funWithUnknownCallsInPlace {
value_1.inc()
}
value_1.inc()
}
}
fun case_3() {
var value_1: Int
funWithExactlyOnceCallsInPlace {
funWithExactlyOnceCallsInPlace {
value_1 = 1
funWithExactlyOnceCallsInPlace {
value_1.inc()
}
}
funWithExactlyOnceCallsInPlace {
value_1.inc()
}
value_1.inc()
}
value_1.inc()
}
fun case_4() {
var value_1: Int
funWithAtMostOnceCallsInPlace {
funWithAtLeastOnceCallsInPlace {
value_1 = 1
}
funWithAtLeastOnceCallsInPlace {
value_1.inc()
}
funWithUnknownCallsInPlace {
value_1.inc()
}
funWithExactlyOnceCallsInPlace {
value_1.inc()
}
value_1.inc()
}
}
fun case_7() {
var value_1: Int
funWithAtLeastOnceCallsInPlace {
funWithAtLeastOnceCallsInPlace {
value_1 = 1
funWithAtMostOnceCallsInPlace {
value_1.inc()
}
}
funWithUnknownCallsInPlace {
value_1.inc()
}
value_1.inc()
}
value_1.inc()
}
fun case_8() {
var value_1: Int
funWithUnknownCallsInPlace {
funWithAtMostOnceCallsInPlace {
funWithAtLeastOnceCallsInPlace {
value_1 = 1
}
funWithExactlyOnceCallsInPlace {
value_1.inc()
}
funWithAtLeastOnceCallsInPlace {
value_1.inc()
}
funWithAtMostOnceCallsInPlace {
value_1.inc()
}
funWithUnknownCallsInPlace {
value_1.inc()
}
}
}
}
fun case_9() {
var value_1: Int
funWithAtMostOnceCallsInPlace {
funWithUnknownCallsInPlace {
funWithExactlyOnceCallsInPlace {
value_1 = 1
}
funWithAtLeastOnceCallsInPlace {
value_1.inc()
}
funWithAtMostOnceCallsInPlace {
value_1.inc()
}
funWithUnknownCallsInPlace {
value_1.inc()
}
}
}
}
@@ -0,0 +1,181 @@
// !LANGUAGE: +AllowContractsForCustomFunctions +UseCallsInPlaceEffect
// !USE_EXPERIMENTAL: kotlin.contracts.ExperimentalContracts
// !WITH_CONTRACT_FUNCTIONS
// !WITH_ENUM_CLASSES
// SKIP_TXT
/*
KOTLIN DIAGNOSTICS NOT LINKED SPEC TEST (POSITIVE)
SECTION: contracts
CATEGORY: analysis, controlFlow, initialization
NUMBER: 3
DESCRIPTION: val/var assignments or subsequent usages with compelx control flow inside/outside lambda of contract function with CallsInPlace effect
*/
fun case_1(value_1: _EnumClass?) {
val value_2: Int
<!DEBUG_INFO_IMPLICIT_EXHAUSTIVE!>when (value_1) {
_EnumClass.NORTH -> funWithExactlyOnceCallsInPlace { value_2 = 1 }
_EnumClass.SOUTH -> funWithExactlyOnceCallsInPlace { value_2 = 2 }
_EnumClass.WEST -> funWithExactlyOnceCallsInPlace { value_2 = 3 }
_EnumClass.EAST -> funWithExactlyOnceCallsInPlace { value_2 = 4 }
null -> funWithExactlyOnceCallsInPlace { value_2 = 5 }
}<!>
value_2.inc()
}
fun case_2(value_1: Any?) {
val value_2: Int
funWithAtMostOnceCallsInPlace {
if (value_1 is String) {
value_2 = 0
} else if (value_1 == null) {
value_2 = 1
} else {
funWithExactlyOnceCallsInPlace { value_2 = 2 }
value_2.dec()
}
value_2.dec()
}
}
class case_3(value_1: Any?) {
var value_2: Int
init {
if (value_1 is String) {
funWithExactlyOnceCallsInPlace { value_2 = 0 }
value_2.div(10)
} else if (value_1 == null) {
funWithAtLeastOnceCallsInPlace { value_2 = 1 }
value_2.div(10)
} else {
value_2 = 2
}
value_2.div(10)
}
}
fun case_4(value_1: _EnumClassSingle?) {
var value_2: Int
funWithAtMostOnceCallsInPlace {
<!DEBUG_INFO_IMPLICIT_EXHAUSTIVE!>when (value_1) {
_EnumClassSingle.EVERYTHING -> {
funWithExactlyOnceCallsInPlace { value_2 = 1 }
++value_2
}
null -> {
funWithAtLeastOnceCallsInPlace { value_2 = 2 }
--value_2
}
}<!>
value_2.minus(5)
}
}
fun case_5() {
var value_2: Int
try {
funWithAtLeastOnceCallsInPlace { value_2 = 10 }
} catch (e: Exception) {
funWithExactlyOnceCallsInPlace { value_2 = 1 }
}
value_2++
}
fun case_6() {
var value_2: Int
try {
funWithAtLeastOnceCallsInPlace { value_2 = 10 }
} catch (e: Exception) {
throw Exception()
} finally {
funWithAtLeastOnceCallsInPlace { value_2 = 10 }
}
value_2++
}
fun case_7() {
var value_1: Int
try {
funWithAtLeastOnceCallsInPlace { value_1 = 10 }
} catch (e: Exception) {
try {
funWithAtLeastOnceCallsInPlace { value_1 = 10 }
} catch (e: Exception) {
throw Exception()
}
} finally {
funWithAtLeastOnceCallsInPlace { value_1 = 10 }
}
println(value_1.inc())
}
fun case_8() {
var value_1: Int
try {
funWithAtLeastOnceCallsInPlace { value_1 = 10 }
} catch (e: Exception) {
try {
funWithAtLeastOnceCallsInPlace { value_1 = 10 }
} catch (e: Exception) {
funWithAtLeastOnceCallsInPlace { value_1 = 10 }
}
}
println(value_1.inc())
}
fun case_9() {
val x: Int
funWithExactlyOnceCallsInPlace outer@ {
funWithAtMostOnceCallsInPlace {
funWithUnknownCallsInPlace {
x = 42
return@outer
}
}
throw Exception()
}
println(x.inc())
}
fun case_10() {
val x: Int
funWithExactlyOnceCallsInPlace outer@ {
funWithAtLeastOnceCallsInPlace {
x = 42
return@outer
}
}
println(x.inc())
}
fun case_11() {
var x: Int
funWithAtLeastOnceCallsInPlace outer@ {
funWithAtMostOnceCallsInPlace {
x = 41
return@outer
}
funWithUnknownCallsInPlace {
x = 42
return@outer
}
return@case_11
}
println(x.inc())
}
@@ -0,0 +1,136 @@
// !LANGUAGE: +AllowContractsForCustomFunctions +UseCallsInPlaceEffect
// !USE_EXPERIMENTAL: kotlin.contracts.ExperimentalContracts
// !WITH_CONTRACT_FUNCTIONS
// SKIP_TXT
/*
KOTLIN DIAGNOSTICS NOT LINKED SPEC TEST (POSITIVE)
SECTION: contracts
CATEGORY: analysis, controlFlow, initialization
NUMBER: 4
DESCRIPTION: CallsInPlace contract functions with name shadowing
*/
fun case_1() {
val <!UNUSED_VARIABLE!>value_1<!>: Int
funWithExactlyOnceCallsInPlace {
val <!NAME_SHADOWING!>value_1<!> = 10
value_1.inc()
}
}
fun case_2() {
val <!UNUSED_VARIABLE!>value_1<!>: Int
funWithExactlyOnceCallsInPlace {
val <!NAME_SHADOWING!>value_1<!>: Int
funWithExactlyOnceCallsInPlace {
value_1 = 10
}
funWithAtLeastOnceCallsInPlace {
value_1.inc()
}
value_1.inc()
}
}
fun case_3() {
val value_1: Int
funWithAtLeastOnceCallsInPlace {
val <!NAME_SHADOWING!>value_1<!>: Int
funWithExactlyOnceCallsInPlace {
value_1 = 10
}
funWithAtMostOnceCallsInPlace {
value_1.inc()
}
value_1.inc()
}
funWithExactlyOnceCallsInPlace {
value_1 = 10
}
value_1.inc()
}
fun case_4() {
val value_1: Int
funWithAtMostOnceCallsInPlace {
val <!NAME_SHADOWING!>value_1<!>: Int
funWithExactlyOnceCallsInPlace {
value_1 = 10
}
funWithUnknownCallsInPlace {
value_1.inc()
}
value_1.inc()
}
funWithExactlyOnceCallsInPlace {
value_1 = 10
}
value_1.inc()
}
fun case_5() {
val value_1: Int
funWithUnknownCallsInPlace {
val <!NAME_SHADOWING!>value_1<!>: Int
funWithExactlyOnceCallsInPlace {
value_1 = 10
}
funWithAtMostOnceCallsInPlace {
value_1.inc()
}
}
funWithExactlyOnceCallsInPlace {
value_1 = 10
}
value_1.inc()
}
fun case_6() {
var value_1: Int
funWithAtLeastOnceCallsInPlace {
val <!NAME_SHADOWING!>value_1<!>: Int
funWithExactlyOnceCallsInPlace {
value_1 = 10
}
funWithAtMostOnceCallsInPlace {
value_1.inc()
}
value_1.inc()
}
funWithAtLeastOnceCallsInPlace { value_1 = 1 }
value_1.dec()
}
fun case_7() {
val value_1: Int
funWithAtLeastOnceCallsInPlace {
var <!NAME_SHADOWING!>value_1<!>: Int
funWithAtLeastOnceCallsInPlace { value_1 = 10 }
funWithUnknownCallsInPlace { value_1.inc() }
value_1.inc()
}
funWithExactlyOnceCallsInPlace { value_1 = 1 }
value_1.dec()
}
fun case_8() {
var value_1: Int
funWithAtLeastOnceCallsInPlace {
var <!NAME_SHADOWING!>value_1<!>: Int
funWithAtLeastOnceCallsInPlace {
value_1 = 10
}
funWithAtLeastOnceCallsInPlace {
value_1.inc()
}
value_1++
}
funWithAtLeastOnceCallsInPlace {
value_1 = 1
}
value_1--
}
@@ -0,0 +1,34 @@
// !LANGUAGE: +AllowContractsForCustomFunctions +UseCallsInPlaceEffect
// !DIAGNOSTICS: -INVISIBLE_REFERENCE -INVISIBLE_MEMBER
// !USE_EXPERIMENTAL: kotlin.contracts.ExperimentalContracts
/*
KOTLIN DIAGNOSTICS NOT LINKED SPEC TEST (POSITIVE)
SECTION: contracts
CATEGORY: analysis, controlFlow, initialization
NUMBER: 5
DESCRIPTION: Smart initialization with correspond contract function with default value before lambda.
ISSUES: KT-26444
*/
// FILE: contracts.kt
package contracts
import kotlin.contracts.*
fun case_1(x: Double = 1.0, block: () -> Unit): Double {
contract { callsInPlace(block, InvocationKind.EXACTLY_ONCE) }
return x
}
// FILE: usages.kt
import contracts.*
fun case_1() {
val value_1: Int
contracts.case_1 { value_1 = 10 }
value_1.inc()
}
@@ -0,0 +1,45 @@
// !LANGUAGE: +AllowContractsForCustomFunctions +UseCallsInPlaceEffect
// !USE_EXPERIMENTAL: kotlin.contracts.ExperimentalContracts
// !WITH_CONTRACT_FUNCTIONS
// SKIP_TXT
/*
KOTLIN DIAGNOSTICS NOT LINKED SPEC TEST (NEGATIVE)
SECTION: contracts
CATEGORY: analysis, controlFlow, unreachableCode
NUMBER: 1
DESCRIPTION: Using not allowed break and continue inside lambda of contract function
*/
fun case_1(value_1: Boolean) {
while (value_1) {
funWithExactlyOnceCallsInPlace {
<!BREAK_OR_CONTINUE_JUMPS_ACROSS_FUNCTION_BOUNDARY!>break<!>
}
println("1")
}
loop@ for (i in 0..10) {
funWithExactlyOnceCallsInPlace {
<!BREAK_OR_CONTINUE_JUMPS_ACROSS_FUNCTION_BOUNDARY!>break@loop<!>
}
println("1")
}
}
fun case_2(value_1: Boolean) {
for (i in 0..10) {
funWithExactlyOnceCallsInPlace {
<!BREAK_OR_CONTINUE_JUMPS_ACROSS_FUNCTION_BOUNDARY!>continue<!>
}
println("1")
}
loop@ while (value_1) {
funWithExactlyOnceCallsInPlace {
<!BREAK_OR_CONTINUE_JUMPS_ACROSS_FUNCTION_BOUNDARY!>continue@loop<!>
}
println("1")
}
}
@@ -0,0 +1,107 @@
// !LANGUAGE: +AllowContractsForCustomFunctions +UseCallsInPlaceEffect
// !DIAGNOSTICS: -UNUSED_VARIABLE
// !USE_EXPERIMENTAL: kotlin.contracts.ExperimentalContracts
// !WITH_CONTRACT_FUNCTIONS
// SKIP_TXT
/*
KOTLIN DIAGNOSTICS NOT LINKED SPEC TEST (POSITIVE)
SECTION: contracts
CATEGORY: analysis, controlFlow, unreachableCode
NUMBER: 1
DESCRIPTION: Unreachable code detection using contract function with CallsInPlace effect
*/
fun case_1() {
funWithExactlyOnceCallsInPlace {
throw Exception()
}
<!UNREACHABLE_CODE!>println("1")<!>
}
fun case_2() {
funWithAtLeastOnceCallsInPlace {
throw Exception()
}
<!UNREACHABLE_CODE!>println("1")<!>
}
fun case_3() {
funWithExactlyOnceCallsInPlace {
return
}
<!UNREACHABLE_CODE!>println("1")<!>
}
fun case_4() {
funWithAtLeastOnceCallsInPlace {
return
}
<!UNREACHABLE_CODE!>println("1")<!>
}
fun case_5(args: Array<String>) {
fun nestedFun_1() {
funWithAtLeastOnceCallsInPlace {
return@nestedFun_1
}
<!UNREACHABLE_CODE!>println("1")<!>
}
fun nestedFun_2() {
args.forEach {
funWithAtLeastOnceCallsInPlace {
return@forEach
}
<!UNREACHABLE_CODE!>println("1")<!>
}
}
fun nestedFun_3() {
fun nestedFun_4() {
funWithAtLeastOnceCallsInPlace {
return@nestedFun_4
}
<!UNREACHABLE_CODE!>println("1")<!>
}
println("1")
}
}
fun case_6(args: Array<String>) {
args.forEach {
funWithExactlyOnceCallsInPlace {
return@forEach
}
<!UNREACHABLE_CODE!>println("1")<!>
}
args.forEach {
fun nestedFun_1() {
funWithExactlyOnceCallsInPlace {
return@nestedFun_1
}
<!UNREACHABLE_CODE!>println("1")<!>
}
}
args.forEach {
fun nestedFun_2() {
funWithExactlyOnceCallsInPlace {
return
}
<!UNREACHABLE_CODE!>println("1")<!>
}
}
}
fun case_7() {
<!UNREACHABLE_CODE!>val value_1 =<!> funWithExactlyOnceCallsInPlace {
throw Exception()
<!UNREACHABLE_CODE!>println(1)<!>
}
<!UNREACHABLE_CODE!>println(value_1)<!>
}
fun case_8() {
<!UNREACHABLE_CODE!>println(<!>funWithExactlyOnceCallsInPlace { return; <!UNREACHABLE_CODE!>println(1)<!> }<!UNREACHABLE_CODE!>)<!>
<!UNREACHABLE_CODE!>return<!>
}
@@ -0,0 +1,77 @@
// !LANGUAGE: +AllowContractsForCustomFunctions +UseCallsInPlaceEffect
// !USE_EXPERIMENTAL: kotlin.contracts.ExperimentalContracts
// !WITH_CONTRACT_FUNCTIONS
// SKIP_TXT
/*
KOTLIN DIAGNOSTICS NOT LINKED SPEC TEST (POSITIVE)
SECTION: contracts
CATEGORY: analysis, controlFlow, unreachableCode
NUMBER: 2
DESCRIPTION: Check for lack of unreachable code report when 'at most once' and 'unknown' invokations in CallsInPlace effect used.
*/
fun case_1() {
funWithAtMostOnceCallsInPlace {
throw Exception()
}
funWithAtMostOnceCallsInPlace {
return
}
println("1")
}
fun case_2() {
funWithUnknownCallsInPlace {
throw Exception()
}
funWithUnknownCallsInPlace {
return
}
println("1")
}
fun case_3() {
funWithExactlyOnceCallsInPlace {
return@funWithExactlyOnceCallsInPlace
}
println("1")
funWithExactlyOnceCallsInPlace {
fun nestedFun_1() {
return@nestedFun_1
}
}
println("1")
fun nestedFun_3() {
fun nestedFun_4() {
funWithExactlyOnceCallsInPlace {
return@nestedFun_4
}
}
println("1")
}
println("1")
}
fun case_4() {
funWithAtLeastOnceCallsInPlace {
return@funWithAtLeastOnceCallsInPlace
}
println("1")
funWithAtLeastOnceCallsInPlace {
fun nestedFun_1() {
return@nestedFun_1
}
}
println("1")
fun nestedFun_2() {
fun nestedFun_3() {
funWithAtLeastOnceCallsInPlace {
return@nestedFun_3
}
}
println("1")
}
println("1")
}
@@ -0,0 +1,92 @@
// !LANGUAGE: +AllowContractsForCustomFunctions +UseCallsInPlaceEffect
// !USE_EXPERIMENTAL: kotlin.contracts.ExperimentalContracts
// !WITH_CONTRACT_FUNCTIONS
// SKIP_TXT
/*
KOTLIN DIAGNOSTICS NOT LINKED SPEC TEST (POSITIVE)
SECTION: contracts
CATEGORY: analysis, controlFlow, unreachableCode
NUMBER: 3
DESCRIPTION: Unreachable code detection using local functions or labdas combined with contract functions with CallsInPlace effect
*/
fun case_1() {
funWithExactlyOnceCallsInPlace {
throw Exception()
}
<!UNREACHABLE_CODE!>println("1")<!>
}
fun case_2() {
funWithAtLeastOnceCallsInPlace {
throw Exception()
}
<!UNREACHABLE_CODE!>println("1")<!>
}
fun case_3() {
funWithExactlyOnceCallsInPlace {
return
}
<!UNREACHABLE_CODE!>println("1")<!>
}
fun case_4() {
funWithAtLeastOnceCallsInPlace {
return
}
<!UNREACHABLE_CODE!>println("1")<!>
}
fun case_5(args: Array<String>) {
fun nestedFun_1() {
funWithAtLeastOnceCallsInPlace {
return@nestedFun_1
}
<!UNREACHABLE_CODE!>println("1")<!>
}
fun nestedFun_2() {
args.forEach {
funWithAtLeastOnceCallsInPlace {
return@forEach
}
<!UNREACHABLE_CODE!>println("1")<!>
}
}
fun nestedFun_3() {
fun nestedFun_4() {
funWithAtLeastOnceCallsInPlace {
return@nestedFun_4
}
<!UNREACHABLE_CODE!>println("1")<!>
}
println("1")
}
}
fun case_6(args: Array<String>) {
args.forEach {
funWithExactlyOnceCallsInPlace {
return@forEach
}
<!UNREACHABLE_CODE!>println("1")<!>
}
args.forEach {
fun nestedFun_1() {
funWithExactlyOnceCallsInPlace {
return@nestedFun_1
}
<!UNREACHABLE_CODE!>println("1")<!>
}
}
args.forEach {
fun nestedFun_2() {
funWithExactlyOnceCallsInPlace {
return
}
<!UNREACHABLE_CODE!>println("1")<!>
}
}
}
@@ -0,0 +1,55 @@
// !LANGUAGE: +AllowContractsForCustomFunctions +UseCallsInPlaceEffect
// !USE_EXPERIMENTAL: kotlin.contracts.ExperimentalContracts
// !WITH_CONTRACT_FUNCTIONS
// SKIP_TXT
/*
KOTLIN DIAGNOSTICS NOT LINKED SPEC TEST (POSITIVE)
SECTION: contracts
CATEGORY: analysis, controlFlow, unreachableCode
NUMBER: 4
DESCRIPTION: Unreachable code detection using nested contract functions with CallsInPlace effect
*/
fun case_1() {
funWithExactlyOnceCallsInPlace {
funWithExactlyOnceCallsInPlace {
funWithExactlyOnceCallsInPlace {
throw Exception()
}
<!UNREACHABLE_CODE!>println("1")<!>
}
<!UNREACHABLE_CODE!>println("2")<!>
}
<!UNREACHABLE_CODE!>println("3")<!>
}
fun case_2() {
funWithAtLeastOnceCallsInPlace {
funWithAtLeastOnceCallsInPlace label_1@ {
funWithAtLeastOnceCallsInPlace {
return@label_1
}
<!UNREACHABLE_CODE!>println("1")<!>
}
println("2")
}
funWithAtLeastOnceCallsInPlace {
return
}
<!UNREACHABLE_CODE!>println("3")<!>
}
fun case_3() {
funWithExactlyOnceCallsInPlace {
funWithExactlyOnceCallsInPlace {
funWithExactlyOnceCallsInPlace {
return<!LABEL_NAME_CLASH!>@funWithExactlyOnceCallsInPlace<!>
}
println("1")
}
println("2")
}
println("3")
}
@@ -0,0 +1,60 @@
// !LANGUAGE: +AllowContractsForCustomFunctions +UseCallsInPlaceEffect
// !USE_EXPERIMENTAL: kotlin.contracts.ExperimentalContracts
// !WITH_CONTRACT_FUNCTIONS
// SKIP_TXT
/*
KOTLIN DIAGNOSTICS NOT LINKED SPEC TEST (POSITIVE)
SECTION: contracts
CATEGORY: analysis, controlFlow, unreachableCode
NUMBER: 5
DESCRIPTION: Unreachable code detection using contract functions with complex control flow inside
*/
fun case_1(b: Boolean?) {
funWithExactlyOnceCallsInPlace {
if (b == null) return
<!DEBUG_INFO_IMPLICIT_EXHAUSTIVE!>when (<!DEBUG_INFO_SMARTCAST!>b<!>) {
true -> {
println(1)
return
}
false -> {
println(2)
throw Exception()
}
}<!>
<!UNREACHABLE_CODE!>println(3)<!>
}
<!UNREACHABLE_CODE!>println(3)<!>
}
fun case_2(b: Boolean?, c: Boolean) {
funWithAtLeastOnceCallsInPlace {
when (b) {
true -> {
println(1)
return
}
else -> {
if (b == null) {
funWithExactlyOnceCallsInPlace {
when {
c == true -> throw Exception()
else -> funWithAtLeastOnceCallsInPlace { return }
}
<!UNREACHABLE_CODE!>println(3)<!>
}
<!UNREACHABLE_CODE!>println(3)<!>
} else {
throw Exception()
}
<!UNREACHABLE_CODE!>println(3)<!>
}
}
<!UNREACHABLE_CODE!>println(3)<!>
}
<!UNREACHABLE_CODE!>println(3)<!>
}
@@ -0,0 +1,68 @@
// !LANGUAGE: +AllowContractsForCustomFunctions +UseCallsInPlaceEffect
// !USE_EXPERIMENTAL: kotlin.contracts.ExperimentalContracts
// !WITH_CONTRACT_FUNCTIONS
// SKIP_TXT
/*
KOTLIN DIAGNOSTICS NOT LINKED SPEC TEST (POSITIVE)
SECTION: contracts
CATEGORY: analysis, controlFlow, unreachableCode
NUMBER: 6
DESCRIPTION: Check for lack of unreachable code report when in complex control flow of contract function lambda not all branches are doing non-local return
*/
fun case_1(b: Boolean?, c: Boolean) {
funWithExactlyOnceCallsInPlace {
if (b == null) return
try {
<!DEBUG_INFO_IMPLICIT_EXHAUSTIVE!>when (<!DEBUG_INFO_SMARTCAST!>b<!>) {
true -> {
println(1)
return
}
false -> {
println(2)
throw Exception()
}
}<!>
} catch (e: Exception) {
if (c) {
return@funWithExactlyOnceCallsInPlace
} else {
return
}
}
<!UNREACHABLE_CODE!>println(3)<!>
}
println(3)
}
fun case_2(b: Boolean?, c: Boolean) {
funWithAtLeastOnceCallsInPlace {
when (b) {
true -> {
println(1)
return
}
else -> {
if (b == null) {
funWithExactlyOnceCallsInPlace {
when {
c == true -> throw Exception()
else -> funWithAtMostOnceCallsInPlace { return }
}
println(3)
}
println(3)
} else {
throw Exception()
}
println(3)
}
}
println(3)
}
println(3)
}
@@ -0,0 +1,33 @@
// !LANGUAGE: +AllowContractsForCustomFunctions +UseCallsInPlaceEffect
// !DIAGNOSTICS: -INVISIBLE_REFERENCE -INVISIBLE_MEMBER
// !USE_EXPERIMENTAL: kotlin.contracts.ExperimentalContracts
/*
KOTLIN DIAGNOSTICS NOT LINKED SPEC TEST (POSITIVE)
SECTION: contracts
CATEGORY: analysis, controlFlow, unreachableCode
NUMBER: 7
DESCRIPTION: Smart initialization with correspond contract function with default value before lambda.
ISSUES: KT-26444
*/
// FILE: contracts.kt
package contracts
import kotlin.contracts.*
fun case_1(x: Double = 1.0, block: () -> Unit): Double {
contract { callsInPlace(block, InvocationKind.AT_LEAST_ONCE) }
return x
}
// FILE: usages.kt
import contracts.*
fun case_1() {
contracts.case_1 { throw Exception() }
<!UNREACHABLE_CODE!>println(1)<!>
}