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,20 @@
// !LANGUAGE: +AllowContractsForCustomFunctions +UseCallsInPlaceEffect
// !DIAGNOSTICS: -INVISIBLE_REFERENCE -INVISIBLE_MEMBER
// !USE_EXPERIMENTAL: kotlin.contracts.ExperimentalContracts
/*
KOTLIN DIAGNOSTICS NOT LINKED SPEC TEST (NEGATIVE)
SECTION: contracts
CATEGORY: declarations, contractFunction
NUMBER: 1
DESCRIPTION: Check that recursion isn't allowed in contract functions with CallsInPlace effect.
*/
import kotlin.contracts.*
inline fun case_1(block: () -> Unit) {
contract { callsInPlace(block, InvocationKind.EXACTLY_ONCE) }
block()
<!RECURSION_IN_INLINE!>case_1<!>(block)
}
@@ -0,0 +1,5 @@
package
public inline fun case_1(/*0*/ block: () -> kotlin.Unit): kotlin.Unit
CallsInPlace(block, EXACTLY_ONCE)
@@ -0,0 +1,98 @@
// !LANGUAGE: +AllowContractsForCustomFunctions +UseCallsInPlaceEffect
// !DIAGNOSTICS: -INVISIBLE_REFERENCE -INVISIBLE_MEMBER
// !USE_EXPERIMENTAL: kotlin.contracts.ExperimentalContracts
// !WITH_CLASSES
/*
KOTLIN DIAGNOSTICS NOT LINKED SPEC TEST (NEGATIVE)
SECTION: contracts
CATEGORY: declarations, contractFunction
NUMBER: 2
DESCRIPTION: Check report about use contracts in literal functions, lambdas or not top-level functions.
ISSUES: KT-26149
*/
import kotlin.contracts.*
fun case_1() {
val fun_1 = fun(block: () -> Unit) {
<!CONTRACT_NOT_ALLOWED!>contract<!> { callsInPlace(block, InvocationKind.EXACTLY_ONCE) }
return block()
}
fun_1 { throw Exception() }
println("1")
}
fun case_2() {
val lambda_1 = { block: () -> Unit ->
<!CONTRACT_NOT_ALLOWED!>contract<!> { callsInPlace(block, InvocationKind.EXACTLY_ONCE) }
block()
}
lambda_1 { throw Exception() }
println("1")
}
object case_3 {
fun case_3(block: () -> Unit) {
<!CONTRACT_NOT_ALLOWED!>contract<!> { callsInPlace(block, InvocationKind.EXACTLY_ONCE) }
return block()
}
}
/*
UNEXPECTED BEHAVIOUR
ISSUES: KT-26244
*/
class case_4 : _ClassLevel3() {
fun <T : Number?>T.case_4_1(): Boolean {
<!CONTRACT_NOT_ALLOWED!>contract<!> { returns(false) implies (<!USELESS_IS_CHECK!>this@case_4 !is _ClassLevel1<!>) }
return this == null
}
fun case_4_2(number: Int?): Boolean {
<!CONTRACT_NOT_ALLOWED!>contract<!> { returns(false) implies (number != null) }
return number == null
}
fun <T>T?.case_4_3(): Boolean {
<!CONTRACT_NOT_ALLOWED!>contract<!> { returns(false) implies (this@case_4_3 !is Number) }
return this@case_4_3 is Number
}
fun <T : <!FINAL_UPPER_BOUND!>Boolean<!>>T.case_4_4() {
<!CONTRACT_NOT_ALLOWED!>contract<!> { returns() implies (!this@case_4_4) }
if (this) throw Exception()
}
fun <T>T.case_4_5_wrap() {
fun case_4_5_contract() {
<!CONTRACT_NOT_ALLOWED!>contract<!> { returns() implies (this@case_4_5_wrap is _ClassLevel1) }
if (this@case_4_5_wrap !is _ClassLevel1) throw Exception()
}
case_4_5_contract()
println("!")
}
fun case_4_5() = _ClassLevel3().case_4_5_wrap()
}
/*
UNEXPECTED BEHAVIOUR
ISSUES: KT-26244
*/
class case_5<T> : _ClassLevel5() {
inner class case_5_1 {
fun <K : Number?>K.case_5_1_1() {
<!CONTRACT_NOT_ALLOWED!>contract<!> { returns() implies (this@case_5_1 !is _ClassLevel1 && <!SENSELESS_COMPARISON!>this@case_5_1 != null<!> || <!USELESS_IS_CHECK!>this@case_5 is _ClassLevel1<!> && this@case_5_1_1 is Float) }
if (!(this@case_5_1 !is _ClassLevel1 && <!SENSELESS_COMPARISON!>this@case_5_1 != null<!> || <!USELESS_IS_CHECK!>this@case_5 is _ClassLevel1<!> && this is Float)) throw Exception()
}
fun case_5_1_2() {
<!CONTRACT_NOT_ALLOWED!>contract<!> { returns() implies (this@case_5_1 !is _ClassLevel1 || <!USELESS_IS_CHECK!>this@case_5 is _ClassLevel1<!> || <!SENSELESS_COMPARISON!>this@case_5_1 == null<!>) }
if (!(this@case_5_1 !is _ClassLevel1 || <!USELESS_IS_CHECK!>this@case_5 is _ClassLevel1<!> || <!SENSELESS_COMPARISON!>this@case_5_1 == null<!>)) throw Exception()
}
}
}
@@ -0,0 +1,132 @@
package
public fun case_1(): kotlin.Unit
public fun case_2(): kotlin.Unit
public final class _Class {
public constructor _Class()
public final val prop_1: kotlin.Int = 1
public final val prop_2: kotlin.Int = 2
public final val prop_3: kotlin.Int = 3
public final operator fun contains(/*0*/ a: kotlin.Char): kotlin.Boolean
public final operator fun contains(/*0*/ a: kotlin.Int): kotlin.Boolean
public final operator fun contains(/*0*/ a: kotlin.Long): kotlin.Boolean
public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
public final fun fun_1(): (kotlin.Int) -> (kotlin.Int) -> kotlin.Int
public final fun fun_2(/*0*/ value_1: kotlin.Int): kotlin.Int
public final fun fun_3(/*0*/ value_1: kotlin.Int): (kotlin.Int) -> kotlin.Int
public final fun getCharArray(/*0*/ value_1: kotlin.Char): kotlin.CharArray
public final fun getIntArray(/*0*/ value_1: kotlin.Int): kotlin.IntArray
public final fun getLongArray(/*0*/ value_1: kotlin.Long): kotlin.LongArray
public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
public final class _NestedClass {
public constructor _NestedClass()
public final val prop_4: kotlin.Int = 4
public final val prop_5: kotlin.Int = 5
public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
}
}
public open class _ClassLevel1 {
public constructor _ClassLevel1()
public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
}
public open class _ClassLevel2 : _ClassLevel1 {
public constructor _ClassLevel2()
public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
}
public open class _ClassLevel3 : _ClassLevel2 {
public constructor _ClassLevel3()
public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
}
public open class _ClassLevel4 : _ClassLevel3 {
public constructor _ClassLevel4()
public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
}
public open class _ClassLevel5 : _ClassLevel4 {
public constructor _ClassLevel5()
public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
}
public final class _ClassLevel6 : _ClassLevel5 {
public constructor _ClassLevel6()
public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
}
public final class _ClassWithCompanionObject {
public constructor _ClassWithCompanionObject()
public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
public companion object Companion {
private constructor Companion()
public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
}
}
public final class _EmptyClass {
public constructor _EmptyClass()
public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
}
public object case_3 {
private constructor case_3()
public final fun case_3(/*0*/ block: () -> kotlin.Unit): kotlin.Unit
public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
}
public final class case_4 : _ClassLevel3 {
public constructor case_4()
public final fun case_4_2(/*0*/ number: kotlin.Int?): kotlin.Boolean
public final fun case_4_5(): kotlin.Unit
public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
public final fun </*0*/ T : kotlin.Number?> T.case_4_1(): kotlin.Boolean
public final fun </*0*/ T> T?.case_4_3(): kotlin.Boolean
public final fun </*0*/ T : kotlin.Boolean> T.case_4_4(): kotlin.Unit
public final fun </*0*/ T> T.case_4_5_wrap(): kotlin.Unit
}
public final class case_5</*0*/ T> : _ClassLevel5 {
public constructor case_5</*0*/ T>()
public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
public final inner class case_5_1 /*captured type parameters: /*0*/ T*/ {
public constructor case_5_1()
public final fun case_5_1_2(): kotlin.Unit
public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
public final fun </*0*/ K : kotlin.Number?> K.case_5_1_1(): kotlin.Unit
}
}
@@ -0,0 +1,27 @@
// !LANGUAGE: +AllowContractsForCustomFunctions +UseCallsInPlaceEffect
// !DIAGNOSTICS: -INVISIBLE_REFERENCE -INVISIBLE_MEMBER
// !USE_EXPERIMENTAL: kotlin.contracts.ExperimentalContracts
/*
KOTLIN DIAGNOSTICS NOT LINKED SPEC TEST (POSITIVE)
SECTION: contracts
CATEGORY: declarations, contractFunction
NUMBER: 1
DESCRIPTION: Check that fun with contract and CallsInPlace effect is an inline function.
UNEXPECTED BEHAVIOUR
ISSUES: KT-26126
*/
import kotlin.contracts.*
fun funWithContractExactlyOnce(block: () -> Unit) { // report about not-inline function is expected
contract { callsInPlace(block, InvocationKind.EXACTLY_ONCE) }
return block()
}
fun case_1() {
val value_1: Int
funWithContractExactlyOnce { value_1 = 10 } // back-end exception
value_1.inc()
}
@@ -0,0 +1,5 @@
package
public fun case_1(): kotlin.Unit
public fun funWithContractExactlyOnce(/*0*/ block: () -> kotlin.Unit): kotlin.Unit
CallsInPlace(block, EXACTLY_ONCE)
@@ -0,0 +1,37 @@
// !LANGUAGE: +AllowContractsForCustomFunctions +UseReturnsEffect
// !DIAGNOSTICS: -INVISIBLE_REFERENCE -INVISIBLE_MEMBER
// !USE_EXPERIMENTAL: kotlin.contracts.ExperimentalContracts
/*
KOTLIN DIAGNOSTICS NOT LINKED SPEC TEST (POSITIVE)
SECTION: contracts
CATEGORY: declarations, contractFunction
NUMBER: 2
DESCRIPTION: Contract function usage before declaration it
*/
import kotlin.contracts.*
fun case_1_1(x: Any?) {
if (case_1_2(x)) {
<!DEBUG_INFO_SMARTCAST!>x<!>.length
}
}
fun case_2_1(x: Number?) {
case_2_2(x)
println(<!DEBUG_INFO_SMARTCAST!>x<!>.toByte())
}
fun case_1_2(x: Any?): Boolean {
contract { returns(true) implies (x is String) }
return x is String
}
fun case_2_2(x: Any?) {
contract { returns() implies(x != null) }
if (x == null) throw Exception()
}
@@ -0,0 +1,10 @@
package
public fun case_1_1(/*0*/ x: kotlin.Any?): kotlin.Unit
public fun case_1_2(/*0*/ x: kotlin.Any?): kotlin.Boolean
Returns(TRUE) -> x is String
public fun case_2_1(/*0*/ x: kotlin.Number?): kotlin.Unit
public fun case_2_2(/*0*/ x: kotlin.Any?): kotlin.Unit
Returns(WILDCARD) -> x != null