diff --git a/compiler/tests-spec/testData/diagnostics/helpers/contractFunctions.kt b/compiler/tests-spec/testData/diagnostics/helpers/contractFunctions.kt new file mode 100644 index 00000000000..92ea9fcd8c3 --- /dev/null +++ b/compiler/tests-spec/testData/diagnostics/helpers/contractFunctions.kt @@ -0,0 +1,182 @@ +import kotlin.contracts.* + +inline fun funWithExactlyOnceCallsInPlace(block: () -> T): T { + contract { callsInPlace(block, InvocationKind.EXACTLY_ONCE) } + return block() +} + +inline fun funWithExactlyOnceCallsInPlace(block: () -> Unit) { + contract { callsInPlace(block, InvocationKind.EXACTLY_ONCE) } + block() +} + +inline fun funWithAtLeastOnceCallsInPlace(block: () -> T): T { + contract { callsInPlace(block, InvocationKind.AT_LEAST_ONCE) } + block() + return block() +} + +inline fun funWithAtLeastOnceCallsInPlace(block: () -> Unit) { + contract { callsInPlace(block, InvocationKind.AT_LEAST_ONCE) } + block() + block() +} + +inline fun funWithAtMostOnceCallsInPlace(block: () -> Unit) { + contract { callsInPlace(block, InvocationKind.AT_MOST_ONCE) } +} + +inline fun funWithUnknownCallsInPlace(block: () -> Unit) { + contract { callsInPlace(block, InvocationKind.UNKNOWN) } + block() +} + +fun funWithReturns(cond: Boolean) { + contract { returns() implies (cond) } + if (!cond) throw Exception() +} + +fun funWithReturnsAndInvertCondition(cond: Boolean) { + contract { returns() implies (!cond) } + if (cond) throw Exception() +} + +fun funWithReturnsAndTypeCheck(value_1: Any?) { + contract { returns() implies (value_1 is String) } + if (value_1 !is String) throw Exception() +} + +fun funWithReturnsAndInvertTypeCheck(value_1: Any?) { + contract { returns() implies (value_1 !is String) } + if (value_1 is String) throw Exception() +} + +fun funWithReturnsAndNotNullCheck(value_1: Any?) { + contract { returns() implies (value_1 != null) } + if (value_1 == null) throw Exception() +} + +fun funWithReturnsAndNullCheck(value_1: Any?) { + contract { returns() implies (value_1 == null) } + if (value_1 != null) throw Exception() +} + +fun funWithReturnsTrue(cond: Boolean): Boolean { + contract { returns(true) implies (cond) } + return cond +} + +fun funWithReturnsTrueAndInvertCondition(cond: Boolean): Boolean { + contract { returns(true) implies (!cond) } + return !cond +} + +fun funWithReturnsTrueAndTypeCheck(value_1: Any?): Boolean { + contract { returns(true) implies (value_1 is String) } + return value_1 is String +} + +fun funWithReturnsTrueAndInvertTypeCheck(value_1: Any?): Boolean { + contract { returns(true) implies (value_1 !is String) } + return value_1 !is String +} + +fun funWithReturnsTrueAndNotNullCheck(value_1: Any?): Boolean { + contract { returns(true) implies (value_1 != null) } + return value_1 != null +} + +fun funWithReturnsTrueAndNullCheck(value_1: Any?): Boolean { + contract { returns(true) implies (value_1 == null) } + return value_1 == null +} + +fun funWithReturnsFalse(cond: Boolean): Boolean { + contract { returns(false) implies (cond) } + return cond +} + +fun funWithReturnsFalseAndInvertCondition(cond: Boolean): Boolean { + contract { returns(false) implies (!cond) } + return !cond +} + +fun funWithReturnsFalseAndTypeCheck(value_1: Any?): Boolean { + contract { returns(false) implies (value_1 is String) } + return value_1 is String +} + +fun funWithReturnsFalseAndInvertTypeCheck(value_1: Any?): Boolean { + contract { returns(false) implies (value_1 !is String) } + return value_1 !is String +} + +fun funWithReturnsFalseAndNotNullCheck(value_1: Number?): Boolean { + contract { returns(false) implies (value_1 != null) } + return value_1 != null +} + +fun funWithReturnsFalseAndNullCheck(value_1: Number?): Boolean { + contract { returns(false) implies (value_1 == null) } + return value_1 == null +} + +fun funWithReturnsNull(cond: Boolean): Boolean? { + contract { returns(null) implies (cond) } + return cond +} + +fun funWithReturnsNullAndInvertCondition(cond: Boolean): Boolean? { + contract { returns(null) implies (!cond) } + return !cond +} + +fun funWithReturnsNullAndTypeCheck(value_1: Any?): Boolean? { + contract { returns(null) implies (value_1 is String) } + return value_1 is String +} + +fun funWithReturnsNullAndInvertTypeCheck(value_1: Any?): Boolean? { + contract { returns(null) implies (value_1 !is String) } + return value_1 !is String +} + +fun funWithReturnsNullAndNotNullCheck(value_1: Number?): Boolean? { + contract { returns(null) implies (value_1 != null) } + return value_1 != null +} + +fun funWithReturnsNullAndNullCheck(value_1: Number?): Boolean? { + contract { returns(null) implies (value_1 == null) } + return value_1 == null +} + +fun funWithReturnsNotNull(cond: Boolean): Boolean? { + contract { returnsNotNull() implies (cond) } + return cond +} + +fun funWithReturnsNotNullAndInvertCondition(cond: Boolean): Boolean? { + contract { returnsNotNull() implies (!cond) } + return !cond +} + +fun funWithReturnsNotNullAndTypeCheck(value_1: Any?): Boolean? { + contract { returnsNotNull() implies (value_1 is String) } + return value_1 is String +} + +fun funWithReturnsNotNullAndInvertTypeCheck(value_1: Any?): Boolean? { + contract { returnsNotNull() implies (value_1 !is String) } + return value_1 !is String +} + +fun funWithReturnsNotNullAndNotNullCheck(value_1: Number?): Boolean? { + contract { returnsNotNull() implies (value_1 != null) } + return value_1 != null +} + +fun funWithReturnsNotNullAndNullCheck(value_1: Number?): Boolean? { + contract { returnsNotNull() implies (value_1 == null) } + return value_1 == null +} diff --git a/compiler/tests-spec/testData/diagnostics/notLinked/contracts/analysis/common/neg/1.kt b/compiler/tests-spec/testData/diagnostics/notLinked/contracts/analysis/common/neg/1.kt new file mode 100644 index 00000000000..09a0daa98f7 --- /dev/null +++ b/compiler/tests-spec/testData/diagnostics/notLinked/contracts/analysis/common/neg/1.kt @@ -0,0 +1,82 @@ +// !LANGUAGE: +AllowContractsForCustomFunctions +UseReturnsEffect +UseCallsInPlaceEffect +// !DIAGNOSTICS: -INVISIBLE_REFERENCE -INVISIBLE_MEMBER +// !USE_EXPERIMENTAL: kotlin.contracts.ExperimentalContracts + +/* + KOTLIN DIAGNOSTICS NOT LINKED SPEC TEST (NEGATIVE) + + SECTION: contracts + CATEGORY: analysis, common + NUMBER: 1 + DESCRIPTION: Analysis by contracts with mixed CallsInPlace and Returns effects. + */ + +// FILE: contracts.kt + +package contracts + +import kotlin.contracts.* + +inline fun case_1(value_1: Int?, block: () -> Unit): Boolean { + contract { + callsInPlace(block, InvocationKind.AT_MOST_ONCE) + returns(true) implies (value_1 != null) + } + + block() + + return value_1 != null +} + +inline fun T?.case_2(value_1: Int?, value_2: Any?, block: () -> Unit): Boolean? { + contract { + callsInPlace(block, InvocationKind.AT_MOST_ONCE) + returns(true) implies (value_1 == null && this@case_2 == null && value_2 !is Boolean?) + returns(false) implies (value_2 is Boolean?) + returns(null) implies ((value_1 != null || this@case_2 != null) && value_2 !is Boolean?) + } + + block() + + if (value_1 != null && this != null && value_2 is Boolean?) return true + if (value_2 !is Boolean?) return false + + return null +} + +// FILE: usages.kt + +import contracts.* + +fun case_1(value_1: Int?) { + val value_2: Int + if (contracts.case_1(value_1) { value_2 = 10 }) { + println(value_2) + } else { + value_1.inv() + println(value_2) + } +} + +fun case_2(value_1: Int?, value_2: Int?, value_3: Any?) { + val value_4: Int + when (value_1.case_2(value_2, value_3) { value_4 = 10 }) { + true -> { + println(value_3?.xor(true)) + println(value_4) + println(value_1.inv()) + println(value_2.inv()) + } + false -> { + println(value_4) + println(value_1) + println(value_2) + } + null -> { + println(value_3?.xor(true)) + println(value_4) + println(value_1) + println(value_2) + } + } +} diff --git a/compiler/tests-spec/testData/diagnostics/notLinked/contracts/analysis/common/neg/1.txt b/compiler/tests-spec/testData/diagnostics/notLinked/contracts/analysis/common/neg/1.txt new file mode 100644 index 00000000000..986e22efab1 --- /dev/null +++ b/compiler/tests-spec/testData/diagnostics/notLinked/contracts/analysis/common/neg/1.txt @@ -0,0 +1,17 @@ +package + +public fun case_1(/*0*/ value_1: kotlin.Int?): kotlin.Unit +public fun case_2(/*0*/ value_1: kotlin.Int?, /*1*/ value_2: kotlin.Int?, /*2*/ value_3: kotlin.Any?): kotlin.Unit + +package contracts { + public inline fun case_1(/*0*/ value_1: kotlin.Int?, /*1*/ block: () -> kotlin.Unit): kotlin.Boolean + CallsInPlace(block, AT_MOST_ONCE) + Returns(TRUE) -> value_1 != null + + public inline fun T?.case_2(/*0*/ value_1: kotlin.Int?, /*1*/ value_2: kotlin.Any?, /*2*/ block: () -> kotlin.Unit): kotlin.Boolean? + CallsInPlace(block, AT_MOST_ONCE) + Returns(TRUE) -> value_1 == null && == null && value_2 !is Boolean? + Returns(FALSE) -> value_2 is Boolean? + Returns(NULL) -> (value_1 != null || != null) && value_2 !is Boolean? + +} diff --git a/compiler/tests-spec/testData/diagnostics/notLinked/contracts/analysis/common/pos/1.kt b/compiler/tests-spec/testData/diagnostics/notLinked/contracts/analysis/common/pos/1.kt new file mode 100644 index 00000000000..bbbbda3d4b7 --- /dev/null +++ b/compiler/tests-spec/testData/diagnostics/notLinked/contracts/analysis/common/pos/1.kt @@ -0,0 +1,78 @@ +// !LANGUAGE: +AllowContractsForCustomFunctions +UseReturnsEffect +UseCallsInPlaceEffect +// !DIAGNOSTICS: -INVISIBLE_REFERENCE -INVISIBLE_MEMBER +// !USE_EXPERIMENTAL: kotlin.contracts.ExperimentalContracts + +/* + KOTLIN DIAGNOSTICS NOT LINKED SPEC TEST (POSITIVE) + + SECTION: contracts + CATEGORY: analysis, common + NUMBER: 1 + DESCRIPTION: Analysis by contracts with mixed CallsInPlace and Returns effects. + */ + +// FILE: contracts.kt + +package contracts + +import kotlin.contracts.* + +inline fun case_1(value_1: Int?, block: () -> Unit): Boolean { + contract { + callsInPlace(block, InvocationKind.EXACTLY_ONCE) + returns(true) implies (value_1 != null) + } + block() + return value_1 != null +} + +inline fun T?.case_2(value_1: Int?, value_2: Any?, block: () -> Unit): Boolean? { + contract { + callsInPlace(block, InvocationKind.EXACTLY_ONCE) + returns(true) implies (value_1 != null && this@case_2 != null && value_2 is Boolean?) + returns(false) implies (value_2 !is Boolean?) + returns(null) implies ((value_1 == null || this@case_2 == null) && value_2 is Boolean?) + } + block() + if (value_1 != null && this != null && value_2 is Boolean?) return true + if (value_2 !is Boolean?) return false + return null +} + +// FILE: usages.kt + +import contracts.* + +fun case_1(value_1: Int?) { + val value_3: Int + if (contracts.case_1(value_1) { value_3 = 10 }) { + value_1.inv() + println(value_3) + } else { + println(value_3) + } +} + +fun case_2(value_1: Int?, value_2: Int?, value_3: Any?) { + val value_4: Int + when (value_1.case_2(value_2, value_3) { value_4 = 10 }) { + true -> { + println(value_3?.xor(true)) + println(value_4) + println(value_1.inv()) + println(value_2.inv()) + } + false -> { + println(value_4) + println(value_1) + println(value_2) + } + null -> { + println(value_3?.xor(true)) + println(value_4) + println(value_1) + println(value_2) + } + } + println(value_4) +} diff --git a/compiler/tests-spec/testData/diagnostics/notLinked/contracts/analysis/common/pos/1.txt b/compiler/tests-spec/testData/diagnostics/notLinked/contracts/analysis/common/pos/1.txt new file mode 100644 index 00000000000..0806547a61c --- /dev/null +++ b/compiler/tests-spec/testData/diagnostics/notLinked/contracts/analysis/common/pos/1.txt @@ -0,0 +1,17 @@ +package + +public fun case_1(/*0*/ value_1: kotlin.Int?): kotlin.Unit +public fun case_2(/*0*/ value_1: kotlin.Int?, /*1*/ value_2: kotlin.Int?, /*2*/ value_3: kotlin.Any?): kotlin.Unit + +package contracts { + public inline fun case_1(/*0*/ value_1: kotlin.Int?, /*1*/ block: () -> kotlin.Unit): kotlin.Boolean + CallsInPlace(block, EXACTLY_ONCE) + Returns(TRUE) -> value_1 != null + + public inline fun T?.case_2(/*0*/ value_1: kotlin.Int?, /*1*/ value_2: kotlin.Any?, /*2*/ block: () -> kotlin.Unit): kotlin.Boolean? + CallsInPlace(block, EXACTLY_ONCE) + Returns(TRUE) -> value_1 != null && != null && value_2 is Boolean? + Returns(FALSE) -> value_2 !is Boolean? + Returns(NULL) -> (value_1 == null || == null) && value_2 is Boolean? + +} diff --git a/compiler/tests-spec/testData/diagnostics/notLinked/contracts/analysis/common/pos/2.kt b/compiler/tests-spec/testData/diagnostics/notLinked/contracts/analysis/common/pos/2.kt new file mode 100644 index 00000000000..4aedf808188 --- /dev/null +++ b/compiler/tests-spec/testData/diagnostics/notLinked/contracts/analysis/common/pos/2.kt @@ -0,0 +1,35 @@ +// !LANGUAGE: +AllowContractsForCustomFunctions +UseReturnsEffect +// !USE_EXPERIMENTAL: kotlin.contracts.ExperimentalContracts +// !WITH_CONTRACT_FUNCTIONS +// SKIP_TXT + +/* + KOTLIN DIAGNOSTICS NOT LINKED SPEC TEST (POSITIVE) + + SECTION: contracts + CATEGORY: analysis, common + NUMBER: 2 + DESCRIPTION: Recursion in the lambda of contract function. + */ + +fun case_1(x: Int): Unit = funWithExactlyOnceCallsInPlace { + if (x == 0) return + if (x == 1) return + return case_1(x - 2) +} + +fun case_2(x: Int) { + funWithAtLeastOnceCallsInPlace { + if (x == 0) return + if (x == 1) return + return case_2(x - 2) + } +} + +fun case_3(x: Int) { + funWithAtMostOnceCallsInPlace { + if (x == 0) return + if (x == 1) return + return case_3(x - 2) + } +} diff --git a/compiler/tests-spec/testData/diagnostics/notLinked/contracts/analysis/controlFlow/initialization/neg/1.kt b/compiler/tests-spec/testData/diagnostics/notLinked/contracts/analysis/controlFlow/initialization/neg/1.kt new file mode 100644 index 00000000000..fb6115baf12 --- /dev/null +++ b/compiler/tests-spec/testData/diagnostics/notLinked/contracts/analysis/controlFlow/initialization/neg/1.kt @@ -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 { value_1 = 10 } + value_1.inc() +} + +fun case_2() { + val value_1: Int + funWithAtMostOnceCallsInPlace { value_1 = 10 } + value_1.inc() +} + +fun case_3() { + val value_1: Int + funWithUnknownCallsInPlace { value_1 = 10 } + value_1.inc() +} + +fun case_4() { + var value_1: Int + var value_2: Int + funWithAtMostOnceCallsInPlace { value_1 = 10 } + funWithUnknownCallsInPlace { value_2 = 10 } + value_1.dec() + value_2.div(10) +} + +class case_5 { + val value_1: Int + val value_2: Int + val value_3: Int + var value_4: Int + var value_5: Int + init { + funWithAtMostOnceCallsInPlace { value_1 = 1 } + funWithUnknownCallsInPlace { value_2 = 1 } + funWithAtLeastOnceCallsInPlace { value_3 = 1 } + funWithAtMostOnceCallsInPlace { value_4 = 2 } + funWithUnknownCallsInPlace { value_5 = 3 } + } +} + +fun case_6() { + val value_1: Int + for (i in 0..1) + funWithExactlyOnceCallsInPlace { value_1 = 10 } + value_1.dec() +} + +fun case_7() { + var value_1: Int + var i = 0 + while (i < 10) { + funWithExactlyOnceCallsInPlace { value_1 = 10 } + i++ + } + value_1.dec() +} + +fun case_8() { + var value_1: Int + if (true) funWithAtLeastOnceCallsInPlace { value_1 = 10 } + value_1.dec() +} diff --git a/compiler/tests-spec/testData/diagnostics/notLinked/contracts/analysis/controlFlow/initialization/neg/2.kt b/compiler/tests-spec/testData/diagnostics/notLinked/contracts/analysis/controlFlow/initialization/neg/2.kt new file mode 100644 index 00000000000..89b5d87b7de --- /dev/null +++ b/compiler/tests-spec/testData/diagnostics/notLinked/contracts/analysis/controlFlow/initialization/neg/2.kt @@ -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 { + 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 { + funWithAtMostOnceCallsInPlace { + value_1 = 1 + } + funWithAtLeastOnceCallsInPlace { + 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 { + value_1.inc() + } + value_1.inc() + } + value_1.inc() +} + +fun case_4() { + var value_1: Int + funWithAtLeastOnceCallsInPlace { + funWithUnknownCallsInPlace { + value_1 = 1 + } + funWithAtLeastOnceCallsInPlace { + value_1.inc() + } + funWithUnknownCallsInPlace { + value_1.inc() + } + funWithExactlyOnceCallsInPlace { + value_1.inc() + } + value_1.inc() + } + value_1.inc() +} diff --git a/compiler/tests-spec/testData/diagnostics/notLinked/contracts/analysis/controlFlow/initialization/neg/3.kt b/compiler/tests-spec/testData/diagnostics/notLinked/contracts/analysis/controlFlow/initialization/neg/3.kt new file mode 100644 index 00000000000..0561f3f1363 --- /dev/null +++ b/compiler/tests-spec/testData/diagnostics/notLinked/contracts/analysis/controlFlow/initialization/neg/3.kt @@ -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 + + 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 } + } + + 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 } + } + value_2.dec() + } + value_2.dec() +} + +class case_3(value_1: Any?) { + var value_2: Int + + init { + if (value_1 is String) { + funWithUnknownCallsInPlace { 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 { + when (value_1) { + _EnumClassSingle.EVERYTHING -> { + funWithExactlyOnceCallsInPlace { value_2 = 1 } + ++value_2 + } + null -> { + funWithUnknownCallsInPlace { value_2 = 2 } + } + } + 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 } + } + + value_2++ +} + +fun case_6() { + var value_2: Int + + try { + funWithAtLeastOnceCallsInPlace { value_2 = 10 } + } catch (e: Exception) { + throw Exception() + } finally { + println(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(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 { + x = 42 + } + return@outer + } + throw Exception() + } + println(x.inc()) +} + +fun case_10() { + val x: Int + funWithExactlyOnceCallsInPlace outer@ { + funWithAtMostOnceCallsInPlace { + 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@outer + } + println(x.inc()) +} diff --git a/compiler/tests-spec/testData/diagnostics/notLinked/contracts/analysis/controlFlow/initialization/neg/4.kt b/compiler/tests-spec/testData/diagnostics/notLinked/contracts/analysis/controlFlow/initialization/neg/4.kt new file mode 100644 index 00000000000..07a78d20456 --- /dev/null +++ b/compiler/tests-spec/testData/diagnostics/notLinked/contracts/analysis/controlFlow/initialization/neg/4.kt @@ -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 value_1 = 10 + value_1.inc() + } + value_1.inc() +} + +fun case_2() { + val value_1: Int + funWithExactlyOnceCallsInPlace { + val value_1: Int + funWithExactlyOnceCallsInPlace { + value_1 = 10 + } + funWithAtLeastOnceCallsInPlace { + value_1.inc() + } + value_1.inc() + } + value_1.inc() +} + +fun case_3() { + val value_1: Int + funWithAtLeastOnceCallsInPlace { + val value_1: Int + funWithAtMostOnceCallsInPlace { + value_1 = 10 + } + funWithAtMostOnceCallsInPlace { + value_1.inc() + } + value_1.inc() + } + funWithAtMostOnceCallsInPlace { + value_1 = 10 + } + value_1.inc() +} + +fun case_6() { + var value_1: Int + funWithAtLeastOnceCallsInPlace { + val value_1: Int + funWithExactlyOnceCallsInPlace { + value_1 = 10 + } + funWithAtMostOnceCallsInPlace { + value_1.inc() + } + value_1.inc() + } + funWithAtMostOnceCallsInPlace { + value_1 = 1 + } + value_1.dec() +} + +fun case_7() { + val value_1: Int + funWithUnknownCallsInPlace { + var value_1: Int + funWithAtLeastOnceCallsInPlace { + value_1 = 10 + } + funWithUnknownCallsInPlace { + value_1.inc() + } + value_1.inc() + } + funWithUnknownCallsInPlace { + value_1 = 1 + } + value_1.dec() +} diff --git a/compiler/tests-spec/testData/diagnostics/notLinked/contracts/analysis/controlFlow/initialization/neg/5.kt b/compiler/tests-spec/testData/diagnostics/notLinked/contracts/analysis/controlFlow/initialization/neg/5.kt new file mode 100644 index 00000000000..d3d8d25f289 --- /dev/null +++ b/compiler/tests-spec/testData/diagnostics/notLinked/contracts/analysis/controlFlow/initialization/neg/5.kt @@ -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({ value_1 = 10 }) + value_1.inc() +} + +fun case_2() { + var value_1: Int + val l = { value_1 = 10 } + funWithAtLeastOnceCallsInPlace(l) + value_1.inc() +} + +fun case_3() { + var value_1: Int + val l = fun () { value_1 = 10 } + funWithAtLeastOnceCallsInPlace(l) + value_1.inc() +} + +fun case_4() { + var value_1: Int + funWithAtLeastOnceCallsInPlace(fun () { value_1 = 10 }) + value_1.inc() +} + +fun case_5() { + val value_1: Int + val o = object { + fun l() { value_1 = 10 } + } + funWithExactlyOnceCallsInPlace(o::l) + value_1.inc() +} diff --git a/compiler/tests-spec/testData/diagnostics/notLinked/contracts/analysis/controlFlow/initialization/neg/6.kt b/compiler/tests-spec/testData/diagnostics/notLinked/contracts/analysis/controlFlow/initialization/neg/6.kt new file mode 100644 index 00000000000..bd9c9a46396 --- /dev/null +++ b/compiler/tests-spec/testData/diagnostics/notLinked/contracts/analysis/controlFlow/initialization/neg/6.kt @@ -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({ value_1 = 11 }) + value_1.inc() +} + +fun case_2() { + var value_1: Int + funWithAtLeastOnceCallsInPlace({ value_1 = 11 }) + value_1.inc() +} diff --git a/compiler/tests-spec/testData/diagnostics/notLinked/contracts/analysis/controlFlow/initialization/pos/1.kt b/compiler/tests-spec/testData/diagnostics/notLinked/contracts/analysis/controlFlow/initialization/pos/1.kt new file mode 100644 index 00000000000..404bcbe539c --- /dev/null +++ b/compiler/tests-spec/testData/diagnostics/notLinked/contracts/analysis/controlFlow/initialization/pos/1.kt @@ -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 } + } +} diff --git a/compiler/tests-spec/testData/diagnostics/notLinked/contracts/analysis/controlFlow/initialization/pos/2.kt b/compiler/tests-spec/testData/diagnostics/notLinked/contracts/analysis/controlFlow/initialization/pos/2.kt new file mode 100644 index 00000000000..cce23d59003 --- /dev/null +++ b/compiler/tests-spec/testData/diagnostics/notLinked/contracts/analysis/controlFlow/initialization/pos/2.kt @@ -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() + } + } + } +} + diff --git a/compiler/tests-spec/testData/diagnostics/notLinked/contracts/analysis/controlFlow/initialization/pos/3.kt b/compiler/tests-spec/testData/diagnostics/notLinked/contracts/analysis/controlFlow/initialization/pos/3.kt new file mode 100644 index 00000000000..0e6d4d4b9d3 --- /dev/null +++ b/compiler/tests-spec/testData/diagnostics/notLinked/contracts/analysis/controlFlow/initialization/pos/3.kt @@ -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 + + 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 { + 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()) +} diff --git a/compiler/tests-spec/testData/diagnostics/notLinked/contracts/analysis/controlFlow/initialization/pos/4.kt b/compiler/tests-spec/testData/diagnostics/notLinked/contracts/analysis/controlFlow/initialization/pos/4.kt new file mode 100644 index 00000000000..5487680921f --- /dev/null +++ b/compiler/tests-spec/testData/diagnostics/notLinked/contracts/analysis/controlFlow/initialization/pos/4.kt @@ -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 value_1: Int + funWithExactlyOnceCallsInPlace { + val value_1 = 10 + value_1.inc() + } +} + +fun case_2() { + val value_1: Int + funWithExactlyOnceCallsInPlace { + val value_1: Int + funWithExactlyOnceCallsInPlace { + value_1 = 10 + } + funWithAtLeastOnceCallsInPlace { + value_1.inc() + } + value_1.inc() + } +} + +fun case_3() { + val value_1: Int + funWithAtLeastOnceCallsInPlace { + val 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 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 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 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 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 value_1: Int + funWithAtLeastOnceCallsInPlace { + value_1 = 10 + } + funWithAtLeastOnceCallsInPlace { + value_1.inc() + } + value_1++ + } + funWithAtLeastOnceCallsInPlace { + value_1 = 1 + } + value_1-- +} + + diff --git a/compiler/tests-spec/testData/diagnostics/notLinked/contracts/analysis/controlFlow/initialization/pos/5.kt b/compiler/tests-spec/testData/diagnostics/notLinked/contracts/analysis/controlFlow/initialization/pos/5.kt new file mode 100644 index 00000000000..f3f7c40824b --- /dev/null +++ b/compiler/tests-spec/testData/diagnostics/notLinked/contracts/analysis/controlFlow/initialization/pos/5.kt @@ -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() +} diff --git a/compiler/tests-spec/testData/diagnostics/notLinked/contracts/analysis/controlFlow/unreachableCode/neg/1.kt b/compiler/tests-spec/testData/diagnostics/notLinked/contracts/analysis/controlFlow/unreachableCode/neg/1.kt new file mode 100644 index 00000000000..e1f9bb32f8b --- /dev/null +++ b/compiler/tests-spec/testData/diagnostics/notLinked/contracts/analysis/controlFlow/unreachableCode/neg/1.kt @@ -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 + } + println("1") + } + + loop@ for (i in 0..10) { + funWithExactlyOnceCallsInPlace { + break@loop + } + println("1") + } +} + +fun case_2(value_1: Boolean) { + for (i in 0..10) { + funWithExactlyOnceCallsInPlace { + continue + } + println("1") + } + + loop@ while (value_1) { + funWithExactlyOnceCallsInPlace { + continue@loop + } + println("1") + } +} diff --git a/compiler/tests-spec/testData/diagnostics/notLinked/contracts/analysis/controlFlow/unreachableCode/pos/1.kt b/compiler/tests-spec/testData/diagnostics/notLinked/contracts/analysis/controlFlow/unreachableCode/pos/1.kt new file mode 100644 index 00000000000..a7fc62faaef --- /dev/null +++ b/compiler/tests-spec/testData/diagnostics/notLinked/contracts/analysis/controlFlow/unreachableCode/pos/1.kt @@ -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() + } + println("1") +} + +fun case_2() { + funWithAtLeastOnceCallsInPlace { + throw Exception() + } + println("1") +} + +fun case_3() { + funWithExactlyOnceCallsInPlace { + return + } + println("1") +} + +fun case_4() { + funWithAtLeastOnceCallsInPlace { + return + } + println("1") +} + +fun case_5(args: Array) { + fun nestedFun_1() { + funWithAtLeastOnceCallsInPlace { + return@nestedFun_1 + } + println("1") + } + fun nestedFun_2() { + args.forEach { + funWithAtLeastOnceCallsInPlace { + return@forEach + } + println("1") + } + } + fun nestedFun_3() { + fun nestedFun_4() { + funWithAtLeastOnceCallsInPlace { + return@nestedFun_4 + } + println("1") + } + println("1") + } +} + +fun case_6(args: Array) { + args.forEach { + funWithExactlyOnceCallsInPlace { + return@forEach + } + println("1") + } + args.forEach { + fun nestedFun_1() { + funWithExactlyOnceCallsInPlace { + return@nestedFun_1 + } + println("1") + } + } + args.forEach { + fun nestedFun_2() { + funWithExactlyOnceCallsInPlace { + return + } + println("1") + } + } +} + +fun case_7() { + val value_1 = funWithExactlyOnceCallsInPlace { + throw Exception() + println(1) + } + println(value_1) +} + + +fun case_8() { + println(funWithExactlyOnceCallsInPlace { return; println(1) }) + return +} diff --git a/compiler/tests-spec/testData/diagnostics/notLinked/contracts/analysis/controlFlow/unreachableCode/pos/2.kt b/compiler/tests-spec/testData/diagnostics/notLinked/contracts/analysis/controlFlow/unreachableCode/pos/2.kt new file mode 100644 index 00000000000..e0de282d281 --- /dev/null +++ b/compiler/tests-spec/testData/diagnostics/notLinked/contracts/analysis/controlFlow/unreachableCode/pos/2.kt @@ -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") +} diff --git a/compiler/tests-spec/testData/diagnostics/notLinked/contracts/analysis/controlFlow/unreachableCode/pos/3.kt b/compiler/tests-spec/testData/diagnostics/notLinked/contracts/analysis/controlFlow/unreachableCode/pos/3.kt new file mode 100644 index 00000000000..fcd9f4f8ea0 --- /dev/null +++ b/compiler/tests-spec/testData/diagnostics/notLinked/contracts/analysis/controlFlow/unreachableCode/pos/3.kt @@ -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() + } + println("1") +} + +fun case_2() { + funWithAtLeastOnceCallsInPlace { + throw Exception() + } + println("1") +} + +fun case_3() { + funWithExactlyOnceCallsInPlace { + return + } + println("1") +} + +fun case_4() { + funWithAtLeastOnceCallsInPlace { + return + } + println("1") +} + +fun case_5(args: Array) { + fun nestedFun_1() { + funWithAtLeastOnceCallsInPlace { + return@nestedFun_1 + } + println("1") + } + fun nestedFun_2() { + args.forEach { + funWithAtLeastOnceCallsInPlace { + return@forEach + } + println("1") + } + } + fun nestedFun_3() { + fun nestedFun_4() { + funWithAtLeastOnceCallsInPlace { + return@nestedFun_4 + } + println("1") + } + println("1") + } +} + +fun case_6(args: Array) { + args.forEach { + funWithExactlyOnceCallsInPlace { + return@forEach + } + println("1") + } + args.forEach { + fun nestedFun_1() { + funWithExactlyOnceCallsInPlace { + return@nestedFun_1 + } + println("1") + } + } + args.forEach { + fun nestedFun_2() { + funWithExactlyOnceCallsInPlace { + return + } + println("1") + } + } +} diff --git a/compiler/tests-spec/testData/diagnostics/notLinked/contracts/analysis/controlFlow/unreachableCode/pos/4.kt b/compiler/tests-spec/testData/diagnostics/notLinked/contracts/analysis/controlFlow/unreachableCode/pos/4.kt new file mode 100644 index 00000000000..acab5fcffec --- /dev/null +++ b/compiler/tests-spec/testData/diagnostics/notLinked/contracts/analysis/controlFlow/unreachableCode/pos/4.kt @@ -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() + } + println("1") + } + println("2") + } + println("3") +} + +fun case_2() { + funWithAtLeastOnceCallsInPlace { + funWithAtLeastOnceCallsInPlace label_1@ { + funWithAtLeastOnceCallsInPlace { + return@label_1 + } + println("1") + } + println("2") + } + funWithAtLeastOnceCallsInPlace { + return + } + println("3") +} + +fun case_3() { + funWithExactlyOnceCallsInPlace { + funWithExactlyOnceCallsInPlace { + funWithExactlyOnceCallsInPlace { + return@funWithExactlyOnceCallsInPlace + } + println("1") + } + println("2") + } + println("3") +} diff --git a/compiler/tests-spec/testData/diagnostics/notLinked/contracts/analysis/controlFlow/unreachableCode/pos/5.kt b/compiler/tests-spec/testData/diagnostics/notLinked/contracts/analysis/controlFlow/unreachableCode/pos/5.kt new file mode 100644 index 00000000000..42154b0bd25 --- /dev/null +++ b/compiler/tests-spec/testData/diagnostics/notLinked/contracts/analysis/controlFlow/unreachableCode/pos/5.kt @@ -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 + + when (b) { + true -> { + println(1) + return + } + false -> { + println(2) + throw Exception() + } + } + 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 -> funWithAtLeastOnceCallsInPlace { return } + } + println(3) + } + println(3) + } else { + throw Exception() + } + println(3) + } + } + println(3) + } + println(3) +} diff --git a/compiler/tests-spec/testData/diagnostics/notLinked/contracts/analysis/controlFlow/unreachableCode/pos/6.kt b/compiler/tests-spec/testData/diagnostics/notLinked/contracts/analysis/controlFlow/unreachableCode/pos/6.kt new file mode 100644 index 00000000000..69e6b145204 --- /dev/null +++ b/compiler/tests-spec/testData/diagnostics/notLinked/contracts/analysis/controlFlow/unreachableCode/pos/6.kt @@ -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 { + when (b) { + true -> { + println(1) + return + } + false -> { + println(2) + throw Exception() + } + } + } catch (e: Exception) { + if (c) { + return@funWithExactlyOnceCallsInPlace + } else { + return + } + } + 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) +} diff --git a/compiler/tests-spec/testData/diagnostics/notLinked/contracts/analysis/controlFlow/unreachableCode/pos/7.kt b/compiler/tests-spec/testData/diagnostics/notLinked/contracts/analysis/controlFlow/unreachableCode/pos/7.kt new file mode 100644 index 00000000000..f6d240f1540 --- /dev/null +++ b/compiler/tests-spec/testData/diagnostics/notLinked/contracts/analysis/controlFlow/unreachableCode/pos/7.kt @@ -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() } + println(1) +} diff --git a/compiler/tests-spec/testData/diagnostics/notLinked/contracts/analysis/controlflow/initialization/pos/5.txt b/compiler/tests-spec/testData/diagnostics/notLinked/contracts/analysis/controlflow/initialization/pos/5.txt new file mode 100644 index 00000000000..2fe9e532ee9 --- /dev/null +++ b/compiler/tests-spec/testData/diagnostics/notLinked/contracts/analysis/controlflow/initialization/pos/5.txt @@ -0,0 +1,9 @@ +package + +public fun case_1(): kotlin.Unit + +package contracts { + public fun case_1(/*0*/ x: kotlin.Double = ..., /*1*/ block: () -> kotlin.Unit): kotlin.Double + CallsInPlace(block, EXACTLY_ONCE) + +} diff --git a/compiler/tests-spec/testData/diagnostics/notLinked/contracts/analysis/controlflow/unreachableCode/pos/7.txt b/compiler/tests-spec/testData/diagnostics/notLinked/contracts/analysis/controlflow/unreachableCode/pos/7.txt new file mode 100644 index 00000000000..6e3641081c8 --- /dev/null +++ b/compiler/tests-spec/testData/diagnostics/notLinked/contracts/analysis/controlflow/unreachableCode/pos/7.txt @@ -0,0 +1,9 @@ +package + +public fun case_1(): kotlin.Unit + +package contracts { + public fun case_1(/*0*/ x: kotlin.Double = ..., /*1*/ block: () -> kotlin.Unit): kotlin.Double + CallsInPlace(block, AT_LEAST_ONCE) + +} diff --git a/compiler/tests-spec/testData/diagnostics/notLinked/contracts/analysis/smartcasts/neg/1.kt b/compiler/tests-spec/testData/diagnostics/notLinked/contracts/analysis/smartcasts/neg/1.kt new file mode 100644 index 00000000000..5143dee4e65 --- /dev/null +++ b/compiler/tests-spec/testData/diagnostics/notLinked/contracts/analysis/smartcasts/neg/1.kt @@ -0,0 +1,95 @@ +// !LANGUAGE: +AllowContractsForCustomFunctions +UseReturnsEffect +// !USE_EXPERIMENTAL: kotlin.contracts.ExperimentalContracts +// !WITH_CONTRACT_FUNCTIONS +// SKIP_TXT + +/* + KOTLIN DIAGNOSTICS NOT LINKED SPEC TEST (NEGATIVE) + + SECTION: contracts + CATEGORY: analysis, smartcasts + NUMBER: 1 + DESCRIPTION: Smartcasts using Returns effects with simple type checking, not-null conditions and custom condition (condition for smartcast outside contract). + */ + +fun case_1(value_1: Any?) { + funWithReturns(value_1 !is String) + println(value_1.length) +} + +fun case_2(value_1: Int?) { + funWithReturnsAndInvertCondition(value_1 != null) + println(value_1.inc()) +} + +fun case_3(value_1: Int?) { + funWithReturns(value_1 == null) + println(value_1.inc()) +} + +fun case_4(value_1: Any?) { + funWithReturnsAndInvertTypeCheck(value_1) + println(value_1.length) +} + +fun case_5(value_1: String?) { + funWithReturnsAndNullCheck(value_1) + println(value_1.length) +} + +fun case_6(value_1: String?) { + funWithReturnsAndNullCheck(value_1) + println(value_1.length) +} + +object case_7_object { + val prop_1: Int? = 10 +} +fun case_7() { + funWithReturns(case_7_object.prop_1 == null) + case_7_object.prop_1.inc() +} + +fun case_8(value_1: Any?) { + if (!funWithReturnsTrue(value_1 is String)) println(value_1.length) + if (!funWithReturnsTrueAndInvertCondition(value_1 !is String)) println(value_1.length) + if (funWithReturnsFalse(value_1 is String)) println(value_1.length) + if (funWithReturnsFalseAndInvertCondition(value_1 !is String)) println(value_1.length) + if (funWithReturnsNotNull(value_1 is String) == null) println(value_1.length) + if (!(funWithReturnsNotNull(value_1 is String) != null)) println(value_1.length) + if (!(funWithReturnsNull(value_1 is String) == null)) println(value_1.length) + if (funWithReturnsNull(value_1 is String) != null) println(value_1.length) +} + +fun case_9(value_1: String?) { + if (!funWithReturnsTrue(value_1 != null)) println(value_1.length) + if (!funWithReturnsTrueAndInvertCondition(value_1 == null)) println(value_1.length) + if (funWithReturnsFalse(value_1 != null)) println(value_1.length) + if (funWithReturnsFalseAndInvertCondition(value_1 == null)) println(value_1.length) + if (funWithReturnsNotNull(value_1 != null) == null) println(value_1.length) + if (funWithReturnsNotNullAndInvertCondition(value_1 == null) == null) println(value_1.length) + if (funWithReturnsNull(value_1 != null) != null) println(value_1.length) + if (funWithReturnsNullAndInvertCondition(value_1 == null) != null) println(value_1.length) +} + +fun case_10(value_1: Any?) { + if (!funWithReturnsTrueAndTypeCheck(value_1)) println(value_1.length) + if (!!funWithReturnsFalseAndTypeCheck(value_1)) println(value_1.length) + if (!(funWithReturnsNotNullAndTypeCheck(value_1) != null)) println(value_1.length) + if (!!(funWithReturnsNotNullAndTypeCheck(value_1) == null)) println(value_1.length) + if (!!(funWithReturnsNullAndTypeCheck(value_1) != null)) println(value_1.length) + if (!(funWithReturnsNullAndTypeCheck(value_1) == null)) println(value_1.length) +} + +fun case_11(value_1: Number?) { + if (!funWithReturnsTrueAndNotNullCheck(value_1)) println(value_1.toByte()) + if (!funWithReturnsTrueAndNullCheck(value_1)) println(value_1) + if (funWithReturnsFalseAndNotNullCheck(value_1)) println(value_1.toByte()) + if (funWithReturnsFalseAndNullCheck(value_1)) println(value_1) + if ((funWithReturnsNotNullAndNotNullCheck(value_1) == null)) println(value_1.toByte()) + if (!!!(funWithReturnsNotNullAndNotNullCheck(value_1) != null)) println(value_1.toByte()) + if (!!(funWithReturnsNotNullAndNullCheck(value_1) == null)) println(value_1) + if (!(funWithReturnsNullAndNotNullCheck(value_1) == null)) println(value_1.toByte()) + if (!!(funWithReturnsNullAndNotNullCheck(value_1) != null)) println(value_1.toByte()) + if (!!!(funWithReturnsNullAndNullCheck(value_1) == null)) println(value_1) +} diff --git a/compiler/tests-spec/testData/diagnostics/notLinked/contracts/analysis/smartcasts/neg/10.kt b/compiler/tests-spec/testData/diagnostics/notLinked/contracts/analysis/smartcasts/neg/10.kt new file mode 100644 index 00000000000..6fff28ba4a2 --- /dev/null +++ b/compiler/tests-spec/testData/diagnostics/notLinked/contracts/analysis/smartcasts/neg/10.kt @@ -0,0 +1,35 @@ +// !LANGUAGE: +AllowContractsForCustomFunctions +UseCallsInPlaceEffect +// !USE_EXPERIMENTAL: kotlin.contracts.ExperimentalContracts +// !WITH_CONTRACT_FUNCTIONS +// SKIP_TXT + +/* + KOTLIN DIAGNOSTICS NOT LINKED SPEC TEST (NEGATIVE) + + SECTION: contracts + CATEGORY: analysis, smartcasts + NUMBER: 10 + DESCRIPTION: Check the lack of smartcasts after non-null assertions or not-null value assignment in lambdas of contract function with 'unknown' or 'at most once' CallsInPlace effects. + */ + +fun case_1(arg: Int?) { + funWithAtMostOnceCallsInPlace { arg!! } + arg.inc() +} + +fun case_2(arg: Int?) { + funWithUnknownCallsInPlace { arg!! } + arg.inc() +} + +fun case_3() { + val value_1: Boolean? + funWithAtMostOnceCallsInPlace { value_1 = false } + value_1.not() +} + +fun case_4() { + val value_1: Boolean? + funWithUnknownCallsInPlace { value_1 = true } + value_1.not() +} diff --git a/compiler/tests-spec/testData/diagnostics/notLinked/contracts/analysis/smartcasts/neg/11.kt b/compiler/tests-spec/testData/diagnostics/notLinked/contracts/analysis/smartcasts/neg/11.kt new file mode 100644 index 00000000000..4772b344107 --- /dev/null +++ b/compiler/tests-spec/testData/diagnostics/notLinked/contracts/analysis/smartcasts/neg/11.kt @@ -0,0 +1,517 @@ +// !LANGUAGE: +AllowContractsForCustomFunctions +UseCallsInPlaceEffect +// !DIAGNOSTICS: -INVISIBLE_REFERENCE -INVISIBLE_MEMBER +// !USE_EXPERIMENTAL: kotlin.contracts.ExperimentalContracts +// !WITH_CONTRACT_FUNCTIONS + +/* + KOTLIN DIAGNOSTICS NOT LINKED SPEC TEST (NEGATIVE) + + SECTION: contracts + CATEGORY: analysis, smartcasts + NUMBER: 11 + DESCRIPTION: Check smartcasts using double negation (returnsFalse/invert type checking/not operator). + UNEXPECTED BEHAVIOUR + ISSUES: KT-26176 + */ + +// FILE: contracts.kt + +package contracts + +import kotlin.contracts.* + +fun case_1(x: Any?): Boolean { + contract { returns(true) implies (x !is Number) } + return x !is Number +} + +fun case_2(x: Any?): Boolean { + contract { returns(true) implies (x !is Number?) } + return x !is Number? +} + +fun case_16_1(value_1: Any?, value_2: Any?): Boolean { + contract { returns(true) implies (value_1 !is String || value_2 !is Number) } + return value_1 !is String || value_2 !is Number +} +fun case_16_2(value_1: Any?, value_2: Any?): Boolean { + contract { returns(false) implies (value_1 !is String || value_2 !is Number) } + return !(value_1 !is String || value_2 !is Number) +} +fun case_16_3(value_1: Any?, value_2: Any?): Boolean? { + contract { returnsNotNull() implies (value_1 !is String || value_2 !is Number) } + return if (value_1 !is String || value_2 !is Number) true else null +} +fun case_16_4(value_1: Any?, value_2: Any?): Boolean? { + contract { returns(null) implies (value_1 !is String || value_2 !is Number) } + return if (value_1 !is String || value_2 !is Number) null else true +} + +fun case_17_1(value_1: Any?, value_2: Any?): Boolean { + contract { returns(true) implies (value_1 !is String || value_2 != null) } + return value_1 !is String || value_2 != null +} +fun case_17_2(value_1: Any?, value_2: Any?): Boolean { + contract { returns(false) implies (value_1 !is String || value_2 != null) } + return !(value_1 !is String || value_2 != null) +} +fun case_17_3(value_1: Any?, value_2: Any?): Boolean? { + contract { returnsNotNull() implies (value_1 !is String || value_2 != null) } + return if (value_1 !is String || value_2 != null) true else null +} +fun case_17_4(value_1: Any?, value_2: Any?): Boolean? { + contract { returns(null) implies (value_1 !is String || value_2 != null) } + return if (value_1 !is String || value_2 != null) null else true +} + +fun case_18_1(value_1: Any?, value_2: Any?, value_3: Any?, value_4: Any?): Boolean { + contract { returns(true) implies (value_1 !is Float? || value_1 == null || value_2 == null || value_3 == null || value_4 == null) } + return value_1 !is Float? || value_1 == null || value_2 == null || value_3 == null || value_4 == null +} +fun case_18_2(value_1: Any?, value_2: Any?, value_3: Any?, value_4: Any?): Boolean { + contract { returns(false) implies (value_1 !is Float? || value_1 == null || value_2 == null || value_3 == null || value_4 == null) } + return !(value_1 !is Float? || value_1 == null || value_2 == null || value_3 == null || value_4 == null) +} +fun case_18_3(value_1: Any?, value_2: Any?, value_3: Any?, value_4: Any?): Boolean? { + contract { returnsNotNull() implies (value_1 !is Float? || value_1 == null || value_2 == null || value_3 == null || value_4 == null) } + return if (value_1 !is Float? || value_1 == null || value_2 == null || value_3 == null || value_4 == null) true else null +} +fun case_18_4(value_1: Any?, value_2: Any?, value_3: Any?, value_4: Any?): Boolean? { + contract { returns(null) implies (value_1 !is Float? || value_1 == null || value_2 == null || value_3 == null || value_4 == null) } + return if (value_1 !is Float? || value_1 == null || value_2 == null || value_3 == null || value_4 == null) null else true +} + +fun T.case_19_1(): Boolean { + contract { returns(true) implies (this@case_19_1 !is String) } + return this@case_19_1 !is String +} +fun T.case_19_2(): Boolean { + contract { returns(false) implies (this@case_19_2 is String) } + return !(this@case_19_2 is String) +} +fun T.case_19_3(): Boolean? { + contract { returnsNotNull() implies (this@case_19_3 is String) } + return if (this@case_19_3 is String) true else null +} +fun T.case_19_4(): Boolean? { + contract { returns(null) implies (this@case_19_4 is String) } + return if (this@case_19_4 is String) null else true +} + +fun T.case_20_1(): Boolean { + contract { returns(true) implies (this@case_20_1 !is Int) } + return this@case_20_1 !is Int +} +fun T.case_20_2(): Boolean { + contract { returns(false) implies (this@case_20_2 is Int) } + return !(this@case_20_2 is Int) +} +fun T.case_20_3(): Boolean? { + contract { returnsNotNull() implies (this@case_20_3 is Int) } + return if (this@case_20_3 is Int) true else null +} +fun T.case_20_4(): Boolean? { + contract { returns(null) implies (this@case_20_4 is Int) } + return if (this@case_20_4 is Int) null else true +} + +fun String> T?.case_21_1(): Boolean { + contract { returns(true) implies (this@case_21_1 != null) } + return this@case_21_1 != null +} +fun String> T?.case_21_2(): Boolean { + contract { returns(true) implies (this@case_21_2 == null) } + return this@case_21_2 == null +} +fun String> T?.case_21_3(): Boolean { + contract { returns(false) implies (this@case_21_3 != null) } + return !(this@case_21_3 != null) +} + +fun T.case_22_1(): Boolean { + contract { returns(true) implies (this@case_22_1 != null) } + return this@case_22_1 != null +} +fun T.case_22_2(): Boolean { + contract { returns(true) implies (this@case_22_2 == null) } + return this@case_22_2 == null +} +fun String> T?.case_22_5(): Boolean? { + contract { returnsNotNull() implies (this@case_22_5 != null) } + return if (this@case_22_5 != null) true else null +} +fun String> T?.case_22_7(): Boolean? { + contract { returns(null) implies (this@case_22_7 != null) } + return if (this@case_22_7 != null) null else true +} + +fun T?.case_23_1(): Boolean { + contract { returns(false) implies (this@case_23_1 == null || this@case_23_1 !is String) } + return !(this@case_23_1 == null || this@case_23_1 !is String) +} +fun T?.case_23_2(): Boolean? { + contract { returnsNotNull() implies (this@case_23_2 == null || this@case_23_2 !is String) } + return if (this@case_23_2 == null || this@case_23_2 !is String) true else null +} +fun T?.case_23_3(): Boolean? { + contract { returns(null) implies (this@case_23_3 == null || this@case_23_3 !is String) } + return if (this@case_23_3 == null || this@case_23_3 !is String) null else true +} + +fun T.case_24_1(): Boolean { + contract { returns(false) implies (this@case_24_1 !is Int || this@case_24_1 == null) } + return !(this@case_24_1 !is Int || this@case_24_1 == null) +} +fun T.case_24_2(): Boolean? { + contract { returnsNotNull() implies (this@case_24_2 !is Int || this@case_24_2 == null) } + return if (this@case_24_2 !is Int || this@case_24_2 == null) true else null +} +fun T.case_24_3(): Boolean? { + contract { returns(null) implies (this@case_24_3 !is Int || this@case_24_3 == null) } + return if (this@case_24_3 !is Int || this@case_24_3 == null) null else true +} + +inline fun T?.case_25_1(): Boolean { + contract { returns(false) implies (this@case_25_1 !is Number || this@case_25_1 !is Int || this@case_25_1 == null) } + return !(this@case_25_1 !is Number || this@case_25_1 !is Int || this@case_25_1 == null) +} +inline fun T?.case_25_2(): Boolean? { + contract { returnsNotNull() implies (this@case_25_2 !is Number || this@case_25_2 !is Int || this@case_25_2 == null) } + return if (this@case_25_2 !is Number || this@case_25_2 !is Int || this@case_25_2 == null) true else null +} +inline fun T?.case_25_3(): Boolean? { + contract { returns(null) implies (this@case_25_3 !is Number || this@case_25_3 !is Int || this@case_25_3 == null) } + return if (this@case_25_3 !is Number || this@case_25_3 !is Int || this@case_25_3 == null) null else true +} + +fun T?.case_26_1(value_1: Int?): Boolean { + contract { returns(false) implies (this@case_26_1 == null || this@case_26_1 !is String || value_1 == null) } + return !(this@case_26_1 == null || this@case_26_1 !is String || value_1 == null) +} +fun T?.case_26_2(value_1: Int?): Boolean? { + contract { returnsNotNull() implies (this@case_26_2 == null || this@case_26_2 !is String || value_1 == null) } + return if (this@case_26_2 == null || this@case_26_2 !is String || value_1 == null) true else null +} +fun T?.case_26_3(value_1: Int?): Boolean? { + contract { returns(null) implies (this@case_26_3 == null || this@case_26_3 !is String || value_1 == null) } + return if (this@case_26_3 == null || this@case_26_3 !is String || value_1 == null) null else true +} + +// FILE: usages.kt + +import contracts.* + +fun case_1(value_1: Any?) { + if (!contracts.case_1(value_1)) println(value_1.toByte()) // no smartcast +} + +fun case_2(value_1: Any?) { + if (!contracts.case_2(value_1)) println(value_1?.toByte()) // no smartcast +} + +fun case_3(number: Int?) { + if (!funWithReturnsTrueAndNullCheck(number)) number.inc() // nullable receiver +} + +fun case_5(value_1: Any?) { + if (!funWithReturnsTrue(value_1 !is String)) println(value_1.length) +} + +fun case_6(value_1: Any?) { + if (!funWithReturnsTrueAndInvertCondition(value_1 is String)) println(value_1.length) + if (funWithReturnsFalse(value_1 !is String)) println(value_1.length) + if (funWithReturnsFalseAndInvertCondition(value_1 is String)) println(value_1.length) + if (!(funWithReturnsNotNullAndInvertCondition(value_1 !is String) != null)) println(value_1.length) + if (!(funWithReturnsNullAndInvertCondition(value_1 !is String) == null)) println(value_1.length) +} + +fun case_7(value_1: Any?) { + if (!funWithReturnsTrue(value_1 == null)) println(value_1.length) +} + +fun case_8(value_1: Any?) { + if (!funWithReturnsTrueAndInvertCondition(value_1 != null)) println(value_1.length) + if (funWithReturnsFalse(value_1 == null)) println(value_1.length) + if (funWithReturnsFalseAndInvertCondition(value_1 != null)) println(value_1.length) +} + +fun case_9(value_1: Any?) { + if (!funWithReturnsTrueAndInvertTypeCheck(value_1)) println(value_1.length) + if (funWithReturnsFalseAndInvertTypeCheck(value_1)) println(value_1.length) +} + +fun case_10(value_1: Number?) { + if (!funWithReturnsTrueAndNullCheck(value_1)) println(value_1.toByte()) + if (funWithReturnsFalseAndNullCheck(value_1)) println(value_1.toByte()) + if (funWithReturnsFalseAndNotNullCheck(value_1)) println(value_1) + if (!(funWithReturnsNotNullAndNullCheck(value_1) != null)) println(value_1) + if (!(funWithReturnsNullAndNullCheck(value_1) == null)) println(value_1) +} + +fun case_11(value_1: Any?, value_2: Any?) { + if (!funWithReturnsTrueAndInvertCondition(value_1 is String && value_2 is Number)) { + println(value_1.length) + println(value_2.toByte()) + } +} + +fun case_12(value_1: Any?, value_2: Any?) { + if (!funWithReturnsTrue(value_1 !is String || value_2 !is Number)) { + println(value_1.length) + println(value_2.toByte()) + } + if (funWithReturnsFalse(value_1 !is String || value_2 !is Number)) { + println(value_1.length) + println(value_2.toByte()) + } +} + +fun case_13(value_1: Any?, value_2: Any?) { + if (!funWithReturnsTrue(value_1 !is String || value_2 != null)) { + println(value_1.length) + println(value_2?.toByte()) + } + if (funWithReturnsFalse(value_1 !is Float? || value_1 == null || value_2 == null)) { + println(value_1.dec()) + println(value_2?.toByte()) + } + if (funWithReturnsNotNull(value_1 !is String || value_2 !is Number) == null) { + println(value_1.length) + println(value_2.toByte()) + } + if (funWithReturnsNull(value_1 !is String || value_2 !is Number) != null) { + println(value_1.length) + println(value_2.toByte()) + } +} + +fun case_14(value_1: Any?, value_2: Any?) { + if (!funWithReturnsTrueAndInvertCondition(value_1 is Float? && value_1 != null && value_2 != null)) { + println(value_1.dec()) + println(value_2?.toByte()) + } + if (funWithReturnsFalseAndInvertCondition(value_1 is String && value_2 is Number)) { + println(value_1.length) + println(value_2.toByte()) + } + if (funWithReturnsFalseAndInvertCondition(value_1 is String && value_2 == null)) { + println(value_1.length) + println(value_2?.toByte()) + } + if (funWithReturnsNotNullAndInvertCondition(value_1 is String && value_2 is Number) == null) { + println(value_1.length) + println(value_2.toByte()) + } + if (funWithReturnsNotNullAndInvertCondition(value_1 is String && value_2 == null) == null) { + println(value_1.length) + println(value_2?.toByte()) + } + if (funWithReturnsNotNull(value_1 is Float? && value_1 != null && value_2 != null) == null) { + println(value_1.dec()) + println(value_2?.toByte()) + } + if (funWithReturnsNullAndInvertCondition(value_1 is String && value_2 is Number) != null) { + println(value_1.length) + println(value_2.toByte()) + } + if (funWithReturnsNullAndInvertCondition(value_1 is String && value_2 == null) != null) { + println(value_1.length) + println(value_2?.toByte()) + } + if (funWithReturnsNull(value_1 is Float? && value_1 != null && value_2 != null) != null) { + println(value_1.dec()) + println(value_2?.toByte()) + } +} + +class case_15_class { + val prop_1: Int? = 10 + + fun case_15(value_1: Any?, value_2: Number?) { + val o = case_15_class() + if (!funWithReturnsTrueAndInvertCondition(value_1 is Float? && value_1 != null && value_2 != null && o.prop_1 != null)) { + println(value_1.dec()) + println(value_2?.toByte()) + println(o.prop_1.plus(3)) + } + if (funWithReturnsFalse(value_1 !is Float? || value_1 == null || value_2 == null || o.prop_1 == null || this.prop_1 == null)) { + println(value_1.dec()) + println(value_2?.toByte()) + println(o.prop_1.plus(3)) + } + if (funWithReturnsNotNull(value_1 !is Float? || value_1 == null || value_2 == null || o.prop_1 == null || this.prop_1 == null) == null) { + println(value_1.dec()) + println(value_2?.toByte()) + println(o.prop_1.plus(3)) + } + if (funWithReturnsNull(value_1 !is Float? || value_1 == null || value_2 == null || o.prop_1 == null || this.prop_1 == null) != null) { + println(value_1.dec()) + println(value_2?.toByte()) + println(o.prop_1.plus(3)) + } + } +} + +fun case_16(value_1: Any?, value_2: Any?) { + if (!contracts.case_16_1(value_1, value_2)) { + println(value_1.length) + println(value_2.toByte()) + } + if (contracts.case_16_2(value_1, value_2)) { + println(value_1.length) + println(value_2.toByte()) + } + if (!(contracts.case_16_3(value_1, value_2) != null)) { + println(value_1.length) + println(value_2.toByte()) + } + if (!(contracts.case_16_4(value_1, value_2) == null)) { + println(value_1.length) + println(value_2.toByte()) + } +} + +fun case_17(value_1: Any?, value_2: Any?) { + if (!contracts.case_17_1(value_1, value_2)) { + println(value_1.length) + println(value_2?.toByte()) + } + if (contracts.case_17_2(value_1, value_2)) { + println(value_1.length) + println(value_2?.toByte()) + } + if (contracts.case_17_3(value_1, value_2) == null) { + println(value_1.length) + println(value_2?.toByte()) + } + if (contracts.case_17_4(value_1, value_2) != null) { + println(value_1.length) + println(value_2?.toByte()) + } +} + +class case_18_class { + val prop_1: Int? = 10 + fun case_18(value_1: Any?, value_2: Number?) { + val o = case_18_class() + if (contracts.case_18_1(value_1, value_2, o.prop_1, this.prop_1)) { + println(value_1.dec()) + println(value_2?.toByte()) + println(o.prop_1.plus(3)) + println(this.prop_1.plus(3)) + } + if (contracts.case_18_2(value_1, value_2, o.prop_1, this.prop_1)) { + println(value_1.dec()) + println(value_2?.toByte()) + println(o.prop_1.plus(3)) + println(this.prop_1.plus(3)) + } + if (contracts.case_18_3(value_1, value_2, o.prop_1, this.prop_1) == null) { + println(value_1.dec()) + println(value_2?.toByte()) + println(o.prop_1.plus(3)) + println(this.prop_1.plus(3)) + } + if (contracts.case_18_4(value_1, value_2, o.prop_1, this.prop_1) != null) { + println(value_1.dec()) + println(value_2?.toByte()) + println(o.prop_1.plus(3)) + println(this.prop_1.plus(3)) + } + } +} + +fun case_19(value_1: Any?) { + if (!value_1.case_19_1()) println(value_1.length) + if (value_1.case_19_2()) println(value_1.length) + if (value_1.case_19_3() == null) println(value_1.length) + if (value_1.case_19_4() != null) println(value_1.length) +} + +fun case_20(value_1: Number) { + when { !value_1.case_20_1() -> println(value_1.inv()) } + when { value_1.case_20_2() -> println(value_1.inv()) } + when { value_1.case_20_3() == null -> println(value_1.inv()) } + when { value_1.case_20_4() != null -> println(value_1.inv()) } +} + +fun case_21(value_1: String?, value_2: String?, value_3: String?, value_4: String?) { + if (!value_1.case_21_1()) println(value_1) + if (!value_2.case_21_2()) println(value_2.length) + when (value_3.case_21_3()) { + true -> println(value_4.length) + false -> println(value_3) + } +} + +fun case_22(value_1: String?) { + when { !value_1.case_22_1() -> println(value_1) } + when { !value_1.case_22_2() -> println(value_1.length) } + when { + value_1.case_22_5() == null -> println(value_1.length) + value_1.case_22_5() != null -> println(value_1) + } + when { + value_1.case_22_7() != null -> println(value_1.length) + value_1.case_22_7() == null -> println(value_1) + } +} + +fun case_23(value_1: Any?, value_2: Any?) { + when { value_1.case_23_1() -> println(value_1.length) } + when { value_2.case_23_2() == null -> println(value_2.length) } + when { value_2.case_23_3() != null -> println(value_2.length) } +} + +fun case_24(value_1: Number?, value_2: Number?) { + if (value_1.case_24_1()) println(value_1.inv()) + if (value_2.case_24_2() == null) println(value_2.inv()) + if (value_2.case_24_3() != null) println(value_2.inv()) +} + +fun case_25(value_1: Any?, value_2: Any?) { + if (value_1.case_25_1()) println(value_1.inv()) + if (value_2.case_25_2() != null) println(value_2.inv()) + if (value_2.case_25_3() == null) println(value_2.inv()) +} + +fun case_26(value_1: Any?, value_2: Int?, value_3: Any?, value_4: Int?) { + when { + value_1.case_26_1(value_2) -> { + println(value_1.length) + println(value_2.inv()) + } + } + when { + value_3.case_26_2(value_4) == null -> { + println(value_3.length) + println(value_4.inv()) + } + } + when { + value_3.case_26_3(value_4) != null -> { + println(value_3.length) + println(value_4.inv()) + } + } +} + +/* + UNEXPECTED BEHAVIOUR + */ +fun case_27(value_1: Any?, value_2: Any?, value_3: Any?) { + funWithReturnsAndInvertCondition(value_1 !is String? || value_2 !is Number && value_3 !is Float) + println(value_1!!.length) + println(value_2.toByte()) + println(value_3.dec()) +} + +/* + UNEXPECTED BEHAVIOUR + */ +fun case_28(value_1: Any?, value_2: Any?, value_3: Any?) { + funWithReturnsAndInvertCondition(value_1 !is String || value_2 !is Number || value_3 !is Any?) + println(value_1!!.length) + println(value_2?.toByte()) + println(value_3.dec()) +} diff --git a/compiler/tests-spec/testData/diagnostics/notLinked/contracts/analysis/smartcasts/neg/11.txt b/compiler/tests-spec/testData/diagnostics/notLinked/contracts/analysis/smartcasts/neg/11.txt new file mode 100644 index 00000000000..34994fa3fac --- /dev/null +++ b/compiler/tests-spec/testData/diagnostics/notLinked/contracts/analysis/smartcasts/neg/11.txt @@ -0,0 +1,278 @@ +package + +public fun case_1(/*0*/ value_1: kotlin.Any?): kotlin.Unit +public fun case_10(/*0*/ value_1: kotlin.Number?): kotlin.Unit +public fun case_11(/*0*/ value_1: kotlin.Any?, /*1*/ value_2: kotlin.Any?): kotlin.Unit +public fun case_12(/*0*/ value_1: kotlin.Any?, /*1*/ value_2: kotlin.Any?): kotlin.Unit +public fun case_13(/*0*/ value_1: kotlin.Any?, /*1*/ value_2: kotlin.Any?): kotlin.Unit +public fun case_14(/*0*/ value_1: kotlin.Any?, /*1*/ value_2: kotlin.Any?): kotlin.Unit +public fun case_16(/*0*/ value_1: kotlin.Any?, /*1*/ value_2: kotlin.Any?): kotlin.Unit +public fun case_17(/*0*/ value_1: kotlin.Any?, /*1*/ value_2: kotlin.Any?): kotlin.Unit +public fun case_19(/*0*/ value_1: kotlin.Any?): kotlin.Unit +public fun case_2(/*0*/ value_1: kotlin.Any?): kotlin.Unit +public fun case_20(/*0*/ value_1: kotlin.Number): kotlin.Unit +public fun case_21(/*0*/ value_1: kotlin.String?, /*1*/ value_2: kotlin.String?, /*2*/ value_3: kotlin.String?, /*3*/ value_4: kotlin.String?): kotlin.Unit +public fun case_22(/*0*/ value_1: kotlin.String?): kotlin.Unit +public fun case_23(/*0*/ value_1: kotlin.Any?, /*1*/ value_2: kotlin.Any?): kotlin.Unit +public fun case_24(/*0*/ value_1: kotlin.Number?, /*1*/ value_2: kotlin.Number?): kotlin.Unit +public fun case_25(/*0*/ value_1: kotlin.Any?, /*1*/ value_2: kotlin.Any?): kotlin.Unit +public fun case_26(/*0*/ value_1: kotlin.Any?, /*1*/ value_2: kotlin.Int?, /*2*/ value_3: kotlin.Any?, /*3*/ value_4: kotlin.Int?): kotlin.Unit +public fun case_27(/*0*/ value_1: kotlin.Any?, /*1*/ value_2: kotlin.Any?, /*2*/ value_3: kotlin.Any?): kotlin.Unit +public fun case_28(/*0*/ value_1: kotlin.Any?, /*1*/ value_2: kotlin.Any?, /*2*/ value_3: kotlin.Any?): kotlin.Unit +public fun case_3(/*0*/ number: kotlin.Int?): kotlin.Unit +public fun case_5(/*0*/ value_1: kotlin.Any?): kotlin.Unit +public fun case_6(/*0*/ value_1: kotlin.Any?): kotlin.Unit +public fun case_7(/*0*/ value_1: kotlin.Any?): kotlin.Unit +public fun case_8(/*0*/ value_1: kotlin.Any?): kotlin.Unit +public fun case_9(/*0*/ value_1: kotlin.Any?): kotlin.Unit +public inline fun funWithAtLeastOnceCallsInPlace(/*0*/ block: () -> T): T + CallsInPlace(block, AT_LEAST_ONCE) + +public inline fun funWithAtLeastOnceCallsInPlace(/*0*/ block: () -> kotlin.Unit): kotlin.Unit + CallsInPlace(block, AT_LEAST_ONCE) + +public inline fun funWithAtMostOnceCallsInPlace(/*0*/ block: () -> kotlin.Unit): kotlin.Unit + CallsInPlace(block, AT_MOST_ONCE) + +public inline fun funWithExactlyOnceCallsInPlace(/*0*/ block: () -> T): T + CallsInPlace(block, EXACTLY_ONCE) + +public inline fun funWithExactlyOnceCallsInPlace(/*0*/ block: () -> kotlin.Unit): kotlin.Unit + CallsInPlace(block, EXACTLY_ONCE) + +public fun funWithReturns(/*0*/ cond: kotlin.Boolean): kotlin.Unit + Returns(WILDCARD) -> cond + +public fun funWithReturnsAndInvertCondition(/*0*/ cond: kotlin.Boolean): kotlin.Unit + Returns(WILDCARD) -> !cond + +public fun funWithReturnsAndInvertTypeCheck(/*0*/ value_1: kotlin.Any?): kotlin.Unit + Returns(WILDCARD) -> value_1 !is String + +public fun funWithReturnsAndNotNullCheck(/*0*/ value_1: kotlin.Any?): kotlin.Unit + Returns(WILDCARD) -> value_1 != null + +public fun funWithReturnsAndNullCheck(/*0*/ value_1: kotlin.Any?): kotlin.Unit + Returns(WILDCARD) -> value_1 == null + +public fun funWithReturnsAndTypeCheck(/*0*/ value_1: kotlin.Any?): kotlin.Unit + Returns(WILDCARD) -> value_1 is String + +public fun funWithReturnsFalse(/*0*/ cond: kotlin.Boolean): kotlin.Boolean + Returns(FALSE) -> cond + +public fun funWithReturnsFalseAndInvertCondition(/*0*/ cond: kotlin.Boolean): kotlin.Boolean + Returns(FALSE) -> !cond + +public fun funWithReturnsFalseAndInvertTypeCheck(/*0*/ value_1: kotlin.Any?): kotlin.Boolean + Returns(FALSE) -> value_1 !is String + +public fun funWithReturnsFalseAndNotNullCheck(/*0*/ value_1: kotlin.Number?): kotlin.Boolean + Returns(FALSE) -> value_1 != null + +public fun funWithReturnsFalseAndNullCheck(/*0*/ value_1: kotlin.Number?): kotlin.Boolean + Returns(FALSE) -> value_1 == null + +public fun funWithReturnsFalseAndTypeCheck(/*0*/ value_1: kotlin.Any?): kotlin.Boolean + Returns(FALSE) -> value_1 is String + +public fun funWithReturnsNotNull(/*0*/ cond: kotlin.Boolean): kotlin.Boolean? + Returns(NOT_NULL) -> cond + +public fun funWithReturnsNotNullAndInvertCondition(/*0*/ cond: kotlin.Boolean): kotlin.Boolean? + Returns(NOT_NULL) -> !cond + +public fun funWithReturnsNotNullAndInvertTypeCheck(/*0*/ value_1: kotlin.Any?): kotlin.Boolean? + Returns(NOT_NULL) -> value_1 !is String + +public fun funWithReturnsNotNullAndNotNullCheck(/*0*/ value_1: kotlin.Number?): kotlin.Boolean? + Returns(NOT_NULL) -> value_1 != null + +public fun funWithReturnsNotNullAndNullCheck(/*0*/ value_1: kotlin.Number?): kotlin.Boolean? + Returns(NOT_NULL) -> value_1 == null + +public fun funWithReturnsNotNullAndTypeCheck(/*0*/ value_1: kotlin.Any?): kotlin.Boolean? + Returns(NOT_NULL) -> value_1 is String + +public fun funWithReturnsNull(/*0*/ cond: kotlin.Boolean): kotlin.Boolean? + Returns(NULL) -> cond + +public fun funWithReturnsNullAndInvertCondition(/*0*/ cond: kotlin.Boolean): kotlin.Boolean? + Returns(NULL) -> !cond + +public fun funWithReturnsNullAndInvertTypeCheck(/*0*/ value_1: kotlin.Any?): kotlin.Boolean? + Returns(NULL) -> value_1 !is String + +public fun funWithReturnsNullAndNotNullCheck(/*0*/ value_1: kotlin.Number?): kotlin.Boolean? + Returns(NULL) -> value_1 != null + +public fun funWithReturnsNullAndNullCheck(/*0*/ value_1: kotlin.Number?): kotlin.Boolean? + Returns(NULL) -> value_1 == null + +public fun funWithReturnsNullAndTypeCheck(/*0*/ value_1: kotlin.Any?): kotlin.Boolean? + Returns(NULL) -> value_1 is String + +public fun funWithReturnsTrue(/*0*/ cond: kotlin.Boolean): kotlin.Boolean + Returns(TRUE) -> cond + +public fun funWithReturnsTrueAndInvertCondition(/*0*/ cond: kotlin.Boolean): kotlin.Boolean + Returns(TRUE) -> !cond + +public fun funWithReturnsTrueAndInvertTypeCheck(/*0*/ value_1: kotlin.Any?): kotlin.Boolean + Returns(TRUE) -> value_1 !is String + +public fun funWithReturnsTrueAndNotNullCheck(/*0*/ value_1: kotlin.Any?): kotlin.Boolean + Returns(TRUE) -> value_1 != null + +public fun funWithReturnsTrueAndNullCheck(/*0*/ value_1: kotlin.Any?): kotlin.Boolean + Returns(TRUE) -> value_1 == null + +public fun funWithReturnsTrueAndTypeCheck(/*0*/ value_1: kotlin.Any?): kotlin.Boolean + Returns(TRUE) -> value_1 is String + +public inline fun funWithUnknownCallsInPlace(/*0*/ block: () -> kotlin.Unit): kotlin.Unit + CallsInPlace(block, UNKNOWN) + +public final class case_15_class { + public constructor case_15_class() + public final val prop_1: kotlin.Int? = 10 + public final fun case_15(/*0*/ value_1: kotlin.Any?, /*1*/ value_2: kotlin.Number?): 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_18_class { + public constructor case_18_class() + public final val prop_1: kotlin.Int? = 10 + public final fun case_18(/*0*/ value_1: kotlin.Any?, /*1*/ value_2: kotlin.Number?): 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 +} + +package contracts { + public fun case_1(/*0*/ x: kotlin.Any?): kotlin.Boolean + Returns(TRUE) -> x !is Number + + public fun case_16_1(/*0*/ value_1: kotlin.Any?, /*1*/ value_2: kotlin.Any?): kotlin.Boolean + Returns(TRUE) -> value_1 !is String || value_2 !is Number + + public fun case_16_2(/*0*/ value_1: kotlin.Any?, /*1*/ value_2: kotlin.Any?): kotlin.Boolean + Returns(FALSE) -> value_1 !is String || value_2 !is Number + + public fun case_16_3(/*0*/ value_1: kotlin.Any?, /*1*/ value_2: kotlin.Any?): kotlin.Boolean? + Returns(NOT_NULL) -> value_1 !is String || value_2 !is Number + + public fun case_16_4(/*0*/ value_1: kotlin.Any?, /*1*/ value_2: kotlin.Any?): kotlin.Boolean? + Returns(NULL) -> value_1 !is String || value_2 !is Number + + public fun case_17_1(/*0*/ value_1: kotlin.Any?, /*1*/ value_2: kotlin.Any?): kotlin.Boolean + Returns(TRUE) -> value_1 !is String || value_2 != null + + public fun case_17_2(/*0*/ value_1: kotlin.Any?, /*1*/ value_2: kotlin.Any?): kotlin.Boolean + Returns(FALSE) -> value_1 !is String || value_2 != null + + public fun case_17_3(/*0*/ value_1: kotlin.Any?, /*1*/ value_2: kotlin.Any?): kotlin.Boolean? + Returns(NOT_NULL) -> value_1 !is String || value_2 != null + + public fun case_17_4(/*0*/ value_1: kotlin.Any?, /*1*/ value_2: kotlin.Any?): kotlin.Boolean? + Returns(NULL) -> value_1 !is String || value_2 != null + + public fun case_18_1(/*0*/ value_1: kotlin.Any?, /*1*/ value_2: kotlin.Any?, /*2*/ value_3: kotlin.Any?, /*3*/ value_4: kotlin.Any?): kotlin.Boolean + Returns(TRUE) -> value_1 !is Float? || value_1 == null || value_2 == null || value_3 == null || value_4 == null + + public fun case_18_2(/*0*/ value_1: kotlin.Any?, /*1*/ value_2: kotlin.Any?, /*2*/ value_3: kotlin.Any?, /*3*/ value_4: kotlin.Any?): kotlin.Boolean + Returns(FALSE) -> value_1 !is Float? || value_1 == null || value_2 == null || value_3 == null || value_4 == null + + public fun case_18_3(/*0*/ value_1: kotlin.Any?, /*1*/ value_2: kotlin.Any?, /*2*/ value_3: kotlin.Any?, /*3*/ value_4: kotlin.Any?): kotlin.Boolean? + Returns(NOT_NULL) -> value_1 !is Float? || value_1 == null || value_2 == null || value_3 == null || value_4 == null + + public fun case_18_4(/*0*/ value_1: kotlin.Any?, /*1*/ value_2: kotlin.Any?, /*2*/ value_3: kotlin.Any?, /*3*/ value_4: kotlin.Any?): kotlin.Boolean? + Returns(NULL) -> value_1 !is Float? || value_1 == null || value_2 == null || value_3 == null || value_4 == null + + public fun case_2(/*0*/ x: kotlin.Any?): kotlin.Boolean + Returns(TRUE) -> x !is Number? + + public fun T.case_19_1(): kotlin.Boolean + Returns(TRUE) -> !is String + + public fun T.case_19_2(): kotlin.Boolean + Returns(FALSE) -> is String + + public fun T.case_19_3(): kotlin.Boolean? + Returns(NOT_NULL) -> is String + + public fun T.case_19_4(): kotlin.Boolean? + Returns(NULL) -> is String + + public fun T.case_20_1(): kotlin.Boolean + Returns(TRUE) -> !is Int + + public fun T.case_20_2(): kotlin.Boolean + Returns(FALSE) -> is Int + + public fun T.case_20_3(): kotlin.Boolean? + Returns(NOT_NULL) -> is Int + + public fun T.case_20_4(): kotlin.Boolean? + Returns(NULL) -> is Int + + public fun T?.case_21_1(): kotlin.Boolean + Returns(TRUE) -> != null + + public fun T?.case_21_2(): kotlin.Boolean + Returns(TRUE) -> == null + + public fun T?.case_21_3(): kotlin.Boolean + Returns(FALSE) -> != null + + public fun T.case_22_1(): kotlin.Boolean + Returns(TRUE) -> != null + + public fun T.case_22_2(): kotlin.Boolean + Returns(TRUE) -> == null + + public fun T?.case_22_5(): kotlin.Boolean? + Returns(NOT_NULL) -> != null + + public fun T?.case_22_7(): kotlin.Boolean? + Returns(NULL) -> != null + + public fun T?.case_23_1(): kotlin.Boolean + Returns(FALSE) -> == null || !is String + + public fun T?.case_23_2(): kotlin.Boolean? + Returns(NOT_NULL) -> == null || !is String + + public fun T?.case_23_3(): kotlin.Boolean? + Returns(NULL) -> == null || !is String + + public fun T.case_24_1(): kotlin.Boolean + Returns(FALSE) -> !is Int || == null + + public fun T.case_24_2(): kotlin.Boolean? + Returns(NOT_NULL) -> !is Int || == null + + public fun T.case_24_3(): kotlin.Boolean? + Returns(NULL) -> !is Int || == null + + public inline fun T?.case_25_1(): kotlin.Boolean + Returns(FALSE) -> !is Number || !is Int || == null + + public inline fun T?.case_25_2(): kotlin.Boolean? + Returns(NOT_NULL) -> !is Number || !is Int || == null + + public inline fun T?.case_25_3(): kotlin.Boolean? + Returns(NULL) -> !is Number || !is Int || == null + + public fun T?.case_26_1(/*0*/ value_1: kotlin.Int?): kotlin.Boolean + Returns(FALSE) -> == null || !is String || value_1 == null + + public fun T?.case_26_2(/*0*/ value_1: kotlin.Int?): kotlin.Boolean? + Returns(NOT_NULL) -> == null || !is String || value_1 == null + + public fun T?.case_26_3(/*0*/ value_1: kotlin.Int?): kotlin.Boolean? + Returns(NULL) -> == null || !is String || value_1 == null + +} diff --git a/compiler/tests-spec/testData/diagnostics/notLinked/contracts/analysis/smartcasts/neg/12.kt b/compiler/tests-spec/testData/diagnostics/notLinked/contracts/analysis/smartcasts/neg/12.kt new file mode 100644 index 00000000000..d52201e3d7c --- /dev/null +++ b/compiler/tests-spec/testData/diagnostics/notLinked/contracts/analysis/smartcasts/neg/12.kt @@ -0,0 +1,81 @@ +// !LANGUAGE: +AllowContractsForCustomFunctions +UseCallsInPlaceEffect +// !DIAGNOSTICS: -INVISIBLE_REFERENCE -INVISIBLE_MEMBER +// !USE_EXPERIMENTAL: kotlin.contracts.ExperimentalContracts +// !WITH_CONTRACT_FUNCTIONS + +/* + KOTLIN DIAGNOSTICS NOT LINKED SPEC TEST (NEGATIVE) + + SECTION: contracts + CATEGORY: analysis, smartcasts + NUMBER: 12 + DESCRIPTION: Check smartcasts with passing same fields of instances of the same class in contract function with conjunction not-null condition. + UNEXPECTED BEHAVIOUR + ISSUES: KT-26300 + */ + +// FILE: contracts.kt + +package contracts + +import kotlin.contracts.* + +fun case_3(value_1: Any?, value_2: Any?, value_3: Any?, value_4: Any?) { + contract { returns() implies (value_1 is Float? && value_1 != null && value_2 != null && value_3 != null && value_4 != null) } + if (!(value_1 is Float? && value_1 != null && value_2 != null && value_3 != null && value_4 != null)) throw Exception() +} + +fun case_4(value_1: Any?, value_2: Any?, value_3: Any?, value_4: Any?): Boolean { + contract { returns(true) implies (value_1 is Float? && value_1 != null && value_2 != null && value_3 != null && value_4 != null) } + return value_1 is Float? && value_1 != null && value_2 != null && value_3 != null && value_4 != null +} + +// FILE: usages.kt + +import contracts.* + +class case_1 { + val prop_1: Int? = 10 + fun case_1(value_1: Any?, value_2: Number?) { + val o = case_1() + funWithReturns(value_1 is Float? && value_1 != null && value_2 != null && o.prop_1 != null && this.prop_1 != null) + println(o.prop_1.plus(3)) + println(this.prop_1.plus(3)) + } +} + +class case_2 { + val prop_1: Int? = 10 + fun case_2(value_1: Any?, value_2: Number?) { + val o = case_2() + if (funWithReturnsTrue(value_1 is Float? && value_1 != null && value_2 != null && o.prop_1 != null && this.prop_1 != null)) { + println(o.prop_1.plus(3)) + println(this.prop_1.plus(3)) + } + if (!funWithReturnsTrueAndInvertCondition(value_1 is Float? && value_1 != null && value_2 != null && o.prop_1 != null && this.prop_1 != null)) { + println(o.prop_1.plus(3)) + println(this.prop_1.plus(3)) + } + } +} + +class case_3 { + val prop_1: Int? = 10 + fun case_3(value_1: Any?, value_2: Number?) { + val o = case_3() + contracts.case_3(value_1, value_2, o.prop_1, this.prop_1) + println(o.prop_1.plus(3)) + println(this.prop_1.plus(3)) + } +} + +class case_4 { + val prop_1: Int? = 10 + fun case_4(value_1: Any?, value_2: Number?) { + val o = case_4() + if (contracts.case_4(value_1, value_2, o.prop_1, this.prop_1)) { + println(o.prop_1.plus(3)) + println(this.prop_1.plus(3)) + } + } +} diff --git a/compiler/tests-spec/testData/diagnostics/notLinked/contracts/analysis/smartcasts/neg/12.txt b/compiler/tests-spec/testData/diagnostics/notLinked/contracts/analysis/smartcasts/neg/12.txt new file mode 100644 index 00000000000..7c453c50349 --- /dev/null +++ b/compiler/tests-spec/testData/diagnostics/notLinked/contracts/analysis/smartcasts/neg/12.txt @@ -0,0 +1,154 @@ +package + +public inline fun funWithAtLeastOnceCallsInPlace(/*0*/ block: () -> T): T + CallsInPlace(block, AT_LEAST_ONCE) + +public inline fun funWithAtLeastOnceCallsInPlace(/*0*/ block: () -> kotlin.Unit): kotlin.Unit + CallsInPlace(block, AT_LEAST_ONCE) + +public inline fun funWithAtMostOnceCallsInPlace(/*0*/ block: () -> kotlin.Unit): kotlin.Unit + CallsInPlace(block, AT_MOST_ONCE) + +public inline fun funWithExactlyOnceCallsInPlace(/*0*/ block: () -> T): T + CallsInPlace(block, EXACTLY_ONCE) + +public inline fun funWithExactlyOnceCallsInPlace(/*0*/ block: () -> kotlin.Unit): kotlin.Unit + CallsInPlace(block, EXACTLY_ONCE) + +public fun funWithReturns(/*0*/ cond: kotlin.Boolean): kotlin.Unit + Returns(WILDCARD) -> cond + +public fun funWithReturnsAndInvertCondition(/*0*/ cond: kotlin.Boolean): kotlin.Unit + Returns(WILDCARD) -> !cond + +public fun funWithReturnsAndInvertTypeCheck(/*0*/ value_1: kotlin.Any?): kotlin.Unit + Returns(WILDCARD) -> value_1 !is String + +public fun funWithReturnsAndNotNullCheck(/*0*/ value_1: kotlin.Any?): kotlin.Unit + Returns(WILDCARD) -> value_1 != null + +public fun funWithReturnsAndNullCheck(/*0*/ value_1: kotlin.Any?): kotlin.Unit + Returns(WILDCARD) -> value_1 == null + +public fun funWithReturnsAndTypeCheck(/*0*/ value_1: kotlin.Any?): kotlin.Unit + Returns(WILDCARD) -> value_1 is String + +public fun funWithReturnsFalse(/*0*/ cond: kotlin.Boolean): kotlin.Boolean + Returns(FALSE) -> cond + +public fun funWithReturnsFalseAndInvertCondition(/*0*/ cond: kotlin.Boolean): kotlin.Boolean + Returns(FALSE) -> !cond + +public fun funWithReturnsFalseAndInvertTypeCheck(/*0*/ value_1: kotlin.Any?): kotlin.Boolean + Returns(FALSE) -> value_1 !is String + +public fun funWithReturnsFalseAndNotNullCheck(/*0*/ value_1: kotlin.Number?): kotlin.Boolean + Returns(FALSE) -> value_1 != null + +public fun funWithReturnsFalseAndNullCheck(/*0*/ value_1: kotlin.Number?): kotlin.Boolean + Returns(FALSE) -> value_1 == null + +public fun funWithReturnsFalseAndTypeCheck(/*0*/ value_1: kotlin.Any?): kotlin.Boolean + Returns(FALSE) -> value_1 is String + +public fun funWithReturnsNotNull(/*0*/ cond: kotlin.Boolean): kotlin.Boolean? + Returns(NOT_NULL) -> cond + +public fun funWithReturnsNotNullAndInvertCondition(/*0*/ cond: kotlin.Boolean): kotlin.Boolean? + Returns(NOT_NULL) -> !cond + +public fun funWithReturnsNotNullAndInvertTypeCheck(/*0*/ value_1: kotlin.Any?): kotlin.Boolean? + Returns(NOT_NULL) -> value_1 !is String + +public fun funWithReturnsNotNullAndNotNullCheck(/*0*/ value_1: kotlin.Number?): kotlin.Boolean? + Returns(NOT_NULL) -> value_1 != null + +public fun funWithReturnsNotNullAndNullCheck(/*0*/ value_1: kotlin.Number?): kotlin.Boolean? + Returns(NOT_NULL) -> value_1 == null + +public fun funWithReturnsNotNullAndTypeCheck(/*0*/ value_1: kotlin.Any?): kotlin.Boolean? + Returns(NOT_NULL) -> value_1 is String + +public fun funWithReturnsNull(/*0*/ cond: kotlin.Boolean): kotlin.Boolean? + Returns(NULL) -> cond + +public fun funWithReturnsNullAndInvertCondition(/*0*/ cond: kotlin.Boolean): kotlin.Boolean? + Returns(NULL) -> !cond + +public fun funWithReturnsNullAndInvertTypeCheck(/*0*/ value_1: kotlin.Any?): kotlin.Boolean? + Returns(NULL) -> value_1 !is String + +public fun funWithReturnsNullAndNotNullCheck(/*0*/ value_1: kotlin.Number?): kotlin.Boolean? + Returns(NULL) -> value_1 != null + +public fun funWithReturnsNullAndNullCheck(/*0*/ value_1: kotlin.Number?): kotlin.Boolean? + Returns(NULL) -> value_1 == null + +public fun funWithReturnsNullAndTypeCheck(/*0*/ value_1: kotlin.Any?): kotlin.Boolean? + Returns(NULL) -> value_1 is String + +public fun funWithReturnsTrue(/*0*/ cond: kotlin.Boolean): kotlin.Boolean + Returns(TRUE) -> cond + +public fun funWithReturnsTrueAndInvertCondition(/*0*/ cond: kotlin.Boolean): kotlin.Boolean + Returns(TRUE) -> !cond + +public fun funWithReturnsTrueAndInvertTypeCheck(/*0*/ value_1: kotlin.Any?): kotlin.Boolean + Returns(TRUE) -> value_1 !is String + +public fun funWithReturnsTrueAndNotNullCheck(/*0*/ value_1: kotlin.Any?): kotlin.Boolean + Returns(TRUE) -> value_1 != null + +public fun funWithReturnsTrueAndNullCheck(/*0*/ value_1: kotlin.Any?): kotlin.Boolean + Returns(TRUE) -> value_1 == null + +public fun funWithReturnsTrueAndTypeCheck(/*0*/ value_1: kotlin.Any?): kotlin.Boolean + Returns(TRUE) -> value_1 is String + +public inline fun funWithUnknownCallsInPlace(/*0*/ block: () -> kotlin.Unit): kotlin.Unit + CallsInPlace(block, UNKNOWN) + +public final class case_1 { + public constructor case_1() + public final val prop_1: kotlin.Int? = 10 + public final fun case_1(/*0*/ value_1: kotlin.Any?, /*1*/ value_2: kotlin.Number?): 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_2 { + public constructor case_2() + public final val prop_1: kotlin.Int? = 10 + public final fun case_2(/*0*/ value_1: kotlin.Any?, /*1*/ value_2: kotlin.Number?): 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_3 { + public constructor case_3() + public final val prop_1: kotlin.Int? = 10 + public final fun case_3(/*0*/ value_1: kotlin.Any?, /*1*/ value_2: kotlin.Number?): 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 { + public constructor case_4() + public final val prop_1: kotlin.Int? = 10 + public final fun case_4(/*0*/ value_1: kotlin.Any?, /*1*/ value_2: kotlin.Number?): 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 +} + +package contracts { + public fun case_3(/*0*/ value_1: kotlin.Any?, /*1*/ value_2: kotlin.Any?, /*2*/ value_3: kotlin.Any?, /*3*/ value_4: kotlin.Any?): kotlin.Unit + Returns(WILDCARD) -> value_1 is Float? && value_1 != null && value_2 != null && value_3 != null && value_4 != null + + public fun case_4(/*0*/ value_1: kotlin.Any?, /*1*/ value_2: kotlin.Any?, /*2*/ value_3: kotlin.Any?, /*3*/ value_4: kotlin.Any?): kotlin.Boolean + Returns(TRUE) -> value_1 is Float? && value_1 != null && value_2 != null && value_3 != null && value_4 != null + +} diff --git a/compiler/tests-spec/testData/diagnostics/notLinked/contracts/analysis/smartcasts/neg/13.kt b/compiler/tests-spec/testData/diagnostics/notLinked/contracts/analysis/smartcasts/neg/13.kt new file mode 100644 index 00000000000..14f257cb960 --- /dev/null +++ b/compiler/tests-spec/testData/diagnostics/notLinked/contracts/analysis/smartcasts/neg/13.kt @@ -0,0 +1,44 @@ +// !LANGUAGE: +AllowContractsForCustomFunctions +UseCallsInPlaceEffect +// !DIAGNOSTICS: -INVISIBLE_REFERENCE -INVISIBLE_MEMBER +// !USE_EXPERIMENTAL: kotlin.contracts.ExperimentalContracts + +/* + KOTLIN DIAGNOSTICS NOT LINKED SPEC TEST (NEGATIVE) + + SECTION: contracts + CATEGORY: analysis, smartcasts + NUMBER: 13 + DESCRIPTION: Check smartcast to upper bound of the types in disjunction. + UNEXPECTED BEHAVIOUR + ISSUES: KT-1982 + */ + +// FILE: contracts.kt + +package contracts + +import kotlin.contracts.* + +fun T?.case_1() { + contract { returns() implies (this@case_1 is Number || this@case_1 is Int) } + if (!(this@case_1 is Number || this@case_1 is Int)) throw Exception() +} + +inline fun T?.case_2(value_2: Number, value_3: Any?, value_4: String?) { + contract { returns() implies ((this@case_2 is Number || this@case_2 is Int) && value_2 is Int && value_3 != null && value_3 is Number && value_4 != null) } + if (!((this is Number || this is Int) && value_2 is Int && value_3 != null && value_3 is Number && value_4 != null)) throw Exception() +} + +// FILE: usages.kt + +import contracts.* + +fun case_1(value_1: Any?) { + value_1.case_1() + println(value_1.toByte()) +} + +fun case_2(value_1: Any?, value_2: Number, value_3: Any?, value_4: String?) { + value_1.case_2(value_2, value_3, value_4) + println(value_1.toByte()) +} diff --git a/compiler/tests-spec/testData/diagnostics/notLinked/contracts/analysis/smartcasts/neg/13.txt b/compiler/tests-spec/testData/diagnostics/notLinked/contracts/analysis/smartcasts/neg/13.txt new file mode 100644 index 00000000000..701299baed8 --- /dev/null +++ b/compiler/tests-spec/testData/diagnostics/notLinked/contracts/analysis/smartcasts/neg/13.txt @@ -0,0 +1,13 @@ +package + +public fun case_1(/*0*/ value_1: kotlin.Any?): kotlin.Unit +public fun case_2(/*0*/ value_1: kotlin.Any?, /*1*/ value_2: kotlin.Number, /*2*/ value_3: kotlin.Any?, /*3*/ value_4: kotlin.String?): kotlin.Unit + +package contracts { + public fun T?.case_1(): kotlin.Unit + Returns(WILDCARD) -> is Number || is Int + + public inline fun T?.case_2(/*0*/ value_2: kotlin.Number, /*1*/ value_3: kotlin.Any?, /*2*/ value_4: kotlin.String?): kotlin.Unit + Returns(WILDCARD) -> ( is Number || is Int) && value_2 is Int && value_3 != null && value_3 is Number && value_4 != null + +} diff --git a/compiler/tests-spec/testData/diagnostics/notLinked/contracts/analysis/smartcasts/neg/14.kt b/compiler/tests-spec/testData/diagnostics/notLinked/contracts/analysis/smartcasts/neg/14.kt new file mode 100644 index 00000000000..6b09e361653 --- /dev/null +++ b/compiler/tests-spec/testData/diagnostics/notLinked/contracts/analysis/smartcasts/neg/14.kt @@ -0,0 +1,59 @@ +// !LANGUAGE: +AllowContractsForCustomFunctions +UseReturnsEffect +// !WITH_CONTRACT_FUNCTIONS +// !DIAGNOSTICS: -INVISIBLE_REFERENCE -INVISIBLE_MEMBER +// !USE_EXPERIMENTAL: kotlin.contracts.ExperimentalContracts + +/* + KOTLIN DIAGNOSTICS NOT LINKED SPEC TEST (NEGATIVE) + + SECTION: contracts + CATEGORY: analysis, smartcasts + NUMBER: 14 + DESCRIPTION: Smartcast using many of the various Returns effects on the same values. + */ + +// FILE: contracts.kt + +package contracts + +import kotlin.contracts.* + +fun T?.case_1_1(): Boolean { + contract { returns(false) implies (this@case_1_1 != null) } + return !(this@case_1_1 != null) +} + +fun T?.case_1_2(): Boolean? { + contract { returns(null) implies (this@case_1_2 is String) } + return if (this@case_1_2 is String) null else true +} + +fun T?.case_2_1(): Boolean { + contract { returns(true) implies (this@case_2_1 is Float) } + return this@case_2_1 is Float +} + +fun T?.case_2_2(): Boolean { + contract { returns(false) implies (this@case_2_2 is Double) } + return !(this@case_2_2 is Double) +} + +// FILE: usages.kt + +import contracts.* + +fun case_1(value_1: Any?) { + if (!(value_1.case_1_1() || value_1.case_1_2() == null)) { + println(value_1.length) + } +} + +/* + UNEXPECTED BEHAVIOUR + ISSUES: KT-1982 + */ +fun case_2(value_1: Any?) { + if (value_1.case_2_1() || !value_1.case_2_2()) { + println(value_1.toByte()) + } +} diff --git a/compiler/tests-spec/testData/diagnostics/notLinked/contracts/analysis/smartcasts/neg/14.txt b/compiler/tests-spec/testData/diagnostics/notLinked/contracts/analysis/smartcasts/neg/14.txt new file mode 100644 index 00000000000..f7bb2754dee --- /dev/null +++ b/compiler/tests-spec/testData/diagnostics/notLinked/contracts/analysis/smartcasts/neg/14.txt @@ -0,0 +1,126 @@ +package + +public fun case_1(/*0*/ value_1: kotlin.Any?): kotlin.Unit +public fun case_2(/*0*/ value_1: kotlin.Any?): kotlin.Unit +public inline fun funWithAtLeastOnceCallsInPlace(/*0*/ block: () -> T): T + CallsInPlace(block, AT_LEAST_ONCE) + +public inline fun funWithAtLeastOnceCallsInPlace(/*0*/ block: () -> kotlin.Unit): kotlin.Unit + CallsInPlace(block, AT_LEAST_ONCE) + +public inline fun funWithAtMostOnceCallsInPlace(/*0*/ block: () -> kotlin.Unit): kotlin.Unit + CallsInPlace(block, AT_MOST_ONCE) + +public inline fun funWithExactlyOnceCallsInPlace(/*0*/ block: () -> T): T + CallsInPlace(block, EXACTLY_ONCE) + +public inline fun funWithExactlyOnceCallsInPlace(/*0*/ block: () -> kotlin.Unit): kotlin.Unit + CallsInPlace(block, EXACTLY_ONCE) + +public fun funWithReturns(/*0*/ cond: kotlin.Boolean): kotlin.Unit + Returns(WILDCARD) -> cond + +public fun funWithReturnsAndInvertCondition(/*0*/ cond: kotlin.Boolean): kotlin.Unit + Returns(WILDCARD) -> !cond + +public fun funWithReturnsAndInvertTypeCheck(/*0*/ value_1: kotlin.Any?): kotlin.Unit + Returns(WILDCARD) -> value_1 !is String + +public fun funWithReturnsAndNotNullCheck(/*0*/ value_1: kotlin.Any?): kotlin.Unit + Returns(WILDCARD) -> value_1 != null + +public fun funWithReturnsAndNullCheck(/*0*/ value_1: kotlin.Any?): kotlin.Unit + Returns(WILDCARD) -> value_1 == null + +public fun funWithReturnsAndTypeCheck(/*0*/ value_1: kotlin.Any?): kotlin.Unit + Returns(WILDCARD) -> value_1 is String + +public fun funWithReturnsFalse(/*0*/ cond: kotlin.Boolean): kotlin.Boolean + Returns(FALSE) -> cond + +public fun funWithReturnsFalseAndInvertCondition(/*0*/ cond: kotlin.Boolean): kotlin.Boolean + Returns(FALSE) -> !cond + +public fun funWithReturnsFalseAndInvertTypeCheck(/*0*/ value_1: kotlin.Any?): kotlin.Boolean + Returns(FALSE) -> value_1 !is String + +public fun funWithReturnsFalseAndNotNullCheck(/*0*/ value_1: kotlin.Number?): kotlin.Boolean + Returns(FALSE) -> value_1 != null + +public fun funWithReturnsFalseAndNullCheck(/*0*/ value_1: kotlin.Number?): kotlin.Boolean + Returns(FALSE) -> value_1 == null + +public fun funWithReturnsFalseAndTypeCheck(/*0*/ value_1: kotlin.Any?): kotlin.Boolean + Returns(FALSE) -> value_1 is String + +public fun funWithReturnsNotNull(/*0*/ cond: kotlin.Boolean): kotlin.Boolean? + Returns(NOT_NULL) -> cond + +public fun funWithReturnsNotNullAndInvertCondition(/*0*/ cond: kotlin.Boolean): kotlin.Boolean? + Returns(NOT_NULL) -> !cond + +public fun funWithReturnsNotNullAndInvertTypeCheck(/*0*/ value_1: kotlin.Any?): kotlin.Boolean? + Returns(NOT_NULL) -> value_1 !is String + +public fun funWithReturnsNotNullAndNotNullCheck(/*0*/ value_1: kotlin.Number?): kotlin.Boolean? + Returns(NOT_NULL) -> value_1 != null + +public fun funWithReturnsNotNullAndNullCheck(/*0*/ value_1: kotlin.Number?): kotlin.Boolean? + Returns(NOT_NULL) -> value_1 == null + +public fun funWithReturnsNotNullAndTypeCheck(/*0*/ value_1: kotlin.Any?): kotlin.Boolean? + Returns(NOT_NULL) -> value_1 is String + +public fun funWithReturnsNull(/*0*/ cond: kotlin.Boolean): kotlin.Boolean? + Returns(NULL) -> cond + +public fun funWithReturnsNullAndInvertCondition(/*0*/ cond: kotlin.Boolean): kotlin.Boolean? + Returns(NULL) -> !cond + +public fun funWithReturnsNullAndInvertTypeCheck(/*0*/ value_1: kotlin.Any?): kotlin.Boolean? + Returns(NULL) -> value_1 !is String + +public fun funWithReturnsNullAndNotNullCheck(/*0*/ value_1: kotlin.Number?): kotlin.Boolean? + Returns(NULL) -> value_1 != null + +public fun funWithReturnsNullAndNullCheck(/*0*/ value_1: kotlin.Number?): kotlin.Boolean? + Returns(NULL) -> value_1 == null + +public fun funWithReturnsNullAndTypeCheck(/*0*/ value_1: kotlin.Any?): kotlin.Boolean? + Returns(NULL) -> value_1 is String + +public fun funWithReturnsTrue(/*0*/ cond: kotlin.Boolean): kotlin.Boolean + Returns(TRUE) -> cond + +public fun funWithReturnsTrueAndInvertCondition(/*0*/ cond: kotlin.Boolean): kotlin.Boolean + Returns(TRUE) -> !cond + +public fun funWithReturnsTrueAndInvertTypeCheck(/*0*/ value_1: kotlin.Any?): kotlin.Boolean + Returns(TRUE) -> value_1 !is String + +public fun funWithReturnsTrueAndNotNullCheck(/*0*/ value_1: kotlin.Any?): kotlin.Boolean + Returns(TRUE) -> value_1 != null + +public fun funWithReturnsTrueAndNullCheck(/*0*/ value_1: kotlin.Any?): kotlin.Boolean + Returns(TRUE) -> value_1 == null + +public fun funWithReturnsTrueAndTypeCheck(/*0*/ value_1: kotlin.Any?): kotlin.Boolean + Returns(TRUE) -> value_1 is String + +public inline fun funWithUnknownCallsInPlace(/*0*/ block: () -> kotlin.Unit): kotlin.Unit + CallsInPlace(block, UNKNOWN) + +package contracts { + public fun T?.case_1_1(): kotlin.Boolean + Returns(FALSE) -> != null + + public fun T?.case_1_2(): kotlin.Boolean? + Returns(NULL) -> is String + + public fun T?.case_2_1(): kotlin.Boolean + Returns(TRUE) -> is Float + + public fun T?.case_2_2(): kotlin.Boolean + Returns(FALSE) -> is Double + +} diff --git a/compiler/tests-spec/testData/diagnostics/notLinked/contracts/analysis/smartcasts/neg/2.kt b/compiler/tests-spec/testData/diagnostics/notLinked/contracts/analysis/smartcasts/neg/2.kt new file mode 100644 index 00000000000..dc3ec5c4fbb --- /dev/null +++ b/compiler/tests-spec/testData/diagnostics/notLinked/contracts/analysis/smartcasts/neg/2.kt @@ -0,0 +1,153 @@ +// !LANGUAGE: +AllowContractsForCustomFunctions +UseReturnsEffect +// !USE_EXPERIMENTAL: kotlin.contracts.ExperimentalContracts +// !WITH_CONTRACT_FUNCTIONS +// SKIP_TXT + +/* + KOTLIN DIAGNOSTICS NOT LINKED SPEC TEST (NEGATIVE) + + SECTION: contracts + CATEGORY: analysis, smartcasts + NUMBER: 2 + DESCRIPTION: Smartcasts using Returns effects with complex (conjunction/disjunction) type checking and not-null conditions outside contract (custom condition). + */ + +fun case_1(value_1: Any?, value_2: Any?) { + funWithReturns(value_1 !is String || value_2 !is Number) + println(value_1.length) + println(value_2.toByte()) +} + +fun case_2(value_1: Any?, value_2: Any?) { + funWithReturnsAndInvertCondition(value_1 is String && value_2 is Number) + println(value_1.length) + println(value_2.toByte()) +} + +fun case_3(value_1: Any?, value_2: Any?) { + funWithReturnsAndInvertCondition(value_1 is String && value_2 == null) + println(value_1.length) + println(value_2?.toByte()) +} + +fun case_4(value_1: Any?, value_2: Number?) { + funWithReturns(value_1 !is Float? || value_1 == null || value_2 == null) + println(value_1.dec()) + println(value_2?.toByte()) +} + +class case_5_class { + val prop_1: Int? = 10 + + fun case_5(value_1: Any?, value_2: Number?) { + val o = case_5_class() + funWithReturns(value_1 !is Float? || value_1 == null || value_2 == null || o.prop_1 == null) + println(value_1.dec()) + println(value_2?.toByte()) + println(o.prop_1.plus(3)) + } +} + +fun case_6(value_1: Any?, value_2: Any) { + if (funWithReturnsTrue(value_1 !is String || value_2 !is Number)) { + println(value_1.length) + println(value_2.toByte()) + } + if (!funWithReturnsFalse(value_1 !is String || value_2 !is Number)) { + println(value_1.length) + println(value_2.toByte()) + } + if (funWithReturnsNotNull(value_1 !is String || value_2 !is Number) != null) { + println(value_1.length) + println(value_2.toByte()) + } + if (funWithReturnsNull(value_1 !is String || value_2 !is Number) == null) { + println(value_1.length) + println(value_2.toByte()) + } +} + +fun case_7(value_1: Any?, value_2: Any?) { + if (funWithReturnsTrueAndInvertCondition(value_1 is String && value_2 is Number)) { + println(value_1.length) + println(value_2.toByte()) + } + if (!funWithReturnsFalseAndInvertCondition(value_1 is String && value_2 is Number)) { + println(value_1.length) + println(value_2.toByte()) + } + if (funWithReturnsNotNullAndInvertCondition(value_1 is String && value_2 is Number) != null) { + println(value_1.length) + println(value_2.toByte()) + } + if (funWithReturnsNullAndInvertCondition(value_1 is String && value_2 is Number) == null) { + println(value_1.length) + println(value_2.toByte()) + } +} + +fun case_8(value_1: Any?, value_2: Any?) { + if (funWithReturnsTrueAndInvertCondition(value_1 is String && value_2 == null)) { + println(value_1.length) + println(value_2?.toByte()) + } + if (!funWithReturnsFalseAndInvertCondition(value_1 is String && value_2 == null)) { + println(value_1.length) + println(value_2?.toByte()) + } + if (funWithReturnsNotNullAndInvertCondition(value_1 is String && value_2 == null) != null) { + println(value_1.length) + println(value_2?.toByte()) + } + if (funWithReturnsNullAndInvertCondition(value_1 is String && value_2 == null) == null) { + println(value_1.length) + println(value_2?.toByte()) + } +} + +fun case_9(value_1: Any?, value_2: Number?) { + if (funWithReturnsTrue(value_1 !is Float? || value_1 == null || value_2 == null)) { + println(value_1.dec()) + println(value_2?.toByte()) + } + if (!funWithReturnsFalse(value_1 !is Float? || value_1 == null || value_2 == null)) { + println(value_1.dec()) + println(value_2?.toByte()) + } + if (funWithReturnsNotNull(value_1 is Float? && value_1 != null && value_2 != null) == null) { + println(value_1.dec()) + println(value_2?.toByte()) + } + if (funWithReturnsNull(value_1 is Float? && value_1 != null && value_2 != null) != null) { + println(value_1.dec()) + println(value_2?.toByte()) + } +} + +class case_10_class { + val prop_1: Int? = 10 + + fun case_10(value_1: Any?, value_2: Number?) { + val o = case_10_class() + if (funWithReturnsTrue(value_1 !is Float? || value_1 == null || value_2 == null || o.prop_1 == null || this.prop_1 == null)) { + println(value_1.dec()) + println(value_2?.toByte()) + println(o.prop_1.plus(3)) + } + if (!funWithReturnsFalse(value_1 !is Float? || value_1 == null || value_2 == null || o.prop_1 == null || this.prop_1 == null)) { + println(value_1.dec()) + println(value_2?.toByte()) + println(o.prop_1.plus(3)) + } + if (funWithReturnsNotNull(value_1 !is Float? || value_1 == null || value_2 == null || o.prop_1 == null || this.prop_1 == null) != null) { + println(value_1.dec()) + println(value_2?.toByte()) + println(o.prop_1.plus(3)) + } + if (funWithReturnsNull(value_1 !is Float? || value_1 == null || value_2 == null || o.prop_1 == null || this.prop_1 == null) == null) { + println(value_1.dec()) + println(value_2?.toByte()) + println(o.prop_1.plus(3)) + } + } +} diff --git a/compiler/tests-spec/testData/diagnostics/notLinked/contracts/analysis/smartcasts/neg/3.kt b/compiler/tests-spec/testData/diagnostics/notLinked/contracts/analysis/smartcasts/neg/3.kt new file mode 100644 index 00000000000..589c6a59a33 --- /dev/null +++ b/compiler/tests-spec/testData/diagnostics/notLinked/contracts/analysis/smartcasts/neg/3.kt @@ -0,0 +1,176 @@ +// !LANGUAGE: +AllowContractsForCustomFunctions +UseReturnsEffect +// !DIAGNOSTICS: -INVISIBLE_REFERENCE -INVISIBLE_MEMBER +// !USE_EXPERIMENTAL: kotlin.contracts.ExperimentalContracts + +/* + KOTLIN DIAGNOSTICS NOT LINKED SPEC TEST (NEGATIVE) + + SECTION: contracts + CATEGORY: analysis, smartcasts + NUMBER: 3 + DESCRIPTION: Smartcasts using Returns effects with complex (conjunction/disjunction) type checking and not-null conditions inside contract. + */ + +// FILE: contracts.kt + +package contracts + +import kotlin.contracts.* + +fun case_1(value_1: Any?, value_2: Any?) { + contract { returns() implies (value_1 !is String || value_2 !is Number) } + if (!(value_1 !is String || value_2 !is Number)) throw Exception() +} + +fun case_2(value_1: Any?, value_2: Any?) { + contract { returns() implies (value_1 !is String || value_2 != null) } + if (!(value_1 !is String || value_2 != null)) throw Exception() +} + +fun case_3(value_1: Any?, value_2: Any?, value_3: Any?, value_4: Any?) { + contract { returns() implies (value_1 !is Float? || value_1 == null || value_2 == null || value_3 == null || value_4 == null) } + if (!(value_1 !is Float? || value_1 == null || value_2 == null || value_3 == null || value_4 == null)) throw Exception() +} + +fun case_4_1(value_1: Any?, value_2: Any?): Boolean { + contract { returns(true) implies (value_1 !is String || value_2 !is Number) } + return value_1 !is String || value_2 !is Number +} +fun case_4_2(value_1: Any?, value_2: Any?): Boolean { + contract { returns(false) implies (value_1 !is String || value_2 !is Number) } + return !(value_1 !is String || value_2 !is Number) +} +fun case_4_3(value_1: Any?, value_2: Any?): Boolean? { + contract { returnsNotNull() implies (value_1 !is String || value_2 !is Number) } + return if (value_1 !is String || value_2 !is Number) true else null +} +fun case_4_4(value_1: Any?, value_2: Any?): Boolean? { + contract { returns(null) implies (value_1 !is String || value_2 !is Number) } + return if (value_1 !is String || value_2 !is Number) null else true +} + +fun case_5_1(value_1: Any?, value_2: Any?): Boolean { + contract { returns(true) implies (value_1 !is String || value_2 != null) } + return value_1 !is String || value_2 != null +} +fun case_5_2(value_1: Any?, value_2: Any?): Boolean { + contract { returns(false) implies (value_1 !is String || value_2 != null) } + return !(value_1 !is String || value_2 != null) +} +fun case_5_3(value_1: Any?, value_2: Any?): Boolean? { + contract { returnsNotNull() implies (value_1 is String && value_2 == null) } + return if (value_1 is String && value_2 == null) true else null +} +fun case_5_4(value_1: Any?, value_2: Any?): Boolean? { + contract { returns(null) implies (value_1 is String && value_2 == null) } + return if (value_1 is String && value_2 == null) null else true +} + +fun case_6_1(value_1: Any?, value_2: Any?, value_3: Any?, value_4: Any?): Boolean { + contract { returns(true) implies (value_1 !is Float? || value_1 == null || value_2 == null || value_3 == null || value_4 == null) } + return value_1 !is Float? || value_1 == null || value_2 == null || value_3 == null || value_4 == null +} +fun case_6_2(value_1: Any?, value_2: Any?, value_3: Any?, value_4: Any?): Boolean { + contract { returns(false) implies (value_1 !is Float? || value_1 == null || value_2 == null || value_3 == null || value_4 == null) } + return !(value_1 !is Float? || value_1 == null || value_2 == null || value_3 == null || value_4 == null) +} +fun case_6_3(value_1: Any?, value_2: Any?, value_3: Any?, value_4: Any?): Boolean? { + contract { returnsNotNull() implies (value_1 is Float? && value_1 != null && value_2 != null && value_3 != null && value_4 != null) } + return if (value_1 is Float? && value_1 != null && value_2 != null && value_3 != null && value_4 != null) true else null +} +fun case_6_4(value_1: Any?, value_2: Any?, value_3: Any?, value_4: Any?): Boolean? { + contract { returns(null) implies (value_1 is Float? && value_1 != null && value_2 != null && value_3 != null && value_4 != null) } + return if (value_1 is Float? && value_1 != null && value_2 != null && value_3 != null && value_4 != null) null else true +} + +// FILE: usages.kt + +import contracts.* + +fun case_1(value_1: Any?, value_2: Any?) { + contracts.case_1(value_1, value_2) + println(value_1.length) + println(value_2.toByte()) +} + +fun case_2(value_1: Any?, value_2: Any?) { + contracts.case_2(value_1, value_2) + println(value_1.length) + println(value_2?.toByte()) +} + +class case_3_class { + val prop_1: Int? = 10 + fun case_3(value_1: Any?, value_2: Number?) { + val o = case_3_class() + contracts.case_3(value_1, value_2, o.prop_1, this.prop_1) + println(value_1.dec()) + println(value_2?.toByte()) + println(o.prop_1.plus(3)) + } +} + +fun case_4(value_1: Any?, value_2: Any?) { + if (contracts.case_4_1(value_1, value_2)) { + println(value_1.length) + println(value_2.toByte()) + } + if (!contracts.case_4_2(value_1, value_2)) { + println(value_1.length) + println(value_2.toByte()) + } + if (contracts.case_4_3(value_1, value_2) != null) { + println(value_1.length) + println(value_2.toByte()) + } + if (contracts.case_4_4(value_1, value_2) == null) { + println(value_1.length) + println(value_2.toByte()) + } +} + +fun case_5(value_1: Any?, value_2: Any?) { + if (contracts.case_5_1(value_1, value_2)) { + println(value_1.length) + println(value_2?.toByte()) + } + if (!contracts.case_5_2(value_1, value_2)) { + println(value_1.length) + println(value_2?.toByte()) + } + if (contracts.case_5_3(value_1, value_2) == null) { + println(value_1.length) + println(value_2?.toByte()) + } + if (contracts.case_5_4(value_1, value_2) != null) { + println(value_1.length) + println(value_2?.toByte()) + } +} + +class case_6_class { + val prop_1: Int? = 10 + fun case_6(value_1: Any?, value_2: Number?) { + val o = case_6_class() + if (contracts.case_6_1(value_1, value_2, o.prop_1, this.prop_1)) { + println(value_1.dec()) + println(value_2?.toByte()) + println(o.prop_1.plus(3)) + } + if (!contracts.case_6_2(value_1, value_2, o.prop_1, this.prop_1)) { + println(value_1.dec()) + println(value_2?.toByte()) + println(o.prop_1.plus(3)) + } + if (contracts.case_6_3(value_1, value_2, o.prop_1, this.prop_1) == null) { + println(value_1.dec()) + println(value_2?.toByte()) + println(o.prop_1.plus(3)) + } + if (contracts.case_6_4(value_1, value_2, o.prop_1, this.prop_1) != null) { + println(value_1.dec()) + println(value_2?.toByte()) + println(o.prop_1.plus(3)) + } + } +} diff --git a/compiler/tests-spec/testData/diagnostics/notLinked/contracts/analysis/smartcasts/neg/3.txt b/compiler/tests-spec/testData/diagnostics/notLinked/contracts/analysis/smartcasts/neg/3.txt new file mode 100644 index 00000000000..c8109fccd32 --- /dev/null +++ b/compiler/tests-spec/testData/diagnostics/notLinked/contracts/analysis/smartcasts/neg/3.txt @@ -0,0 +1,72 @@ +package + +public fun case_1(/*0*/ value_1: kotlin.Any?, /*1*/ value_2: kotlin.Any?): kotlin.Unit +public fun case_2(/*0*/ value_1: kotlin.Any?, /*1*/ value_2: kotlin.Any?): kotlin.Unit +public fun case_4(/*0*/ value_1: kotlin.Any?, /*1*/ value_2: kotlin.Any?): kotlin.Unit +public fun case_5(/*0*/ value_1: kotlin.Any?, /*1*/ value_2: kotlin.Any?): kotlin.Unit + +public final class case_3_class { + public constructor case_3_class() + public final val prop_1: kotlin.Int? = 10 + public final fun case_3(/*0*/ value_1: kotlin.Any?, /*1*/ value_2: kotlin.Number?): 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_6_class { + public constructor case_6_class() + public final val prop_1: kotlin.Int? = 10 + public final fun case_6(/*0*/ value_1: kotlin.Any?, /*1*/ value_2: kotlin.Number?): 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 +} + +package contracts { + public fun case_1(/*0*/ value_1: kotlin.Any?, /*1*/ value_2: kotlin.Any?): kotlin.Unit + Returns(WILDCARD) -> value_1 !is String || value_2 !is Number + + public fun case_2(/*0*/ value_1: kotlin.Any?, /*1*/ value_2: kotlin.Any?): kotlin.Unit + Returns(WILDCARD) -> value_1 !is String || value_2 != null + + public fun case_3(/*0*/ value_1: kotlin.Any?, /*1*/ value_2: kotlin.Any?, /*2*/ value_3: kotlin.Any?, /*3*/ value_4: kotlin.Any?): kotlin.Unit + Returns(WILDCARD) -> value_1 !is Float? || value_1 == null || value_2 == null || value_3 == null || value_4 == null + + public fun case_4_1(/*0*/ value_1: kotlin.Any?, /*1*/ value_2: kotlin.Any?): kotlin.Boolean + Returns(TRUE) -> value_1 !is String || value_2 !is Number + + public fun case_4_2(/*0*/ value_1: kotlin.Any?, /*1*/ value_2: kotlin.Any?): kotlin.Boolean + Returns(FALSE) -> value_1 !is String || value_2 !is Number + + public fun case_4_3(/*0*/ value_1: kotlin.Any?, /*1*/ value_2: kotlin.Any?): kotlin.Boolean? + Returns(NOT_NULL) -> value_1 !is String || value_2 !is Number + + public fun case_4_4(/*0*/ value_1: kotlin.Any?, /*1*/ value_2: kotlin.Any?): kotlin.Boolean? + Returns(NULL) -> value_1 !is String || value_2 !is Number + + public fun case_5_1(/*0*/ value_1: kotlin.Any?, /*1*/ value_2: kotlin.Any?): kotlin.Boolean + Returns(TRUE) -> value_1 !is String || value_2 != null + + public fun case_5_2(/*0*/ value_1: kotlin.Any?, /*1*/ value_2: kotlin.Any?): kotlin.Boolean + Returns(FALSE) -> value_1 !is String || value_2 != null + + public fun case_5_3(/*0*/ value_1: kotlin.Any?, /*1*/ value_2: kotlin.Any?): kotlin.Boolean? + Returns(NOT_NULL) -> value_1 is String && value_2 == null + + public fun case_5_4(/*0*/ value_1: kotlin.Any?, /*1*/ value_2: kotlin.Any?): kotlin.Boolean? + Returns(NULL) -> value_1 is String && value_2 == null + + public fun case_6_1(/*0*/ value_1: kotlin.Any?, /*1*/ value_2: kotlin.Any?, /*2*/ value_3: kotlin.Any?, /*3*/ value_4: kotlin.Any?): kotlin.Boolean + Returns(TRUE) -> value_1 !is Float? || value_1 == null || value_2 == null || value_3 == null || value_4 == null + + public fun case_6_2(/*0*/ value_1: kotlin.Any?, /*1*/ value_2: kotlin.Any?, /*2*/ value_3: kotlin.Any?, /*3*/ value_4: kotlin.Any?): kotlin.Boolean + Returns(FALSE) -> value_1 !is Float? || value_1 == null || value_2 == null || value_3 == null || value_4 == null + + public fun case_6_3(/*0*/ value_1: kotlin.Any?, /*1*/ value_2: kotlin.Any?, /*2*/ value_3: kotlin.Any?, /*3*/ value_4: kotlin.Any?): kotlin.Boolean? + Returns(NOT_NULL) -> value_1 is Float? && value_1 != null && value_2 != null && value_3 != null && value_4 != null + + public fun case_6_4(/*0*/ value_1: kotlin.Any?, /*1*/ value_2: kotlin.Any?, /*2*/ value_3: kotlin.Any?, /*3*/ value_4: kotlin.Any?): kotlin.Boolean? + Returns(NULL) -> value_1 is Float? && value_1 != null && value_2 != null && value_3 != null && value_4 != null + +} diff --git a/compiler/tests-spec/testData/diagnostics/notLinked/contracts/analysis/smartcasts/neg/4.kt b/compiler/tests-spec/testData/diagnostics/notLinked/contracts/analysis/smartcasts/neg/4.kt new file mode 100644 index 00000000000..2a53f560662 --- /dev/null +++ b/compiler/tests-spec/testData/diagnostics/notLinked/contracts/analysis/smartcasts/neg/4.kt @@ -0,0 +1,192 @@ +// !LANGUAGE: +AllowContractsForCustomFunctions +UseReturnsEffect +// !DIAGNOSTICS: -INVISIBLE_REFERENCE -INVISIBLE_MEMBER +// !USE_EXPERIMENTAL: kotlin.contracts.ExperimentalContracts + +/* + KOTLIN DIAGNOSTICS NOT LINKED SPEC TEST (NEGATIVE) + + SECTION: contracts + CATEGORY: analysis, smartcasts + NUMBER: 4 + DESCRIPTION: Smartcasts using Returns effects with simple type checking and not-null conditions on receiver inside contract. + */ + +// FILE: contracts.kt + +package contracts + +import kotlin.contracts.* + +fun T.case_1() { + contract { returns() implies (this@case_1 !is String) } + if (!(this@case_1 !is String)) throw Exception() +} + +fun T.case_2() { + contract { returns() implies (this@case_2 !is Int) } + if (!(this@case_2 !is Int)) throw Exception() +} + +fun String> T?.case_3_1() { + contract { returns() implies (this@case_3_1 == null) } + if (!(this@case_3_1 == null)) throw Exception() +} +fun String> T?.case_3_2() { + contract { returns() implies (this@case_3_2 != null) } + if (!(this@case_3_2 != null)) throw Exception() +} + +fun T.case_4_1() { + contract { returns() implies (this@case_4_1 == null) } + if (!(this@case_4_1 == null)) throw Exception() +} +fun T.case_4_2() { + contract { returns() implies (this@case_4_2 != null) } + if (!(this@case_4_2 != null)) throw Exception() +} + +fun T.case_5_1(): Boolean { + contract { returns(true) implies (this@case_5_1 !is String) } + return this@case_5_1 !is String +} +fun T.case_5_2(): Boolean { + contract { returns(false) implies (this@case_5_2 !is String) } + return !(this@case_5_2 !is String) +} +fun T.case_5_3(): Boolean? { + contract { returnsNotNull() implies (this@case_5_3 !is String) } + return if (this@case_5_3 !is String) true else null +} +fun T.case_5_4(): Boolean? { + contract { returns(null) implies (this@case_5_4 !is String) } + return if (this@case_5_4 !is String) null else true +} + +fun T.case_6_1(): Boolean { + contract { returns(true) implies (this@case_6_1 !is Int) } + return this@case_6_1 !is Int +} +fun T.case_6_2(): Boolean { + contract { returns(false) implies (this@case_6_2 !is Int) } + return !(this@case_6_2 !is Int) +} +fun T.case_6_3(): Boolean? { + contract { returnsNotNull() implies (this@case_6_3 !is Int) } + return if (this@case_6_3 !is Int) true else null +} +fun T.case_6_4(): Boolean? { + contract { returns(null) implies (this@case_6_4 !is Int) } + return if (this@case_6_4 !is Int) null else true +} + +fun String> T?.case_7_1(): Boolean { + contract { returns(true) implies (this@case_7_1 == null) } + return this@case_7_1 == null +} +fun String> T?.case_7_2(): Boolean { + contract { returns(false) implies (this@case_7_2 != null) } + return !(this@case_7_2 != null) +} +fun String> T?.case_7_3(): Boolean? { + contract { returnsNotNull() implies (this@case_7_3 != null) } + return if (this@case_7_3 != null) true else null +} +fun String> T?.case_7_4(): Boolean? { + contract { returns(null) implies (this@case_7_4 != null) } + return if (this@case_7_4 != null) null else true +} + +fun T.case_8_1(): Boolean { + contract { returns(true) implies (this@case_8_1 == null) } + return this@case_8_1 == null +} +fun T.case_8_2(): Boolean { + contract { returns(false) implies (this@case_8_2 != null) } + return !(this@case_8_2 != null) +} +fun T.case_8_3(): Boolean? { + contract { returnsNotNull() implies (this@case_8_3 != null) } + return if (this@case_8_3 != null) true else null +} +fun T.case_8_4(): Boolean? { + contract { returns(null) implies (this@case_8_4 != null) } + return if (this@case_8_4 != null) null else true +} + +fun T.case_9(): Boolean? { + contract { returnsNotNull() implies (this@case_9 != null) } + return if (this@case_9 != null) true else null +} + +fun T.case_10(): Boolean? { + contract { returnsNotNull() implies (this@case_10 == null) } + return if (this@case_10 == null) true else null +} + +// FILE: usages.kt + +import contracts.* + +fun case_1(value_1: Any?) { + value_1.case_1() + println(value_1.length) +} + +fun case_2(value_1: Number) { + value_1.case_2() + println(value_1.inv()) +} + +fun case_3(value_1: String?, value_2: String?) { + value_1.case_3_1() + println(value_1.length) + value_2.case_3_2() + println(value_2) +} + +fun case_4(value_1: String?, value_2: String?) { + value_1.case_4_1() + println(value_1.length) + value_2.case_4_2() + println(value_2) +} + +fun case_5(value_1: Any?) { + if (value_1.case_5_1()) println(value_1.length) + if (!value_1.case_5_2()) println(value_1.length) + if (value_1.case_5_3() != null) println(value_1.length) + if (value_1.case_5_4() == null) println(value_1.length) +} + +fun case_6(value_1: Number) { + when { value_1.case_6_1() -> println(value_1.inv()) } + when { !value_1.case_6_2() -> println(value_1.inv()) } + when { value_1.case_6_3() != null -> println(value_1.inv()) } + when { value_1.case_6_4() != null -> println(value_1.inv()) } +} + +fun case_7(value_1: String?, value_2: String?) { + if (value_1.case_7_1()) println(value_1.length) + if (value_2.case_7_2()) println(value_2) + if (!(value_2.case_7_3() == null)) println(value_2) + if (value_2.case_7_3() == null) println(value_2) +} + +fun case_8(value_1: String?, value_2: String?) { + when { value_1.case_8_1() -> println(value_1.length) } + when { value_2.case_8_2() -> println(value_2) } + when { !(value_2.case_8_3() == null) -> println(value_2) } + when { value_2.case_8_3() == null -> println(value_2) } +} + +fun case_9(value_1: Number?) { + if (value_1?.case_9() == null) println(value_1.toByte()) +} + +fun case_10(value_1: Number?) { + if (value_1?.case_10() == null) { + println(value_1.toByte()) + } else { + println(value_1.toByte()) + } +} diff --git a/compiler/tests-spec/testData/diagnostics/notLinked/contracts/analysis/smartcasts/neg/4.txt b/compiler/tests-spec/testData/diagnostics/notLinked/contracts/analysis/smartcasts/neg/4.txt new file mode 100644 index 00000000000..aad9d6d3771 --- /dev/null +++ b/compiler/tests-spec/testData/diagnostics/notLinked/contracts/analysis/smartcasts/neg/4.txt @@ -0,0 +1,87 @@ +package + +public fun case_1(/*0*/ value_1: kotlin.Any?): kotlin.Unit +public fun case_10(/*0*/ value_1: kotlin.Number?): kotlin.Unit +public fun case_2(/*0*/ value_1: kotlin.Number): kotlin.Unit +public fun case_3(/*0*/ value_1: kotlin.String?, /*1*/ value_2: kotlin.String?): kotlin.Unit +public fun case_4(/*0*/ value_1: kotlin.String?, /*1*/ value_2: kotlin.String?): kotlin.Unit +public fun case_5(/*0*/ value_1: kotlin.Any?): kotlin.Unit +public fun case_6(/*0*/ value_1: kotlin.Number): kotlin.Unit +public fun case_7(/*0*/ value_1: kotlin.String?, /*1*/ value_2: kotlin.String?): kotlin.Unit +public fun case_8(/*0*/ value_1: kotlin.String?, /*1*/ value_2: kotlin.String?): kotlin.Unit +public fun case_9(/*0*/ value_1: kotlin.Number?): kotlin.Unit + +package contracts { + public fun T.case_1(): kotlin.Unit + Returns(WILDCARD) -> !is String + + public fun T.case_10(): kotlin.Boolean? + Returns(NOT_NULL) -> == null + + public fun T.case_2(): kotlin.Unit + Returns(WILDCARD) -> !is Int + + public fun T?.case_3_1(): kotlin.Unit + Returns(WILDCARD) -> == null + + public fun T?.case_3_2(): kotlin.Unit + Returns(WILDCARD) -> != null + + public fun T.case_4_1(): kotlin.Unit + Returns(WILDCARD) -> == null + + public fun T.case_4_2(): kotlin.Unit + Returns(WILDCARD) -> != null + + public fun T.case_5_1(): kotlin.Boolean + Returns(TRUE) -> !is String + + public fun T.case_5_2(): kotlin.Boolean + Returns(FALSE) -> !is String + + public fun T.case_5_3(): kotlin.Boolean? + Returns(NOT_NULL) -> !is String + + public fun T.case_5_4(): kotlin.Boolean? + Returns(NULL) -> !is String + + public fun T.case_6_1(): kotlin.Boolean + Returns(TRUE) -> !is Int + + public fun T.case_6_2(): kotlin.Boolean + Returns(FALSE) -> !is Int + + public fun T.case_6_3(): kotlin.Boolean? + Returns(NOT_NULL) -> !is Int + + public fun T.case_6_4(): kotlin.Boolean? + Returns(NULL) -> !is Int + + public fun T?.case_7_1(): kotlin.Boolean + Returns(TRUE) -> == null + + public fun T?.case_7_2(): kotlin.Boolean + Returns(FALSE) -> != null + + public fun T?.case_7_3(): kotlin.Boolean? + Returns(NOT_NULL) -> != null + + public fun T?.case_7_4(): kotlin.Boolean? + Returns(NULL) -> != null + + public fun T.case_8_1(): kotlin.Boolean + Returns(TRUE) -> == null + + public fun T.case_8_2(): kotlin.Boolean + Returns(FALSE) -> != null + + public fun T.case_8_3(): kotlin.Boolean? + Returns(NOT_NULL) -> != null + + public fun T.case_8_4(): kotlin.Boolean? + Returns(NULL) -> != null + + public fun T.case_9(): kotlin.Boolean? + Returns(NOT_NULL) -> != null + +} diff --git a/compiler/tests-spec/testData/diagnostics/notLinked/contracts/analysis/smartcasts/neg/5.kt b/compiler/tests-spec/testData/diagnostics/notLinked/contracts/analysis/smartcasts/neg/5.kt new file mode 100644 index 00000000000..9a2d091ee9b --- /dev/null +++ b/compiler/tests-spec/testData/diagnostics/notLinked/contracts/analysis/smartcasts/neg/5.kt @@ -0,0 +1,124 @@ +// !LANGUAGE: +AllowContractsForCustomFunctions +UseReturnsEffect +// !DIAGNOSTICS: -INVISIBLE_REFERENCE -INVISIBLE_MEMBER +// !USE_EXPERIMENTAL: kotlin.contracts.ExperimentalContracts + +/* + KOTLIN DIAGNOSTICS NOT LINKED SPEC TEST (NEGATIVE) + + SECTION: contracts + CATEGORY: analysis, smartcasts + NUMBER: 5 + DESCRIPTION: Smartcasts using Returns effects with complex (conjunction/disjunction) type checking and not-null conditions on receiver inside contract. + */ + +// FILE: contracts.kt + +package contracts + +import kotlin.contracts.* + +fun T?.case_1() { + contract { returns() implies (this@case_1 == null || this@case_1 !is String) } + if (!(this@case_1 == null || this@case_1 !is String)) throw Exception() +} + +fun T.case_2() { + contract { returns() implies (this@case_2 !is Int || this@case_2 == null) } + if (!(this@case_2 !is Int || this@case_2 == null)) throw Exception() +} + +inline fun T?.case_3() { + contract { returns() implies (this@case_3 !is Number || this@case_3 !is Int || this@case_3 == null) } + if (!(this@case_3 !is Number || this@case_3 !is Int || this@case_3 == null)) throw Exception() +} + +fun T?.case_4_1(): Boolean { + contract { returns(true) implies (this@case_4_1 == null || this@case_4_1 !is String) } + return this@case_4_1 == null || this@case_4_1 !is String +} +fun T?.case_4_2(): Boolean { + contract { returns(false) implies (this@case_4_2 == null || this@case_4_2 !is String) } + return !(this@case_4_2 == null || this@case_4_2 !is String) +} +fun T?.case_4_3(): Boolean? { + contract { returnsNotNull() implies (this@case_4_3 == null || this@case_4_3 !is String) } + return if (this@case_4_3 == null || this@case_4_3 !is String) true else null +} +fun T?.case_4_4(): Boolean? { + contract { returns(null) implies (this@case_4_4 == null || this@case_4_4 !is String) } + return if (this@case_4_4 == null || this@case_4_4 !is String) null else true +} + +fun T.case_5_1(): Boolean { + contract { returns(true) implies (this@case_5_1 !is Int || this@case_5_1 == null) } + return this@case_5_1 !is Int || this@case_5_1 == null +} +fun T.case_5_2(): Boolean { + contract { returns(false) implies (this@case_5_2 !is Int || this@case_5_2 == null) } + return !(this@case_5_2 !is Int || this@case_5_2 == null) +} +fun T.case_5_3(): Boolean? { + contract { returnsNotNull() implies (this@case_5_3 !is Int || this@case_5_3 == null) } + return if (this@case_5_3 !is Int || this@case_5_3 == null) true else null +} +fun T.case_5_4(): Boolean? { + contract { returns(null) implies (this@case_5_4 !is Int || this@case_5_4 == null) } + return if (this@case_5_4 !is Int || this@case_5_4 == null) null else true +} + +inline fun T?.case_6_1(): Boolean { + contract { returns(true) implies (this@case_6_1 !is Number || this@case_6_1 !is Int || this@case_6_1 == null) } + return this@case_6_1 !is Number || this@case_6_1 !is Int || this@case_6_1 == null +} +inline fun T?.case_6_2(): Boolean { + contract { returns(false) implies (this@case_6_2 !is Number || this@case_6_2 !is Int || this@case_6_2 == null) } + return !(this@case_6_2 !is Number || this@case_6_2 !is Int || this@case_6_2 == null) +} +inline fun T?.case_6_3(): Boolean? { + contract { returnsNotNull() implies (this@case_6_3 is Number && this@case_6_3 is Int && this@case_6_3 != null) } + return if (this@case_6_3 is Number && this@case_6_3 is Int && this@case_6_3 != null) true else null +} +inline fun T?.case_6_4(): Boolean? { + contract { returns(null) implies (this@case_6_4 is Number && this@case_6_4 is Int && this@case_6_4 != null) } + return if (this@case_6_4 is Number && this@case_6_4 is Int && this@case_6_4 != null) null else true +} + +// FILE: usages.kt + +import contracts.* + +fun case_1(value_1: Any?) { + value_1.case_1() + println(value_1.length) +} + +fun case_2(value_1: Number?) { + value_1.case_2() + println(value_1.inv()) +} + +fun case_3(value_1: Any?) { + value_1.case_3() + println(value_1.inv()) +} + +fun case_4(value_1: Any?, value_2: Any?, value_3: Any?) { + when { value_1.case_4_1() -> println(value_1.length) } + when { !value_2.case_4_2() -> println(value_2.length) } + when { value_3.case_4_3() != null -> println(value_3.length) } + when { value_3.case_4_4() == null -> println(value_3.length) } +} + +fun case_5(value_1: Number?, value_2: Number?, value_3: Number?) { + if (value_1.case_5_1()) println(value_1.inv()) + if (!value_2.case_5_2()) println(value_2.inv()) + if (value_3.case_5_3() != null) println(value_3.inv()) + if (value_3.case_5_4() == null) println(value_3.inv()) +} + +fun case_6(value_1: Any?, value_2: Any?, value_3: Any?) { + if (value_1.case_6_1()) println(value_1.inv()) + if (!value_2.case_6_2()) println(value_2.inv()) + if (value_3.case_6_3() == null) println(value_3.inv()) + if (value_3.case_6_4() != null) println(value_3.inv()) +} diff --git a/compiler/tests-spec/testData/diagnostics/notLinked/contracts/analysis/smartcasts/neg/5.txt b/compiler/tests-spec/testData/diagnostics/notLinked/contracts/analysis/smartcasts/neg/5.txt new file mode 100644 index 00000000000..d3c926be788 --- /dev/null +++ b/compiler/tests-spec/testData/diagnostics/notLinked/contracts/analysis/smartcasts/neg/5.txt @@ -0,0 +1,56 @@ +package + +public fun case_1(/*0*/ value_1: kotlin.Any?): kotlin.Unit +public fun case_2(/*0*/ value_1: kotlin.Number?): kotlin.Unit +public fun case_3(/*0*/ value_1: kotlin.Any?): kotlin.Unit +public fun case_4(/*0*/ value_1: kotlin.Any?, /*1*/ value_2: kotlin.Any?, /*2*/ value_3: kotlin.Any?): kotlin.Unit +public fun case_5(/*0*/ value_1: kotlin.Number?, /*1*/ value_2: kotlin.Number?, /*2*/ value_3: kotlin.Number?): kotlin.Unit +public fun case_6(/*0*/ value_1: kotlin.Any?, /*1*/ value_2: kotlin.Any?, /*2*/ value_3: kotlin.Any?): kotlin.Unit + +package contracts { + public fun T?.case_1(): kotlin.Unit + Returns(WILDCARD) -> == null || !is String + + public fun T.case_2(): kotlin.Unit + Returns(WILDCARD) -> !is Int || == null + + public inline fun T?.case_3(): kotlin.Unit + Returns(WILDCARD) -> !is Number || !is Int || == null + + public fun T?.case_4_1(): kotlin.Boolean + Returns(TRUE) -> == null || !is String + + public fun T?.case_4_2(): kotlin.Boolean + Returns(FALSE) -> == null || !is String + + public fun T?.case_4_3(): kotlin.Boolean? + Returns(NOT_NULL) -> == null || !is String + + public fun T?.case_4_4(): kotlin.Boolean? + Returns(NULL) -> == null || !is String + + public fun T.case_5_1(): kotlin.Boolean + Returns(TRUE) -> !is Int || == null + + public fun T.case_5_2(): kotlin.Boolean + Returns(FALSE) -> !is Int || == null + + public fun T.case_5_3(): kotlin.Boolean? + Returns(NOT_NULL) -> !is Int || == null + + public fun T.case_5_4(): kotlin.Boolean? + Returns(NULL) -> !is Int || == null + + public inline fun T?.case_6_1(): kotlin.Boolean + Returns(TRUE) -> !is Number || !is Int || == null + + public inline fun T?.case_6_2(): kotlin.Boolean + Returns(FALSE) -> !is Number || !is Int || == null + + public inline fun T?.case_6_3(): kotlin.Boolean? + Returns(NOT_NULL) -> is Number && is Int && != null + + public inline fun T?.case_6_4(): kotlin.Boolean? + Returns(NULL) -> is Number && is Int && != null + +} diff --git a/compiler/tests-spec/testData/diagnostics/notLinked/contracts/analysis/smartcasts/neg/6.kt b/compiler/tests-spec/testData/diagnostics/notLinked/contracts/analysis/smartcasts/neg/6.kt new file mode 100644 index 00000000000..7d9a52618d0 --- /dev/null +++ b/compiler/tests-spec/testData/diagnostics/notLinked/contracts/analysis/smartcasts/neg/6.kt @@ -0,0 +1,209 @@ +// !LANGUAGE: +AllowContractsForCustomFunctions +UseReturnsEffect +// !DIAGNOSTICS: -INVISIBLE_REFERENCE -INVISIBLE_MEMBER +// !USE_EXPERIMENTAL: kotlin.contracts.ExperimentalContracts + +/* + KOTLIN DIAGNOSTICS NOT LINKED SPEC TEST (NEGATIVE) + + SECTION: contracts + CATEGORY: analysis, smartcasts + NUMBER: 6 + DESCRIPTION: Smartcasts using Returns effects with complex (conjunction/disjunction) type checking and not-null conditions on receiver and some values (mixed) inside contract. + */ + +// FILE: contracts.kt + +package contracts + +import kotlin.contracts.* + +fun T?.case_1(value_1: Int?) { + contract { returns() implies (this@case_1 == null || this@case_1 !is String || value_1 == null) } + if (!(this@case_1 == null || this@case_1 !is String || value_1 == null)) throw Exception() +} + +fun T.case_2(value_2: Any?) { + contract { returns() implies (this@case_2 !is Int || this@case_2 == null || value_2 !is Number || value_2 == null) } + if (!(this@case_2 !is Int || this@case_2 == null || value_2 !is Number || value_2 == null)) throw Exception() +} + +fun T?.case_3(value_2: Any?) { + contract { returns() implies (this@case_3 !is Number || this@case_3 !is Int || this@case_3 == null || value_2 == null) } + if (!(this@case_3 !is Number || this@case_3 !is Int || this@case_3 == null || value_2 == null)) throw Exception() +} + +inline fun T?.case_4(value_2: Number, value_3: Any?, value_4: String?) { + contract { returns() implies ((this@case_4 !is Number && this@case_4 !is Int) || value_2 !is Int || value_3 == null || value_3 !is Number || value_4 == null) } + if (!((this@case_4 !is Number && this@case_4 !is Int) || value_2 !is Int || value_3 == null || value_3 !is Number || value_4 == null)) throw Exception() +} + +fun T?.case_5_1(value_1: Int?): Boolean { + contract { returns(true) implies (this@case_5_1 == null || this@case_5_1 !is String || value_1 == null) } + return this@case_5_1 == null || this@case_5_1 !is String || value_1 == null +} +fun T?.case_5_2(value_1: Int?): Boolean { + contract { returns(false) implies (this@case_5_2 == null || this@case_5_2 !is String || value_1 == null) } + return !(this@case_5_2 == null || this@case_5_2 !is String || value_1 == null) +} +fun T?.case_5_3(value_1: Int?): Boolean? { + contract { returnsNotNull() implies (this@case_5_3 == null || this@case_5_3 !is String || value_1 == null) } + return if (this@case_5_3 == null || this@case_5_3 !is String || value_1 == null) true else null +} +fun T?.case_5_4(value_1: Int?): Boolean? { + contract { returns(null) implies (this@case_5_4 == null || this@case_5_4 !is String || value_1 == null) } + return if (this@case_5_4 == null || this@case_5_4 !is String || value_1 == null) null else true +} + +fun T.case_6_1(value_2: Any?): Boolean { + contract { returns(true) implies (this@case_6_1 !is Int || this@case_6_1 == null || value_2 !is Number || value_2 == null) } + return this@case_6_1 !is Int || this@case_6_1 == null || value_2 !is Number || value_2 == null +} +fun T.case_6_2(value_2: Any?): Boolean { + contract { returns(false) implies (this@case_6_2 !is Int || this@case_6_2 == null || value_2 !is Number || value_2 == null) } + return !(this@case_6_2 !is Int || this@case_6_2 == null || value_2 !is Number || value_2 == null) +} +fun T.case_6_3(value_2: Any?): Boolean? { + contract { returnsNotNull() implies (this@case_6_3 !is Int || this@case_6_3 == null || value_2 !is Number || value_2 == null) } + return if (this@case_6_3 !is Int || this@case_6_3 == null || value_2 !is Number || value_2 == null) true else null +} +fun T.case_6_4(value_2: Any?): Boolean? { + contract { returns(null) implies (this@case_6_4 !is Int || this@case_6_4 == null || value_2 !is Number || value_2 == null) } + return if (this@case_6_4 !is Int || this@case_6_4 == null || value_2 !is Number || value_2 == null) null else true +} + +fun T?.case_7_1(value_2: Any?): Boolean { + contract { returns(true) implies (this@case_7_1 !is Number || this@case_7_1 !is Int || this@case_7_1 == null || value_2 == null) } + return this@case_7_1 !is Number || this@case_7_1 !is Int || this@case_7_1 == null || value_2 == null +} +fun T?.case_7_2(value_2: Any?): Boolean { + contract { returns(false) implies (this@case_7_2 !is Number || this@case_7_2 !is Int || this@case_7_2 == null || value_2 == null) } + return !(this@case_7_2 !is Number || this@case_7_2 !is Int || this@case_7_2 == null || value_2 == null) +} +fun T?.case_7_3(value_2: Any?): Boolean? { + contract { returnsNotNull() implies (this@case_7_3 !is Number || this@case_7_3 !is Int || this@case_7_3 == null || value_2 == null) } + return if (this@case_7_3 !is Number || this@case_7_3 !is Int || this@case_7_3 == null || value_2 == null) true else null +} +fun T?.case_7_4(value_2: Any?): Boolean? { + contract { returns(null) implies (this@case_7_4 !is Number || this@case_7_4 !is Int || this@case_7_4 == null || value_2 == null) } + return if (this@case_7_4 !is Number || this@case_7_4 !is Int || this@case_7_4 == null || value_2 == null) null else true +} + +inline fun T?.case_8_1(value_2: Number, value_3: Any?, value_4: String?): Boolean { + contract { returns(true) implies ((this@case_8_1 !is Number && this@case_8_1 !is Int) || value_2 !is Int || value_3 == null || value_3 !is Number || value_4 == null) } + return (this@case_8_1 !is Number && this@case_8_1 !is Int) || value_2 !is Int || value_3 == null || value_3 !is Number || value_4 == null +} +inline fun T?.case_8_2(value_2: Number, value_3: Any?, value_4: String?): Boolean { + contract { returns(false) implies ((this@case_8_2 !is Number && this@case_8_2 !is Int) || value_2 !is Int || value_3 == null || value_3 !is Number || value_4 == null) } + return !((this@case_8_2 !is Number && this@case_8_2 !is Int) || value_2 !is Int || value_3 == null || value_3 !is Number || value_4 == null) +} +inline fun T?.case_8_3(value_2: Number, value_3: Any?, value_4: String?): Boolean? { + contract { returnsNotNull() implies ((this@case_8_3 is Number || this@case_8_3 is Int) && value_2 is Int && value_3 != null && value_3 is Number && value_4 != null) } + return if ((this@case_8_3 is Number || this@case_8_3 is Int) && value_2 is Int && value_3 != null && value_3 is Number && value_4 != null) true else null +} +inline fun T?.case_8_4(value_2: Number, value_3: Any?, value_4: String?): Boolean? { + contract { returns(null) implies ((this@case_8_4 is Number || this@case_8_4 is Int) && value_2 is Int && value_3 != null && value_3 is Number && value_4 != null) } + return if ((this@case_8_4 is Number || this@case_8_4 is Int) && value_2 is Int && value_3 != null && value_3 is Number && value_4 != null) null else true +} + +// FILE: usages.kt + +import contracts.* + +fun case_1(value_1: Any?, value_2: Int?) { + value_1.case_1(value_2) + println(value_1.length) + println(value_2.inv()) +} + +fun case_2(value_1: Number?, value_2: Any?) { + value_1.case_2(value_2) + println(value_1.inv()) + println(value_2.toByte()) +} + +fun case_3(value_1: Any?, value_2: String?) { + value_1.case_3(value_2) + println(value_1.inv()) + println(value_2.length) +} + +fun case_4(value_1: Any?, value_2: Number, value_3: Any?, value_4: String?) { + value_1.case_4(value_2, value_3, value_4) + println(value_2.inv()) + println(value_3.toByte()) + println(value_4.length) +} + +fun case_5(value_1: Any?, value_2: Int?, value_3: Any?, value_4: Int?, value_5: Any?, value_6: Int?) { + when { + value_1.case_5_1(value_2) -> { + println(value_1.length) + println(value_2.inv()) + } + } + when { + !value_3.case_5_2(value_4) -> { + println(value_3.length) + println(value_4.inv()) + } + } + when { + value_5.case_5_3(value_6) != null -> { + println(value_5.length) + println(value_6.inv()) + } + } +} + +fun case_6(value_1: Number?, value_2: Any?, value_3: Number?, value_4: Any?, value_5: Number?, value_6: Any?) { + if (value_1.case_6_1(value_2)) { + println(value_1.inv()) + println(value_2.toByte()) + } + if (!value_3.case_6_2(value_4)) { + println(value_3.inv()) + println(value_4.toByte()) + } + if (value_5.case_6_3(value_6) != null) { + println(value_5.inv()) + println(value_6.toByte()) + } + if (value_5.case_6_4(value_6) == null) { + println(value_5.inv()) + println(value_6.toByte()) + } +} + +fun case_7(value_1: Any?, value_2: String?, value_3: Any?, value_4: String?, value_5: Any?, value_6: String?) { + if (value_1.case_7_1(value_2)) { + println(value_1.inv()) + println(value_2.length) + } + if (value_3.case_7_2(value_4)) { + println(value_3.inv()) + println(value_4.length) + } + if (value_5.case_7_3(value_6) != null) { + println(value_5.inv()) + println(value_6.length) + } + if (value_5.case_7_4(value_6) == null) { + println(value_5.inv()) + println(value_6.length) + } +} + +fun case_8(value_1: Any?, value_2: Number, value_3: Any?, value_4: String?) { + when { value_1.case_8_1(value_2, value_3, value_4) -> println(value_2.inv()) } + when { value_1.case_8_1(value_2, value_3, value_4) -> println(value_3.toByte()) } + when { value_1.case_8_1(value_2, value_3, value_4) -> println(value_4.length) } + when { !value_1.case_8_2(value_2, value_3, value_4) -> println(value_2.inv()) } + when { !value_1.case_8_2(value_2, value_3, value_4) -> println(value_3.toByte()) } + when { !value_1.case_8_2(value_2, value_3, value_4) -> println(value_4.length) } + when { value_1.case_8_3(value_2, value_3, value_4) == null -> println(value_2.inv()) } + when { value_1.case_8_3(value_2, value_3, value_4) == null -> println(value_3.toByte()) } + when { value_1.case_8_3(value_2, value_3, value_4) == null -> println(value_4.length) } + when { value_1.case_8_4(value_2, value_3, value_4) != null -> println(value_2.inv()) } + when { value_1.case_8_4(value_2, value_3, value_4) != null -> println(value_3.toByte()) } + when { value_1.case_8_4(value_2, value_3, value_4) != null -> println(value_4.length) } +} diff --git a/compiler/tests-spec/testData/diagnostics/notLinked/contracts/analysis/smartcasts/neg/6.txt b/compiler/tests-spec/testData/diagnostics/notLinked/contracts/analysis/smartcasts/neg/6.txt new file mode 100644 index 00000000000..e9638a568d1 --- /dev/null +++ b/compiler/tests-spec/testData/diagnostics/notLinked/contracts/analysis/smartcasts/neg/6.txt @@ -0,0 +1,73 @@ +package + +public fun case_1(/*0*/ value_1: kotlin.Any?, /*1*/ value_2: kotlin.Int?): kotlin.Unit +public fun case_2(/*0*/ value_1: kotlin.Number?, /*1*/ value_2: kotlin.Any?): kotlin.Unit +public fun case_3(/*0*/ value_1: kotlin.Any?, /*1*/ value_2: kotlin.String?): kotlin.Unit +public fun case_4(/*0*/ value_1: kotlin.Any?, /*1*/ value_2: kotlin.Number, /*2*/ value_3: kotlin.Any?, /*3*/ value_4: kotlin.String?): kotlin.Unit +public fun case_5(/*0*/ value_1: kotlin.Any?, /*1*/ value_2: kotlin.Int?, /*2*/ value_3: kotlin.Any?, /*3*/ value_4: kotlin.Int?, /*4*/ value_5: kotlin.Any?, /*5*/ value_6: kotlin.Int?): kotlin.Unit +public fun case_6(/*0*/ value_1: kotlin.Number?, /*1*/ value_2: kotlin.Any?, /*2*/ value_3: kotlin.Number?, /*3*/ value_4: kotlin.Any?, /*4*/ value_5: kotlin.Number?, /*5*/ value_6: kotlin.Any?): kotlin.Unit +public fun case_7(/*0*/ value_1: kotlin.Any?, /*1*/ value_2: kotlin.String?, /*2*/ value_3: kotlin.Any?, /*3*/ value_4: kotlin.String?, /*4*/ value_5: kotlin.Any?, /*5*/ value_6: kotlin.String?): kotlin.Unit +public fun case_8(/*0*/ value_1: kotlin.Any?, /*1*/ value_2: kotlin.Number, /*2*/ value_3: kotlin.Any?, /*3*/ value_4: kotlin.String?): kotlin.Unit + +package contracts { + public fun T?.case_1(/*0*/ value_1: kotlin.Int?): kotlin.Unit + Returns(WILDCARD) -> == null || !is String || value_1 == null + + public fun T.case_2(/*0*/ value_2: kotlin.Any?): kotlin.Unit + Returns(WILDCARD) -> !is Int || == null || value_2 !is Number || value_2 == null + + public fun T?.case_3(/*0*/ value_2: kotlin.Any?): kotlin.Unit + Returns(WILDCARD) -> !is Number || !is Int || == null || value_2 == null + + public inline fun T?.case_4(/*0*/ value_2: kotlin.Number, /*1*/ value_3: kotlin.Any?, /*2*/ value_4: kotlin.String?): kotlin.Unit + Returns(WILDCARD) -> ( !is Number && !is Int) || value_2 !is Int || value_3 == null || value_3 !is Number || value_4 == null + + public fun T?.case_5_1(/*0*/ value_1: kotlin.Int?): kotlin.Boolean + Returns(TRUE) -> == null || !is String || value_1 == null + + public fun T?.case_5_2(/*0*/ value_1: kotlin.Int?): kotlin.Boolean + Returns(FALSE) -> == null || !is String || value_1 == null + + public fun T?.case_5_3(/*0*/ value_1: kotlin.Int?): kotlin.Boolean? + Returns(NOT_NULL) -> == null || !is String || value_1 == null + + public fun T?.case_5_4(/*0*/ value_1: kotlin.Int?): kotlin.Boolean? + Returns(NULL) -> == null || !is String || value_1 == null + + public fun T.case_6_1(/*0*/ value_2: kotlin.Any?): kotlin.Boolean + Returns(TRUE) -> !is Int || == null || value_2 !is Number || value_2 == null + + public fun T.case_6_2(/*0*/ value_2: kotlin.Any?): kotlin.Boolean + Returns(FALSE) -> !is Int || == null || value_2 !is Number || value_2 == null + + public fun T.case_6_3(/*0*/ value_2: kotlin.Any?): kotlin.Boolean? + Returns(NOT_NULL) -> !is Int || == null || value_2 !is Number || value_2 == null + + public fun T.case_6_4(/*0*/ value_2: kotlin.Any?): kotlin.Boolean? + Returns(NULL) -> !is Int || == null || value_2 !is Number || value_2 == null + + public fun T?.case_7_1(/*0*/ value_2: kotlin.Any?): kotlin.Boolean + Returns(TRUE) -> !is Number || !is Int || == null || value_2 == null + + public fun T?.case_7_2(/*0*/ value_2: kotlin.Any?): kotlin.Boolean + Returns(FALSE) -> !is Number || !is Int || == null || value_2 == null + + public fun T?.case_7_3(/*0*/ value_2: kotlin.Any?): kotlin.Boolean? + Returns(NOT_NULL) -> !is Number || !is Int || == null || value_2 == null + + public fun T?.case_7_4(/*0*/ value_2: kotlin.Any?): kotlin.Boolean? + Returns(NULL) -> !is Number || !is Int || == null || value_2 == null + + public inline fun T?.case_8_1(/*0*/ value_2: kotlin.Number, /*1*/ value_3: kotlin.Any?, /*2*/ value_4: kotlin.String?): kotlin.Boolean + Returns(TRUE) -> ( !is Number && !is Int) || value_2 !is Int || value_3 == null || value_3 !is Number || value_4 == null + + public inline fun T?.case_8_2(/*0*/ value_2: kotlin.Number, /*1*/ value_3: kotlin.Any?, /*2*/ value_4: kotlin.String?): kotlin.Boolean + Returns(FALSE) -> ( !is Number && !is Int) || value_2 !is Int || value_3 == null || value_3 !is Number || value_4 == null + + public inline fun T?.case_8_3(/*0*/ value_2: kotlin.Number, /*1*/ value_3: kotlin.Any?, /*2*/ value_4: kotlin.String?): kotlin.Boolean? + Returns(NOT_NULL) -> ( is Number || is Int) && value_2 is Int && value_3 != null && value_3 is Number && value_4 != null + + public inline fun T?.case_8_4(/*0*/ value_2: kotlin.Number, /*1*/ value_3: kotlin.Any?, /*2*/ value_4: kotlin.String?): kotlin.Boolean? + Returns(NULL) -> ( is Number || is Int) && value_2 is Int && value_3 != null && value_3 is Number && value_4 != null + +} diff --git a/compiler/tests-spec/testData/diagnostics/notLinked/contracts/analysis/smartcasts/neg/7.kt b/compiler/tests-spec/testData/diagnostics/notLinked/contracts/analysis/smartcasts/neg/7.kt new file mode 100644 index 00000000000..488fe6ee6f1 --- /dev/null +++ b/compiler/tests-spec/testData/diagnostics/notLinked/contracts/analysis/smartcasts/neg/7.kt @@ -0,0 +1,354 @@ +// !LANGUAGE: +AllowContractsForCustomFunctions +UseReturnsEffect +// !DIAGNOSTICS: -INVISIBLE_REFERENCE -INVISIBLE_MEMBER +// !USE_EXPERIMENTAL: kotlin.contracts.ExperimentalContracts + +/* + KOTLIN DIAGNOSTICS NOT LINKED SPEC TEST (NEGATIVE) + + SECTION: contracts + CATEGORY: analysis, smartcasts + NUMBER: 7 + DESCRIPTION: Smartcasts using Returns effects with nested or subsequent contract function calls. + */ + +// FILE: contracts.kt + +package contracts + +import kotlin.contracts.* + +fun case_1_1(value_1: Int?) { + contract { returns() implies (value_1 == null) } + if (!(value_1 == null)) throw Exception() +} +fun case_1_2(value_1: Int?) { + contract { returns() implies (value_1 != null) } + if (!(value_1 != null)) throw Exception() +} + +fun case_2_1(value_1: Number?) { + contract { returns() implies (value_1 !is Float) } + if (!(value_1 !is Float)) throw Exception() +} +fun case_2_2(value_1: Number?) { + contract { returns() implies (value_1 !is Int) } + if (!(value_1 !is Int)) throw Exception() +} + +fun case_3_1(value_1: Any?) { + contract { returns() implies (value_1 !is String) } + if (!(value_1 !is String)) throw Exception() +} +fun case_3_2(value_1: Any?) { + contract { returns() implies (value_1 is String) } + if (!(value_1 is String)) throw Exception() +} + +fun case_4_1(value_1: Any?) { + contract { returns() implies (value_1 !is Number?) } + if (!(value_1 !is Number?)) throw Exception() +} +fun case_4_2(value_1: Number?) { + contract { returns() implies (value_1 == null) } + if (!(value_1 == null)) throw Exception() +} +fun case_4_3(value_1: Number) { + contract { returns() implies (value_1 !is Int) } + if (!(value_1 !is Int)) throw Exception() +} + +fun case_5_1(value_1: Int?): Boolean { + contract { returns(true) implies (value_1 == null) } + return value_1 == null +} +fun case_5_2(value_1: Int?): Boolean { + contract { returns(true) implies (value_1 != null) } + return value_1 != null +} +fun case_5_3(value_1: Int?): Boolean { + contract { returns(false) implies (value_1 == null) } + return !(value_1 == null) +} +fun case_5_4(value_1: Int?): Boolean { + contract { returns(false) implies (value_1 != null) } + return !(value_1 != null) +} +fun case_5_5(value_1: Int?): Boolean? { + contract { returnsNotNull() implies (value_1 == null) } + return if (value_1 == null) true else null +} +fun case_5_6(value_1: Int?): Boolean? { + contract { returnsNotNull() implies (value_1 != null) } + return if (value_1 != null) true else null +} +fun case_5_7(value_1: Int?): Boolean? { + contract { returns(null) implies (value_1 == null) } + return if (value_1 == null) null else true +} +fun case_5_8(value_1: Int?): Boolean? { + contract { returns(null) implies (value_1 != null) } + return if (value_1 != null) null else true +} + +fun case_6_1(value_1: Number?): Boolean { + contract { returns(true) implies (value_1 !is Float) } + return value_1 !is Float +} +fun case_6_2(value_1: Number?): Boolean { + contract { returns(true) implies (value_1 !is Int) } + return value_1 !is Int +} +fun case_6_3(value_1: Number?): Boolean { + contract { returns(false) implies (value_1 !is Float) } + return !(value_1 !is Float) +} +fun case_6_4(value_1: Number?): Boolean { + contract { returns(false) implies (value_1 !is Int) } + return !(value_1 !is Int) +} +fun case_6_5(value_1: Number?): Boolean? { + contract { returnsNotNull() implies (value_1 !is Float) } + return if (value_1 !is Float) true else null +} +fun case_6_6(value_1: Number?): Boolean? { + contract { returnsNotNull() implies (value_1 !is Int) } + return if (value_1 !is Int) true else null +} +fun case_6_7(value_1: Number?): Boolean? { + contract { returns(null) implies (value_1 !is Float) } + return if (value_1 !is Float) null else true +} +fun case_6_8(value_1: Number?): Boolean? { + contract { returns(null) implies (value_1 !is Int) } + return if (value_1 !is Int) null else true +} + +fun case_7_1(value_1: Any?): Boolean { + contract { returns(true) implies (value_1 !is String) } + return value_1 !is String +} +fun case_7_2(value_1: Any?): Boolean { + contract { returns(true) implies (value_1 is String) } + return value_1 is String +} +fun case_7_3(value_1: Any?): Boolean { + contract { returns(false) implies (value_1 !is String) } + return !(value_1 !is String) +} +fun case_7_4(value_1: Any?): Boolean { + contract { returns(false) implies (value_1 is String) } + return !(value_1 is String) +} +fun case_7_5(value_1: Any?): Boolean? { + contract { returnsNotNull() implies (value_1 !is String) } + return if (value_1 !is String) true else null +} +fun case_7_6(value_1: Any?): Boolean? { + contract { returnsNotNull() implies (value_1 is String) } + return if (value_1 is String) true else null +} +fun case_7_7(value_1: Any?): Boolean? { + contract { returns(null) implies (value_1 !is String) } + return if (value_1 !is String) null else true +} +fun case_7_8(value_1: Any?): Boolean? { + contract { returns(null) implies (value_1 is String) } + return if (value_1 is String) null else true +} + +fun case_8_1(value_1: Any?): Boolean { + contract { returns(true) implies (value_1 !is Number?) } + return value_1 !is Number? +} +fun case_8_2(value_1: Number?): Boolean { + contract { returns(true) implies (value_1 == null) } + return value_1 == null +} +fun case_8_3(value_1: Number): Boolean { + contract { returns(true) implies (value_1 !is Int) } + return value_1 !is Int +} +fun case_8_4(value_1: Any?): Boolean { + contract { returns(false) implies (value_1 !is Number?) } + return !(value_1 !is Number?) +} +fun case_8_5(value_1: Number?): Boolean { + contract { returns(false) implies (value_1 == null) } + return !(value_1 == null) +} +fun case_8_6(value_1: Number): Boolean { + contract { returns(false) implies (value_1 !is Int) } + return !(value_1 !is Int) +} +fun case_8_7(value_1: Any?): Boolean? { + contract { returnsNotNull() implies (value_1 is Number?) } + return if (value_1 is Number?) true else null +} +fun case_8_8(value_1: Number?): Boolean? { + contract { returnsNotNull() implies (value_1 == null) } + return if (value_1 == null) true else null +} +fun case_8_9(value_1: Number): Boolean? { + contract { returnsNotNull() implies (value_1 !is Int) } + return if (value_1 !is Int) true else null +} +fun case_8_10(value_1: Any?): Boolean? { + contract { returns(null) implies (value_1 is Number?) } + return if (value_1 is Number?) null else true +} +fun case_8_11(value_1: Number?): Boolean? { + contract { returns(null) implies (value_1 == null) } + return if (value_1 == null) null else true +} +fun case_8_12(value_1: Number): Boolean? { + contract { returns(null) implies (value_1 !is Int) } + return if (value_1 !is Int) null else true +} + +// FILE: usages.kt + +import contracts.* + +fun case_1(value_1: Int?) { + case_1_1(value_1) + value_1.inv() + case_1_2(value_1) + value_1.inv() + case_1_1(value_1) + value_1.inv() +} + +fun case_2(value_1: Number?) { + case_2_1(value_1) + value_1.toByte() + case_2_2(value_1) + value_1.inv() +} + +fun case_3(value_1: Any?) { + case_3_1(value_1) + value_1.length + case_3_2(value_1) + value_1.length +} + +fun case_4(value_1: Any?) { + case_4_1(value_1) + value_1?.toByte() + case_4_2(value_1) + value_1.toByte() + case_4_3(value_1) + value_1.inv() +} + +fun case_5(value_1: Int?) { + if (case_5_1(value_1)) { + value_1.inv() + if (case_5_2(value_1)) { + value_1.inv() + case_5_1(value_1) + value_1.inv() + } + } + if (!case_5_3(value_1)) { + value_1.inv() + if (!case_5_4(value_1)) { + value_1.inv() + case_5_1(value_1) + value_1.inv() + } + } + if (case_5_5(value_1) != null) { + value_1.inv() + if (case_5_6(value_1) != null) { + value_1.inv() + case_5_1(value_1) + value_1.inv() + } + } + if (case_5_7(value_1) == null) { + value_1.inv() + if (case_5_8(value_1) == null) { + value_1.inv() + case_5_1(value_1) + value_1.inv() + } + } +} + +fun case_6(value_1: Number?) { + when { + case_6_1(value_1) -> { + value_1.toByte() + when { case_6_2(value_1) -> value_1.inv() } + } + } + when { + !case_6_3(value_1) -> { + value_1.toByte() + when { !case_6_4(value_1) -> value_1.inv() } + } + } + when { + case_6_5(value_1) != null -> { + value_1.toByte() + when { case_6_6(value_1) != null -> value_1.inv() } + } + } + when { + case_6_7(value_1) == null -> { + value_1.toByte() + when { case_6_8(value_1) == null -> value_1.inv() } + } + } +} + +fun case_7(value_1: Any?) { + if (case_7_1(value_1)) { + value_1.length + if (case_7_2(value_1)) value_1.length + } + if (!case_7_3(value_1)) { + value_1.length + if (!case_7_4(value_1)) value_1.length + } + if (case_7_5(value_1) != null) { + value_1.length + if (case_7_6(value_1) != null) value_1.length + } + if (case_7_7(value_1) == null) { + value_1.length + if (case_7_8(value_1) == null) value_1.length + } +} + +fun case_8(value_1: Any?) { + if (case_8_1(value_1)) { + value_1?.toByte() + if (case_8_2(value_1)) { + value_1.toByte() + if (case_8_3(value_1)) value_1.inv() + } + } + if (!case_8_4(value_1)) { + value_1?.toByte() + if (!case_8_5(value_1)) { + value_1.toByte() + if (!case_8_6(value_1)) value_1.inv() + } + } + if (case_8_7(value_1) == null) { + value_1?.toByte() + if (case_8_8(value_1) != null) { + value_1.toByte() + if (case_8_9(value_1) != null) value_1.inv() + } + } + if (case_8_10(value_1) != null) { + value_1?.toByte() + if (case_8_11(value_1) == null) { + value_1.toByte() + if (case_8_12(value_1) == null) value_1.inv() + } + } +} diff --git a/compiler/tests-spec/testData/diagnostics/notLinked/contracts/analysis/smartcasts/neg/7.txt b/compiler/tests-spec/testData/diagnostics/notLinked/contracts/analysis/smartcasts/neg/7.txt new file mode 100644 index 00000000000..df120ae0adf --- /dev/null +++ b/compiler/tests-spec/testData/diagnostics/notLinked/contracts/analysis/smartcasts/neg/7.txt @@ -0,0 +1,148 @@ +package + +public fun case_1(/*0*/ value_1: kotlin.Int?): kotlin.Unit +public fun case_2(/*0*/ value_1: kotlin.Number?): kotlin.Unit +public fun case_3(/*0*/ value_1: kotlin.Any?): kotlin.Unit +public fun case_4(/*0*/ value_1: kotlin.Any?): kotlin.Unit +public fun case_5(/*0*/ value_1: kotlin.Int?): kotlin.Unit +public fun case_6(/*0*/ value_1: kotlin.Number?): kotlin.Unit +public fun case_7(/*0*/ value_1: kotlin.Any?): kotlin.Unit +public fun case_8(/*0*/ value_1: kotlin.Any?): kotlin.Unit + +package contracts { + public fun case_1_1(/*0*/ value_1: kotlin.Int?): kotlin.Unit + Returns(WILDCARD) -> value_1 == null + + public fun case_1_2(/*0*/ value_1: kotlin.Int?): kotlin.Unit + Returns(WILDCARD) -> value_1 != null + + public fun case_2_1(/*0*/ value_1: kotlin.Number?): kotlin.Unit + Returns(WILDCARD) -> value_1 !is Float + + public fun case_2_2(/*0*/ value_1: kotlin.Number?): kotlin.Unit + Returns(WILDCARD) -> value_1 !is Int + + public fun case_3_1(/*0*/ value_1: kotlin.Any?): kotlin.Unit + Returns(WILDCARD) -> value_1 !is String + + public fun case_3_2(/*0*/ value_1: kotlin.Any?): kotlin.Unit + Returns(WILDCARD) -> value_1 is String + + public fun case_4_1(/*0*/ value_1: kotlin.Any?): kotlin.Unit + Returns(WILDCARD) -> value_1 !is Number? + + public fun case_4_2(/*0*/ value_1: kotlin.Number?): kotlin.Unit + Returns(WILDCARD) -> value_1 == null + + public fun case_4_3(/*0*/ value_1: kotlin.Number): kotlin.Unit + Returns(WILDCARD) -> value_1 !is Int + + public fun case_5_1(/*0*/ value_1: kotlin.Int?): kotlin.Boolean + Returns(TRUE) -> value_1 == null + + public fun case_5_2(/*0*/ value_1: kotlin.Int?): kotlin.Boolean + Returns(TRUE) -> value_1 != null + + public fun case_5_3(/*0*/ value_1: kotlin.Int?): kotlin.Boolean + Returns(FALSE) -> value_1 == null + + public fun case_5_4(/*0*/ value_1: kotlin.Int?): kotlin.Boolean + Returns(FALSE) -> value_1 != null + + public fun case_5_5(/*0*/ value_1: kotlin.Int?): kotlin.Boolean? + Returns(NOT_NULL) -> value_1 == null + + public fun case_5_6(/*0*/ value_1: kotlin.Int?): kotlin.Boolean? + Returns(NOT_NULL) -> value_1 != null + + public fun case_5_7(/*0*/ value_1: kotlin.Int?): kotlin.Boolean? + Returns(NULL) -> value_1 == null + + public fun case_5_8(/*0*/ value_1: kotlin.Int?): kotlin.Boolean? + Returns(NULL) -> value_1 != null + + public fun case_6_1(/*0*/ value_1: kotlin.Number?): kotlin.Boolean + Returns(TRUE) -> value_1 !is Float + + public fun case_6_2(/*0*/ value_1: kotlin.Number?): kotlin.Boolean + Returns(TRUE) -> value_1 !is Int + + public fun case_6_3(/*0*/ value_1: kotlin.Number?): kotlin.Boolean + Returns(FALSE) -> value_1 !is Float + + public fun case_6_4(/*0*/ value_1: kotlin.Number?): kotlin.Boolean + Returns(FALSE) -> value_1 !is Int + + public fun case_6_5(/*0*/ value_1: kotlin.Number?): kotlin.Boolean? + Returns(NOT_NULL) -> value_1 !is Float + + public fun case_6_6(/*0*/ value_1: kotlin.Number?): kotlin.Boolean? + Returns(NOT_NULL) -> value_1 !is Int + + public fun case_6_7(/*0*/ value_1: kotlin.Number?): kotlin.Boolean? + Returns(NULL) -> value_1 !is Float + + public fun case_6_8(/*0*/ value_1: kotlin.Number?): kotlin.Boolean? + Returns(NULL) -> value_1 !is Int + + public fun case_7_1(/*0*/ value_1: kotlin.Any?): kotlin.Boolean + Returns(TRUE) -> value_1 !is String + + public fun case_7_2(/*0*/ value_1: kotlin.Any?): kotlin.Boolean + Returns(TRUE) -> value_1 is String + + public fun case_7_3(/*0*/ value_1: kotlin.Any?): kotlin.Boolean + Returns(FALSE) -> value_1 !is String + + public fun case_7_4(/*0*/ value_1: kotlin.Any?): kotlin.Boolean + Returns(FALSE) -> value_1 is String + + public fun case_7_5(/*0*/ value_1: kotlin.Any?): kotlin.Boolean? + Returns(NOT_NULL) -> value_1 !is String + + public fun case_7_6(/*0*/ value_1: kotlin.Any?): kotlin.Boolean? + Returns(NOT_NULL) -> value_1 is String + + public fun case_7_7(/*0*/ value_1: kotlin.Any?): kotlin.Boolean? + Returns(NULL) -> value_1 !is String + + public fun case_7_8(/*0*/ value_1: kotlin.Any?): kotlin.Boolean? + Returns(NULL) -> value_1 is String + + public fun case_8_1(/*0*/ value_1: kotlin.Any?): kotlin.Boolean + Returns(TRUE) -> value_1 !is Number? + + public fun case_8_10(/*0*/ value_1: kotlin.Any?): kotlin.Boolean? + Returns(NULL) -> value_1 is Number? + + public fun case_8_11(/*0*/ value_1: kotlin.Number?): kotlin.Boolean? + Returns(NULL) -> value_1 == null + + public fun case_8_12(/*0*/ value_1: kotlin.Number): kotlin.Boolean? + Returns(NULL) -> value_1 !is Int + + public fun case_8_2(/*0*/ value_1: kotlin.Number?): kotlin.Boolean + Returns(TRUE) -> value_1 == null + + public fun case_8_3(/*0*/ value_1: kotlin.Number): kotlin.Boolean + Returns(TRUE) -> value_1 !is Int + + public fun case_8_4(/*0*/ value_1: kotlin.Any?): kotlin.Boolean + Returns(FALSE) -> value_1 !is Number? + + public fun case_8_5(/*0*/ value_1: kotlin.Number?): kotlin.Boolean + Returns(FALSE) -> value_1 == null + + public fun case_8_6(/*0*/ value_1: kotlin.Number): kotlin.Boolean + Returns(FALSE) -> value_1 !is Int + + public fun case_8_7(/*0*/ value_1: kotlin.Any?): kotlin.Boolean? + Returns(NOT_NULL) -> value_1 is Number? + + public fun case_8_8(/*0*/ value_1: kotlin.Number?): kotlin.Boolean? + Returns(NOT_NULL) -> value_1 == null + + public fun case_8_9(/*0*/ value_1: kotlin.Number): kotlin.Boolean? + Returns(NOT_NULL) -> value_1 !is Int + +} diff --git a/compiler/tests-spec/testData/diagnostics/notLinked/contracts/analysis/smartcasts/neg/8.kt b/compiler/tests-spec/testData/diagnostics/notLinked/contracts/analysis/smartcasts/neg/8.kt new file mode 100644 index 00000000000..afa114ee411 --- /dev/null +++ b/compiler/tests-spec/testData/diagnostics/notLinked/contracts/analysis/smartcasts/neg/8.kt @@ -0,0 +1,132 @@ +// !LANGUAGE: +AllowContractsForCustomFunctions +UseReturnsEffect +// !WITH_CONTRACT_FUNCTIONS +// !DIAGNOSTICS: -INVISIBLE_REFERENCE -INVISIBLE_MEMBER +// !USE_EXPERIMENTAL: kotlin.contracts.ExperimentalContracts + +/* + KOTLIN DIAGNOSTICS NOT LINKED SPEC TEST (NEGATIVE) + + SECTION: contracts + CATEGORY: analysis, smartcasts + NUMBER: 8 + DESCRIPTION: Smartcasts using some Returns effects. + */ + +// FILE: contracts.kt + +package contracts + +import kotlin.contracts.* + +fun T?.case_3(value_1: Int?, value_2: Boolean): Boolean { + contract { + returns(true) implies (value_1 != null) + returns(false) implies (value_1 == null && !value_2) + returns(null) implies (value_1 == null && value_2) + } + + return value_1 == null +} + +fun case_4(value_1: Number, block: (() -> Unit)?): Boolean? { + contract { + returns(true) implies (block != null) + returns(false) implies (value_1 is Int) + returns(null) implies (block == null) + } + + return value_1 == null +} + +fun String?.case_5(value_1: Number?): Boolean? { + contract { + returns(true) implies (value_1 == null) + returns(false) implies (this@case_5 == null) + returnsNotNull() implies (value_1 is Int) + } + + return value_1 == null +} + +fun T?.case_6(value_1: Number, value_2: String?): Boolean? { + contract { + returns(true) implies (this@case_6 == null) + returns(false) implies (value_1 is Int) + returns(null) implies (this@case_6 is String) + returnsNotNull() implies (value_2 == null) + } + + return value_1 == null +} + +// FILE: usages.kt + +import contracts.* + +fun case_1(value_1: Any?) { + funWithReturns(value_1 !is Number?) + println(value_1?.toByte()) + if (funWithReturnsTrue(value_1 !is Number)) { + println(value_1.toByte()) + if (funWithReturnsNotNull(value_1 is Int) == null) println(value_1.inv()) + } +} + +fun case_2(value_1: Any?) { + if (!funWithReturnsFalse(value_1 !is Number?)) { + println(value_1?.toByte()) + funWithReturns(value_1 !is Number) + println(value_1.toByte()) + if (funWithReturnsNull(value_1 !is Int) == null) println(value_1.inv()) + } +} + +fun case_3(value_1: Int?, value_2: Any?) { + if (!value_1.case_3(value_1, value_2 is Number?)) { + println(value_2?.toByte()) + println(value_1) + } else if (value_1.case_3(value_1, value_2 is Number?)) { + println(value_2?.toByte()) + } else { + println(value_2?.toByte()) + } +} + +fun case_4(value_1: Number, value_2: (() -> Unit)?) { + if (contracts.case_4(value_1, value_2) == true) { + value_1.inv() + } else if (contracts.case_4(value_1, value_2) == false) { + println(value_2) + } else if (contracts.case_4(value_1, value_2) == null) { + value_2() + } +} + +fun case_5(value_1: Number?, value_2: String?) { + when (value_2.case_5(value_1)) { + true -> { + println(value_2.length) + println(value_1.toByte()) + } + false -> { + println(value_2.length) + println(value_1.inv()) + } + } +} + +fun case_6(value_1: Number, value_2: String?, value_3: Any?) { + when (value_3.case_6(value_1, value_2)) { + true -> { + println(value_3.equals("")) + println(value_2.length) + } + false -> { + println(value_3.length) + println(value_2.length) + } + null -> { + println(value_1.inv()) + } + } +} diff --git a/compiler/tests-spec/testData/diagnostics/notLinked/contracts/analysis/smartcasts/neg/8.txt b/compiler/tests-spec/testData/diagnostics/notLinked/contracts/analysis/smartcasts/neg/8.txt new file mode 100644 index 00000000000..efa9424d96f --- /dev/null +++ b/compiler/tests-spec/testData/diagnostics/notLinked/contracts/analysis/smartcasts/neg/8.txt @@ -0,0 +1,139 @@ +package + +public fun case_1(/*0*/ value_1: kotlin.Any?): kotlin.Unit +public fun case_2(/*0*/ value_1: kotlin.Any?): kotlin.Unit +public fun case_3(/*0*/ value_1: kotlin.Int?, /*1*/ value_2: kotlin.Any?): kotlin.Unit +public fun case_4(/*0*/ value_1: kotlin.Number, /*1*/ value_2: (() -> kotlin.Unit)?): kotlin.Unit +public fun case_5(/*0*/ value_1: kotlin.Number?, /*1*/ value_2: kotlin.String?): kotlin.Unit +public fun case_6(/*0*/ value_1: kotlin.Number, /*1*/ value_2: kotlin.String?, /*2*/ value_3: kotlin.Any?): kotlin.Unit +public inline fun funWithAtLeastOnceCallsInPlace(/*0*/ block: () -> T): T + CallsInPlace(block, AT_LEAST_ONCE) + +public inline fun funWithAtLeastOnceCallsInPlace(/*0*/ block: () -> kotlin.Unit): kotlin.Unit + CallsInPlace(block, AT_LEAST_ONCE) + +public inline fun funWithAtMostOnceCallsInPlace(/*0*/ block: () -> kotlin.Unit): kotlin.Unit + CallsInPlace(block, AT_MOST_ONCE) + +public inline fun funWithExactlyOnceCallsInPlace(/*0*/ block: () -> T): T + CallsInPlace(block, EXACTLY_ONCE) + +public inline fun funWithExactlyOnceCallsInPlace(/*0*/ block: () -> kotlin.Unit): kotlin.Unit + CallsInPlace(block, EXACTLY_ONCE) + +public fun funWithReturns(/*0*/ cond: kotlin.Boolean): kotlin.Unit + Returns(WILDCARD) -> cond + +public fun funWithReturnsAndInvertCondition(/*0*/ cond: kotlin.Boolean): kotlin.Unit + Returns(WILDCARD) -> !cond + +public fun funWithReturnsAndInvertTypeCheck(/*0*/ value_1: kotlin.Any?): kotlin.Unit + Returns(WILDCARD) -> value_1 !is String + +public fun funWithReturnsAndNotNullCheck(/*0*/ value_1: kotlin.Any?): kotlin.Unit + Returns(WILDCARD) -> value_1 != null + +public fun funWithReturnsAndNullCheck(/*0*/ value_1: kotlin.Any?): kotlin.Unit + Returns(WILDCARD) -> value_1 == null + +public fun funWithReturnsAndTypeCheck(/*0*/ value_1: kotlin.Any?): kotlin.Unit + Returns(WILDCARD) -> value_1 is String + +public fun funWithReturnsFalse(/*0*/ cond: kotlin.Boolean): kotlin.Boolean + Returns(FALSE) -> cond + +public fun funWithReturnsFalseAndInvertCondition(/*0*/ cond: kotlin.Boolean): kotlin.Boolean + Returns(FALSE) -> !cond + +public fun funWithReturnsFalseAndInvertTypeCheck(/*0*/ value_1: kotlin.Any?): kotlin.Boolean + Returns(FALSE) -> value_1 !is String + +public fun funWithReturnsFalseAndNotNullCheck(/*0*/ value_1: kotlin.Number?): kotlin.Boolean + Returns(FALSE) -> value_1 != null + +public fun funWithReturnsFalseAndNullCheck(/*0*/ value_1: kotlin.Number?): kotlin.Boolean + Returns(FALSE) -> value_1 == null + +public fun funWithReturnsFalseAndTypeCheck(/*0*/ value_1: kotlin.Any?): kotlin.Boolean + Returns(FALSE) -> value_1 is String + +public fun funWithReturnsNotNull(/*0*/ cond: kotlin.Boolean): kotlin.Boolean? + Returns(NOT_NULL) -> cond + +public fun funWithReturnsNotNullAndInvertCondition(/*0*/ cond: kotlin.Boolean): kotlin.Boolean? + Returns(NOT_NULL) -> !cond + +public fun funWithReturnsNotNullAndInvertTypeCheck(/*0*/ value_1: kotlin.Any?): kotlin.Boolean? + Returns(NOT_NULL) -> value_1 !is String + +public fun funWithReturnsNotNullAndNotNullCheck(/*0*/ value_1: kotlin.Number?): kotlin.Boolean? + Returns(NOT_NULL) -> value_1 != null + +public fun funWithReturnsNotNullAndNullCheck(/*0*/ value_1: kotlin.Number?): kotlin.Boolean? + Returns(NOT_NULL) -> value_1 == null + +public fun funWithReturnsNotNullAndTypeCheck(/*0*/ value_1: kotlin.Any?): kotlin.Boolean? + Returns(NOT_NULL) -> value_1 is String + +public fun funWithReturnsNull(/*0*/ cond: kotlin.Boolean): kotlin.Boolean? + Returns(NULL) -> cond + +public fun funWithReturnsNullAndInvertCondition(/*0*/ cond: kotlin.Boolean): kotlin.Boolean? + Returns(NULL) -> !cond + +public fun funWithReturnsNullAndInvertTypeCheck(/*0*/ value_1: kotlin.Any?): kotlin.Boolean? + Returns(NULL) -> value_1 !is String + +public fun funWithReturnsNullAndNotNullCheck(/*0*/ value_1: kotlin.Number?): kotlin.Boolean? + Returns(NULL) -> value_1 != null + +public fun funWithReturnsNullAndNullCheck(/*0*/ value_1: kotlin.Number?): kotlin.Boolean? + Returns(NULL) -> value_1 == null + +public fun funWithReturnsNullAndTypeCheck(/*0*/ value_1: kotlin.Any?): kotlin.Boolean? + Returns(NULL) -> value_1 is String + +public fun funWithReturnsTrue(/*0*/ cond: kotlin.Boolean): kotlin.Boolean + Returns(TRUE) -> cond + +public fun funWithReturnsTrueAndInvertCondition(/*0*/ cond: kotlin.Boolean): kotlin.Boolean + Returns(TRUE) -> !cond + +public fun funWithReturnsTrueAndInvertTypeCheck(/*0*/ value_1: kotlin.Any?): kotlin.Boolean + Returns(TRUE) -> value_1 !is String + +public fun funWithReturnsTrueAndNotNullCheck(/*0*/ value_1: kotlin.Any?): kotlin.Boolean + Returns(TRUE) -> value_1 != null + +public fun funWithReturnsTrueAndNullCheck(/*0*/ value_1: kotlin.Any?): kotlin.Boolean + Returns(TRUE) -> value_1 == null + +public fun funWithReturnsTrueAndTypeCheck(/*0*/ value_1: kotlin.Any?): kotlin.Boolean + Returns(TRUE) -> value_1 is String + +public inline fun funWithUnknownCallsInPlace(/*0*/ block: () -> kotlin.Unit): kotlin.Unit + CallsInPlace(block, UNKNOWN) + +package contracts { + public fun case_4(/*0*/ value_1: kotlin.Number, /*1*/ block: (() -> kotlin.Unit)?): kotlin.Boolean? + Returns(TRUE) -> block != null + Returns(FALSE) -> value_1 is Int + Returns(NULL) -> block == null + + public fun T?.case_3(/*0*/ value_1: kotlin.Int?, /*1*/ value_2: kotlin.Boolean): kotlin.Boolean + Returns(TRUE) -> value_1 != null + Returns(FALSE) -> value_1 == null && (!value_2) + Returns(NULL) -> value_1 == null && value_2 + + public fun kotlin.String?.case_5(/*0*/ value_1: kotlin.Number?): kotlin.Boolean? + Returns(TRUE) -> value_1 == null + Returns(FALSE) -> == null + Returns(NOT_NULL) -> value_1 is Int + + public fun T?.case_6(/*0*/ value_1: kotlin.Number, /*1*/ value_2: kotlin.String?): kotlin.Boolean? + Returns(TRUE) -> == null + Returns(FALSE) -> value_1 is Int + Returns(NULL) -> is String + Returns(NOT_NULL) -> value_2 == null + +} diff --git a/compiler/tests-spec/testData/diagnostics/notLinked/contracts/analysis/smartcasts/neg/9.kt b/compiler/tests-spec/testData/diagnostics/notLinked/contracts/analysis/smartcasts/neg/9.kt new file mode 100644 index 00000000000..9a6a8bc8909 --- /dev/null +++ b/compiler/tests-spec/testData/diagnostics/notLinked/contracts/analysis/smartcasts/neg/9.kt @@ -0,0 +1,37 @@ +// !LANGUAGE: +AllowContractsForCustomFunctions +UseCallsInPlaceEffect +// !USE_EXPERIMENTAL: kotlin.contracts.ExperimentalContracts +// !WITH_CONTRACT_FUNCTIONS +// SKIP_TXT + +/* + KOTLIN DIAGNOSTICS NOT LINKED SPEC TEST (NEGATIVE) + + SECTION: contracts + CATEGORY: analysis, smartcasts + NUMBER: 9 + DESCRIPTION: Smartcasts after non-null assertions or not-null value assignment in lambdas of contract function with 'exactly once' or 'at least once' CallsInPlace effects. + UNEXPECTED BEHAVIOUR + ISSUES: KT-26148 + */ + +fun case_1(arg: Int?) { + funWithExactlyOnceCallsInPlace { arg!! } + arg.inc() +} + +fun case_2(arg: Int?) { + funWithAtLeastOnceCallsInPlace { arg!! } + arg.inc() +} + +fun case_3() { + val value_1: Boolean? + funWithExactlyOnceCallsInPlace { value_1 = false } + value_1.not() +} + +fun case_4() { + val value_1: Boolean? + funWithAtLeastOnceCallsInPlace { value_1 = true } + value_1.not() +} diff --git a/compiler/tests-spec/testData/diagnostics/notLinked/contracts/analysis/smartcasts/pos/1.kt b/compiler/tests-spec/testData/diagnostics/notLinked/contracts/analysis/smartcasts/pos/1.kt new file mode 100644 index 00000000000..2037ec36ab2 --- /dev/null +++ b/compiler/tests-spec/testData/diagnostics/notLinked/contracts/analysis/smartcasts/pos/1.kt @@ -0,0 +1,88 @@ +// !LANGUAGE: +AllowContractsForCustomFunctions +UseReturnsEffect +// !USE_EXPERIMENTAL: kotlin.contracts.ExperimentalContracts +// !WITH_CONTRACT_FUNCTIONS +// SKIP_TXT + +/* + KOTLIN DIAGNOSTICS NOT LINKED SPEC TEST (POSITIVE) + + SECTION: contracts + CATEGORY: analysis, smartcasts + NUMBER: 1 + DESCRIPTION: Smartcasts using Returns effects with simple type checking, not-null conditions and custom condition (condition for smartcast outside contract). + */ + +fun case_1(value_1: Any?) { + funWithReturns(value_1 is String) + println(value_1.length) +} + +fun case_2(value_1: Int?) { + funWithReturns(value_1 != null) + println(value_1.inc()) +} + +fun case_3(value_1: Int?) { + funWithReturns(value_1 == null) + println(value_1) +} + +fun case_4(value_1: Any?) { + funWithReturnsAndTypeCheck(value_1) + println(value_1.length) +} + +fun case_5(value_1: String?) { + funWithReturnsAndNotNullCheck(value_1) + println(value_1.length) +} + +fun case_6(value_1: String?) { + funWithReturnsAndNullCheck(value_1) + println(value_1) +} + +object case_7_object { + val prop_1: Int? = 10 +} +fun case_7() { + funWithReturnsAndInvertCondition(case_7_object.prop_1 == null) + case_7_object.prop_1.inc() +} + +fun case_8(value_1: Any?) { + if (funWithReturnsTrue(value_1 is String)) println(value_1.length) + if (funWithReturnsTrueAndInvertCondition(value_1 !is String)) println(value_1.length) + if (!funWithReturnsFalse(value_1 is String)) println(value_1.length) + if (!funWithReturnsFalseAndInvertCondition(value_1 !is String)) println(value_1.length) + if (funWithReturnsNotNull(value_1 is String) != null) println(value_1.length) + if (!(funWithReturnsNotNull(value_1 is String) == null)) println(value_1.length) +} + +fun case_9(value_1: String?) { + if (funWithReturnsTrue(value_1 != null)) println(value_1.length) + if (funWithReturnsTrueAndInvertCondition(value_1 == null)) println(value_1.length) + if (!funWithReturnsFalse(value_1 != null)) println(value_1.length) + if (!funWithReturnsFalseAndInvertCondition(value_1 == null)) println(value_1.length) + if (funWithReturnsNotNull(value_1 != null) != null) println(value_1.length) + if (!(funWithReturnsNotNull(value_1 != null) == null)) println(value_1.length) + if (!(funWithReturnsNotNullAndInvertCondition(value_1 == null) == null)) println(value_1.length) +} + +fun case_10(value_1: Any?) { + if (funWithReturnsTrueAndTypeCheck(value_1)) println(value_1.length) + if (!funWithReturnsFalseAndTypeCheck(value_1)) println(value_1.length) + if (funWithReturnsNotNullAndTypeCheck(value_1) != null) println(value_1.length) + if (!(funWithReturnsNotNullAndTypeCheck(value_1) == null)) println(value_1.length) +} + +fun case_11(value_1: Number?, value_2: Int?) { + if (funWithReturnsTrueAndNotNullCheck(value_1)) println(value_1.toByte()) + if (funWithReturnsTrueAndNullCheck(value_1)) println(value_1) + if (!funWithReturnsFalseAndNotNullCheck(value_2)) value_2.inc() + if (!funWithReturnsFalseAndNotNullCheck(value_1)) println(value_1.toByte()) + if (!funWithReturnsFalseAndNullCheck(value_1)) println(value_1) + if (!(funWithReturnsNotNullAndNotNullCheck(value_1) == null)) println(value_1.toByte()) + if (funWithReturnsNotNullAndNotNullCheck(value_1) != null) println(value_1.toByte()) + if (funWithReturnsNotNullAndNullCheck(value_1) != null) println(value_1) +} diff --git a/compiler/tests-spec/testData/diagnostics/notLinked/contracts/analysis/smartcasts/pos/10.kt b/compiler/tests-spec/testData/diagnostics/notLinked/contracts/analysis/smartcasts/pos/10.kt new file mode 100644 index 00000000000..27bed38b602 --- /dev/null +++ b/compiler/tests-spec/testData/diagnostics/notLinked/contracts/analysis/smartcasts/pos/10.kt @@ -0,0 +1,46 @@ +// !LANGUAGE: +AllowContractsForCustomFunctions +UseReturnsEffect +// !WITH_CONTRACT_FUNCTIONS +// !DIAGNOSTICS: -INVISIBLE_REFERENCE -INVISIBLE_MEMBER -UNUSED_PARAMETER +// !USE_EXPERIMENTAL: kotlin.contracts.ExperimentalContracts + +/* + KOTLIN DIAGNOSTICS NOT LINKED SPEC TEST (POSITIVE) + + SECTION: contracts + CATEGORY: analysis, smartcasts + NUMBER: 10 + DESCRIPTION: Smartcasts with correspond contract function with default value in last parameter. + ISSUES: KT-26444 + */ + +// FILE: contracts.kt + +package contracts + +import kotlin.contracts.* + +fun case_1(value_1: Int?, value_2: Int? = 10): Boolean { + contract { returns(true) implies (value_1 != null) } + return value_1 != null +} + +fun case_2(value_1: Int? = 10, value_2: Int? = 10, value_3: Int? = 10): Boolean { + contract { returns(true) implies (value_2 != null) } + return value_1 != null +} + +// FILE: usages.kt + +import contracts.* + +fun case_1(value_1: Int?) { + if (contracts.case_1(value_1)) { + value_1.inc() + } +} + +fun case_2(value_1: Int?) { + if (contracts.case_2(10, value_1)) { + value_1.inc() + } +} diff --git a/compiler/tests-spec/testData/diagnostics/notLinked/contracts/analysis/smartcasts/pos/10.txt b/compiler/tests-spec/testData/diagnostics/notLinked/contracts/analysis/smartcasts/pos/10.txt new file mode 100644 index 00000000000..6135a6a9431 --- /dev/null +++ b/compiler/tests-spec/testData/diagnostics/notLinked/contracts/analysis/smartcasts/pos/10.txt @@ -0,0 +1,120 @@ +package + +public fun case_1(/*0*/ value_1: kotlin.Int?): kotlin.Unit +public fun case_2(/*0*/ value_1: kotlin.Int?): kotlin.Unit +public inline fun funWithAtLeastOnceCallsInPlace(/*0*/ block: () -> T): T + CallsInPlace(block, AT_LEAST_ONCE) + +public inline fun funWithAtLeastOnceCallsInPlace(/*0*/ block: () -> kotlin.Unit): kotlin.Unit + CallsInPlace(block, AT_LEAST_ONCE) + +public inline fun funWithAtMostOnceCallsInPlace(/*0*/ block: () -> kotlin.Unit): kotlin.Unit + CallsInPlace(block, AT_MOST_ONCE) + +public inline fun funWithExactlyOnceCallsInPlace(/*0*/ block: () -> T): T + CallsInPlace(block, EXACTLY_ONCE) + +public inline fun funWithExactlyOnceCallsInPlace(/*0*/ block: () -> kotlin.Unit): kotlin.Unit + CallsInPlace(block, EXACTLY_ONCE) + +public fun funWithReturns(/*0*/ cond: kotlin.Boolean): kotlin.Unit + Returns(WILDCARD) -> cond + +public fun funWithReturnsAndInvertCondition(/*0*/ cond: kotlin.Boolean): kotlin.Unit + Returns(WILDCARD) -> !cond + +public fun funWithReturnsAndInvertTypeCheck(/*0*/ value_1: kotlin.Any?): kotlin.Unit + Returns(WILDCARD) -> value_1 !is String + +public fun funWithReturnsAndNotNullCheck(/*0*/ value_1: kotlin.Any?): kotlin.Unit + Returns(WILDCARD) -> value_1 != null + +public fun funWithReturnsAndNullCheck(/*0*/ value_1: kotlin.Any?): kotlin.Unit + Returns(WILDCARD) -> value_1 == null + +public fun funWithReturnsAndTypeCheck(/*0*/ value_1: kotlin.Any?): kotlin.Unit + Returns(WILDCARD) -> value_1 is String + +public fun funWithReturnsFalse(/*0*/ cond: kotlin.Boolean): kotlin.Boolean + Returns(FALSE) -> cond + +public fun funWithReturnsFalseAndInvertCondition(/*0*/ cond: kotlin.Boolean): kotlin.Boolean + Returns(FALSE) -> !cond + +public fun funWithReturnsFalseAndInvertTypeCheck(/*0*/ value_1: kotlin.Any?): kotlin.Boolean + Returns(FALSE) -> value_1 !is String + +public fun funWithReturnsFalseAndNotNullCheck(/*0*/ value_1: kotlin.Number?): kotlin.Boolean + Returns(FALSE) -> value_1 != null + +public fun funWithReturnsFalseAndNullCheck(/*0*/ value_1: kotlin.Number?): kotlin.Boolean + Returns(FALSE) -> value_1 == null + +public fun funWithReturnsFalseAndTypeCheck(/*0*/ value_1: kotlin.Any?): kotlin.Boolean + Returns(FALSE) -> value_1 is String + +public fun funWithReturnsNotNull(/*0*/ cond: kotlin.Boolean): kotlin.Boolean? + Returns(NOT_NULL) -> cond + +public fun funWithReturnsNotNullAndInvertCondition(/*0*/ cond: kotlin.Boolean): kotlin.Boolean? + Returns(NOT_NULL) -> !cond + +public fun funWithReturnsNotNullAndInvertTypeCheck(/*0*/ value_1: kotlin.Any?): kotlin.Boolean? + Returns(NOT_NULL) -> value_1 !is String + +public fun funWithReturnsNotNullAndNotNullCheck(/*0*/ value_1: kotlin.Number?): kotlin.Boolean? + Returns(NOT_NULL) -> value_1 != null + +public fun funWithReturnsNotNullAndNullCheck(/*0*/ value_1: kotlin.Number?): kotlin.Boolean? + Returns(NOT_NULL) -> value_1 == null + +public fun funWithReturnsNotNullAndTypeCheck(/*0*/ value_1: kotlin.Any?): kotlin.Boolean? + Returns(NOT_NULL) -> value_1 is String + +public fun funWithReturnsNull(/*0*/ cond: kotlin.Boolean): kotlin.Boolean? + Returns(NULL) -> cond + +public fun funWithReturnsNullAndInvertCondition(/*0*/ cond: kotlin.Boolean): kotlin.Boolean? + Returns(NULL) -> !cond + +public fun funWithReturnsNullAndInvertTypeCheck(/*0*/ value_1: kotlin.Any?): kotlin.Boolean? + Returns(NULL) -> value_1 !is String + +public fun funWithReturnsNullAndNotNullCheck(/*0*/ value_1: kotlin.Number?): kotlin.Boolean? + Returns(NULL) -> value_1 != null + +public fun funWithReturnsNullAndNullCheck(/*0*/ value_1: kotlin.Number?): kotlin.Boolean? + Returns(NULL) -> value_1 == null + +public fun funWithReturnsNullAndTypeCheck(/*0*/ value_1: kotlin.Any?): kotlin.Boolean? + Returns(NULL) -> value_1 is String + +public fun funWithReturnsTrue(/*0*/ cond: kotlin.Boolean): kotlin.Boolean + Returns(TRUE) -> cond + +public fun funWithReturnsTrueAndInvertCondition(/*0*/ cond: kotlin.Boolean): kotlin.Boolean + Returns(TRUE) -> !cond + +public fun funWithReturnsTrueAndInvertTypeCheck(/*0*/ value_1: kotlin.Any?): kotlin.Boolean + Returns(TRUE) -> value_1 !is String + +public fun funWithReturnsTrueAndNotNullCheck(/*0*/ value_1: kotlin.Any?): kotlin.Boolean + Returns(TRUE) -> value_1 != null + +public fun funWithReturnsTrueAndNullCheck(/*0*/ value_1: kotlin.Any?): kotlin.Boolean + Returns(TRUE) -> value_1 == null + +public fun funWithReturnsTrueAndTypeCheck(/*0*/ value_1: kotlin.Any?): kotlin.Boolean + Returns(TRUE) -> value_1 is String + +public inline fun funWithUnknownCallsInPlace(/*0*/ block: () -> kotlin.Unit): kotlin.Unit + CallsInPlace(block, UNKNOWN) + +package contracts { + public fun case_1(/*0*/ value_1: kotlin.Int?, /*1*/ value_2: kotlin.Int? = ...): kotlin.Boolean + Returns(TRUE) -> value_1 != null + + public fun case_2(/*0*/ value_1: kotlin.Int? = ..., /*1*/ value_2: kotlin.Int? = ..., /*2*/ value_3: kotlin.Int? = ...): kotlin.Boolean + Returns(TRUE) -> value_2 != null + +} diff --git a/compiler/tests-spec/testData/diagnostics/notLinked/contracts/analysis/smartcasts/pos/2.kt b/compiler/tests-spec/testData/diagnostics/notLinked/contracts/analysis/smartcasts/pos/2.kt new file mode 100644 index 00000000000..4d79a4be6e6 --- /dev/null +++ b/compiler/tests-spec/testData/diagnostics/notLinked/contracts/analysis/smartcasts/pos/2.kt @@ -0,0 +1,153 @@ +// !LANGUAGE: +AllowContractsForCustomFunctions +UseReturnsEffect +// !USE_EXPERIMENTAL: kotlin.contracts.ExperimentalContracts +// !WITH_CONTRACT_FUNCTIONS +// SKIP_TXT + +/* + KOTLIN DIAGNOSTICS NOT LINKED SPEC TEST (POSITIVE) + + SECTION: contracts + CATEGORY: analysis, smartcasts + NUMBER: 2 + DESCRIPTION: Smartcasts using Returns effects with complex (conjunction/disjunction) type checking and not-null conditions outside contract (custom condition). + */ + +fun case_1(value_1: Any?, value_2: Any?) { + funWithReturns(value_1 is String && value_2 is Number) + println(value_1.length) + println(value_2.toByte()) +} + +fun case_2(value_1: Any?, value_2: Any?) { + funWithReturnsAndInvertCondition(value_1 !is String || value_2 !is Number) + println(value_1.length) + println(value_2.toByte()) +} + +fun case_3(value_1: Any?, value_2: Any?) { + funWithReturnsAndInvertCondition(value_1 !is String || value_2 != null) + println(value_1.length) + println(value_2?.toByte()) +} + +fun case_4(value_1: Any?, value_2: Number?) { + funWithReturns(value_1 is Float? && value_1 != null && value_2 != null) + println(value_1.dec()) + println(value_2?.toByte()) +} + +class case_5_class { + val prop_1: Int? = 10 + + fun case_5(value_1: Any?, value_2: Number?) { + val o = case_5_class() + funWithReturns(value_1 is Float? && value_1 != null && value_2 != null && o.prop_1 != null) + println(value_1.dec()) + println(value_2?.toByte()) + println(o.prop_1.plus(3)) + } +} + +fun case_6(value_1: Any?, value_2: Any) { + if (funWithReturnsTrue(value_1 is String && value_2 is Number)) { + println(value_1.length) + println(value_2.toByte()) + } + if (!funWithReturnsFalse(value_1 is String && value_2 is Number)) { + println(value_1.length) + println(value_2.toByte()) + } + if (funWithReturnsNotNull(value_1 is String && value_2 is Number) != null) { + println(value_1.length) + println(value_2.toByte()) + } + if (funWithReturnsNull(value_1 is String && value_2 is Number) == null) { + println(value_1.length) + println(value_2.toByte()) + } +} + +fun case_7(value_1: Any?, value_2: Any?) { + if (funWithReturnsTrueAndInvertCondition(value_1 !is String || value_2 !is Number)) { + println(value_1.length) + println(value_2.toByte()) + } + if (!funWithReturnsFalseAndInvertCondition(value_1 !is String || value_2 !is Number)) { + println(value_1.length) + println(value_2.toByte()) + } + if (funWithReturnsNotNullAndInvertCondition(value_1 !is String || value_2 !is Number) != null) { + println(value_1.length) + println(value_2.toByte()) + } + if (funWithReturnsNullAndInvertCondition(value_1 !is String || value_2 !is Number) == null) { + println(value_1.length) + println(value_2.toByte()) + } +} + +fun case_8(value_1: Any?, value_2: Any?) { + if (funWithReturnsTrueAndInvertCondition(value_1 !is String || value_2 != null)) { + println(value_1.length) + println(value_2?.toByte()) + } + if (!funWithReturnsFalseAndInvertCondition(value_1 !is String || value_2 != null)) { + println(value_1.length) + println(value_2?.toByte()) + } + if (funWithReturnsNotNullAndInvertCondition(value_1 !is String || value_2 != null) != null) { + println(value_1.length) + println(value_2?.toByte()) + } + if (funWithReturnsNullAndInvertCondition(value_1 !is String || value_2 != null) == null) { + println(value_1.length) + println(value_2?.toByte()) + } +} + +fun case_9(value_1: Any?, value_2: Number?) { + if (funWithReturnsTrue(value_1 is Float? && value_1 != null && value_2 != null)) { + println(value_1.dec()) + println(value_2?.toByte()) + } + if (!funWithReturnsFalse(value_1 is Float? && value_1 != null && value_2 != null)) { + println(value_1.dec()) + println(value_2?.toByte()) + } + if (funWithReturnsNotNull(value_1 is Float? && value_1 != null && value_2 != null) != null) { + println(value_1.dec()) + println(value_2?.toByte()) + } + if (funWithReturnsNull(value_1 is Float? && value_1 != null && value_2 != null) == null) { + println(value_1.dec()) + println(value_2?.toByte()) + } +} + +class case_10_class { + val prop_1: Int? = 10 + + fun case_10(value_1: Any?, value_2: Number?) { + val o = case_10_class() + if (funWithReturnsTrue(value_1 is Float? && value_1 != null && value_2 != null && o.prop_1 != null && this.prop_1 != null)) { + println(value_1.dec()) + println(value_2?.toByte()) + println(o.prop_1.plus(3)) + } + if (!funWithReturnsFalse(value_1 is Float? && value_1 != null && value_2 != null && o.prop_1 != null && this.prop_1 != null)) { + println(value_1.dec()) + println(value_2?.toByte()) + println(o.prop_1.plus(3)) + } + if (funWithReturnsNotNull(value_1 is Float? && value_1 != null && value_2 != null && o.prop_1 != null && this.prop_1 != null) != null) { + println(value_1.dec()) + println(value_2?.toByte()) + println(o.prop_1.plus(3)) + } + if (funWithReturnsNull(value_1 is Float? && value_1 != null && value_2 != null && o.prop_1 != null && this.prop_1 != null) == null) { + println(value_1.dec()) + println(value_2?.toByte()) + println(o.prop_1.plus(3)) + } + } +} diff --git a/compiler/tests-spec/testData/diagnostics/notLinked/contracts/analysis/smartcasts/pos/3.kt b/compiler/tests-spec/testData/diagnostics/notLinked/contracts/analysis/smartcasts/pos/3.kt new file mode 100644 index 00000000000..5f3b7f47212 --- /dev/null +++ b/compiler/tests-spec/testData/diagnostics/notLinked/contracts/analysis/smartcasts/pos/3.kt @@ -0,0 +1,176 @@ +// !LANGUAGE: +AllowContractsForCustomFunctions +UseReturnsEffect +// !DIAGNOSTICS: -INVISIBLE_REFERENCE -INVISIBLE_MEMBER +// !USE_EXPERIMENTAL: kotlin.contracts.ExperimentalContracts + +/* + KOTLIN DIAGNOSTICS NOT LINKED SPEC TEST (POSITIVE) + + SECTION: contracts + CATEGORY: analysis, smartcasts + NUMBER: 3 + DESCRIPTION: Smartcasts using Returns effects with complex (conjunction/disjunction) type checking and not-null conditions inside contract. + */ + +// FILE: contracts.kt + +package contracts + +import kotlin.contracts.* + +fun case_1(value_1: Any?, value_2: Any?) { + contract { returns() implies (value_1 is String && value_2 is Number) } + if (!(value_1 is String && value_2 is Number)) throw Exception() +} + +fun case_2(value_1: Any?, value_2: Any?) { + contract { returns() implies (value_1 is String && value_2 == null) } + if (!(value_1 is String && value_2 == null)) throw Exception() +} + +fun case_3(value_1: Any?, value_2: Any?, value_3: Any?, value_4: Any?) { + contract { returns() implies (value_1 is Float? && value_1 != null && value_2 != null && value_3 != null && value_4 != null) } + if (!(value_1 is Float? && value_1 != null && value_2 != null && value_3 != null && value_4 != null)) throw Exception() +} + +fun case_4_1(value_1: Any?, value_2: Any?): Boolean { + contract { returns(true) implies (value_1 is String && value_2 is Number) } + return value_1 is String && value_2 is Number +} +fun case_4_2(value_1: Any?, value_2: Any?): Boolean { + contract { returns(false) implies (value_1 is String && value_2 is Number) } + return !(value_1 is String && value_2 is Number) +} +fun case_4_3(value_1: Any?, value_2: Any?): Boolean? { + contract { returnsNotNull() implies (value_1 is String && value_2 is Number) } + return if (value_1 is String && value_2 is Number) true else null +} +fun case_4_4(value_1: Any?, value_2: Any?): Boolean? { + contract { returns(null) implies (value_1 is String && value_2 is Number) } + return if (value_1 is String && value_2 is Number) null else true +} + +fun case_5_1(value_1: Any?, value_2: Any?): Boolean { + contract { returns(true) implies (value_1 is String && value_2 == null) } + return value_1 is String && value_2 == null +} +fun case_5_2(value_1: Any?, value_2: Any?): Boolean { + contract { returns(false) implies (value_1 is String && value_2 == null) } + return !(value_1 is String && value_2 == null) +} +fun case_5_3(value_1: Any?, value_2: Any?): Boolean? { + contract { returnsNotNull() implies (value_1 is String && value_2 == null) } + return if (value_1 is String && value_2 == null) true else null +} +fun case_5_4(value_1: Any?, value_2: Any?): Boolean? { + contract { returns(null) implies (value_1 is String && value_2 == null) } + return if (value_1 is String && value_2 == null) null else true +} + +fun case_6_1(value_1: Any?, value_2: Any?, value_3: Any?, value_4: Any?): Boolean { + contract { returns(true) implies (value_1 is Float? && value_1 != null && value_2 != null && value_3 != null && value_4 != null) } + return value_1 is Float? && value_1 != null && value_2 != null && value_3 != null && value_4 != null +} +fun case_6_2(value_1: Any?, value_2: Any?, value_3: Any?, value_4: Any?): Boolean { + contract { returns(false) implies (value_1 is Float? && value_1 != null && value_2 != null && value_3 != null && value_4 != null) } + return !(value_1 is Float? && value_1 != null && value_2 != null && value_3 != null && value_4 != null) +} +fun case_6_3(value_1: Any?, value_2: Any?, value_3: Any?, value_4: Any?): Boolean? { + contract { returnsNotNull() implies (value_1 is Float? && value_1 != null && value_2 != null && value_3 != null && value_4 != null) } + return if (value_1 is Float? && value_1 != null && value_2 != null && value_3 != null && value_4 != null) true else null +} +fun case_6_4(value_1: Any?, value_2: Any?, value_3: Any?, value_4: Any?): Boolean? { + contract { returns(null) implies (value_1 is Float? && value_1 != null && value_2 != null && value_3 != null && value_4 != null) } + return if (value_1 is Float? && value_1 != null && value_2 != null && value_3 != null && value_4 != null) null else true +} + +// FILE: usages.kt + +import contracts.* + +fun case_1(value_1: Any?, value_2: Any?) { + contracts.case_1(value_1, value_2) + println(value_1.length) + println(value_2.toByte()) +} + +fun case_2(value_1: Any?, value_2: Any?) { + contracts.case_2(value_1, value_2) + println(value_1.length) + println(value_2?.toByte()) +} + +class case_3_class { + val prop_1: Int? = 10 + fun case_3(value_1: Any?, value_2: Number?) { + val o = case_3_class() + contracts.case_3(value_1, value_2, o.prop_1, this.prop_1) + println(value_1.dec()) + println(value_2?.toByte()) + println(o.prop_1.plus(3)) + } +} + +fun case_4(value_1: Any?, value_2: Any?) { + if (contracts.case_4_1(value_1, value_2)) { + println(value_1.length) + println(value_2.toByte()) + } + if (!contracts.case_4_2(value_1, value_2)) { + println(value_1.length) + println(value_2.toByte()) + } + if (contracts.case_4_3(value_1, value_2) != null) { + println(value_1.length) + println(value_2.toByte()) + } + if (contracts.case_4_4(value_1, value_2) == null) { + println(value_1.length) + println(value_2.toByte()) + } +} + +fun case_5(value_1: Any?, value_2: Any?) { + if (contracts.case_5_1(value_1, value_2)) { + println(value_1.length) + println(value_2?.toByte()) + } + if (!contracts.case_5_2(value_1, value_2)) { + println(value_1.length) + println(value_2?.toByte()) + } + if (contracts.case_5_3(value_1, value_2) != null) { + println(value_1.length) + println(value_2?.toByte()) + } + if (contracts.case_5_4(value_1, value_2) == null) { + println(value_1.length) + println(value_2?.toByte()) + } +} + +class case_6_class { + val prop_1: Int? = 10 + fun case_6(value_1: Any?, value_2: Number?) { + val o = case_6_class() + if (contracts.case_6_1(value_1, value_2, o.prop_1, this.prop_1)) { + println(value_1.dec()) + println(value_2?.toByte()) + println(o.prop_1.plus(3)) + } + if (!contracts.case_6_2(value_1, value_2, o.prop_1, this.prop_1)) { + println(value_1.dec()) + println(value_2?.toByte()) + println(o.prop_1.plus(3)) + } + if (contracts.case_6_3(value_1, value_2, o.prop_1, this.prop_1) != null) { + println(value_1.dec()) + println(value_2?.toByte()) + println(o.prop_1.plus(3)) + } + if (contracts.case_6_4(value_1, value_2, o.prop_1, this.prop_1) == null) { + println(value_1.dec()) + println(value_2?.toByte()) + println(o.prop_1.plus(3)) + } + } +} diff --git a/compiler/tests-spec/testData/diagnostics/notLinked/contracts/analysis/smartcasts/pos/3.txt b/compiler/tests-spec/testData/diagnostics/notLinked/contracts/analysis/smartcasts/pos/3.txt new file mode 100644 index 00000000000..7b9aab5ea67 --- /dev/null +++ b/compiler/tests-spec/testData/diagnostics/notLinked/contracts/analysis/smartcasts/pos/3.txt @@ -0,0 +1,72 @@ +package + +public fun case_1(/*0*/ value_1: kotlin.Any?, /*1*/ value_2: kotlin.Any?): kotlin.Unit +public fun case_2(/*0*/ value_1: kotlin.Any?, /*1*/ value_2: kotlin.Any?): kotlin.Unit +public fun case_4(/*0*/ value_1: kotlin.Any?, /*1*/ value_2: kotlin.Any?): kotlin.Unit +public fun case_5(/*0*/ value_1: kotlin.Any?, /*1*/ value_2: kotlin.Any?): kotlin.Unit + +public final class case_3_class { + public constructor case_3_class() + public final val prop_1: kotlin.Int? = 10 + public final fun case_3(/*0*/ value_1: kotlin.Any?, /*1*/ value_2: kotlin.Number?): 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_6_class { + public constructor case_6_class() + public final val prop_1: kotlin.Int? = 10 + public final fun case_6(/*0*/ value_1: kotlin.Any?, /*1*/ value_2: kotlin.Number?): 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 +} + +package contracts { + public fun case_1(/*0*/ value_1: kotlin.Any?, /*1*/ value_2: kotlin.Any?): kotlin.Unit + Returns(WILDCARD) -> value_1 is String && value_2 is Number + + public fun case_2(/*0*/ value_1: kotlin.Any?, /*1*/ value_2: kotlin.Any?): kotlin.Unit + Returns(WILDCARD) -> value_1 is String && value_2 == null + + public fun case_3(/*0*/ value_1: kotlin.Any?, /*1*/ value_2: kotlin.Any?, /*2*/ value_3: kotlin.Any?, /*3*/ value_4: kotlin.Any?): kotlin.Unit + Returns(WILDCARD) -> value_1 is Float? && value_1 != null && value_2 != null && value_3 != null && value_4 != null + + public fun case_4_1(/*0*/ value_1: kotlin.Any?, /*1*/ value_2: kotlin.Any?): kotlin.Boolean + Returns(TRUE) -> value_1 is String && value_2 is Number + + public fun case_4_2(/*0*/ value_1: kotlin.Any?, /*1*/ value_2: kotlin.Any?): kotlin.Boolean + Returns(FALSE) -> value_1 is String && value_2 is Number + + public fun case_4_3(/*0*/ value_1: kotlin.Any?, /*1*/ value_2: kotlin.Any?): kotlin.Boolean? + Returns(NOT_NULL) -> value_1 is String && value_2 is Number + + public fun case_4_4(/*0*/ value_1: kotlin.Any?, /*1*/ value_2: kotlin.Any?): kotlin.Boolean? + Returns(NULL) -> value_1 is String && value_2 is Number + + public fun case_5_1(/*0*/ value_1: kotlin.Any?, /*1*/ value_2: kotlin.Any?): kotlin.Boolean + Returns(TRUE) -> value_1 is String && value_2 == null + + public fun case_5_2(/*0*/ value_1: kotlin.Any?, /*1*/ value_2: kotlin.Any?): kotlin.Boolean + Returns(FALSE) -> value_1 is String && value_2 == null + + public fun case_5_3(/*0*/ value_1: kotlin.Any?, /*1*/ value_2: kotlin.Any?): kotlin.Boolean? + Returns(NOT_NULL) -> value_1 is String && value_2 == null + + public fun case_5_4(/*0*/ value_1: kotlin.Any?, /*1*/ value_2: kotlin.Any?): kotlin.Boolean? + Returns(NULL) -> value_1 is String && value_2 == null + + public fun case_6_1(/*0*/ value_1: kotlin.Any?, /*1*/ value_2: kotlin.Any?, /*2*/ value_3: kotlin.Any?, /*3*/ value_4: kotlin.Any?): kotlin.Boolean + Returns(TRUE) -> value_1 is Float? && value_1 != null && value_2 != null && value_3 != null && value_4 != null + + public fun case_6_2(/*0*/ value_1: kotlin.Any?, /*1*/ value_2: kotlin.Any?, /*2*/ value_3: kotlin.Any?, /*3*/ value_4: kotlin.Any?): kotlin.Boolean + Returns(FALSE) -> value_1 is Float? && value_1 != null && value_2 != null && value_3 != null && value_4 != null + + public fun case_6_3(/*0*/ value_1: kotlin.Any?, /*1*/ value_2: kotlin.Any?, /*2*/ value_3: kotlin.Any?, /*3*/ value_4: kotlin.Any?): kotlin.Boolean? + Returns(NOT_NULL) -> value_1 is Float? && value_1 != null && value_2 != null && value_3 != null && value_4 != null + + public fun case_6_4(/*0*/ value_1: kotlin.Any?, /*1*/ value_2: kotlin.Any?, /*2*/ value_3: kotlin.Any?, /*3*/ value_4: kotlin.Any?): kotlin.Boolean? + Returns(NULL) -> value_1 is Float? && value_1 != null && value_2 != null && value_3 != null && value_4 != null + +} diff --git a/compiler/tests-spec/testData/diagnostics/notLinked/contracts/analysis/smartcasts/pos/4.kt b/compiler/tests-spec/testData/diagnostics/notLinked/contracts/analysis/smartcasts/pos/4.kt new file mode 100644 index 00000000000..e6cb98d597c --- /dev/null +++ b/compiler/tests-spec/testData/diagnostics/notLinked/contracts/analysis/smartcasts/pos/4.kt @@ -0,0 +1,266 @@ +// !LANGUAGE: +AllowContractsForCustomFunctions +UseReturnsEffect +// !DIAGNOSTICS: -INVISIBLE_REFERENCE -INVISIBLE_MEMBER +// !USE_EXPERIMENTAL: kotlin.contracts.ExperimentalContracts + +/* + KOTLIN DIAGNOSTICS NOT LINKED SPEC TEST (POSITIVE) + + SECTION: contracts + CATEGORY: analysis, smartcasts + NUMBER: 4 + DESCRIPTION: Smartcasts using Returns effects with simple type checking and not-null conditions on receiver inside contract. + */ + +// FILE: contracts.kt + +package contracts + +import kotlin.contracts.* + +fun T.case_1() { + contract { returns() implies (this@case_1 is String) } + if (!(this@case_1 is String)) throw Exception() +} + +fun T.case_2() { + contract { returns() implies (this@case_2 is Int) } + if (!(this@case_2 is Int)) throw Exception() +} + +fun String> T?.case_3_1() { + contract { returns() implies (this@case_3_1 != null) } + if (!(this@case_3_1 != null)) throw Exception() +} +fun String> T?.case_3_2() { + contract { returns() implies (this@case_3_2 == null) } + if (!(this@case_3_2 == null)) throw Exception() +} + +fun T.case_4_1() { + contract { returns() implies (this@case_4_1 != null) } + if (!(this@case_4_1 != null)) throw Exception() +} +fun T.case_4_2() { + contract { returns() implies (this@case_4_2 == null) } + if (!(this@case_4_2 == null)) throw Exception() +} + +fun T.case_5_1(): Boolean { + contract { returns(true) implies (this@case_5_1 is String) } + return this@case_5_1 is String +} +fun T.case_5_2(): Boolean { + contract { returns(false) implies (this@case_5_2 is String) } + return !(this@case_5_2 is String) +} +fun T.case_5_3(): Boolean? { + contract { returnsNotNull() implies (this@case_5_3 is String) } + return if (this@case_5_3 is String) true else null +} +fun T.case_5_4(): Boolean? { + contract { returns(null) implies (this@case_5_4 is String) } + return if (this@case_5_4 is String) null else true +} + +fun T.case_6_1(): Boolean { + contract { returns(true) implies (this@case_6_1 is Int) } + return this@case_6_1 is Int +} +fun T.case_6_2(): Boolean { + contract { returns(false) implies (this@case_6_2 is Int) } + return !(this@case_6_2 is Int) +} +fun T.case_6_3(): Boolean? { + contract { returnsNotNull() implies (this@case_6_3 is Int) } + return if (this@case_6_3 is Int) true else null +} +fun T.case_6_4(): Boolean? { + contract { returns(null) implies (this@case_6_4 is Int) } + return if (this@case_6_4 is Int) null else true +} + +fun String> T?.case_7_1(): Boolean { + contract { returns(true) implies (this@case_7_1 != null) } + return this@case_7_1 != null +} +fun String> T?.case_7_2(): Boolean { + contract { returns(true) implies (this@case_7_2 == null) } + return this@case_7_2 == null +} +fun String> T?.case_7_3(): Boolean? { + contract { returnsNotNull() implies (this@case_7_3 == null) } + return if (this@case_7_3 == null) true else null +} +fun String> T?.case_7_4(): Boolean? { + contract { returns(null) implies (this@case_7_4 == null) } + return if (this@case_7_4 == null) null else true +} +fun String> T?.case_7_5(): Boolean { + contract { returns(false) implies (this@case_7_5 == null) } + return !(this@case_7_5 == null) +} +fun String> T?.case_7_6(): Boolean? { + contract { returnsNotNull() implies (this@case_7_6 != null) } + return if (this@case_7_6 != null) true else null +} +fun String> T?.case_7_7(): Boolean? { + contract { returns(null) implies (this@case_7_7 != null) } + return if (this@case_7_7 != null) null else true +} +fun String> T?.case_7_8(): Boolean { + contract { returns(false) implies (this@case_7_8 != null) } + return !(this@case_7_8 != null) +} +fun String> T?.case_7_9(): Boolean { + contract { returns(false) implies (this@case_7_9 == null) } + return !(this@case_7_9 == null) +} +fun String> T?.case_7_10(): Boolean? { + contract { returnsNotNull() implies (this@case_7_10 == null) } + return if (this@case_7_10 == null) true else null +} +fun String> T?.case_7_11(): Boolean? { + contract { returns(null) implies (this@case_7_11 == null) } + return if (this@case_7_11 == null) null else true +} + +fun T.case_8_1(): Boolean { + contract { returns(true) implies (this@case_8_1 != null) } + return this@case_8_1 != null +} +fun T.case_8_2(): Boolean { + contract { returns(true) implies (this@case_8_2 == null) } + return this@case_8_2 == null +} +fun T.case_8_3(): Boolean? { + contract { returnsNotNull() implies (this@case_8_3 == null) } + return if (this@case_8_3 == null) true else null +} +fun T.case_8_4(): Boolean? { + contract { returns(null) implies (this@case_8_4 == null) } + return if (this@case_8_4 == null) null else true +} + +fun T.case_9_1(): Boolean? { + contract { returnsNotNull() implies (this@case_9_1 != null) } + return if (this@case_9_1 != null) true else null +} +fun T.case_9_2(): Boolean? { + contract { returns(null) implies (this@case_9_2 != null) } + return if (this@case_9_2 != null) null else true +} + +fun T.case_10_1(): Boolean? { + contract { returns(null) implies (this@case_10_1 != null) } + return if (this@case_10_1 != null) null else true +} +fun T.case_10_2(): Boolean? { + contract { returns(null) implies (this@case_10_2 != null) } + return if (this@case_10_2 != null) null else true +} + +// FILE: usages.kt + +import contracts.* + +fun case_1(value_1: Any?) { + value_1.case_1() + println(value_1.length) +} + +fun case_2(value_1: Number) { + value_1.case_2() + println(value_1.inv()) +} + +fun case_3(value_1: String?, value_2: String?) { + value_1.case_3_1() + println(value_1.length) + value_2.case_3_2() + println(value_2) +} + +fun case_4(value_1: String?, value_2: String?) { + value_1.case_4_1() + println(value_1.length) + value_2.case_4_2() + println(value_2) +} + +fun case_5(value_1: Any?) { + if (value_1.case_5_1()) println(value_1.length) + if (!value_1.case_5_2()) println(value_1.length) + if (value_1.case_5_3() != null) println(value_1.length) + if (value_1.case_5_4() == null) println(value_1.length) +} + +fun case_6(value_1: Number) { + when { value_1.case_6_1() -> println(value_1.inv()) } + when { !value_1.case_6_2() -> println(value_1.inv()) } + when { value_1.case_6_3() != null -> println(value_1.inv()) } + when { value_1.case_6_4() == null -> println(value_1.inv()) } +} + +fun case_7(value_1: String?) { + if (value_1.case_7_1()) println(value_1.length) + if (value_1.case_7_2()) println(value_1) + if (!(value_1.case_7_3() == null)) println(value_1) + if (!(value_1.case_7_4() != null)) println(value_1) + if (!value_1.case_7_5()) println(value_1) + else println(value_1) + when (value_1.case_7_6() == null) { + true -> println(value_1) + false -> println(value_1.length) + } + if (value_1.case_7_7() != null) println(value_1) + else println(value_1.length) + when { + !value_1.case_7_8() -> println(value_1) + value_1.case_7_8() -> println(value_1) + } + when { + !value_1.case_7_9() -> println(value_1) + value_1.case_7_9() -> println(value_1) + } + when { + value_1.case_7_10() == null -> println(value_1) + value_1.case_7_10() != null -> println(value_1) + } + when { + value_1.case_7_11() != null -> println(value_1) + value_1.case_7_11() == null -> println(value_1) + } +} + +fun case_8(value_1: String?, value_2: String?) { + when { value_1.case_8_1() -> println(value_1.length) } + when { value_2.case_8_2() -> println(value_2) } + when { !(value_2.case_8_3() == null) -> println(value_2) } + when { !(value_2.case_8_4() != null) -> println(value_2) } +} + +/* + UNEXPECTED BEHAVIOUR + KT-26382 + */ +fun case_9(value_1: Number?) { + if (value_1?.case_9_1() != null) println(value_1.toByte()) + if (value_1?.case_9_2() != null) println(value_1.toByte()) +} + +/* + UNEXPECTED BEHAVIOUR + ISSUES: KT-26382 + */ +fun case_10(value_1: Number?, value_2: Number?) { + if (value_1?.case_10_1() == null) { + println(value_1.toByte()) + } else { + println(value_1.toByte()) + } + if (value_2?.case_10_2() != null) { + println(value_2.toByte()) + } else { + println(value_2.toByte()) + } +} diff --git a/compiler/tests-spec/testData/diagnostics/notLinked/contracts/analysis/smartcasts/pos/4.txt b/compiler/tests-spec/testData/diagnostics/notLinked/contracts/analysis/smartcasts/pos/4.txt new file mode 100644 index 00000000000..98908b86e86 --- /dev/null +++ b/compiler/tests-spec/testData/diagnostics/notLinked/contracts/analysis/smartcasts/pos/4.txt @@ -0,0 +1,114 @@ +package + +public fun case_1(/*0*/ value_1: kotlin.Any?): kotlin.Unit +public fun case_10(/*0*/ value_1: kotlin.Number?, /*1*/ value_2: kotlin.Number?): kotlin.Unit +public fun case_2(/*0*/ value_1: kotlin.Number): kotlin.Unit +public fun case_3(/*0*/ value_1: kotlin.String?, /*1*/ value_2: kotlin.String?): kotlin.Unit +public fun case_4(/*0*/ value_1: kotlin.String?, /*1*/ value_2: kotlin.String?): kotlin.Unit +public fun case_5(/*0*/ value_1: kotlin.Any?): kotlin.Unit +public fun case_6(/*0*/ value_1: kotlin.Number): kotlin.Unit +public fun case_7(/*0*/ value_1: kotlin.String?): kotlin.Unit +public fun case_8(/*0*/ value_1: kotlin.String?, /*1*/ value_2: kotlin.String?): kotlin.Unit +public fun case_9(/*0*/ value_1: kotlin.Number?): kotlin.Unit + +package contracts { + public fun T.case_1(): kotlin.Unit + Returns(WILDCARD) -> is String + + public fun T.case_10_1(): kotlin.Boolean? + Returns(NULL) -> != null + + public fun T.case_10_2(): kotlin.Boolean? + Returns(NULL) -> != null + + public fun T.case_2(): kotlin.Unit + Returns(WILDCARD) -> is Int + + public fun T?.case_3_1(): kotlin.Unit + Returns(WILDCARD) -> != null + + public fun T?.case_3_2(): kotlin.Unit + Returns(WILDCARD) -> == null + + public fun T.case_4_1(): kotlin.Unit + Returns(WILDCARD) -> != null + + public fun T.case_4_2(): kotlin.Unit + Returns(WILDCARD) -> == null + + public fun T.case_5_1(): kotlin.Boolean + Returns(TRUE) -> is String + + public fun T.case_5_2(): kotlin.Boolean + Returns(FALSE) -> is String + + public fun T.case_5_3(): kotlin.Boolean? + Returns(NOT_NULL) -> is String + + public fun T.case_5_4(): kotlin.Boolean? + Returns(NULL) -> is String + + public fun T.case_6_1(): kotlin.Boolean + Returns(TRUE) -> is Int + + public fun T.case_6_2(): kotlin.Boolean + Returns(FALSE) -> is Int + + public fun T.case_6_3(): kotlin.Boolean? + Returns(NOT_NULL) -> is Int + + public fun T.case_6_4(): kotlin.Boolean? + Returns(NULL) -> is Int + + public fun T?.case_7_1(): kotlin.Boolean + Returns(TRUE) -> != null + + public fun T?.case_7_10(): kotlin.Boolean? + Returns(NOT_NULL) -> == null + + public fun T?.case_7_11(): kotlin.Boolean? + Returns(NULL) -> == null + + public fun T?.case_7_2(): kotlin.Boolean + Returns(TRUE) -> == null + + public fun T?.case_7_3(): kotlin.Boolean? + Returns(NOT_NULL) -> == null + + public fun T?.case_7_4(): kotlin.Boolean? + Returns(NULL) -> == null + + public fun T?.case_7_5(): kotlin.Boolean + Returns(FALSE) -> == null + + public fun T?.case_7_6(): kotlin.Boolean? + Returns(NOT_NULL) -> != null + + public fun T?.case_7_7(): kotlin.Boolean? + Returns(NULL) -> != null + + public fun T?.case_7_8(): kotlin.Boolean + Returns(FALSE) -> != null + + public fun T?.case_7_9(): kotlin.Boolean + Returns(FALSE) -> == null + + public fun T.case_8_1(): kotlin.Boolean + Returns(TRUE) -> != null + + public fun T.case_8_2(): kotlin.Boolean + Returns(TRUE) -> == null + + public fun T.case_8_3(): kotlin.Boolean? + Returns(NOT_NULL) -> == null + + public fun T.case_8_4(): kotlin.Boolean? + Returns(NULL) -> == null + + public fun T.case_9_1(): kotlin.Boolean? + Returns(NOT_NULL) -> != null + + public fun T.case_9_2(): kotlin.Boolean? + Returns(NULL) -> != null + +} diff --git a/compiler/tests-spec/testData/diagnostics/notLinked/contracts/analysis/smartcasts/pos/5.kt b/compiler/tests-spec/testData/diagnostics/notLinked/contracts/analysis/smartcasts/pos/5.kt new file mode 100644 index 00000000000..5d52c917d89 --- /dev/null +++ b/compiler/tests-spec/testData/diagnostics/notLinked/contracts/analysis/smartcasts/pos/5.kt @@ -0,0 +1,124 @@ +// !LANGUAGE: +AllowContractsForCustomFunctions +UseReturnsEffect +// !DIAGNOSTICS: -INVISIBLE_REFERENCE -INVISIBLE_MEMBER +// !USE_EXPERIMENTAL: kotlin.contracts.ExperimentalContracts + +/* + KOTLIN DIAGNOSTICS NOT LINKED SPEC TEST (POSITIVE) + + SECTION: contracts + CATEGORY: analysis, smartcasts + NUMBER: 5 + DESCRIPTION: Smartcasts using Returns effects with complex (conjunction/disjunction) type checking and not-null conditions on receiver inside contract. + */ + +// FILE: contracts.kt + +package contracts + +import kotlin.contracts.* + +fun T?.case_1() { + contract { returns() implies (this@case_1 != null && this@case_1 is String) } + if (!(this@case_1 != null && this@case_1 is String)) throw Exception() +} + +fun T.case_2() { + contract { returns() implies (this@case_2 is Int && this@case_2 != null) } + if (!(this@case_2 is Int && this@case_2 != null)) throw Exception() +} + +inline fun T?.case_3() { + contract { returns() implies (this@case_3 is Number && this@case_3 is Int && this@case_3 != null) } + if (!(this@case_3 is Number && this@case_3 is Int && this@case_3 != null)) throw Exception() +} + +fun T?.case_4_1(): Boolean { + contract { returns(true) implies (this@case_4_1 != null && this@case_4_1 is String) } + return this@case_4_1 != null && this@case_4_1 is String +} +fun T?.case_4_2(): Boolean { + contract { returns(false) implies (this@case_4_2 != null && this@case_4_2 is String) } + return !(this@case_4_2 != null && this@case_4_2 is String) +} +fun T?.case_4_3(): Boolean? { + contract { returnsNotNull() implies (this@case_4_3 != null && this@case_4_3 is String) } + return if (this@case_4_3 != null && this@case_4_3 is String) true else null +} +fun T?.case_4_4(): Boolean? { + contract { returns(null) implies (this@case_4_4 != null && this@case_4_4 is String) } + return if (this@case_4_4 != null && this@case_4_4 is String) null else true +} + +fun T.case_5_1(): Boolean { + contract { returns(true) implies (this@case_5_1 is Int && this@case_5_1 != null) } + return this@case_5_1 is Int && this@case_5_1 != null +} +fun T.case_5_2(): Boolean { + contract { returns(false) implies (this@case_5_2 is Int && this@case_5_2 != null) } + return !(this@case_5_2 is Int && this@case_5_2 != null) +} +fun T.case_5_3(): Boolean? { + contract { returnsNotNull() implies (this@case_5_3 is Int && this@case_5_3 != null) } + return if (this@case_5_3 is Int && this@case_5_3 != null) true else null +} +fun T.case_5_4(): Boolean? { + contract { returns(null) implies (this@case_5_4 is Int && this@case_5_4 != null) } + return if (this@case_5_4 is Int && this@case_5_4 != null) null else true +} + +inline fun T?.case_6_1(): Boolean { + contract { returns(true) implies (this@case_6_1 is Number && this@case_6_1 is Int && this@case_6_1 != null) } + return this@case_6_1 is Number && this@case_6_1 is Int && this@case_6_1 != null +} +inline fun T?.case_6_2(): Boolean { + contract { returns(false) implies (this@case_6_2 is Number && this@case_6_2 is Int && this@case_6_2 != null) } + return !(this@case_6_2 is Number && this@case_6_2 is Int && this@case_6_2 != null) +} +inline fun T?.case_6_3(): Boolean? { + contract { returnsNotNull() implies (this@case_6_3 is Number && this@case_6_3 is Int && this@case_6_3 != null) } + return if (this@case_6_3 is Number && this@case_6_3 is Int && this@case_6_3 != null) true else null +} +inline fun T?.case_6_4(): Boolean? { + contract { returns(null) implies (this@case_6_4 is Number && this@case_6_4 is Int && this@case_6_4 != null) } + return if (this@case_6_4 is Number && this@case_6_4 is Int && this@case_6_4 != null) null else true +} + +// FILE: usages.kt + +import contracts.* + +fun case_1(value_1: Any?) { + value_1.case_1() + println(value_1.length) +} + +fun case_2(value_1: Number?) { + value_1.case_2() + println(value_1.inv()) +} + +fun case_3(value_1: Any?) { + value_1.case_3() + println(value_1.inv()) +} + +fun case_4(value_1: Any?, value_2: Any?, value_3: Any?) { + when { value_1.case_4_1() -> println(value_1.length) } + when { !value_2.case_4_2() -> println(value_2.length) } + when { value_3.case_4_3() != null -> println(value_3.length) } + when { value_3.case_4_4() == null -> println(value_3.length) } +} + +fun case_5(value_1: Number?, value_2: Number?, value_3: Number?) { + if (value_1.case_5_1()) println(value_1.inv()) + if (!value_2.case_5_2()) println(value_2.inv()) + if (value_3.case_5_3() != null) println(value_3.inv()) + if (value_3.case_5_4() == null) println(value_3.inv()) +} + +fun case_6(value_1: Any?, value_2: Any?, value_3: Any?) { + if (value_1.case_6_1()) println(value_1.inv()) + if (!value_2.case_6_2()) println(value_2.inv()) + if (value_3.case_6_3() != null) println(value_3.inv()) + if (value_3.case_6_4() == null) println(value_3.inv()) +} diff --git a/compiler/tests-spec/testData/diagnostics/notLinked/contracts/analysis/smartcasts/pos/5.txt b/compiler/tests-spec/testData/diagnostics/notLinked/contracts/analysis/smartcasts/pos/5.txt new file mode 100644 index 00000000000..4eeef5ed13b --- /dev/null +++ b/compiler/tests-spec/testData/diagnostics/notLinked/contracts/analysis/smartcasts/pos/5.txt @@ -0,0 +1,56 @@ +package + +public fun case_1(/*0*/ value_1: kotlin.Any?): kotlin.Unit +public fun case_2(/*0*/ value_1: kotlin.Number?): kotlin.Unit +public fun case_3(/*0*/ value_1: kotlin.Any?): kotlin.Unit +public fun case_4(/*0*/ value_1: kotlin.Any?, /*1*/ value_2: kotlin.Any?, /*2*/ value_3: kotlin.Any?): kotlin.Unit +public fun case_5(/*0*/ value_1: kotlin.Number?, /*1*/ value_2: kotlin.Number?, /*2*/ value_3: kotlin.Number?): kotlin.Unit +public fun case_6(/*0*/ value_1: kotlin.Any?, /*1*/ value_2: kotlin.Any?, /*2*/ value_3: kotlin.Any?): kotlin.Unit + +package contracts { + public fun T?.case_1(): kotlin.Unit + Returns(WILDCARD) -> != null && is String + + public fun T.case_2(): kotlin.Unit + Returns(WILDCARD) -> is Int && != null + + public inline fun T?.case_3(): kotlin.Unit + Returns(WILDCARD) -> is Number && is Int && != null + + public fun T?.case_4_1(): kotlin.Boolean + Returns(TRUE) -> != null && is String + + public fun T?.case_4_2(): kotlin.Boolean + Returns(FALSE) -> != null && is String + + public fun T?.case_4_3(): kotlin.Boolean? + Returns(NOT_NULL) -> != null && is String + + public fun T?.case_4_4(): kotlin.Boolean? + Returns(NULL) -> != null && is String + + public fun T.case_5_1(): kotlin.Boolean + Returns(TRUE) -> is Int && != null + + public fun T.case_5_2(): kotlin.Boolean + Returns(FALSE) -> is Int && != null + + public fun T.case_5_3(): kotlin.Boolean? + Returns(NOT_NULL) -> is Int && != null + + public fun T.case_5_4(): kotlin.Boolean? + Returns(NULL) -> is Int && != null + + public inline fun T?.case_6_1(): kotlin.Boolean + Returns(TRUE) -> is Number && is Int && != null + + public inline fun T?.case_6_2(): kotlin.Boolean + Returns(FALSE) -> is Number && is Int && != null + + public inline fun T?.case_6_3(): kotlin.Boolean? + Returns(NOT_NULL) -> is Number && is Int && != null + + public inline fun T?.case_6_4(): kotlin.Boolean? + Returns(NULL) -> is Number && is Int && != null + +} diff --git a/compiler/tests-spec/testData/diagnostics/notLinked/contracts/analysis/smartcasts/pos/6.kt b/compiler/tests-spec/testData/diagnostics/notLinked/contracts/analysis/smartcasts/pos/6.kt new file mode 100644 index 00000000000..ee3de2e0153 --- /dev/null +++ b/compiler/tests-spec/testData/diagnostics/notLinked/contracts/analysis/smartcasts/pos/6.kt @@ -0,0 +1,215 @@ +// !LANGUAGE: +AllowContractsForCustomFunctions +UseReturnsEffect +// !DIAGNOSTICS: -INVISIBLE_REFERENCE -INVISIBLE_MEMBER +// !USE_EXPERIMENTAL: kotlin.contracts.ExperimentalContracts + +/* + KOTLIN DIAGNOSTICS NOT LINKED SPEC TEST (POSITIVE) + + SECTION: contracts + CATEGORY: analysis, smartcasts + NUMBER: 6 + DESCRIPTION: Smartcasts using Returns effects with complex (conjunction/disjunction) type checking and not-null conditions on receiver and some values (mixed) inside contract. + */ + +// FILE: contracts.kt + +package contracts + +import kotlin.contracts.* + +fun T?.case_1(value_1: Int?) { + contract { returns() implies (this@case_1 != null && this@case_1 is String && value_1 != null) } + if (!(this@case_1 != null && this@case_1 is String && value_1 != null)) throw Exception() +} + +fun T.case_2(value_2: Any?) { + contract { returns() implies (this@case_2 is Int && this@case_2 != null && value_2 is Number && value_2 != null) } + if (!(this@case_2 is Int && this@case_2 != null && value_2 is Number && value_2 != null)) throw Exception() +} + +fun T?.case_3(value_2: Any?) { + contract { returns() implies (this@case_3 is Number && this@case_3 is Int && this@case_3 != null && value_2 != null) } + if (!(this@case_3 is Number && this@case_3 is Int && this@case_3 != null && value_2 != null)) throw Exception() +} + +inline fun T?.case_4(value_2: Number, value_3: Any?, value_4: String?) { + contract { returns() implies ((this@case_4 is Number || this@case_4 is Int) && value_2 is Int && value_3 != null && value_3 is Number && value_4 != null) } + if (!((this@case_4 is Number || this@case_4 is Int) && value_2 is Int && value_3 != null && value_3 is Number && value_4 != null)) throw Exception() +} + +fun T?.case_5_1(value_1: Int?): Boolean { + contract { returns(true) implies (this@case_5_1 != null && this@case_5_1 is String && value_1 != null) } + return this@case_5_1 != null && this@case_5_1 is String && value_1 != null +} +fun T?.case_5_2(value_1: Int?): Boolean { + contract { returns(false) implies (this@case_5_2 != null && this@case_5_2 is String && value_1 != null) } + return !(this@case_5_2 != null && this@case_5_2 is String && value_1 != null) +} +fun T?.case_5_3(value_1: Int?): Boolean? { + contract { returnsNotNull() implies (this@case_5_3 != null && this@case_5_3 is String && value_1 != null) } + return if (this@case_5_3 != null && this@case_5_3 is String && value_1 != null) true else null +} +fun T?.case_5_4(value_1: Int?): Boolean? { + contract { returns(null) implies (this@case_5_4 != null && this@case_5_4 is String && value_1 != null) } + return if (this@case_5_4 != null && this@case_5_4 is String && value_1 != null) null else true +} + +fun T.case_6_1(value_2: Any?): Boolean { + contract { returns(true) implies (this@case_6_1 is Int && this@case_6_1 != null && value_2 is Number && value_2 != null) } + return this@case_6_1 is Int && this@case_6_1 != null && value_2 is Number && value_2 != null +} +fun T.case_6_2(value_2: Any?): Boolean { + contract { returns(false) implies (this@case_6_2 is Int && this@case_6_2 != null && value_2 is Number && value_2 != null) } + return !(this@case_6_2 is Int && this@case_6_2 != null && value_2 is Number && value_2 != null) +} +fun T.case_6_3(value_2: Any?): Boolean? { + contract { returnsNotNull() implies (this@case_6_3 is Int && this@case_6_3 != null && value_2 is Number && value_2 != null) } + return if (this@case_6_3 is Int && this@case_6_3 != null && value_2 is Number && value_2 != null) true else null +} +fun T.case_6_4(value_2: Any?): Boolean? { + contract { returns(null) implies (this@case_6_4 is Int && this@case_6_4 != null && value_2 is Number && value_2 != null) } + return if (this@case_6_4 is Int && this@case_6_4 != null && value_2 is Number && value_2 != null) null else true +} + +fun T?.case_7_1(value_2: Any?): Boolean { + contract { returns(true) implies (this@case_7_1 is Number && this@case_7_1 is Int && this@case_7_1 != null && value_2 != null) } + return this@case_7_1 is Number && this@case_7_1 is Int && this@case_7_1 != null && value_2 != null +} +fun T?.case_7_2(value_2: Any?): Boolean { + contract { returns(true) implies (this@case_7_2 is Number && this@case_7_2 is Int && this@case_7_2 != null && value_2 != null) } + return this@case_7_2 is Number && this@case_7_2 is Int && this@case_7_2 != null && value_2 != null +} +fun T?.case_7_3(value_2: Any?): Boolean? { + contract { returnsNotNull() implies (this@case_7_3 is Number && this@case_7_3 is Int && this@case_7_3 != null && value_2 != null) } + return if (this@case_7_3 is Number && this@case_7_3 is Int && this@case_7_3 != null && value_2 != null) true else null +} +fun T?.case_7_4(value_2: Any?): Boolean? { + contract { returns(null) implies (this@case_7_4 is Number && this@case_7_4 is Int && this@case_7_4 != null && value_2 != null) } + return if (this@case_7_4 is Number && this@case_7_4 is Int && this@case_7_4 != null && value_2 != null) null else true +} + +inline fun T?.case_8_1(value_2: Number, value_3: Any?, value_4: String?): Boolean { + contract { returns(true) implies ((this@case_8_1 is Number || this@case_8_1 is Int) && value_2 is Int && value_3 != null && value_3 is Number && value_4 != null) } + return (this@case_8_1 is Number || this@case_8_1 is Int) && value_2 is Int && value_3 != null && value_3 is Number && value_4 != null +} +inline fun T?.case_8_2(value_2: Number, value_3: Any?, value_4: String?): Boolean { + contract { returns(false) implies ((this@case_8_2 is Number || this@case_8_2 is Int) && value_2 is Int && value_3 != null && value_3 is Number && value_4 != null) } + return !((this@case_8_2 is Number || this@case_8_2 is Int) && value_2 is Int && value_3 != null && value_3 is Number && value_4 != null) +} +inline fun T?.case_8_3(value_2: Number, value_3: Any?, value_4: String?): Boolean? { + contract { returnsNotNull() implies ((this@case_8_3 is Number || this@case_8_3 is Int) && value_2 is Int && value_3 != null && value_3 is Number && value_4 != null) } + return if ((this@case_8_3 is Number || this@case_8_3 is Int) && value_2 is Int && value_3 != null && value_3 is Number && value_4 != null) true else null +} +inline fun T?.case_8_4(value_2: Number, value_3: Any?, value_4: String?): Boolean? { + contract { returns(null) implies ((this@case_8_4 is Number || this@case_8_4 is Int) && value_2 is Int && value_3 != null && value_3 is Number && value_4 != null) } + return if ((this@case_8_4 is Number || this@case_8_4 is Int) && value_2 is Int && value_3 != null && value_3 is Number && value_4 != null) null else true +} + +// FILE: usages.kt + +import contracts.* + +fun case_1(value_1: Any?, value_2: Int?) { + value_1.case_1(value_2) + println(value_1.length) + println(value_2.inv()) +} + +fun case_2(value_1: Number?, value_2: Any?) { + value_1.case_2(value_2) + println(value_1.inv()) + println(value_2.toByte()) +} + +fun case_3(value_1: Any?, value_2: String?) { + value_1.case_3(value_2) + println(value_1.inv()) + println(value_2.length) +} + +fun case_4(value_1: Any?, value_2: Number, value_3: Any?, value_4: String?) { + value_1.case_4(value_2, value_3, value_4) + println(value_2.inv()) + println(value_3.toByte()) + println(value_4.length) +} + +fun case_5(value_1: Any?, value_2: Int?, value_3: Any?, value_4: Int?, value_5: Any?, value_6: Int?) { + when { + value_1.case_5_1(value_2) -> { + println(value_1.length) + println(value_2.inv()) + } + } + when { + !value_3.case_5_2(value_4) -> { + println(value_3.length) + println(value_4.inv()) + } + } + when { + value_5.case_5_3(value_6) != null -> { + println(value_5.length) + println(value_6.inv()) + } + } + when { + value_5.case_5_4(value_6) == null -> { + println(value_5.length) + println(value_6.inv()) + } + } +} + +fun case_6(value_1: Number?, value_2: Any?, value_3: Number?, value_4: Any?, value_5: Number?, value_6: Any?) { + if (value_1.case_6_1(value_2)) { + println(value_1.inv()) + println(value_2.toByte()) + } + if (!value_3.case_6_2(value_4)) { + println(value_3.inv()) + println(value_4.toByte()) + } + if (value_5.case_6_3(value_6) != null) { + println(value_5.inv()) + println(value_6.toByte()) + } + if (value_5.case_6_4(value_6) == null) { + println(value_5.inv()) + println(value_6.toByte()) + } +} + +fun case_7(value_1: Any?, value_2: String?, value_3: Any?, value_4: String?, value_5: Any?, value_6: String?) { + if (value_1.case_7_1(value_2)) { + println(value_1.inv()) + println(value_2.length) + } + if (value_3.case_7_2(value_4)) { + println(value_3.inv()) + println(value_4.length) + } + if (value_5.case_7_3(value_6) != null) { + println(value_5.inv()) + println(value_6.length) + } + if (value_5.case_7_4(value_6) == null) { + println(value_5.inv()) + println(value_6.length) + } +} + +fun case_8(value_1: Any?, value_2: Number, value_3: Any?, value_4: String?, value_5: Any?, value_6: Number, value_7: Any?, value_8: String?) { + when { value_1.case_8_1(value_2, value_3, value_4) -> println(value_2.inv()) } + when { value_1.case_8_1(value_2, value_3, value_4) -> println(value_3.toByte()) } + when { value_1.case_8_1(value_2, value_3, value_4) -> println(value_4.length) } + when { !value_5.case_8_2(value_6, value_7, value_8) -> println(value_6.inv()) } + when { !value_5.case_8_2(value_6, value_7, value_8) -> println(value_7.toByte()) } + when { !value_5.case_8_2(value_6, value_7, value_8) -> println(value_8.length) } + when { value_5.case_8_3(value_6, value_7, value_8) != null -> println(value_6.inv()) } + when { value_5.case_8_3(value_6, value_7, value_8) != null -> println(value_7.toByte()) } + when { value_5.case_8_3(value_6, value_7, value_8) != null -> println(value_8.length) } + when { value_5.case_8_4(value_6, value_7, value_8) == null -> println(value_6.inv()) } + when { value_5.case_8_4(value_6, value_7, value_8) == null -> println(value_7.toByte()) } + when { value_5.case_8_4(value_6, value_7, value_8) == null -> println(value_8.length) } +} diff --git a/compiler/tests-spec/testData/diagnostics/notLinked/contracts/analysis/smartcasts/pos/6.txt b/compiler/tests-spec/testData/diagnostics/notLinked/contracts/analysis/smartcasts/pos/6.txt new file mode 100644 index 00000000000..5d82a078ba9 --- /dev/null +++ b/compiler/tests-spec/testData/diagnostics/notLinked/contracts/analysis/smartcasts/pos/6.txt @@ -0,0 +1,73 @@ +package + +public fun case_1(/*0*/ value_1: kotlin.Any?, /*1*/ value_2: kotlin.Int?): kotlin.Unit +public fun case_2(/*0*/ value_1: kotlin.Number?, /*1*/ value_2: kotlin.Any?): kotlin.Unit +public fun case_3(/*0*/ value_1: kotlin.Any?, /*1*/ value_2: kotlin.String?): kotlin.Unit +public fun case_4(/*0*/ value_1: kotlin.Any?, /*1*/ value_2: kotlin.Number, /*2*/ value_3: kotlin.Any?, /*3*/ value_4: kotlin.String?): kotlin.Unit +public fun case_5(/*0*/ value_1: kotlin.Any?, /*1*/ value_2: kotlin.Int?, /*2*/ value_3: kotlin.Any?, /*3*/ value_4: kotlin.Int?, /*4*/ value_5: kotlin.Any?, /*5*/ value_6: kotlin.Int?): kotlin.Unit +public fun case_6(/*0*/ value_1: kotlin.Number?, /*1*/ value_2: kotlin.Any?, /*2*/ value_3: kotlin.Number?, /*3*/ value_4: kotlin.Any?, /*4*/ value_5: kotlin.Number?, /*5*/ value_6: kotlin.Any?): kotlin.Unit +public fun case_7(/*0*/ value_1: kotlin.Any?, /*1*/ value_2: kotlin.String?, /*2*/ value_3: kotlin.Any?, /*3*/ value_4: kotlin.String?, /*4*/ value_5: kotlin.Any?, /*5*/ value_6: kotlin.String?): kotlin.Unit +public fun case_8(/*0*/ value_1: kotlin.Any?, /*1*/ value_2: kotlin.Number, /*2*/ value_3: kotlin.Any?, /*3*/ value_4: kotlin.String?, /*4*/ value_5: kotlin.Any?, /*5*/ value_6: kotlin.Number, /*6*/ value_7: kotlin.Any?, /*7*/ value_8: kotlin.String?): kotlin.Unit + +package contracts { + public fun T?.case_1(/*0*/ value_1: kotlin.Int?): kotlin.Unit + Returns(WILDCARD) -> != null && is String && value_1 != null + + public fun T.case_2(/*0*/ value_2: kotlin.Any?): kotlin.Unit + Returns(WILDCARD) -> is Int && != null && value_2 is Number && value_2 != null + + public fun T?.case_3(/*0*/ value_2: kotlin.Any?): kotlin.Unit + Returns(WILDCARD) -> is Number && is Int && != null && value_2 != null + + public inline fun T?.case_4(/*0*/ value_2: kotlin.Number, /*1*/ value_3: kotlin.Any?, /*2*/ value_4: kotlin.String?): kotlin.Unit + Returns(WILDCARD) -> ( is Number || is Int) && value_2 is Int && value_3 != null && value_3 is Number && value_4 != null + + public fun T?.case_5_1(/*0*/ value_1: kotlin.Int?): kotlin.Boolean + Returns(TRUE) -> != null && is String && value_1 != null + + public fun T?.case_5_2(/*0*/ value_1: kotlin.Int?): kotlin.Boolean + Returns(FALSE) -> != null && is String && value_1 != null + + public fun T?.case_5_3(/*0*/ value_1: kotlin.Int?): kotlin.Boolean? + Returns(NOT_NULL) -> != null && is String && value_1 != null + + public fun T?.case_5_4(/*0*/ value_1: kotlin.Int?): kotlin.Boolean? + Returns(NULL) -> != null && is String && value_1 != null + + public fun T.case_6_1(/*0*/ value_2: kotlin.Any?): kotlin.Boolean + Returns(TRUE) -> is Int && != null && value_2 is Number && value_2 != null + + public fun T.case_6_2(/*0*/ value_2: kotlin.Any?): kotlin.Boolean + Returns(FALSE) -> is Int && != null && value_2 is Number && value_2 != null + + public fun T.case_6_3(/*0*/ value_2: kotlin.Any?): kotlin.Boolean? + Returns(NOT_NULL) -> is Int && != null && value_2 is Number && value_2 != null + + public fun T.case_6_4(/*0*/ value_2: kotlin.Any?): kotlin.Boolean? + Returns(NULL) -> is Int && != null && value_2 is Number && value_2 != null + + public fun T?.case_7_1(/*0*/ value_2: kotlin.Any?): kotlin.Boolean + Returns(TRUE) -> is Number && is Int && != null && value_2 != null + + public fun T?.case_7_2(/*0*/ value_2: kotlin.Any?): kotlin.Boolean + Returns(TRUE) -> is Number && is Int && != null && value_2 != null + + public fun T?.case_7_3(/*0*/ value_2: kotlin.Any?): kotlin.Boolean? + Returns(NOT_NULL) -> is Number && is Int && != null && value_2 != null + + public fun T?.case_7_4(/*0*/ value_2: kotlin.Any?): kotlin.Boolean? + Returns(NULL) -> is Number && is Int && != null && value_2 != null + + public inline fun T?.case_8_1(/*0*/ value_2: kotlin.Number, /*1*/ value_3: kotlin.Any?, /*2*/ value_4: kotlin.String?): kotlin.Boolean + Returns(TRUE) -> ( is Number || is Int) && value_2 is Int && value_3 != null && value_3 is Number && value_4 != null + + public inline fun T?.case_8_2(/*0*/ value_2: kotlin.Number, /*1*/ value_3: kotlin.Any?, /*2*/ value_4: kotlin.String?): kotlin.Boolean + Returns(FALSE) -> ( is Number || is Int) && value_2 is Int && value_3 != null && value_3 is Number && value_4 != null + + public inline fun T?.case_8_3(/*0*/ value_2: kotlin.Number, /*1*/ value_3: kotlin.Any?, /*2*/ value_4: kotlin.String?): kotlin.Boolean? + Returns(NOT_NULL) -> ( is Number || is Int) && value_2 is Int && value_3 != null && value_3 is Number && value_4 != null + + public inline fun T?.case_8_4(/*0*/ value_2: kotlin.Number, /*1*/ value_3: kotlin.Any?, /*2*/ value_4: kotlin.String?): kotlin.Boolean? + Returns(NULL) -> ( is Number || is Int) && value_2 is Int && value_3 != null && value_3 is Number && value_4 != null + +} diff --git a/compiler/tests-spec/testData/diagnostics/notLinked/contracts/analysis/smartcasts/pos/7.kt b/compiler/tests-spec/testData/diagnostics/notLinked/contracts/analysis/smartcasts/pos/7.kt new file mode 100644 index 00000000000..960cc733a29 --- /dev/null +++ b/compiler/tests-spec/testData/diagnostics/notLinked/contracts/analysis/smartcasts/pos/7.kt @@ -0,0 +1,350 @@ +// !LANGUAGE: +AllowContractsForCustomFunctions +UseReturnsEffect +// !DIAGNOSTICS: -INVISIBLE_REFERENCE -INVISIBLE_MEMBER +// !USE_EXPERIMENTAL: kotlin.contracts.ExperimentalContracts + +/* + KOTLIN DIAGNOSTICS NOT LINKED SPEC TEST (POSITIVE) + + SECTION: contracts + CATEGORY: analysis, smartcasts + NUMBER: 7 + DESCRIPTION: Smartcasts using Returns effects with nested or subsequent contract function calls. + */ + +// FILE: contracts.kt + +package contracts + +import kotlin.contracts.* + +fun case_1_1(value_1: Int?) { + contract { returns() implies (value_1 != null) } + if (!(value_1 != null)) throw Exception() +} +fun case_1_2(value_1: Int?) { + contract { returns() implies (value_1 == null) } + if (!(value_1 == null)) throw Exception() +} + +fun case_2_1(value_1: Number?) { + contract { returns() implies (value_1 is Float) } + if (!(value_1 is Float)) throw Exception() +} +fun case_2_2(value_1: Number?) { + contract { returns() implies (value_1 is Int) } + if (!(value_1 is Int)) throw Exception() +} + +fun case_3_1(value_1: Any?) { + contract { returns() implies (value_1 is String) } + if (!(value_1 is String)) throw Exception() +} +fun case_3_2(value_1: Any?) { + contract { returns() implies (value_1 !is String) } + if (!(value_1 !is String)) throw Exception() +} + +fun case_4_1(value_1: Any?) { + contract { returns() implies (value_1 is Number?) } + if (!(value_1 is Number?)) throw Exception() +} +fun case_4_2(value_1: Number?) { + contract { returns() implies (value_1 != null) } + if (!(value_1 != null)) throw Exception() +} +fun case_4_3(value_1: Number) { + contract { returns() implies (value_1 is Int) } + if (!(value_1 is Int)) throw Exception() +} + +fun case_5_1(value_1: Int?): Boolean { + contract { returns(true) implies (value_1 != null) } + return value_1 != null +} +fun case_5_2(value_1: Int?): Boolean { + contract { returns(true) implies (value_1 == null) } + return value_1 == null +} +fun case_5_3(value_1: Int?): Boolean { + contract { returns(false) implies (value_1 != null) } + return !(value_1 != null) +} +fun case_5_4(value_1: Int?): Boolean { + contract { returns(false) implies (value_1 == null) } + return !(value_1 == null) +} +fun case_5_5(value_1: Int?): Boolean? { + contract { returnsNotNull() implies (value_1 != null) } + return if (value_1 != null) true else null +} +fun case_5_6(value_1: Int?): Boolean? { + contract { returnsNotNull() implies (value_1 == null) } + return if (value_1 == null) true else null +} +fun case_5_7(value_1: Int?): Boolean? { + contract { returns(null) implies (value_1 != null) } + return if (value_1 != null) null else true +} +fun case_5_8(value_1: Int?): Boolean? { + contract { returns(null) implies (value_1 == null) } + return if (value_1 == null) null else true +} + +fun case_6_1(value_1: Number?): Boolean { + contract { returns(true) implies (value_1 is Float) } + return value_1 is Float +} +fun case_6_2(value_1: Number?): Boolean { + contract { returns(true) implies (value_1 is Int) } + return value_1 is Int +} +fun case_6_3(value_1: Number?): Boolean { + contract { returns(false) implies (value_1 is Float) } + return !(value_1 is Float) +} +fun case_6_4(value_1: Number?): Boolean { + contract { returns(false) implies (value_1 is Int) } + return !(value_1 is Int) +} +fun case_6_5(value_1: Number?): Boolean? { + contract { returnsNotNull() implies (value_1 is Float) } + return if (value_1 is Float) true else null +} +fun case_6_6(value_1: Number?): Boolean? { + contract { returnsNotNull() implies (value_1 is Int) } + return if (value_1 is Int) true else null +} +fun case_6_7(value_1: Number?): Boolean? { + contract { returns(null) implies (value_1 is Float) } + return if (value_1 is Float) null else true +} +fun case_6_8(value_1: Number?): Boolean? { + contract { returns(null) implies (value_1 is Int) } + return if (value_1 is Int) null else true +} + +fun case_7_1(value_1: Any?): Boolean { + contract { returns(true) implies (value_1 is String) } + return value_1 is String +} +fun case_7_2(value_1: Any?): Boolean { + contract { returns(true) implies (value_1 !is String) } + return value_1 !is String +} +fun case_7_3(value_1: Any?): Boolean { + contract { returns(false) implies (value_1 is String) } + return !(value_1 is String) +} +fun case_7_4(value_1: Any?): Boolean { + contract { returns(false) implies (value_1 !is String) } + return !(value_1 !is String) +} +fun case_7_5(value_1: Any?): Boolean? { + contract { returnsNotNull() implies (value_1 is String) } + return if (value_1 is String) true else null +} +fun case_7_6(value_1: Any?): Boolean? { + contract { returnsNotNull() implies (value_1 !is String) } + return if (value_1 !is String) true else null +} +fun case_7_7(value_1: Any?): Boolean? { + contract { returns(null) implies (value_1 is String) } + return if (value_1 is String) null else true +} +fun case_7_8(value_1: Any?): Boolean? { + contract { returns(null) implies (value_1 !is String) } + return if (value_1 !is String) null else true +} + +fun case_8_1(value_1: Any?): Boolean { + contract { returns(true) implies (value_1 is Number?) } + return value_1 is Number? +} +fun case_8_2(value_1: Number?): Boolean { + contract { returns(true) implies (value_1 != null) } + return value_1 != null +} +fun case_8_3(value_1: Number): Boolean { + contract { returns(true) implies (value_1 is Int) } + return value_1 is Int +} +fun case_8_4(value_1: Any?): Boolean { + contract { returns(false) implies (value_1 is Number?) } + return !(value_1 is Number?) +} +fun case_8_5(value_1: Number?): Boolean { + contract { returns(false) implies (value_1 != null) } + return !(value_1 != null) +} +fun case_8_6(value_1: Number): Boolean { + contract { returns(false) implies (value_1 is Int) } + return !(value_1 is Int) +} +fun case_8_7(value_1: Any?): Boolean? { + contract { returnsNotNull() implies (value_1 is Number?) } + return if (value_1 is Number?) true else null +} +fun case_8_8(value_1: Number?): Boolean? { + contract { returnsNotNull() implies (value_1 != null) } + return if (value_1 != null) true else null +} +fun case_8_9(value_1: Number): Boolean? { + contract { returnsNotNull() implies (value_1 is Int) } + return if (value_1 is Int) true else null +} +fun case_8_10(value_1: Any?): Boolean? { + contract { returns(null) implies (value_1 is Number?) } + return if (value_1 is Number?) null else true +} +fun case_8_11(value_1: Number?): Boolean? { + contract { returns(null) implies (value_1 != null) } + return if (value_1 != null) null else true +} +fun case_8_12(value_1: Number): Boolean? { + contract { returns(null) implies (value_1 is Int) } + return if (value_1 is Int) null else true +} + +// FILE: usages.kt + +import contracts.* + +fun case_1(value_1: Int?) { + case_1_1(value_1) + value_1.inv() + case_1_2(value_1) + value_1.inv() + case_1_1(value_1) + value_1.inv() +} + +fun case_2(value_1: Number?) { + case_2_1(value_1) + value_1.toByte() + case_2_2(value_1) + value_1.inv() +} + +fun case_3(value_1: Any?) { + case_3_1(value_1) + value_1.length + case_3_2(value_1) + value_1.length +} + +fun case_4(value_1: Any?) { + case_4_1(value_1) + value_1?.toByte() + case_4_2(value_1) + value_1.toByte() + case_4_3(value_1) + value_1.inv() +} + +fun case_5(value_1: Int?, value_2: Int?) { + if (case_5_1(value_1)) { + value_1.inv() + if (case_5_2(value_1)) { + value_1.inv() + value_1.inv() + } + } + if (!case_5_3(value_2)) { + value_2.inv() + if (!case_5_4(value_2)) { + value_2.inv() + value_2.inv() + } + } + if (case_5_5(value_2) != null) { + value_2.inv() + if (case_5_6(value_2) != null) { + value_2.inv() + value_2.inv() + } + } + if (case_5_7(value_2) == null) { + value_2.inv() + if (case_5_8(value_2) == null) { + value_2.inv() + value_2.inv() + } + } +} + +fun case_6(value_1: Number?, value_2: Number?) { + when { + case_6_1(value_1) -> { + value_1.toByte() + when { case_6_2(value_1) -> value_1.inv() } + } + } + when { + !case_6_3(value_2) -> { + value_2.toByte() + when { !case_6_4(value_2) -> value_2.inv() } + } + } + when { + case_6_5(value_2) != null -> { + value_2.toByte() + when { case_6_6(value_2) != null -> value_2.inv() } + } + } + when { + case_6_7(value_2) == null -> { + value_2.toByte() + when { case_6_8(value_2) == null -> value_2.inv() } + } + } +} + +fun case_7(value_1: Any?, value_2: Any?) { + if (case_7_1(value_1)) { + value_1.length + if (case_7_2(value_1)) value_1.length + } + if (!case_7_3(value_2)) { + value_2.length + if (!case_7_4(value_2)) value_2.length + } + if (case_7_5(value_2) != null) { + value_2.length + if (case_7_6(value_2) != null) value_2.length + } + if (case_7_7(value_2) == null) { + value_2.length + if (case_7_8(value_2) == null) value_2.length + } +} + +fun case_8(value_1: Any?, value_2: Any?) { + if (case_8_1(value_1)) { + value_1?.toByte() + if (case_8_2(value_1)) { + value_1.toByte() + if (case_8_3(value_1)) value_1.inv() + } + } + if (!case_8_4(value_2)) { + value_2?.toByte() + if (!case_8_5(value_2)) { + value_2.toByte() + if (!case_8_6(value_2)) value_2.inv() + } + } + if (case_8_7(value_2) != null) { + value_2?.toByte() + if (case_8_8(value_2) != null) { + value_2.toByte() + if (case_8_9(value_2) != null) value_2.inv() + } + } + if (case_8_10(value_2) == null) { + value_2?.toByte() + if (case_8_11(value_2) == null) { + value_2.toByte() + if (case_8_12(value_2) == null) value_2.inv() + } + } +} diff --git a/compiler/tests-spec/testData/diagnostics/notLinked/contracts/analysis/smartcasts/pos/7.txt b/compiler/tests-spec/testData/diagnostics/notLinked/contracts/analysis/smartcasts/pos/7.txt new file mode 100644 index 00000000000..1262bbb4718 --- /dev/null +++ b/compiler/tests-spec/testData/diagnostics/notLinked/contracts/analysis/smartcasts/pos/7.txt @@ -0,0 +1,148 @@ +package + +public fun case_1(/*0*/ value_1: kotlin.Int?): kotlin.Unit +public fun case_2(/*0*/ value_1: kotlin.Number?): kotlin.Unit +public fun case_3(/*0*/ value_1: kotlin.Any?): kotlin.Unit +public fun case_4(/*0*/ value_1: kotlin.Any?): kotlin.Unit +public fun case_5(/*0*/ value_1: kotlin.Int?, /*1*/ value_2: kotlin.Int?): kotlin.Unit +public fun case_6(/*0*/ value_1: kotlin.Number?, /*1*/ value_2: kotlin.Number?): kotlin.Unit +public fun case_7(/*0*/ value_1: kotlin.Any?, /*1*/ value_2: kotlin.Any?): kotlin.Unit +public fun case_8(/*0*/ value_1: kotlin.Any?, /*1*/ value_2: kotlin.Any?): kotlin.Unit + +package contracts { + public fun case_1_1(/*0*/ value_1: kotlin.Int?): kotlin.Unit + Returns(WILDCARD) -> value_1 != null + + public fun case_1_2(/*0*/ value_1: kotlin.Int?): kotlin.Unit + Returns(WILDCARD) -> value_1 == null + + public fun case_2_1(/*0*/ value_1: kotlin.Number?): kotlin.Unit + Returns(WILDCARD) -> value_1 is Float + + public fun case_2_2(/*0*/ value_1: kotlin.Number?): kotlin.Unit + Returns(WILDCARD) -> value_1 is Int + + public fun case_3_1(/*0*/ value_1: kotlin.Any?): kotlin.Unit + Returns(WILDCARD) -> value_1 is String + + public fun case_3_2(/*0*/ value_1: kotlin.Any?): kotlin.Unit + Returns(WILDCARD) -> value_1 !is String + + public fun case_4_1(/*0*/ value_1: kotlin.Any?): kotlin.Unit + Returns(WILDCARD) -> value_1 is Number? + + public fun case_4_2(/*0*/ value_1: kotlin.Number?): kotlin.Unit + Returns(WILDCARD) -> value_1 != null + + public fun case_4_3(/*0*/ value_1: kotlin.Number): kotlin.Unit + Returns(WILDCARD) -> value_1 is Int + + public fun case_5_1(/*0*/ value_1: kotlin.Int?): kotlin.Boolean + Returns(TRUE) -> value_1 != null + + public fun case_5_2(/*0*/ value_1: kotlin.Int?): kotlin.Boolean + Returns(TRUE) -> value_1 == null + + public fun case_5_3(/*0*/ value_1: kotlin.Int?): kotlin.Boolean + Returns(FALSE) -> value_1 != null + + public fun case_5_4(/*0*/ value_1: kotlin.Int?): kotlin.Boolean + Returns(FALSE) -> value_1 == null + + public fun case_5_5(/*0*/ value_1: kotlin.Int?): kotlin.Boolean? + Returns(NOT_NULL) -> value_1 != null + + public fun case_5_6(/*0*/ value_1: kotlin.Int?): kotlin.Boolean? + Returns(NOT_NULL) -> value_1 == null + + public fun case_5_7(/*0*/ value_1: kotlin.Int?): kotlin.Boolean? + Returns(NULL) -> value_1 != null + + public fun case_5_8(/*0*/ value_1: kotlin.Int?): kotlin.Boolean? + Returns(NULL) -> value_1 == null + + public fun case_6_1(/*0*/ value_1: kotlin.Number?): kotlin.Boolean + Returns(TRUE) -> value_1 is Float + + public fun case_6_2(/*0*/ value_1: kotlin.Number?): kotlin.Boolean + Returns(TRUE) -> value_1 is Int + + public fun case_6_3(/*0*/ value_1: kotlin.Number?): kotlin.Boolean + Returns(FALSE) -> value_1 is Float + + public fun case_6_4(/*0*/ value_1: kotlin.Number?): kotlin.Boolean + Returns(FALSE) -> value_1 is Int + + public fun case_6_5(/*0*/ value_1: kotlin.Number?): kotlin.Boolean? + Returns(NOT_NULL) -> value_1 is Float + + public fun case_6_6(/*0*/ value_1: kotlin.Number?): kotlin.Boolean? + Returns(NOT_NULL) -> value_1 is Int + + public fun case_6_7(/*0*/ value_1: kotlin.Number?): kotlin.Boolean? + Returns(NULL) -> value_1 is Float + + public fun case_6_8(/*0*/ value_1: kotlin.Number?): kotlin.Boolean? + Returns(NULL) -> value_1 is Int + + public fun case_7_1(/*0*/ value_1: kotlin.Any?): kotlin.Boolean + Returns(TRUE) -> value_1 is String + + public fun case_7_2(/*0*/ value_1: kotlin.Any?): kotlin.Boolean + Returns(TRUE) -> value_1 !is String + + public fun case_7_3(/*0*/ value_1: kotlin.Any?): kotlin.Boolean + Returns(FALSE) -> value_1 is String + + public fun case_7_4(/*0*/ value_1: kotlin.Any?): kotlin.Boolean + Returns(FALSE) -> value_1 !is String + + public fun case_7_5(/*0*/ value_1: kotlin.Any?): kotlin.Boolean? + Returns(NOT_NULL) -> value_1 is String + + public fun case_7_6(/*0*/ value_1: kotlin.Any?): kotlin.Boolean? + Returns(NOT_NULL) -> value_1 !is String + + public fun case_7_7(/*0*/ value_1: kotlin.Any?): kotlin.Boolean? + Returns(NULL) -> value_1 is String + + public fun case_7_8(/*0*/ value_1: kotlin.Any?): kotlin.Boolean? + Returns(NULL) -> value_1 !is String + + public fun case_8_1(/*0*/ value_1: kotlin.Any?): kotlin.Boolean + Returns(TRUE) -> value_1 is Number? + + public fun case_8_10(/*0*/ value_1: kotlin.Any?): kotlin.Boolean? + Returns(NULL) -> value_1 is Number? + + public fun case_8_11(/*0*/ value_1: kotlin.Number?): kotlin.Boolean? + Returns(NULL) -> value_1 != null + + public fun case_8_12(/*0*/ value_1: kotlin.Number): kotlin.Boolean? + Returns(NULL) -> value_1 is Int + + public fun case_8_2(/*0*/ value_1: kotlin.Number?): kotlin.Boolean + Returns(TRUE) -> value_1 != null + + public fun case_8_3(/*0*/ value_1: kotlin.Number): kotlin.Boolean + Returns(TRUE) -> value_1 is Int + + public fun case_8_4(/*0*/ value_1: kotlin.Any?): kotlin.Boolean + Returns(FALSE) -> value_1 is Number? + + public fun case_8_5(/*0*/ value_1: kotlin.Number?): kotlin.Boolean + Returns(FALSE) -> value_1 != null + + public fun case_8_6(/*0*/ value_1: kotlin.Number): kotlin.Boolean + Returns(FALSE) -> value_1 is Int + + public fun case_8_7(/*0*/ value_1: kotlin.Any?): kotlin.Boolean? + Returns(NOT_NULL) -> value_1 is Number? + + public fun case_8_8(/*0*/ value_1: kotlin.Number?): kotlin.Boolean? + Returns(NOT_NULL) -> value_1 != null + + public fun case_8_9(/*0*/ value_1: kotlin.Number): kotlin.Boolean? + Returns(NOT_NULL) -> value_1 is Int + +} diff --git a/compiler/tests-spec/testData/diagnostics/notLinked/contracts/analysis/smartcasts/pos/8.kt b/compiler/tests-spec/testData/diagnostics/notLinked/contracts/analysis/smartcasts/pos/8.kt new file mode 100644 index 00000000000..c0d12453e68 --- /dev/null +++ b/compiler/tests-spec/testData/diagnostics/notLinked/contracts/analysis/smartcasts/pos/8.kt @@ -0,0 +1,138 @@ +// !LANGUAGE: +AllowContractsForCustomFunctions +UseReturnsEffect +// !WITH_CONTRACT_FUNCTIONS +// !DIAGNOSTICS: -INVISIBLE_REFERENCE -INVISIBLE_MEMBER +// !USE_EXPERIMENTAL: kotlin.contracts.ExperimentalContracts + +/* + KOTLIN DIAGNOSTICS NOT LINKED SPEC TEST (POSITIVE) + + SECTION: contracts + CATEGORY: analysis, smartcasts + NUMBER: 8 + DESCRIPTION: Smartcasts using some Returns effects. + */ + +// FILE: contracts.kt + +package contracts + +import kotlin.contracts.* + +fun T?.case_3(value_1: Int?, value_2: Boolean): Boolean { + contract { + returns(true) implies (value_1 == null) + returns(false) implies (value_1 != null && value_2) + returns(null) implies (value_1 != null && !value_2) + } + + return value_1 == null +} + +fun case_4(value_1: Number, block: (() -> Unit)?): Boolean? { + contract { + returns(true) implies (value_1 is Int) + returns(false) implies (block == null) + returns(null) implies (block != null) + } + + return value_1 == null +} + +fun String?.case_5(value_1: Number?): Boolean? { + contract { + returns(true) implies (value_1 != null) + returns(false) implies (value_1 is Int) + returnsNotNull() implies (this@case_5 != null) + } + + return value_1 == null +} + +fun T?.case_6(value_1: Number, value_2: String?): Boolean? { + contract { + returns(true) implies (this@case_6 != null) + returns(false) implies (this@case_6 is String) + returns(null) implies (value_1 is Int) + returnsNotNull() implies (value_2 != null) + } + + return value_1 == null +} + +// FILE: usages.kt + +import contracts.* + +fun case_1(value_1: Any?) { + funWithReturns(value_1 is Number?) + println(value_1?.toByte()) + if (funWithReturnsTrue(value_1 is Number)) { + println(value_1.toByte()) + if (funWithReturnsNotNull(value_1 is Int) != null) println(value_1.inv()) + } +} + +fun case_2(value_1: Any?) { + if (!funWithReturnsFalse(value_1 is Number?)) { + println(value_1?.toByte()) + funWithReturns(value_1 is Number) + println(value_1.toByte()) + if (funWithReturnsNull(value_1 is Int) == null) println(value_1.inv()) + } +} + +fun case_3(value_1: Int?, value_2: Any?) { + if (!value_1.case_3(value_1, value_2 is Number?)) { + println(value_2?.toByte()) + println(value_1.inv()) + } else if (value_1.case_3(value_1, value_2 is Number?)) { + println(value_1) + } else { + println(value_1.inv()) + } +} + +fun case_4(value_1: Number, value_2: (() -> Unit)?) { + if (contracts.case_4(value_1, value_2) == true) { + value_1.inv() + } else if (contracts.case_4(value_1, value_2) == false) { + println(value_2) + } else if (contracts.case_4(value_1, value_2) == null) { + value_2() + } +} + +/* + UNEXPECTED BEHAVIOUR: unsafe calls + */ +fun case_5(value_1: Number?, value_2: String?) { + when (value_2.case_5(value_1)) { + true -> { + println(value_2.length) + println(value_1.toByte()) + } + false -> { + println(value_2.length) + println(value_1.inv()) + } + } +} + +/* + UNEXPECTED BEHAVIOUR: unsafe calls + */ +fun case_6(value_1: Number, value_2: String?, value_3: Any?) { + when (value_3.case_6(value_1, value_2)) { + true -> { + println(value_3.equals("")) + println(value_2.length) + } + false -> { + println(value_3.length) + println(value_2.length) + } + null -> { + println(value_1.inv()) + } + } +} diff --git a/compiler/tests-spec/testData/diagnostics/notLinked/contracts/analysis/smartcasts/pos/8.txt b/compiler/tests-spec/testData/diagnostics/notLinked/contracts/analysis/smartcasts/pos/8.txt new file mode 100644 index 00000000000..6596c240522 --- /dev/null +++ b/compiler/tests-spec/testData/diagnostics/notLinked/contracts/analysis/smartcasts/pos/8.txt @@ -0,0 +1,139 @@ +package + +public fun case_1(/*0*/ value_1: kotlin.Any?): kotlin.Unit +public fun case_2(/*0*/ value_1: kotlin.Any?): kotlin.Unit +public fun case_3(/*0*/ value_1: kotlin.Int?, /*1*/ value_2: kotlin.Any?): kotlin.Unit +public fun case_4(/*0*/ value_1: kotlin.Number, /*1*/ value_2: (() -> kotlin.Unit)?): kotlin.Unit +public fun case_5(/*0*/ value_1: kotlin.Number?, /*1*/ value_2: kotlin.String?): kotlin.Unit +public fun case_6(/*0*/ value_1: kotlin.Number, /*1*/ value_2: kotlin.String?, /*2*/ value_3: kotlin.Any?): kotlin.Unit +public inline fun funWithAtLeastOnceCallsInPlace(/*0*/ block: () -> T): T + CallsInPlace(block, AT_LEAST_ONCE) + +public inline fun funWithAtLeastOnceCallsInPlace(/*0*/ block: () -> kotlin.Unit): kotlin.Unit + CallsInPlace(block, AT_LEAST_ONCE) + +public inline fun funWithAtMostOnceCallsInPlace(/*0*/ block: () -> kotlin.Unit): kotlin.Unit + CallsInPlace(block, AT_MOST_ONCE) + +public inline fun funWithExactlyOnceCallsInPlace(/*0*/ block: () -> T): T + CallsInPlace(block, EXACTLY_ONCE) + +public inline fun funWithExactlyOnceCallsInPlace(/*0*/ block: () -> kotlin.Unit): kotlin.Unit + CallsInPlace(block, EXACTLY_ONCE) + +public fun funWithReturns(/*0*/ cond: kotlin.Boolean): kotlin.Unit + Returns(WILDCARD) -> cond + +public fun funWithReturnsAndInvertCondition(/*0*/ cond: kotlin.Boolean): kotlin.Unit + Returns(WILDCARD) -> !cond + +public fun funWithReturnsAndInvertTypeCheck(/*0*/ value_1: kotlin.Any?): kotlin.Unit + Returns(WILDCARD) -> value_1 !is String + +public fun funWithReturnsAndNotNullCheck(/*0*/ value_1: kotlin.Any?): kotlin.Unit + Returns(WILDCARD) -> value_1 != null + +public fun funWithReturnsAndNullCheck(/*0*/ value_1: kotlin.Any?): kotlin.Unit + Returns(WILDCARD) -> value_1 == null + +public fun funWithReturnsAndTypeCheck(/*0*/ value_1: kotlin.Any?): kotlin.Unit + Returns(WILDCARD) -> value_1 is String + +public fun funWithReturnsFalse(/*0*/ cond: kotlin.Boolean): kotlin.Boolean + Returns(FALSE) -> cond + +public fun funWithReturnsFalseAndInvertCondition(/*0*/ cond: kotlin.Boolean): kotlin.Boolean + Returns(FALSE) -> !cond + +public fun funWithReturnsFalseAndInvertTypeCheck(/*0*/ value_1: kotlin.Any?): kotlin.Boolean + Returns(FALSE) -> value_1 !is String + +public fun funWithReturnsFalseAndNotNullCheck(/*0*/ value_1: kotlin.Number?): kotlin.Boolean + Returns(FALSE) -> value_1 != null + +public fun funWithReturnsFalseAndNullCheck(/*0*/ value_1: kotlin.Number?): kotlin.Boolean + Returns(FALSE) -> value_1 == null + +public fun funWithReturnsFalseAndTypeCheck(/*0*/ value_1: kotlin.Any?): kotlin.Boolean + Returns(FALSE) -> value_1 is String + +public fun funWithReturnsNotNull(/*0*/ cond: kotlin.Boolean): kotlin.Boolean? + Returns(NOT_NULL) -> cond + +public fun funWithReturnsNotNullAndInvertCondition(/*0*/ cond: kotlin.Boolean): kotlin.Boolean? + Returns(NOT_NULL) -> !cond + +public fun funWithReturnsNotNullAndInvertTypeCheck(/*0*/ value_1: kotlin.Any?): kotlin.Boolean? + Returns(NOT_NULL) -> value_1 !is String + +public fun funWithReturnsNotNullAndNotNullCheck(/*0*/ value_1: kotlin.Number?): kotlin.Boolean? + Returns(NOT_NULL) -> value_1 != null + +public fun funWithReturnsNotNullAndNullCheck(/*0*/ value_1: kotlin.Number?): kotlin.Boolean? + Returns(NOT_NULL) -> value_1 == null + +public fun funWithReturnsNotNullAndTypeCheck(/*0*/ value_1: kotlin.Any?): kotlin.Boolean? + Returns(NOT_NULL) -> value_1 is String + +public fun funWithReturnsNull(/*0*/ cond: kotlin.Boolean): kotlin.Boolean? + Returns(NULL) -> cond + +public fun funWithReturnsNullAndInvertCondition(/*0*/ cond: kotlin.Boolean): kotlin.Boolean? + Returns(NULL) -> !cond + +public fun funWithReturnsNullAndInvertTypeCheck(/*0*/ value_1: kotlin.Any?): kotlin.Boolean? + Returns(NULL) -> value_1 !is String + +public fun funWithReturnsNullAndNotNullCheck(/*0*/ value_1: kotlin.Number?): kotlin.Boolean? + Returns(NULL) -> value_1 != null + +public fun funWithReturnsNullAndNullCheck(/*0*/ value_1: kotlin.Number?): kotlin.Boolean? + Returns(NULL) -> value_1 == null + +public fun funWithReturnsNullAndTypeCheck(/*0*/ value_1: kotlin.Any?): kotlin.Boolean? + Returns(NULL) -> value_1 is String + +public fun funWithReturnsTrue(/*0*/ cond: kotlin.Boolean): kotlin.Boolean + Returns(TRUE) -> cond + +public fun funWithReturnsTrueAndInvertCondition(/*0*/ cond: kotlin.Boolean): kotlin.Boolean + Returns(TRUE) -> !cond + +public fun funWithReturnsTrueAndInvertTypeCheck(/*0*/ value_1: kotlin.Any?): kotlin.Boolean + Returns(TRUE) -> value_1 !is String + +public fun funWithReturnsTrueAndNotNullCheck(/*0*/ value_1: kotlin.Any?): kotlin.Boolean + Returns(TRUE) -> value_1 != null + +public fun funWithReturnsTrueAndNullCheck(/*0*/ value_1: kotlin.Any?): kotlin.Boolean + Returns(TRUE) -> value_1 == null + +public fun funWithReturnsTrueAndTypeCheck(/*0*/ value_1: kotlin.Any?): kotlin.Boolean + Returns(TRUE) -> value_1 is String + +public inline fun funWithUnknownCallsInPlace(/*0*/ block: () -> kotlin.Unit): kotlin.Unit + CallsInPlace(block, UNKNOWN) + +package contracts { + public fun case_4(/*0*/ value_1: kotlin.Number, /*1*/ block: (() -> kotlin.Unit)?): kotlin.Boolean? + Returns(TRUE) -> value_1 is Int + Returns(FALSE) -> block == null + Returns(NULL) -> block != null + + public fun T?.case_3(/*0*/ value_1: kotlin.Int?, /*1*/ value_2: kotlin.Boolean): kotlin.Boolean + Returns(TRUE) -> value_1 == null + Returns(FALSE) -> value_1 != null && value_2 + Returns(NULL) -> value_1 != null && (!value_2) + + public fun kotlin.String?.case_5(/*0*/ value_1: kotlin.Number?): kotlin.Boolean? + Returns(TRUE) -> value_1 != null + Returns(FALSE) -> value_1 is Int + Returns(NOT_NULL) -> != null + + public fun T?.case_6(/*0*/ value_1: kotlin.Number, /*1*/ value_2: kotlin.String?): kotlin.Boolean? + Returns(TRUE) -> != null + Returns(FALSE) -> is String + Returns(NULL) -> value_1 is Int + Returns(NOT_NULL) -> value_2 != null + +} diff --git a/compiler/tests-spec/testData/diagnostics/notLinked/contracts/analysis/smartcasts/pos/9.kt b/compiler/tests-spec/testData/diagnostics/notLinked/contracts/analysis/smartcasts/pos/9.kt new file mode 100644 index 00000000000..73e5aae0fff --- /dev/null +++ b/compiler/tests-spec/testData/diagnostics/notLinked/contracts/analysis/smartcasts/pos/9.kt @@ -0,0 +1,140 @@ +// !LANGUAGE: +AllowContractsForCustomFunctions +UseReturnsEffect +// !WITH_CONTRACT_FUNCTIONS +// !WITH_BASIC_TYPES +// !DIAGNOSTICS: -INVISIBLE_REFERENCE -INVISIBLE_MEMBER +// !USE_EXPERIMENTAL: kotlin.contracts.ExperimentalContracts + +/* + KOTLIN DIAGNOSTICS NOT LINKED SPEC TEST (POSITIVE) + + SECTION: contracts + CATEGORY: analysis, smartcasts + NUMBER: 9 + DESCRIPTION: Smartcast using complex condition with some contract functions (Returns effect). + */ + +// FILE: contracts.kt + +package contracts + +import kotlin.contracts.* + +fun T?.case_4(): Boolean { + contract { returns(true) implies (this@case_4 != null) } + return this@case_4 != null +} + +fun T?.case_4_1(): Boolean { + contract { returns(false) implies (this@case_4_1 != null) } + return !(this@case_4_1 != null) +} + +fun T?.case_4_2(): Boolean? { + contract { returns(null) implies (this@case_4_2 is String) } + return if (this@case_4_2 is String) null else true +} + +fun T?.case_11_1(): Boolean { + contract { returns(false) implies (this@case_11_1 != null) } + return !(this@case_11_1 != null) +} + +fun T?.case_11_2(): Boolean? { + contract { returns(null) implies (this@case_11_2 is String) } + return if (this@case_11_2 is String) null else true +} + +fun T?.case_12(): Boolean { + contract { returns(false) implies (this@case_12 is String) } + return if (this@case_12 is String) false else true +} + +// FILE: usages.kt + +import contracts.* + +fun case_1(value_1: Any?) { + if (funWithReturnsTrue(value_1 is String) && funWithReturnsTrueAndNotNullCheck(value_1)) { + println(value_1.length) + } +} + +fun case_2(value_1: Any?) { + if (!funWithReturnsFalse(value_1 is String) && !funWithReturnsTrueAndNullCheck(value_1)) { + println(value_1.length) + } +} + +fun case_3(value_1: Any?) { + if (funWithReturnsNull(value_1 is String?) == null && funWithReturnsTrue(value_1 != null)) { + println(value_1.length) + } +} + +fun case_4(value_1: Any?) { + if (!value_1.case_4_1() && value_1.case_4_2() == null) { + println(value_1.length) + } +} + +fun case_5(value_1: Any?, value_2: Boolean) { + if (!funWithReturnsFalse(value_1 is String) && value_2) { + println(value_1.length) + } +} + +fun case_6(value_1: Any?, value_2: Boolean?) { + if (funWithReturnsNull(value_1 is String) == null && value_2 != null && value_2) { + println(value_1.length) + } +} + +fun case_7(value_1: String?) { + if (funWithReturnsTrueAndNotNullCheck(value_1) && true) { + println(value_1.length) + } +} + +fun case_8(value_1: Any?) { + if (funWithReturnsTrueAndNullCheck(value_1) && false) { + println(value_1) + } +} + +/* + UNEXPECTED BEHAVIOUR: unreachable code + */ +fun case_9(value_1: Any?) { + if (funWithReturnsFalse(value_1 is String) || funWithReturnsFalse(value_1 is Int)) { + + } else { + println(value_1.length) + println(value_1.inv()) + } +} + +/* + UNEXPECTED BEHAVIOUR: unreachable code + */ +fun case_10(value_1: Any?) { + if (funWithReturnsFalse(value_1 is String) || getBoolean()) { + + } else { + println(value_1.length) + } +} + +fun case_11(value_1: Any?) { + if (!(value_1.case_11_1() || value_1.case_11_2() != null)) { + println(value_1.length) + } +} + +fun case_12(value_1: Any?) { + if (!value_1.case_12() || !value_1.case_12()) { + println(value_1.length) + } + if (!value_1.case_12() && !value_1.case_12()) { + println(value_1.length) + } +} diff --git a/compiler/tests-spec/testData/diagnostics/notLinked/contracts/analysis/smartcasts/pos/9.txt b/compiler/tests-spec/testData/diagnostics/notLinked/contracts/analysis/smartcasts/pos/9.txt new file mode 100644 index 00000000000..7d24c0cdee6 --- /dev/null +++ b/compiler/tests-spec/testData/diagnostics/notLinked/contracts/analysis/smartcasts/pos/9.txt @@ -0,0 +1,176 @@ +package + +public fun case_1(/*0*/ value_1: kotlin.Any?): kotlin.Unit +public fun case_10(/*0*/ value_1: kotlin.Any?): kotlin.Unit +public fun case_11(/*0*/ value_1: kotlin.Any?): kotlin.Unit +public fun case_12(/*0*/ value_1: kotlin.Any?): kotlin.Unit +public fun case_2(/*0*/ value_1: kotlin.Any?): kotlin.Unit +public fun case_3(/*0*/ value_1: kotlin.Any?): kotlin.Unit +public fun case_4(/*0*/ value_1: kotlin.Any?): kotlin.Unit +public fun case_5(/*0*/ value_1: kotlin.Any?, /*1*/ value_2: kotlin.Boolean): kotlin.Unit +public fun case_6(/*0*/ value_1: kotlin.Any?, /*1*/ value_2: kotlin.Boolean?): kotlin.Unit +public fun case_7(/*0*/ value_1: kotlin.String?): kotlin.Unit +public fun case_8(/*0*/ value_1: kotlin.Any?): kotlin.Unit +public fun case_9(/*0*/ value_1: kotlin.Any?): kotlin.Unit +public inline fun funWithAtLeastOnceCallsInPlace(/*0*/ block: () -> T): T + CallsInPlace(block, AT_LEAST_ONCE) + +public inline fun funWithAtLeastOnceCallsInPlace(/*0*/ block: () -> kotlin.Unit): kotlin.Unit + CallsInPlace(block, AT_LEAST_ONCE) + +public inline fun funWithAtMostOnceCallsInPlace(/*0*/ block: () -> kotlin.Unit): kotlin.Unit + CallsInPlace(block, AT_MOST_ONCE) + +public inline fun funWithExactlyOnceCallsInPlace(/*0*/ block: () -> T): T + CallsInPlace(block, EXACTLY_ONCE) + +public inline fun funWithExactlyOnceCallsInPlace(/*0*/ block: () -> kotlin.Unit): kotlin.Unit + CallsInPlace(block, EXACTLY_ONCE) + +public fun funWithReturns(/*0*/ cond: kotlin.Boolean): kotlin.Unit + Returns(WILDCARD) -> cond + +public fun funWithReturnsAndInvertCondition(/*0*/ cond: kotlin.Boolean): kotlin.Unit + Returns(WILDCARD) -> !cond + +public fun funWithReturnsAndInvertTypeCheck(/*0*/ value_1: kotlin.Any?): kotlin.Unit + Returns(WILDCARD) -> value_1 !is String + +public fun funWithReturnsAndNotNullCheck(/*0*/ value_1: kotlin.Any?): kotlin.Unit + Returns(WILDCARD) -> value_1 != null + +public fun funWithReturnsAndNullCheck(/*0*/ value_1: kotlin.Any?): kotlin.Unit + Returns(WILDCARD) -> value_1 == null + +public fun funWithReturnsAndTypeCheck(/*0*/ value_1: kotlin.Any?): kotlin.Unit + Returns(WILDCARD) -> value_1 is String + +public fun funWithReturnsFalse(/*0*/ cond: kotlin.Boolean): kotlin.Boolean + Returns(FALSE) -> cond + +public fun funWithReturnsFalseAndInvertCondition(/*0*/ cond: kotlin.Boolean): kotlin.Boolean + Returns(FALSE) -> !cond + +public fun funWithReturnsFalseAndInvertTypeCheck(/*0*/ value_1: kotlin.Any?): kotlin.Boolean + Returns(FALSE) -> value_1 !is String + +public fun funWithReturnsFalseAndNotNullCheck(/*0*/ value_1: kotlin.Number?): kotlin.Boolean + Returns(FALSE) -> value_1 != null + +public fun funWithReturnsFalseAndNullCheck(/*0*/ value_1: kotlin.Number?): kotlin.Boolean + Returns(FALSE) -> value_1 == null + +public fun funWithReturnsFalseAndTypeCheck(/*0*/ value_1: kotlin.Any?): kotlin.Boolean + Returns(FALSE) -> value_1 is String + +public fun funWithReturnsNotNull(/*0*/ cond: kotlin.Boolean): kotlin.Boolean? + Returns(NOT_NULL) -> cond + +public fun funWithReturnsNotNullAndInvertCondition(/*0*/ cond: kotlin.Boolean): kotlin.Boolean? + Returns(NOT_NULL) -> !cond + +public fun funWithReturnsNotNullAndInvertTypeCheck(/*0*/ value_1: kotlin.Any?): kotlin.Boolean? + Returns(NOT_NULL) -> value_1 !is String + +public fun funWithReturnsNotNullAndNotNullCheck(/*0*/ value_1: kotlin.Number?): kotlin.Boolean? + Returns(NOT_NULL) -> value_1 != null + +public fun funWithReturnsNotNullAndNullCheck(/*0*/ value_1: kotlin.Number?): kotlin.Boolean? + Returns(NOT_NULL) -> value_1 == null + +public fun funWithReturnsNotNullAndTypeCheck(/*0*/ value_1: kotlin.Any?): kotlin.Boolean? + Returns(NOT_NULL) -> value_1 is String + +public fun funWithReturnsNull(/*0*/ cond: kotlin.Boolean): kotlin.Boolean? + Returns(NULL) -> cond + +public fun funWithReturnsNullAndInvertCondition(/*0*/ cond: kotlin.Boolean): kotlin.Boolean? + Returns(NULL) -> !cond + +public fun funWithReturnsNullAndInvertTypeCheck(/*0*/ value_1: kotlin.Any?): kotlin.Boolean? + Returns(NULL) -> value_1 !is String + +public fun funWithReturnsNullAndNotNullCheck(/*0*/ value_1: kotlin.Number?): kotlin.Boolean? + Returns(NULL) -> value_1 != null + +public fun funWithReturnsNullAndNullCheck(/*0*/ value_1: kotlin.Number?): kotlin.Boolean? + Returns(NULL) -> value_1 == null + +public fun funWithReturnsNullAndTypeCheck(/*0*/ value_1: kotlin.Any?): kotlin.Boolean? + Returns(NULL) -> value_1 is String + +public fun funWithReturnsTrue(/*0*/ cond: kotlin.Boolean): kotlin.Boolean + Returns(TRUE) -> cond + +public fun funWithReturnsTrueAndInvertCondition(/*0*/ cond: kotlin.Boolean): kotlin.Boolean + Returns(TRUE) -> !cond + +public fun funWithReturnsTrueAndInvertTypeCheck(/*0*/ value_1: kotlin.Any?): kotlin.Boolean + Returns(TRUE) -> value_1 !is String + +public fun funWithReturnsTrueAndNotNullCheck(/*0*/ value_1: kotlin.Any?): kotlin.Boolean + Returns(TRUE) -> value_1 != null + +public fun funWithReturnsTrueAndNullCheck(/*0*/ value_1: kotlin.Any?): kotlin.Boolean + Returns(TRUE) -> value_1 == null + +public fun funWithReturnsTrueAndTypeCheck(/*0*/ value_1: kotlin.Any?): kotlin.Boolean + Returns(TRUE) -> value_1 is String + +public inline fun funWithUnknownCallsInPlace(/*0*/ block: () -> kotlin.Unit): kotlin.Unit + CallsInPlace(block, UNKNOWN) + +public fun getAny(): kotlin.Any +public fun getBoolean(/*0*/ arg: kotlin.Any = ...): kotlin.Boolean +public fun getByte(/*0*/ arg: kotlin.Any = ...): kotlin.Byte +public fun getChar(/*0*/ arg: kotlin.Any = ...): kotlin.Char +public fun getDouble(/*0*/ arg: kotlin.Any = ...): kotlin.Double +public fun getFloat(/*0*/ arg: kotlin.Any = ...): kotlin.Float +public fun getInt(/*0*/ arg: kotlin.Any = ...): kotlin.Int +public fun getList(): kotlin.collections.MutableList +public fun getLong(/*0*/ arg: kotlin.Any = ...): kotlin.Long +public fun getNothing(): kotlin.Nothing +public fun getShort(/*0*/ arg: kotlin.Any = ...): kotlin.Short +public fun getString(/*0*/ arg: kotlin.Any = ...): kotlin.String +public fun getUnit(): () -> kotlin.Unit + +public final class _BasicTypesProvider { + public constructor _BasicTypesProvider() + public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean + public final fun getAny(): kotlin.Any + public final fun getBoolean(/*0*/ arg: kotlin.Any = ...): kotlin.Boolean + public final fun getByte(/*0*/ arg: kotlin.Any = ...): kotlin.Byte + public final fun getChar(/*0*/ arg: kotlin.Any = ...): kotlin.Char + public final fun getDouble(/*0*/ arg: kotlin.Any = ...): kotlin.Double + public final fun getFloat(/*0*/ arg: kotlin.Any = ...): kotlin.Float + public final fun getInt(/*0*/ arg: kotlin.Any = ...): kotlin.Int + public final fun getList(): kotlin.collections.MutableList + public final fun getLong(/*0*/ arg: kotlin.Any = ...): kotlin.Long + public final fun getNothing(): kotlin.Nothing + public final fun getShort(/*0*/ arg: kotlin.Any = ...): kotlin.Short + public final fun getString(/*0*/ arg: kotlin.Any = ...): kotlin.String + public final fun getUnit(): () -> kotlin.Unit + public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int + public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String +} + +package contracts { + public fun T?.case_11_1(): kotlin.Boolean + Returns(FALSE) -> != null + + public fun T?.case_11_2(): kotlin.Boolean? + Returns(NULL) -> is String + + public fun T?.case_12(): kotlin.Boolean + Returns(FALSE) -> is String + + public fun T?.case_4(): kotlin.Boolean + Returns(TRUE) -> != null + + public fun T?.case_4_1(): kotlin.Boolean + Returns(FALSE) -> != null + + public fun T?.case_4_2(): kotlin.Boolean? + Returns(NULL) -> is String + +} diff --git a/compiler/tests-spec/testData/diagnostics/notLinked/contracts/declarations/contractBuilder/common/neg/1.kt b/compiler/tests-spec/testData/diagnostics/notLinked/contracts/declarations/contractBuilder/common/neg/1.kt new file mode 100644 index 00000000000..83a9db54dc4 --- /dev/null +++ b/compiler/tests-spec/testData/diagnostics/notLinked/contracts/declarations/contractBuilder/common/neg/1.kt @@ -0,0 +1,44 @@ +// !LANGUAGE: +AllowContractsForCustomFunctions +UseCallsInPlaceEffect +// !DIAGNOSTICS: -INVISIBLE_REFERENCE -INVISIBLE_MEMBER -UNUSED_VARIABLE -UNUSED_PARAMETER -UNREACHABLE_CODE -UNUSED_EXPRESSION +// !USE_EXPERIMENTAL: kotlin.contracts.ExperimentalContracts + +/* + KOTLIN DIAGNOSTICS NOT LINKED SPEC TEST (NEGATIVE) + + SECTION: contracts + CATEGORY: declarations, contractBuilder, common + NUMBER: 1 + DESCRIPTION: Contract isn't first statement. + */ + +import kotlin.contracts.* + +inline fun case_1(block: () -> Unit) { + val value_1 = 1 + contract { } + return block() +} + +inline fun case_2(block: () -> Unit) { + 10 - 1 + contract { + callsInPlace(block, InvocationKind.EXACTLY_ONCE) + } + return block() +} + +inline fun case_3(block: () -> Unit) { + throw Exception() + contract { + callsInPlace(block, InvocationKind.UNKNOWN) + } + return block() +} + +// это в негативные, +inline fun case_4(block: () -> Unit) { + .0009 + return contract { + callsInPlace(block, InvocationKind.EXACTLY_ONCE) + } +} diff --git a/compiler/tests-spec/testData/diagnostics/notLinked/contracts/declarations/contractBuilder/common/neg/1.txt b/compiler/tests-spec/testData/diagnostics/notLinked/contracts/declarations/contractBuilder/common/neg/1.txt new file mode 100644 index 00000000000..af31e695912 --- /dev/null +++ b/compiler/tests-spec/testData/diagnostics/notLinked/contracts/declarations/contractBuilder/common/neg/1.txt @@ -0,0 +1,6 @@ +package + +public inline fun case_1(/*0*/ block: () -> kotlin.Unit): kotlin.Unit +public inline fun case_2(/*0*/ block: () -> kotlin.Unit): kotlin.Unit +public inline fun case_3(/*0*/ block: () -> kotlin.Unit): kotlin.Unit +public inline fun case_4(/*0*/ block: () -> kotlin.Unit): kotlin.Unit diff --git a/compiler/tests-spec/testData/diagnostics/notLinked/contracts/declarations/contractBuilder/common/neg/10.kt b/compiler/tests-spec/testData/diagnostics/notLinked/contracts/declarations/contractBuilder/common/neg/10.kt new file mode 100644 index 00000000000..0e8e332f3f0 --- /dev/null +++ b/compiler/tests-spec/testData/diagnostics/notLinked/contracts/declarations/contractBuilder/common/neg/10.kt @@ -0,0 +1,22 @@ +// !LANGUAGE: +AllowContractsForCustomFunctions +UseCallsInPlaceEffect +// !DIAGNOSTICS: -INVISIBLE_REFERENCE -INVISIBLE_MEMBER +// !USE_EXPERIMENTAL: kotlin.contracts.ExperimentalContracts + +/* + KOTLIN DIAGNOSTICS NOT LINKED SPEC TEST (NEGATIVE) + + SECTION: contracts + CATEGORY: declarations, contractBuilder, common + NUMBER: 10 + DESCRIPTION: Contract with label after 'contract' keyword. + ISSUES: KT-26153 + */ + +import kotlin.contracts.* + +inline fun case_1(block: () -> Unit) { + contract test@ { + callsInPlace(block, InvocationKind.EXACTLY_ONCE) + } + return block() +} diff --git a/compiler/tests-spec/testData/diagnostics/notLinked/contracts/declarations/contractBuilder/common/neg/10.txt b/compiler/tests-spec/testData/diagnostics/notLinked/contracts/declarations/contractBuilder/common/neg/10.txt new file mode 100644 index 00000000000..6510924e4d2 --- /dev/null +++ b/compiler/tests-spec/testData/diagnostics/notLinked/contracts/declarations/contractBuilder/common/neg/10.txt @@ -0,0 +1,3 @@ +package + +public inline fun case_1(/*0*/ block: () -> kotlin.Unit): kotlin.Unit diff --git a/compiler/tests-spec/testData/diagnostics/notLinked/contracts/declarations/contractBuilder/common/neg/11.kt b/compiler/tests-spec/testData/diagnostics/notLinked/contracts/declarations/contractBuilder/common/neg/11.kt new file mode 100644 index 00000000000..4673e158e1b --- /dev/null +++ b/compiler/tests-spec/testData/diagnostics/notLinked/contracts/declarations/contractBuilder/common/neg/11.kt @@ -0,0 +1,29 @@ +// !LANGUAGE: +AllowContractsForCustomFunctions +UseCallsInPlaceEffect +// !DIAGNOSTICS: -INVISIBLE_REFERENCE -INVISIBLE_MEMBER +// !USE_EXPERIMENTAL: kotlin.contracts.ExperimentalContracts + +/* + KOTLIN DIAGNOSTICS NOT LINKED SPEC TEST (NEGATIVE) + + SECTION: contracts + CATEGORY: declarations, contractBuilder, common + NUMBER: 11 + DESCRIPTION: Functions with contracts and external contract builder. + ISSUES: KT-26186 + */ + +import kotlin.contracts.* + +internal inline fun contractBuilder(block: () -> Unit): ContractBuilder.() -> Unit = { + callsInPlace(block, InvocationKind.EXACTLY_ONCE) +} + +internal inline fun case_1(block: () -> Unit) { + contract(contractBuilder(block)) + return block() +} + +internal inline fun case_2(block: () -> Unit) { + contract(builder = contractBuilder(block)) + return block() +} diff --git a/compiler/tests-spec/testData/diagnostics/notLinked/contracts/declarations/contractBuilder/common/neg/11.txt b/compiler/tests-spec/testData/diagnostics/notLinked/contracts/declarations/contractBuilder/common/neg/11.txt new file mode 100644 index 00000000000..20c7cc6c019 --- /dev/null +++ b/compiler/tests-spec/testData/diagnostics/notLinked/contracts/declarations/contractBuilder/common/neg/11.txt @@ -0,0 +1,5 @@ +package + +internal inline fun case_1(/*0*/ block: () -> kotlin.Unit): kotlin.Unit +internal inline fun case_2(/*0*/ block: () -> kotlin.Unit): kotlin.Unit +internal inline fun contractBuilder(/*0*/ block: () -> kotlin.Unit): kotlin.contracts.ContractBuilder.() -> kotlin.Unit diff --git a/compiler/tests-spec/testData/diagnostics/notLinked/contracts/declarations/contractBuilder/common/neg/12.kt b/compiler/tests-spec/testData/diagnostics/notLinked/contracts/declarations/contractBuilder/common/neg/12.kt new file mode 100644 index 00000000000..fa178a38d01 --- /dev/null +++ b/compiler/tests-spec/testData/diagnostics/notLinked/contracts/declarations/contractBuilder/common/neg/12.kt @@ -0,0 +1,36 @@ +// !LANGUAGE: +AllowContractsForCustomFunctions +UseCallsInPlaceEffect +// !DIAGNOSTICS: -INVISIBLE_REFERENCE -INVISIBLE_MEMBER +// !USE_EXPERIMENTAL: kotlin.contracts.ExperimentalContracts + +/* + KOTLIN DIAGNOSTICS NOT LINKED SPEC TEST (NEGATIVE) + + SECTION: contracts + CATEGORY: declarations, contractBuilder, common + NUMBER: 12 + DESCRIPTION: Functions with contracts and external effect builder. + ISSUES: KT-26186 + */ + +import kotlin.contracts.* + +internal inline fun ContractBuilder.callsInPlaceEffectBuilder(block: () -> Unit) = + callsInPlace(block, InvocationKind.EXACTLY_ONCE) + +internal fun ContractBuilder.returnsEffectBuilder(value_1: Int?) = + returns(true) implies (value_1 != null) + +internal inline fun case_1(block: () -> Unit) { + contract(builder = { callsInPlaceEffectBuilder(block) }) + return block() +} + +internal inline fun case_2(block: () -> Unit) { + contract { callsInPlaceEffectBuilder(block) } + return block() +} + +internal inline fun case_3(value_1: Int?, block: () -> Unit) { + contract({ returnsEffectBuilder(value_1); callsInPlaceEffectBuilder(block) }) + return block() +} diff --git a/compiler/tests-spec/testData/diagnostics/notLinked/contracts/declarations/contractBuilder/common/neg/12.txt b/compiler/tests-spec/testData/diagnostics/notLinked/contracts/declarations/contractBuilder/common/neg/12.txt new file mode 100644 index 00000000000..c1b20218243 --- /dev/null +++ b/compiler/tests-spec/testData/diagnostics/notLinked/contracts/declarations/contractBuilder/common/neg/12.txt @@ -0,0 +1,7 @@ +package + +internal inline fun case_1(/*0*/ block: () -> kotlin.Unit): kotlin.Unit +internal inline fun case_2(/*0*/ block: () -> kotlin.Unit): kotlin.Unit +internal inline fun case_3(/*0*/ value_1: kotlin.Int?, /*1*/ block: () -> kotlin.Unit): kotlin.Unit +internal inline fun kotlin.contracts.ContractBuilder.callsInPlaceEffectBuilder(/*0*/ block: () -> kotlin.Unit): kotlin.contracts.CallsInPlace +internal fun kotlin.contracts.ContractBuilder.returnsEffectBuilder(/*0*/ value_1: kotlin.Int?): kotlin.contracts.ConditionalEffect diff --git a/compiler/tests-spec/testData/diagnostics/notLinked/contracts/declarations/contractBuilder/common/neg/2.kt b/compiler/tests-spec/testData/diagnostics/notLinked/contracts/declarations/contractBuilder/common/neg/2.kt new file mode 100644 index 00000000000..e28f05319fb --- /dev/null +++ b/compiler/tests-spec/testData/diagnostics/notLinked/contracts/declarations/contractBuilder/common/neg/2.kt @@ -0,0 +1,34 @@ +// !LANGUAGE: +AllowContractsForCustomFunctions +UseCallsInPlaceEffect +// !DIAGNOSTICS: -INVISIBLE_REFERENCE -INVISIBLE_MEMBER -UNUSED_VARIABLE -UNUSED_PARAMETER -UNREACHABLE_CODE -UNUSED_EXPRESSION +// !USE_EXPERIMENTAL: kotlin.contracts.ExperimentalContracts + +/* + KOTLIN DIAGNOSTICS NOT LINKED SPEC TEST (NEGATIVE) + + SECTION: contracts + CATEGORY: declarations, contractBuilder, common + NUMBER: 2 + DESCRIPTION: contracts with not allowed simple conditions in implies + */ + +import kotlin.contracts.* + +fun case_1(value_1: Boolean): Boolean { + contract { returns(true) implies (value_1 == true) } + return value_1 == true +} + +fun case_2(value_1: Boolean): Boolean? { + contract { returnsNotNull() implies (value_1 != false) } + return if (value_1 != false) true else null +} + +fun case_3(value_1: String): Boolean { + contract { returns(false) implies (value_1 != "") } + return !(value_1 != "") +} + +fun case_4(value_1: Int): Boolean? { + contract { returns(null) implies (value_1 == 0) } + return if (value_1 == 0) null else true +} diff --git a/compiler/tests-spec/testData/diagnostics/notLinked/contracts/declarations/contractBuilder/common/neg/2.txt b/compiler/tests-spec/testData/diagnostics/notLinked/contracts/declarations/contractBuilder/common/neg/2.txt new file mode 100644 index 00000000000..6c4419e1c4d --- /dev/null +++ b/compiler/tests-spec/testData/diagnostics/notLinked/contracts/declarations/contractBuilder/common/neg/2.txt @@ -0,0 +1,6 @@ +package + +public fun case_1(/*0*/ value_1: kotlin.Boolean): kotlin.Boolean +public fun case_2(/*0*/ value_1: kotlin.Boolean): kotlin.Boolean? +public fun case_3(/*0*/ value_1: kotlin.String): kotlin.Boolean +public fun case_4(/*0*/ value_1: kotlin.Int): kotlin.Boolean? diff --git a/compiler/tests-spec/testData/diagnostics/notLinked/contracts/declarations/contractBuilder/common/neg/3.kt b/compiler/tests-spec/testData/diagnostics/notLinked/contracts/declarations/contractBuilder/common/neg/3.kt new file mode 100644 index 00000000000..1423b2c658f --- /dev/null +++ b/compiler/tests-spec/testData/diagnostics/notLinked/contracts/declarations/contractBuilder/common/neg/3.kt @@ -0,0 +1,44 @@ +// !LANGUAGE: +AllowContractsForCustomFunctions +UseCallsInPlaceEffect +// !DIAGNOSTICS: -INVISIBLE_REFERENCE -INVISIBLE_MEMBER -UNUSED_VARIABLE -UNUSED_PARAMETER -UNREACHABLE_CODE -UNUSED_EXPRESSION +// !USE_EXPERIMENTAL: kotlin.contracts.ExperimentalContracts + +/* + KOTLIN DIAGNOSTICS NOT LINKED SPEC TEST (NEGATIVE) + + SECTION: contracts + CATEGORY: declarations, contractBuilder, common + NUMBER: 3 + DESCRIPTION: contracts with not allowed complex conditions in implies. + */ + +import kotlin.contracts.* + +fun case_1(value_1: Boolean?): Boolean { + contract { returns(true) implies (value_1 != null && value_1 == false) } + return value_1 != null && value_1 == false +} + +fun case_2(value_1: Boolean, value_2: Boolean): Boolean? { + contract { returnsNotNull() implies (value_1 != false || value_2) } + return if (value_1 != false || value_2) true else null +} + +fun case_3(value_1: String?, value_2: Boolean): Boolean { + contract { returns(false) implies (value_1 != null && value_2 != true) } + return !(value_1 != null && value_2 != true) +} + +fun case_4(value_1: Nothing?, value_2: Boolean?): Boolean? { + contract { returns(null) implies (value_1 == null || value_2 != null || value_2 == false) } + return if (value_1 == null || value_2 != null || value_2 == false) null else true +} + +fun case_5(value_1: Any?, value_2: String?): Boolean? { + contract { returns(null) implies (value_1 != null && value_2 != null || value_2 == ".") } + return if (value_1 != null && value_2 != null || value_2 == ".") null else true +} + +fun case_6(value_1: Boolean, value_2: Int?): Boolean? { + contract { returns(null) implies (value_2 == null && value_1 || value_2 == 0) } + return if (value_2 == null && value_1 || value_2 == 0) null else true +} diff --git a/compiler/tests-spec/testData/diagnostics/notLinked/contracts/declarations/contractBuilder/common/neg/3.txt b/compiler/tests-spec/testData/diagnostics/notLinked/contracts/declarations/contractBuilder/common/neg/3.txt new file mode 100644 index 00000000000..cc5789d3935 --- /dev/null +++ b/compiler/tests-spec/testData/diagnostics/notLinked/contracts/declarations/contractBuilder/common/neg/3.txt @@ -0,0 +1,8 @@ +package + +public fun case_1(/*0*/ value_1: kotlin.Boolean?): kotlin.Boolean +public fun case_2(/*0*/ value_1: kotlin.Boolean, /*1*/ value_2: kotlin.Boolean): kotlin.Boolean? +public fun case_3(/*0*/ value_1: kotlin.String?, /*1*/ value_2: kotlin.Boolean): kotlin.Boolean +public fun case_4(/*0*/ value_1: kotlin.Nothing?, /*1*/ value_2: kotlin.Boolean?): kotlin.Boolean? +public fun case_5(/*0*/ value_1: kotlin.Any?, /*1*/ value_2: kotlin.String?): kotlin.Boolean? +public fun case_6(/*0*/ value_1: kotlin.Boolean, /*1*/ value_2: kotlin.Int?): kotlin.Boolean? diff --git a/compiler/tests-spec/testData/diagnostics/notLinked/contracts/declarations/contractBuilder/common/neg/4.kt b/compiler/tests-spec/testData/diagnostics/notLinked/contracts/declarations/contractBuilder/common/neg/4.kt new file mode 100644 index 00000000000..fa3db6793f6 --- /dev/null +++ b/compiler/tests-spec/testData/diagnostics/notLinked/contracts/declarations/contractBuilder/common/neg/4.kt @@ -0,0 +1,29 @@ +// !LANGUAGE: +AllowContractsForCustomFunctions +UseCallsInPlaceEffect +// !DIAGNOSTICS: -INVISIBLE_REFERENCE -INVISIBLE_MEMBER -UNUSED_VARIABLE -UNUSED_PARAMETER -UNREACHABLE_CODE -UNUSED_EXPRESSION +// !USE_EXPERIMENTAL: kotlin.contracts.ExperimentalContracts + +/* + KOTLIN DIAGNOSTICS NOT LINKED SPEC TEST (NEGATIVE) + + SECTION: contracts + CATEGORY: declarations, contractBuilder, common + NUMBER: 4 + DESCRIPTION: contracts with not allowed conditions with constants in implies. + */ + +import kotlin.contracts.* + +fun case_1(): Boolean? { + contract { returnsNotNull() implies (null) } + return true +} + +fun case_2(): Boolean { + contract { returns(false) implies 0.000001 } + return true +} + +fun case_3(): Boolean? { + contract { returns(null) implies "" } + return null +} diff --git a/compiler/tests-spec/testData/diagnostics/notLinked/contracts/declarations/contractBuilder/common/neg/4.txt b/compiler/tests-spec/testData/diagnostics/notLinked/contracts/declarations/contractBuilder/common/neg/4.txt new file mode 100644 index 00000000000..615bb9556d2 --- /dev/null +++ b/compiler/tests-spec/testData/diagnostics/notLinked/contracts/declarations/contractBuilder/common/neg/4.txt @@ -0,0 +1,5 @@ +package + +public fun case_1(): kotlin.Boolean? +public fun case_2(): kotlin.Boolean +public fun case_3(): kotlin.Boolean? diff --git a/compiler/tests-spec/testData/diagnostics/notLinked/contracts/declarations/contractBuilder/common/neg/5.kt b/compiler/tests-spec/testData/diagnostics/notLinked/contracts/declarations/contractBuilder/common/neg/5.kt new file mode 100644 index 00000000000..2f920135435 --- /dev/null +++ b/compiler/tests-spec/testData/diagnostics/notLinked/contracts/declarations/contractBuilder/common/neg/5.kt @@ -0,0 +1,57 @@ +// !LANGUAGE: +AllowContractsForCustomFunctions +UseCallsInPlaceEffect +// !DIAGNOSTICS: -INVISIBLE_REFERENCE -INVISIBLE_MEMBER -UNUSED_VARIABLE -UNUSED_PARAMETER -UNREACHABLE_CODE -UNUSED_EXPRESSION +// !USE_EXPERIMENTAL: kotlin.contracts.ExperimentalContracts + +/* + KOTLIN DIAGNOSTICS NOT LINKED SPEC TEST (NEGATIVE) + + SECTION: contracts + CATEGORY: declarations, contractBuilder, common + NUMBER: 5 + DESCRIPTION: contracts with not allowed expressions in implies. + */ + +import kotlin.contracts.* + +fun case_1(): Boolean { + contract { returns(true) implies (-10) } + return true +} + +fun case_2(): Boolean { + contract { returnsNotNull() implies (return return return) } + return true +} + +fun case_3(): Boolean { + contract { returns(false) implies ("..." + "$value_1") } + return true +} + +/* + UNEXPECTED BEHAVIOUR + ISSUES: KT-25948 + */ +fun case_4(): Boolean { + contract { returns(null) implies throw Exception() } + return true +} + +/* + UNEXPECTED BEHAVIOUR + ISSUES: KT-26386 + */ +fun case_5(): Boolean? { + contract { returns(null) implies case_5() } + return null +} + +fun case_6(): Boolean? { + contract { returns(null) implies listOf(0) } + return null +} + +fun case_7(value_1: Boolean): Boolean? { + contract { returns(null) implies contract { returns(null) implies (!value_1) } } + return null +} diff --git a/compiler/tests-spec/testData/diagnostics/notLinked/contracts/declarations/contractBuilder/common/neg/5.txt b/compiler/tests-spec/testData/diagnostics/notLinked/contracts/declarations/contractBuilder/common/neg/5.txt new file mode 100644 index 00000000000..3dc2b09c64b --- /dev/null +++ b/compiler/tests-spec/testData/diagnostics/notLinked/contracts/declarations/contractBuilder/common/neg/5.txt @@ -0,0 +1,8 @@ +package + +public fun case_1(): kotlin.Boolean +public fun case_2(): kotlin.Boolean +public fun case_3(): kotlin.Boolean +public fun case_4(): kotlin.Boolean +public fun case_6(): kotlin.Boolean? +public fun case_7(/*0*/ value_1: kotlin.Boolean): kotlin.Boolean? diff --git a/compiler/tests-spec/testData/diagnostics/notLinked/contracts/declarations/contractBuilder/common/neg/6.kt b/compiler/tests-spec/testData/diagnostics/notLinked/contracts/declarations/contractBuilder/common/neg/6.kt new file mode 100644 index 00000000000..459a2006d52 --- /dev/null +++ b/compiler/tests-spec/testData/diagnostics/notLinked/contracts/declarations/contractBuilder/common/neg/6.kt @@ -0,0 +1,72 @@ +// !LANGUAGE: +AllowContractsForCustomFunctions +UseCallsInPlaceEffect +// !DIAGNOSTICS: -INVISIBLE_REFERENCE -INVISIBLE_MEMBER -UNUSED_VARIABLE -UNUSED_PARAMETER -UNREACHABLE_CODE -UNUSED_EXPRESSION +// !USE_EXPERIMENTAL: kotlin.contracts.ExperimentalContracts +// !WITH_BASIC_TYPES + +/* + KOTLIN DIAGNOSTICS NOT LINKED SPEC TEST (NEGATIVE) + + SECTION: contracts + CATEGORY: declarations, contractBuilder, common + NUMBER: 6 + DESCRIPTION: contracts with not function parameters in implies. + */ + +import kotlin.contracts.* + +object case_1 { + val value_1 = getBoolean() + const val value_2 = true + private const val value_3 = false + + fun case_1_1(): Boolean? { + contract { returnsNotNull() implies (value_1) } + return if (value_1) true else null + } + + fun case_1_2(): Boolean? { + contract { returns(null) implies (value_2) } + return if (value_2) null else true + } + + fun case_1_3(): Boolean { + contract { returns(true) implies (value_3) } + return value_3 + } +} + +class case_2(value_5: Boolean, val value_1: Boolean) { + val value_2 = getBoolean() + + companion object { + const val value_3 = false + private const val value_4 = true + } + + init { + fun case_2_1(): Boolean { + contract { returns(false) implies (value_5) } + return !(value_5) + } + } + + fun case_2_2(): Boolean? { + contract { returns(null) implies (value_1) } + return if (value_1) null else true + } + + fun case_2_3(): Boolean { + contract { returns(true) implies (value_2) } + return value_2 + } + + fun case_2_4(): Boolean { + contract { returns(false) implies (value_3) } + return !(value_3) + } + + inline fun K.case_2_5(): Boolean? { + contract { returnsNotNull() implies (value_4) } + return if (value_4) true else null + } +} diff --git a/compiler/tests-spec/testData/diagnostics/notLinked/contracts/declarations/contractBuilder/common/neg/6.txt b/compiler/tests-spec/testData/diagnostics/notLinked/contracts/declarations/contractBuilder/common/neg/6.txt new file mode 100644 index 00000000000..f0818fcf962 --- /dev/null +++ b/compiler/tests-spec/testData/diagnostics/notLinked/contracts/declarations/contractBuilder/common/neg/6.txt @@ -0,0 +1,70 @@ +package + +public fun getAny(): kotlin.Any +public fun getBoolean(/*0*/ arg: kotlin.Any = ...): kotlin.Boolean +public fun getByte(/*0*/ arg: kotlin.Any = ...): kotlin.Byte +public fun getChar(/*0*/ arg: kotlin.Any = ...): kotlin.Char +public fun getDouble(/*0*/ arg: kotlin.Any = ...): kotlin.Double +public fun getFloat(/*0*/ arg: kotlin.Any = ...): kotlin.Float +public fun getInt(/*0*/ arg: kotlin.Any = ...): kotlin.Int +public fun getList(): kotlin.collections.MutableList +public fun getLong(/*0*/ arg: kotlin.Any = ...): kotlin.Long +public fun getNothing(): kotlin.Nothing +public fun getShort(/*0*/ arg: kotlin.Any = ...): kotlin.Short +public fun getString(/*0*/ arg: kotlin.Any = ...): kotlin.String +public fun getUnit(): () -> kotlin.Unit + +public final class _BasicTypesProvider { + public constructor _BasicTypesProvider() + public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean + public final fun getAny(): kotlin.Any + public final fun getBoolean(/*0*/ arg: kotlin.Any = ...): kotlin.Boolean + public final fun getByte(/*0*/ arg: kotlin.Any = ...): kotlin.Byte + public final fun getChar(/*0*/ arg: kotlin.Any = ...): kotlin.Char + public final fun getDouble(/*0*/ arg: kotlin.Any = ...): kotlin.Double + public final fun getFloat(/*0*/ arg: kotlin.Any = ...): kotlin.Float + public final fun getInt(/*0*/ arg: kotlin.Any = ...): kotlin.Int + public final fun getList(): kotlin.collections.MutableList + public final fun getLong(/*0*/ arg: kotlin.Any = ...): kotlin.Long + public final fun getNothing(): kotlin.Nothing + public final fun getShort(/*0*/ arg: kotlin.Any = ...): kotlin.Short + public final fun getString(/*0*/ arg: kotlin.Any = ...): kotlin.String + public final fun getUnit(): () -> kotlin.Unit + public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int + public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String +} + +public object case_1 { + private constructor case_1() + public final val value_1: kotlin.Boolean + public const final val value_2: kotlin.Boolean = true + private const final val value_3: kotlin.Boolean = false + public final fun case_1_1(): kotlin.Boolean? + public final fun case_1_2(): kotlin.Boolean? + public final fun case_1_3(): kotlin.Boolean + 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_2 { + public constructor case_2(/*0*/ value_5: kotlin.Boolean, /*1*/ value_1: kotlin.Boolean) + public final val value_1: kotlin.Boolean + public final val value_2: kotlin.Boolean + public final fun case_2_2(): kotlin.Boolean? + public final fun case_2_3(): kotlin.Boolean + public final fun case_2_4(): kotlin.Boolean + 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 inline fun K.case_2_5(): kotlin.Boolean? + + public companion object Companion { + private constructor Companion() + public const final val value_3: kotlin.Boolean = false + private const final val value_4: kotlin.Boolean = true + 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 + } +} diff --git a/compiler/tests-spec/testData/diagnostics/notLinked/contracts/declarations/contractBuilder/common/neg/7.kt b/compiler/tests-spec/testData/diagnostics/notLinked/contracts/declarations/contractBuilder/common/neg/7.kt new file mode 100644 index 00000000000..cd312ad2a5a --- /dev/null +++ b/compiler/tests-spec/testData/diagnostics/notLinked/contracts/declarations/contractBuilder/common/neg/7.kt @@ -0,0 +1,23 @@ +// !LANGUAGE: +AllowContractsForCustomFunctions +UseCallsInPlaceEffect +// !DIAGNOSTICS: -INVISIBLE_REFERENCE -INVISIBLE_MEMBER -UNUSED_VARIABLE -UNUSED_PARAMETER -UNREACHABLE_CODE -UNUSED_EXPRESSION +// !USE_EXPERIMENTAL: kotlin.contracts.ExperimentalContracts +// !WITH_BASIC_TYPES + +/* + KOTLIN DIAGNOSTICS NOT LINKED SPEC TEST (NEGATIVE) + + SECTION: contracts + CATEGORY: declarations, contractBuilder, common + NUMBER: 7 + DESCRIPTION: Contract function with 'this' labeled by not current extensible object + ISSUES: KT-26149 + */ + +import kotlin.contracts.* + +fun T?.case_3() { + fun K?.case_3_1(): Boolean { + contract { returns(true) implies (this@case_3 != null) } + return this@case_3 != null + } +} diff --git a/compiler/tests-spec/testData/diagnostics/notLinked/contracts/declarations/contractBuilder/common/neg/7.txt b/compiler/tests-spec/testData/diagnostics/notLinked/contracts/declarations/contractBuilder/common/neg/7.txt new file mode 100644 index 00000000000..a4a7449bc0f --- /dev/null +++ b/compiler/tests-spec/testData/diagnostics/notLinked/contracts/declarations/contractBuilder/common/neg/7.txt @@ -0,0 +1,36 @@ +package + +public fun getAny(): kotlin.Any +public fun getBoolean(/*0*/ arg: kotlin.Any = ...): kotlin.Boolean +public fun getByte(/*0*/ arg: kotlin.Any = ...): kotlin.Byte +public fun getChar(/*0*/ arg: kotlin.Any = ...): kotlin.Char +public fun getDouble(/*0*/ arg: kotlin.Any = ...): kotlin.Double +public fun getFloat(/*0*/ arg: kotlin.Any = ...): kotlin.Float +public fun getInt(/*0*/ arg: kotlin.Any = ...): kotlin.Int +public fun getList(): kotlin.collections.MutableList +public fun getLong(/*0*/ arg: kotlin.Any = ...): kotlin.Long +public fun getNothing(): kotlin.Nothing +public fun getShort(/*0*/ arg: kotlin.Any = ...): kotlin.Short +public fun getString(/*0*/ arg: kotlin.Any = ...): kotlin.String +public fun getUnit(): () -> kotlin.Unit +public fun T?.case_3(): kotlin.Unit + +public final class _BasicTypesProvider { + public constructor _BasicTypesProvider() + public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean + public final fun getAny(): kotlin.Any + public final fun getBoolean(/*0*/ arg: kotlin.Any = ...): kotlin.Boolean + public final fun getByte(/*0*/ arg: kotlin.Any = ...): kotlin.Byte + public final fun getChar(/*0*/ arg: kotlin.Any = ...): kotlin.Char + public final fun getDouble(/*0*/ arg: kotlin.Any = ...): kotlin.Double + public final fun getFloat(/*0*/ arg: kotlin.Any = ...): kotlin.Float + public final fun getInt(/*0*/ arg: kotlin.Any = ...): kotlin.Int + public final fun getList(): kotlin.collections.MutableList + public final fun getLong(/*0*/ arg: kotlin.Any = ...): kotlin.Long + public final fun getNothing(): kotlin.Nothing + public final fun getShort(/*0*/ arg: kotlin.Any = ...): kotlin.Short + public final fun getString(/*0*/ arg: kotlin.Any = ...): kotlin.String + public final fun getUnit(): () -> kotlin.Unit + public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int + public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String +} diff --git a/compiler/tests-spec/testData/diagnostics/notLinked/contracts/declarations/contractBuilder/common/neg/8.kt b/compiler/tests-spec/testData/diagnostics/notLinked/contracts/declarations/contractBuilder/common/neg/8.kt new file mode 100644 index 00000000000..8102a5d827e --- /dev/null +++ b/compiler/tests-spec/testData/diagnostics/notLinked/contracts/declarations/contractBuilder/common/neg/8.kt @@ -0,0 +1,29 @@ +// !LANGUAGE: +AllowContractsForCustomFunctions +UseCallsInPlaceEffect +// !DIAGNOSTICS: -INVISIBLE_REFERENCE -INVISIBLE_MEMBER -UNUSED_VARIABLE -UNUSED_PARAMETER -UNREACHABLE_CODE -UNUSED_EXPRESSION +// !USE_EXPERIMENTAL: kotlin.contracts.ExperimentalContracts + +/* + KOTLIN DIAGNOSTICS NOT LINKED SPEC TEST (NEGATIVE) + + SECTION: contracts + CATEGORY: declarations, contractBuilder, common + NUMBER: 8 + DESCRIPTION: Not allowed empty contract. + */ + +import kotlin.contracts.* + +inline fun case_1(block: () -> Unit) { + contract { } + return block() +} + +inline fun case_2(block: () -> Unit) { + contract({ }) + return block() +} + +inline fun case_3(block: () -> Unit) { + contract(builder = { }) + return block() +} \ No newline at end of file diff --git a/compiler/tests-spec/testData/diagnostics/notLinked/contracts/declarations/contractBuilder/common/neg/8.txt b/compiler/tests-spec/testData/diagnostics/notLinked/contracts/declarations/contractBuilder/common/neg/8.txt new file mode 100644 index 00000000000..29a4868f0b9 --- /dev/null +++ b/compiler/tests-spec/testData/diagnostics/notLinked/contracts/declarations/contractBuilder/common/neg/8.txt @@ -0,0 +1,5 @@ +package + +public inline fun case_1(/*0*/ block: () -> kotlin.Unit): kotlin.Unit +public inline fun case_2(/*0*/ block: () -> kotlin.Unit): kotlin.Unit +public inline fun case_3(/*0*/ block: () -> kotlin.Unit): kotlin.Unit diff --git a/compiler/tests-spec/testData/diagnostics/notLinked/contracts/declarations/contractBuilder/common/neg/9.kt b/compiler/tests-spec/testData/diagnostics/notLinked/contracts/declarations/contractBuilder/common/neg/9.kt new file mode 100644 index 00000000000..2828239250c --- /dev/null +++ b/compiler/tests-spec/testData/diagnostics/notLinked/contracts/declarations/contractBuilder/common/neg/9.kt @@ -0,0 +1,20 @@ +// !LANGUAGE: +AllowContractsForCustomFunctions +UseCallsInPlaceEffect +// !DIAGNOSTICS: -INVISIBLE_REFERENCE -INVISIBLE_MEMBER -UNUSED_VARIABLE -UNUSED_PARAMETER -UNREACHABLE_CODE -UNUSED_EXPRESSION +// !USE_EXPERIMENTAL: kotlin.contracts.ExperimentalContracts + +/* + KOTLIN DIAGNOSTICS NOT LINKED SPEC TEST (NEGATIVE) + + SECTION: contracts + CATEGORY: declarations, contractBuilder, common + NUMBER: 9 + DESCRIPTION: Function with contract as returned expression. + */ + +import kotlin.contracts.* + +inline fun case_1(block: () -> Unit) = { + contract { + callsInPlace(block, InvocationKind.EXACTLY_ONCE) + } +} \ No newline at end of file diff --git a/compiler/tests-spec/testData/diagnostics/notLinked/contracts/declarations/contractBuilder/common/neg/9.txt b/compiler/tests-spec/testData/diagnostics/notLinked/contracts/declarations/contractBuilder/common/neg/9.txt new file mode 100644 index 00000000000..23cae209abd --- /dev/null +++ b/compiler/tests-spec/testData/diagnostics/notLinked/contracts/declarations/contractBuilder/common/neg/9.txt @@ -0,0 +1,3 @@ +package + +public inline fun case_1(/*0*/ block: () -> kotlin.Unit): () -> kotlin.Unit diff --git a/compiler/tests-spec/testData/diagnostics/notLinked/contracts/declarations/contractBuilder/common/pos/1.kt b/compiler/tests-spec/testData/diagnostics/notLinked/contracts/declarations/contractBuilder/common/pos/1.kt new file mode 100644 index 00000000000..5a5c4ecd094 --- /dev/null +++ b/compiler/tests-spec/testData/diagnostics/notLinked/contracts/declarations/contractBuilder/common/pos/1.kt @@ -0,0 +1,39 @@ +// !LANGUAGE: +AllowContractsForCustomFunctions +UseCallsInPlaceEffect +// !DIAGNOSTICS: -INVISIBLE_REFERENCE -INVISIBLE_MEMBER +// !USE_EXPERIMENTAL: kotlin.contracts.ExperimentalContracts + +/* + KOTLIN DIAGNOSTICS NOT LINKED SPEC TEST (POSITIVE) + + SECTION: contracts + CATEGORY: declarations, contractBuilder, common + NUMBER: 1 + DESCRIPTION: Functions with simple contracts. + */ + +import kotlin.contracts.* + +inline fun case_1(block: () -> Unit) { + contract { callsInPlace(block, InvocationKind.EXACTLY_ONCE) } + return block() +} + +inline fun case_2(value_1: Int?, block: () -> Unit): Boolean { + contract { + callsInPlace(block, InvocationKind.EXACTLY_ONCE) + returns(true) implies (value_1 != null) + } + block() + return value_1 != null +} + +inline fun T?.case_3(value_1: Int?, value_2: Boolean, value_3: Int?, block: () -> Unit): Boolean? { + contract { + callsInPlace(block, InvocationKind.EXACTLY_ONCE) + returns(true) implies (value_1 != null) + returns(false) implies (!value_2) + returnsNotNull() implies (this@case_3 != null && value_3 != null) + } + block() + return value_1 != null +} diff --git a/compiler/tests-spec/testData/diagnostics/notLinked/contracts/declarations/contractBuilder/common/pos/1.txt b/compiler/tests-spec/testData/diagnostics/notLinked/contracts/declarations/contractBuilder/common/pos/1.txt new file mode 100644 index 00000000000..d78c6f540f4 --- /dev/null +++ b/compiler/tests-spec/testData/diagnostics/notLinked/contracts/declarations/contractBuilder/common/pos/1.txt @@ -0,0 +1,15 @@ +package + +public inline fun case_1(/*0*/ block: () -> kotlin.Unit): kotlin.Unit + CallsInPlace(block, EXACTLY_ONCE) + +public inline fun case_2(/*0*/ value_1: kotlin.Int?, /*1*/ block: () -> kotlin.Unit): kotlin.Boolean + CallsInPlace(block, EXACTLY_ONCE) + Returns(TRUE) -> value_1 != null + +public inline fun T?.case_3(/*0*/ value_1: kotlin.Int?, /*1*/ value_2: kotlin.Boolean, /*2*/ value_3: kotlin.Int?, /*3*/ block: () -> kotlin.Unit): kotlin.Boolean? + CallsInPlace(block, EXACTLY_ONCE) + Returns(TRUE) -> value_1 != null + Returns(FALSE) -> !value_2 + Returns(NOT_NULL) -> != null && value_3 != null + diff --git a/compiler/tests-spec/testData/diagnostics/notLinked/contracts/declarations/contractBuilder/common/pos/2.kt b/compiler/tests-spec/testData/diagnostics/notLinked/contracts/declarations/contractBuilder/common/pos/2.kt new file mode 100644 index 00000000000..4624ad2677c --- /dev/null +++ b/compiler/tests-spec/testData/diagnostics/notLinked/contracts/declarations/contractBuilder/common/pos/2.kt @@ -0,0 +1,58 @@ +// !LANGUAGE: +AllowContractsForCustomFunctions +UseCallsInPlaceEffect +// !DIAGNOSTICS: -INVISIBLE_REFERENCE -INVISIBLE_MEMBER -UNUSED_VARIABLE +// !USE_EXPERIMENTAL: kotlin.contracts.ExperimentalContracts +// !WITH_FUNCTIONS + +/* + KOTLIN DIAGNOSTICS NOT LINKED SPEC TEST (POSITIVE) + + SECTION: contracts + CATEGORY: declarations, contractBuilder, common + NUMBER: 2 + DESCRIPTION: Contract is first statement in control flow terms, but not in tokens order terms. + UNEXPECTED BEHAVIOUR + ISSUES: KT-26153 + */ + +import kotlin.contracts.* + +internal inline fun case_1(block: () -> Unit) { + return contract { + callsInPlace(block, InvocationKind.EXACTLY_ONCE) + } +} + +fun case_2() = contract { } + +inline fun case_3(block: () -> Unit) { + val value_1 = contract { + callsInPlace(block, InvocationKind.EXACTLY_ONCE) + } + block() +} + +inline fun case_4(block: () -> Unit) { + (contract { + callsInPlace(block, InvocationKind.EXACTLY_ONCE) + }) + return block() +} + +inline fun case_5(block: () -> Unit) { + test@ contract { + callsInPlace(block, InvocationKind.EXACTLY_ONCE) + } + return block() +} + +inline fun case_6(block: () -> Unit) { + throw Exception(contract { + callsInPlace(block, InvocationKind.EXACTLY_ONCE) + }.toString()) +} + +inline fun case_7(block: () -> Unit) { + _funWithAnyArg(contract { + callsInPlace(block, InvocationKind.EXACTLY_ONCE) + }) +} diff --git a/compiler/tests-spec/testData/diagnostics/notLinked/contracts/declarations/contractBuilder/common/pos/2.txt b/compiler/tests-spec/testData/diagnostics/notLinked/contracts/declarations/contractBuilder/common/pos/2.txt new file mode 100644 index 00000000000..cfffa64eac8 --- /dev/null +++ b/compiler/tests-spec/testData/diagnostics/notLinked/contracts/declarations/contractBuilder/common/pos/2.txt @@ -0,0 +1,11 @@ +package + +public fun _funWithAnyArg(/*0*/ value_1: kotlin.Any): kotlin.Int +public fun _funWithoutArgs(): kotlin.Int +internal inline fun case_1(/*0*/ block: () -> kotlin.Unit): kotlin.Unit +public fun case_2(): kotlin.Unit +public inline fun case_3(/*0*/ block: () -> kotlin.Unit): kotlin.Unit +public inline fun case_4(/*0*/ block: () -> kotlin.Unit): kotlin.Unit +public inline fun case_5(/*0*/ block: () -> kotlin.Unit): kotlin.Unit +public inline fun case_6(/*0*/ block: () -> kotlin.Unit): kotlin.Unit +public inline fun case_7(/*0*/ block: () -> kotlin.Unit): kotlin.Unit diff --git a/compiler/tests-spec/testData/diagnostics/notLinked/contracts/declarations/contractBuilder/common/pos/3.kt b/compiler/tests-spec/testData/diagnostics/notLinked/contracts/declarations/contractBuilder/common/pos/3.kt new file mode 100644 index 00000000000..bd9e822f4e2 --- /dev/null +++ b/compiler/tests-spec/testData/diagnostics/notLinked/contracts/declarations/contractBuilder/common/pos/3.kt @@ -0,0 +1,24 @@ +// !LANGUAGE: +AllowContractsForCustomFunctions +UseCallsInPlaceEffect +// !DIAGNOSTICS: -INVISIBLE_REFERENCE -INVISIBLE_MEMBER +// !USE_EXPERIMENTAL: kotlin.contracts.ExperimentalContracts + +/* + KOTLIN DIAGNOSTICS NOT LINKED SPEC TEST (POSITIVE) + + SECTION: contracts + CATEGORY: declarations, contractBuilder, common + NUMBER: 3 + DESCRIPTION: Functions with contract and builder lambda in parentheses. + */ + +import kotlin.contracts.* + +inline fun case_1(block: () -> Unit) { + contract({ callsInPlace(block, InvocationKind.EXACTLY_ONCE) }) + return block() +} + +inline fun case_2(block: () -> Unit) { + contract(builder = { callsInPlace(block, InvocationKind.EXACTLY_ONCE) }) + return block() +} diff --git a/compiler/tests-spec/testData/diagnostics/notLinked/contracts/declarations/contractBuilder/common/pos/3.txt b/compiler/tests-spec/testData/diagnostics/notLinked/contracts/declarations/contractBuilder/common/pos/3.txt new file mode 100644 index 00000000000..75f54d017b5 --- /dev/null +++ b/compiler/tests-spec/testData/diagnostics/notLinked/contracts/declarations/contractBuilder/common/pos/3.txt @@ -0,0 +1,8 @@ +package + +public inline fun case_1(/*0*/ block: () -> kotlin.Unit): kotlin.Unit + CallsInPlace(block, EXACTLY_ONCE) + +public inline fun case_2(/*0*/ block: () -> kotlin.Unit): kotlin.Unit + CallsInPlace(block, EXACTLY_ONCE) + diff --git a/compiler/tests-spec/testData/diagnostics/notLinked/contracts/declarations/contractBuilder/common/pos/4.kt b/compiler/tests-spec/testData/diagnostics/notLinked/contracts/declarations/contractBuilder/common/pos/4.kt new file mode 100644 index 00000000000..bce396a8ba8 --- /dev/null +++ b/compiler/tests-spec/testData/diagnostics/notLinked/contracts/declarations/contractBuilder/common/pos/4.kt @@ -0,0 +1,54 @@ +// !LANGUAGE: +AllowContractsForCustomFunctions +UseCallsInPlaceEffect +// !DIAGNOSTICS: -INVISIBLE_REFERENCE -INVISIBLE_MEMBER -UNUSED_VARIABLE +// !USE_EXPERIMENTAL: kotlin.contracts.ExperimentalContracts + +/* + KOTLIN DIAGNOSTICS NOT LINKED SPEC TEST (POSITIVE) + + SECTION: contracts + CATEGORY: declarations, contractBuilder, common + NUMBER: 4 + DESCRIPTION: Contract isn't in the first position. + UNEXPECTED BEHAVIOUR + ISSUES: KT-26191 + */ + +import kotlin.contracts.* + +fun case_1(value_1: Int?) { + println("!") + contract { + returns(true) implies (value_1 != null) + } as ContractBuilder +} + +fun case_2(value_1: Int?) { + 100 + 10 + throw Exception(contract { + returns(true) implies (value_1 != null) + }.toString()) +} + +fun case_3(value_1: Int?) { + for (i in 0..10) { + println(i) + } + return contract { + returns(true) implies (value_1 != null) + } +} + +fun case_4(value_1: Int?) { + val f = 10 - 20 + val g = contract { + returns(true) implies (value_1 != null) + } +} + +fun case_5(number: Int?): Boolean { + val value_1 = number != null + contract { + returns(false) implies (value_1) + } as ContractBuilder + return number == null +} diff --git a/compiler/tests-spec/testData/diagnostics/notLinked/contracts/declarations/contractBuilder/common/pos/4.txt b/compiler/tests-spec/testData/diagnostics/notLinked/contracts/declarations/contractBuilder/common/pos/4.txt new file mode 100644 index 00000000000..49234c3e061 --- /dev/null +++ b/compiler/tests-spec/testData/diagnostics/notLinked/contracts/declarations/contractBuilder/common/pos/4.txt @@ -0,0 +1,7 @@ +package + +public fun case_1(/*0*/ value_1: kotlin.Int?): kotlin.Unit +public fun case_2(/*0*/ value_1: kotlin.Int?): kotlin.Unit +public fun case_3(/*0*/ value_1: kotlin.Int?): kotlin.Unit +public fun case_4(/*0*/ value_1: kotlin.Int?): kotlin.Unit +public fun case_5(/*0*/ number: kotlin.Int?): kotlin.Boolean diff --git a/compiler/tests-spec/testData/diagnostics/notLinked/contracts/declarations/contractBuilder/common/pos/5.kt b/compiler/tests-spec/testData/diagnostics/notLinked/contracts/declarations/contractBuilder/common/pos/5.kt new file mode 100644 index 00000000000..ca6c46eec14 --- /dev/null +++ b/compiler/tests-spec/testData/diagnostics/notLinked/contracts/declarations/contractBuilder/common/pos/5.kt @@ -0,0 +1,21 @@ +// !LANGUAGE: +AllowContractsForCustomFunctions +UseCallsInPlaceEffect +// !DIAGNOSTICS: -INVISIBLE_REFERENCE -INVISIBLE_MEMBER -UNUSED_VARIABLE -UNUSED_PARAMETER -UNREACHABLE_CODE -UNUSED_EXPRESSION +// !USE_EXPERIMENTAL: kotlin.contracts.ExperimentalContracts + +/* + KOTLIN DIAGNOSTICS NOT LINKED SPEC TEST (POSITIVE) + + SECTION: contracts + CATEGORY: declarations, contractBuilder, common + NUMBER: 5 + DESCRIPTION: Contract function with CallsInPlace effect with not allowed implies. + UNEXPECTED BEHAVIOUR + ISSUES: KT-26409 + */ + +import kotlin.contracts.* + +fun case_1(value_1: Any?, block: () -> Unit) { + contract { callsInPlace(block, InvocationKind.EXACTLY_ONCE) implies (value_1 != null) } + if (value_1 != null) block() +} diff --git a/compiler/tests-spec/testData/diagnostics/notLinked/contracts/declarations/contractBuilder/common/pos/5.txt b/compiler/tests-spec/testData/diagnostics/notLinked/contracts/declarations/contractBuilder/common/pos/5.txt new file mode 100644 index 00000000000..c4c60e0a3aa --- /dev/null +++ b/compiler/tests-spec/testData/diagnostics/notLinked/contracts/declarations/contractBuilder/common/pos/5.txt @@ -0,0 +1,4 @@ +package + +public fun case_1(/*0*/ value_1: kotlin.Any?, /*1*/ block: () -> kotlin.Unit): kotlin.Unit + CallsInPlace(block, EXACTLY_ONCE) -> value_1 != null diff --git a/compiler/tests-spec/testData/diagnostics/notLinked/contracts/declarations/contractBuilder/common/pos/6.kt b/compiler/tests-spec/testData/diagnostics/notLinked/contracts/declarations/contractBuilder/common/pos/6.kt new file mode 100644 index 00000000000..4c41a966631 --- /dev/null +++ b/compiler/tests-spec/testData/diagnostics/notLinked/contracts/declarations/contractBuilder/common/pos/6.kt @@ -0,0 +1,26 @@ +// !LANGUAGE: +AllowContractsForCustomFunctions +UseCallsInPlaceEffect +// !DIAGNOSTICS: -INVISIBLE_REFERENCE -INVISIBLE_MEMBER -UNUSED_VARIABLE -UNUSED_PARAMETER -UNREACHABLE_CODE -UNUSED_EXPRESSION +// !USE_EXPERIMENTAL: kotlin.contracts.ExperimentalContracts + +/* + KOTLIN DIAGNOSTICS NOT LINKED SPEC TEST (POSITIVE) + + SECTION: contracts + CATEGORY: declarations, contractBuilder, common + NUMBER: 6 + DESCRIPTION: contracts with not allowed conditions with boolean constants or constant expressions in implies. + UNEXPECTED BEHAVIOUR + ISSUES: KT-26491 + */ + +import kotlin.contracts.* + +fun case_1(): Boolean { + contract { returns(true) implies true } + return true +} + +fun case_2(): Boolean { + contract { returns(true) implies (true || false) } + return true || false +} \ No newline at end of file diff --git a/compiler/tests-spec/testData/diagnostics/notLinked/contracts/declarations/contractBuilder/common/pos/6.txt b/compiler/tests-spec/testData/diagnostics/notLinked/contracts/declarations/contractBuilder/common/pos/6.txt new file mode 100644 index 00000000000..2f6a1654cbb --- /dev/null +++ b/compiler/tests-spec/testData/diagnostics/notLinked/contracts/declarations/contractBuilder/common/pos/6.txt @@ -0,0 +1,7 @@ +package + +public fun case_1(): kotlin.Boolean + Returns(TRUE) -> TRUE + +public fun case_2(): kotlin.Boolean + Returns(TRUE) -> TRUE || FALSE diff --git a/compiler/tests-spec/testData/diagnostics/notLinked/contracts/declarations/contractBuilder/effects/callsInPlace/neg/1.kt b/compiler/tests-spec/testData/diagnostics/notLinked/contracts/declarations/contractBuilder/effects/callsInPlace/neg/1.kt new file mode 100644 index 00000000000..643a9e40b5a --- /dev/null +++ b/compiler/tests-spec/testData/diagnostics/notLinked/contracts/declarations/contractBuilder/effects/callsInPlace/neg/1.kt @@ -0,0 +1,42 @@ +// !LANGUAGE: +AllowContractsForCustomFunctions +UseCallsInPlaceEffect +// !DIAGNOSTICS: -INVISIBLE_REFERENCE -INVISIBLE_MEMBER +// !USE_EXPERIMENTAL: kotlin.contracts.ExperimentalContracts + +/* + KOTLIN DIAGNOSTICS NOT LINKED SPEC TEST (NEGATIVE) + + SECTION: contracts + CATEGORY: declarations, contractBuilder, effects, callsInPlace + NUMBER: 1 + DESCRIPTION: contract functions with CallsInPlace with dynamic InvocationKind. + ISSUES: KT-26152 + */ + +import kotlin.contracts.* + +internal val invocationKind: InvocationKind = InvocationKind.EXACTLY_ONCE + +internal object SampleObject { + val invocationKind = InvocationKind.EXACTLY_ONCE +} + +internal inline fun case_1(invocationKind: InvocationKind, block: () -> Unit) { + contract { callsInPlace(block, invocationKind) } + return block() +} + +inline fun case_2(invocationKind: T, block: () -> Unit) { + contract { callsInPlace(block, invocationKind) } + return block() +} + +internal inline fun case_3(block: () -> Unit) { + contract { callsInPlace(block, invocationKind) } + return block() +} + +internal inline fun case_4(block: () -> Unit) { + contract { callsInPlace(block, SampleObject.invocationKind) } + return block() +} + diff --git a/compiler/tests-spec/testData/diagnostics/notLinked/contracts/declarations/contractBuilder/effects/callsInPlace/neg/1.txt b/compiler/tests-spec/testData/diagnostics/notLinked/contracts/declarations/contractBuilder/effects/callsInPlace/neg/1.txt new file mode 100644 index 00000000000..bb7f825e378 --- /dev/null +++ b/compiler/tests-spec/testData/diagnostics/notLinked/contracts/declarations/contractBuilder/effects/callsInPlace/neg/1.txt @@ -0,0 +1,15 @@ +package + +internal val invocationKind: kotlin.contracts.InvocationKind +internal inline fun case_1(/*0*/ invocationKind: kotlin.contracts.InvocationKind, /*1*/ block: () -> kotlin.Unit): kotlin.Unit +public inline fun case_2(/*0*/ invocationKind: T, /*1*/ block: () -> kotlin.Unit): kotlin.Unit +internal inline fun case_3(/*0*/ block: () -> kotlin.Unit): kotlin.Unit +internal inline fun case_4(/*0*/ block: () -> kotlin.Unit): kotlin.Unit + +internal object SampleObject { + private constructor SampleObject() + public final val invocationKind: kotlin.contracts.InvocationKind + 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 +} diff --git a/compiler/tests-spec/testData/diagnostics/notLinked/contracts/declarations/contractBuilder/effects/callsInPlace/pos/1.kt b/compiler/tests-spec/testData/diagnostics/notLinked/contracts/declarations/contractBuilder/effects/callsInPlace/pos/1.kt new file mode 100644 index 00000000000..a7b6df89825 --- /dev/null +++ b/compiler/tests-spec/testData/diagnostics/notLinked/contracts/declarations/contractBuilder/effects/callsInPlace/pos/1.kt @@ -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: declarations, contractBuilder, effects, callsInPlace + NUMBER: 1 + DESCRIPTION: contract functions with CallsInPlace effects with different invocation kinds. + */ + +import kotlin.contracts.* + +inline fun case_1(block: () -> Unit) { + contract { callsInPlace(block, InvocationKind.EXACTLY_ONCE) } + return block() +} + +inline fun case_2(block: () -> Unit) { + contract { callsInPlace(block, InvocationKind.AT_MOST_ONCE) } + return block() +} + +inline fun case_3(block: () -> Unit) { + contract { callsInPlace(block, InvocationKind.AT_LEAST_ONCE) } + return block() +} + +inline fun case_4(block: () -> Unit) { + contract { callsInPlace(block, InvocationKind.UNKNOWN) } + return block() +} diff --git a/compiler/tests-spec/testData/diagnostics/notLinked/contracts/declarations/contractBuilder/effects/callsInPlace/pos/1.txt b/compiler/tests-spec/testData/diagnostics/notLinked/contracts/declarations/contractBuilder/effects/callsInPlace/pos/1.txt new file mode 100644 index 00000000000..0997b20fb72 --- /dev/null +++ b/compiler/tests-spec/testData/diagnostics/notLinked/contracts/declarations/contractBuilder/effects/callsInPlace/pos/1.txt @@ -0,0 +1,13 @@ +package + +public inline fun case_1(/*0*/ block: () -> kotlin.Unit): kotlin.Unit + CallsInPlace(block, EXACTLY_ONCE) + +public inline fun case_2(/*0*/ block: () -> kotlin.Unit): kotlin.Unit + CallsInPlace(block, AT_MOST_ONCE) + +public inline fun case_3(/*0*/ block: () -> kotlin.Unit): kotlin.Unit + CallsInPlace(block, AT_LEAST_ONCE) + +public inline fun case_4(/*0*/ block: () -> kotlin.Unit): kotlin.Unit + CallsInPlace(block, UNKNOWN) diff --git a/compiler/tests-spec/testData/diagnostics/notLinked/contracts/declarations/contractBuilder/effects/callsInPlace/pos/2.kt b/compiler/tests-spec/testData/diagnostics/notLinked/contracts/declarations/contractBuilder/effects/callsInPlace/pos/2.kt new file mode 100644 index 00000000000..0826a3b4a13 --- /dev/null +++ b/compiler/tests-spec/testData/diagnostics/notLinked/contracts/declarations/contractBuilder/effects/callsInPlace/pos/2.kt @@ -0,0 +1,32 @@ +// !LANGUAGE: +AllowContractsForCustomFunctions +UseCallsInPlaceEffect +// !DIAGNOSTICS: -INVISIBLE_REFERENCE -INVISIBLE_MEMBER +// !USE_EXPERIMENTAL: kotlin.contracts.ExperimentalContracts + +/* + KOTLIN DIAGNOSTICS NOT LINKED SPEC TEST (POSITIVE) + + SECTION: contracts + CATEGORY: declarations, contractBuilder, effects, callsInPlace + NUMBER: 2 + DESCRIPTION: functions with contract and duplicate CallsInPlace. + UNEXPECTED BEHAVIOUR + ISSUES: KT-26150 + */ + +import kotlin.contracts.* + +inline fun case_1(block: () -> Unit) { + contract { + callsInPlace(block, InvocationKind.EXACTLY_ONCE) + callsInPlace(block, InvocationKind.EXACTLY_ONCE) + } + return block() +} + +inline fun case_2(block: () -> Unit) { + contract { + callsInPlace(block, InvocationKind.EXACTLY_ONCE) + callsInPlace(block, InvocationKind.AT_MOST_ONCE) + } + return block() +} diff --git a/compiler/tests-spec/testData/diagnostics/notLinked/contracts/declarations/contractBuilder/effects/callsInPlace/pos/2.txt b/compiler/tests-spec/testData/diagnostics/notLinked/contracts/declarations/contractBuilder/effects/callsInPlace/pos/2.txt new file mode 100644 index 00000000000..aba002d3c4c --- /dev/null +++ b/compiler/tests-spec/testData/diagnostics/notLinked/contracts/declarations/contractBuilder/effects/callsInPlace/pos/2.txt @@ -0,0 +1,9 @@ +package + +public inline fun case_1(/*0*/ block: () -> kotlin.Unit): kotlin.Unit + CallsInPlace(block, EXACTLY_ONCE) + CallsInPlace(block, EXACTLY_ONCE) + +public inline fun case_2(/*0*/ block: () -> kotlin.Unit): kotlin.Unit + CallsInPlace(block, EXACTLY_ONCE) + CallsInPlace(block, AT_MOST_ONCE) diff --git a/compiler/tests-spec/testData/diagnostics/notLinked/contracts/declarations/contractBuilder/effects/callsInPlace/pos/3.kt b/compiler/tests-spec/testData/diagnostics/notLinked/contracts/declarations/contractBuilder/effects/callsInPlace/pos/3.kt new file mode 100644 index 00000000000..a461958883b --- /dev/null +++ b/compiler/tests-spec/testData/diagnostics/notLinked/contracts/declarations/contractBuilder/effects/callsInPlace/pos/3.kt @@ -0,0 +1,25 @@ +// !LANGUAGE: +AllowContractsForCustomFunctions +UseCallsInPlaceEffect +// !DIAGNOSTICS: -INVISIBLE_REFERENCE -INVISIBLE_MEMBER -UNUSED_VARIABLE +// !USE_EXPERIMENTAL: kotlin.contracts.ExperimentalContracts + +/* + KOTLIN DIAGNOSTICS NOT LINKED SPEC TEST (POSITIVE) + + SECTION: contracts + CATEGORY: declarations, contractBuilder, effects, callsInPlace + NUMBER: 3 + DESCRIPTION: Contract with 'this' in first parameter of CallsInPlace. + UNEXPECTED BEHAVIOUR + ISSUES: KT-26294 + */ + +import kotlin.contracts.* + +inline fun > T.case_1(block: () -> Unit) { + contract { + callsInPlace(this@case_1, InvocationKind.EXACTLY_ONCE) + callsInPlace(block, InvocationKind.EXACTLY_ONCE) + } + block() + (this@case_1 as kotlin.reflect.KFunction<*>).call() +} diff --git a/compiler/tests-spec/testData/diagnostics/notLinked/contracts/declarations/contractBuilder/effects/callsInPlace/pos/3.txt b/compiler/tests-spec/testData/diagnostics/notLinked/contracts/declarations/contractBuilder/effects/callsInPlace/pos/3.txt new file mode 100644 index 00000000000..75cdd2e2725 --- /dev/null +++ b/compiler/tests-spec/testData/diagnostics/notLinked/contracts/declarations/contractBuilder/effects/callsInPlace/pos/3.txt @@ -0,0 +1,6 @@ +package + +public inline fun > T.case_1(/*0*/ block: () -> kotlin.Unit): kotlin.Unit + CallsInPlace(, EXACTLY_ONCE) + CallsInPlace(block, EXACTLY_ONCE) + diff --git a/compiler/tests-spec/testData/diagnostics/notLinked/contracts/declarations/contractBuilder/effects/common/neg/1.kt b/compiler/tests-spec/testData/diagnostics/notLinked/contracts/declarations/contractBuilder/effects/common/neg/1.kt new file mode 100644 index 00000000000..94334ae6d1c --- /dev/null +++ b/compiler/tests-spec/testData/diagnostics/notLinked/contracts/declarations/contractBuilder/effects/common/neg/1.kt @@ -0,0 +1,57 @@ +// !LANGUAGE: +AllowContractsForCustomFunctions +UseCallsInPlaceEffect +// !DIAGNOSTICS: -INVISIBLE_REFERENCE -INVISIBLE_MEMBER -UNUSED_VARIABLE +// !USE_EXPERIMENTAL: kotlin.contracts.ExperimentalContracts + +/* + KOTLIN DIAGNOSTICS NOT LINKED SPEC TEST (NEGATIVE) + + SECTION: contracts + CATEGORY: declarations, contractBuilder, effects, common + NUMBER: 1 + DESCRIPTION: Indirect effect functions call. + ISSUES: KT-26175 + */ + +import kotlin.contracts.* + +inline fun case_1(block: () -> Unit) { + contract { + { callsInPlace(block, InvocationKind.EXACTLY_ONCE) }() + } + return block() +} + +fun case_2(x: Any?): Boolean { + contract { + returns(true).apply { implies (x is Number) } // 'Returns' as result + } + return x is Number +} + +fun case_3(x: Any?): Boolean { + contract { + returns(true).also { it implies (x is Number) } // 'Returns' as result + } + return x is Number +} + +fun case_4(x: Any?): Boolean { + contract { + returns(true).let { it implies (x is Number) } // 'ConditionalEffect' as result + } + return x is Number +} + +fun case_5(x: Any?): Boolean { + contract { + returns(true).run { implies (x is Number) } // 'ConditionalEffect' as result + } + return x is Number +} + +fun case_6(x: Any?): Boolean { + contract { + returns(true).takeIf { it implies (x is Number); false } // null, must be unrecognized effect + } + return x is Number +} diff --git a/compiler/tests-spec/testData/diagnostics/notLinked/contracts/declarations/contractBuilder/effects/common/neg/1.txt b/compiler/tests-spec/testData/diagnostics/notLinked/contracts/declarations/contractBuilder/effects/common/neg/1.txt new file mode 100644 index 00000000000..ab107f281f2 --- /dev/null +++ b/compiler/tests-spec/testData/diagnostics/notLinked/contracts/declarations/contractBuilder/effects/common/neg/1.txt @@ -0,0 +1,8 @@ +package + +public inline fun case_1(/*0*/ block: () -> kotlin.Unit): kotlin.Unit +public fun case_2(/*0*/ x: kotlin.Any?): kotlin.Boolean +public fun case_3(/*0*/ x: kotlin.Any?): kotlin.Boolean +public fun case_4(/*0*/ x: kotlin.Any?): kotlin.Boolean +public fun case_5(/*0*/ x: kotlin.Any?): kotlin.Boolean +public fun case_6(/*0*/ x: kotlin.Any?): kotlin.Boolean diff --git a/compiler/tests-spec/testData/diagnostics/notLinked/contracts/declarations/contractBuilder/effects/returns/neg/1.kt b/compiler/tests-spec/testData/diagnostics/notLinked/contracts/declarations/contractBuilder/effects/returns/neg/1.kt new file mode 100644 index 00000000000..2035a1dffff --- /dev/null +++ b/compiler/tests-spec/testData/diagnostics/notLinked/contracts/declarations/contractBuilder/effects/returns/neg/1.kt @@ -0,0 +1,35 @@ +// !LANGUAGE: +AllowContractsForCustomFunctions +UseCallsInPlaceEffect +// !DIAGNOSTICS: -INVISIBLE_REFERENCE -INVISIBLE_MEMBER +// !USE_EXPERIMENTAL: kotlin.contracts.ExperimentalContracts + +/* + KOTLIN DIAGNOSTICS NOT LINKED SPEC TEST (NEGATIVE) + + SECTION: contracts + CATEGORY: declarations, contractBuilder, effects, returns + NUMBER: 1 + DESCRIPTION: Using equality with expressions in implies. + ISSUES: KT-26178 + */ + +import kotlin.contracts.* + +fun case_1(x: Any?): Boolean { + contract { returns(true) implies (x == -.15f) } + return x !is Number +} + +fun case_2(x: Any?): Boolean { + contract { returns(true) implies (x == "..." + ".") } + return x !is Number +} + +fun case_3(x: Int, y: Int): Boolean { + contract { returns(true) implies (x > y) } + return x > y +} + +fun case_4(x: Any?, y: Any?): Boolean { + contract { returns(true) implies (x == y.toString()) } + return x !is Number +} diff --git a/compiler/tests-spec/testData/diagnostics/notLinked/contracts/declarations/contractBuilder/effects/returns/neg/1.txt b/compiler/tests-spec/testData/diagnostics/notLinked/contracts/declarations/contractBuilder/effects/returns/neg/1.txt new file mode 100644 index 00000000000..730f998758f --- /dev/null +++ b/compiler/tests-spec/testData/diagnostics/notLinked/contracts/declarations/contractBuilder/effects/returns/neg/1.txt @@ -0,0 +1,6 @@ +package + +public fun case_1(/*0*/ x: kotlin.Any?): kotlin.Boolean +public fun case_2(/*0*/ x: kotlin.Any?): kotlin.Boolean +public fun case_3(/*0*/ x: kotlin.Int, /*1*/ y: kotlin.Int): kotlin.Boolean +public fun case_4(/*0*/ x: kotlin.Any?, /*1*/ y: kotlin.Any?): kotlin.Boolean diff --git a/compiler/tests-spec/testData/diagnostics/notLinked/contracts/declarations/contractBuilder/effects/returns/neg/2.kt b/compiler/tests-spec/testData/diagnostics/notLinked/contracts/declarations/contractBuilder/effects/returns/neg/2.kt new file mode 100644 index 00000000000..cceaee17288 --- /dev/null +++ b/compiler/tests-spec/testData/diagnostics/notLinked/contracts/declarations/contractBuilder/effects/returns/neg/2.kt @@ -0,0 +1,42 @@ +// !LANGUAGE: +AllowContractsForCustomFunctions +UseCallsInPlaceEffect +// !DIAGNOSTICS: -INVISIBLE_REFERENCE -INVISIBLE_MEMBER +// !USE_EXPERIMENTAL: kotlin.contracts.ExperimentalContracts + +/* + KOTLIN DIAGNOSTICS NOT LINKED SPEC TEST (NEGATIVE) + + SECTION: contracts + CATEGORY: declarations, contractBuilder, effects, returns + NUMBER: 2 + DESCRIPTION: Using equality with not labeled 'this' in implies. + */ + +import kotlin.contracts.* + +fun Any?.case_1(): Boolean { + contract { + returns(true) implies (this != null) + } + return this != null +} + +fun Any?.case_2(): Boolean { + contract { + returnsNotNull() implies (this is Number?) + } + return this is Number? +} + +fun T?.case_3(): Boolean { + contract { + returnsNotNull() implies (this != null) + } + return this != null +} + +inline fun T.case_4(): Boolean { + contract { + returns(null) implies (this is Int) + } + return this is Int +} diff --git a/compiler/tests-spec/testData/diagnostics/notLinked/contracts/declarations/contractBuilder/effects/returns/neg/2.txt b/compiler/tests-spec/testData/diagnostics/notLinked/contracts/declarations/contractBuilder/effects/returns/neg/2.txt new file mode 100644 index 00000000000..0ad590ba1f3 --- /dev/null +++ b/compiler/tests-spec/testData/diagnostics/notLinked/contracts/declarations/contractBuilder/effects/returns/neg/2.txt @@ -0,0 +1,6 @@ +package + +public fun kotlin.Any?.case_1(): kotlin.Boolean +public fun kotlin.Any?.case_2(): kotlin.Boolean +public fun T?.case_3(): kotlin.Boolean +public inline fun T.case_4(): kotlin.Boolean diff --git a/compiler/tests-spec/testData/diagnostics/notLinked/contracts/declarations/contractBuilder/effects/returns/neg/3.kt b/compiler/tests-spec/testData/diagnostics/notLinked/contracts/declarations/contractBuilder/effects/returns/neg/3.kt new file mode 100644 index 00000000000..6d3fb9b3f9a --- /dev/null +++ b/compiler/tests-spec/testData/diagnostics/notLinked/contracts/declarations/contractBuilder/effects/returns/neg/3.kt @@ -0,0 +1,23 @@ +// !LANGUAGE: +AllowContractsForCustomFunctions +UseCallsInPlaceEffect +// !DIAGNOSTICS: -INVISIBLE_REFERENCE -INVISIBLE_MEMBER +// !USE_EXPERIMENTAL: kotlin.contracts.ExperimentalContracts +// !WITH_OBJECTS + +/* + KOTLIN DIAGNOSTICS NOT LINKED SPEC TEST (NEGATIVE) + + SECTION: contracts + CATEGORY: declarations, contractBuilder, effects, returns + NUMBER: 3 + DESCRIPTION: Using reference equality in implies. + ISSUES: KT-26177 + */ + +import kotlin.contracts.* + +fun case_1(x: Any?): Boolean { + contract { + returns(true) implies (x === _EmptyObject) // should be not allowed + } + return x === _EmptyObject +} diff --git a/compiler/tests-spec/testData/diagnostics/notLinked/contracts/declarations/contractBuilder/effects/returns/neg/3.txt b/compiler/tests-spec/testData/diagnostics/notLinked/contracts/declarations/contractBuilder/effects/returns/neg/3.txt new file mode 100644 index 00000000000..5c2549c2e28 --- /dev/null +++ b/compiler/tests-spec/testData/diagnostics/notLinked/contracts/declarations/contractBuilder/effects/returns/neg/3.txt @@ -0,0 +1,10 @@ +package + +public fun case_1(/*0*/ x: kotlin.Any?): kotlin.Boolean + +public object _EmptyObject { + private constructor _EmptyObject() + 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 +} diff --git a/compiler/tests-spec/testData/diagnostics/notLinked/contracts/declarations/contractBuilder/effects/returns/neg/4.kt b/compiler/tests-spec/testData/diagnostics/notLinked/contracts/declarations/contractBuilder/effects/returns/neg/4.kt new file mode 100644 index 00000000000..22499ac09d7 --- /dev/null +++ b/compiler/tests-spec/testData/diagnostics/notLinked/contracts/declarations/contractBuilder/effects/returns/neg/4.kt @@ -0,0 +1,30 @@ +// !LANGUAGE: +AllowContractsForCustomFunctions +UseCallsInPlaceEffect +// !DIAGNOSTICS: -INVISIBLE_REFERENCE -INVISIBLE_MEMBER +// !USE_EXPERIMENTAL: kotlin.contracts.ExperimentalContracts + +/* + KOTLIN DIAGNOSTICS NOT LINKED SPEC TEST (NEGATIVE) + + SECTION: contracts + CATEGORY: declarations, contractBuilder, effects, returns + NUMBER: 4 + DESCRIPTION: Using equality with literals in implies. + ISSUES: KT-26178 + */ + +import kotlin.contracts.* + +fun case_1(x: Any?): Boolean { + contract { returns(true) implies (x == .15f) } + return x == .15f +} + +fun case_2(x: Any?) { + contract { returns() implies (x == "...") } + if (x != "...") throw Exception() +} + +fun case_3(x: Any?): Boolean { + contract { returns(true) implies (x == '-') } + return x == '-' +} diff --git a/compiler/tests-spec/testData/diagnostics/notLinked/contracts/declarations/contractBuilder/effects/returns/neg/4.txt b/compiler/tests-spec/testData/diagnostics/notLinked/contracts/declarations/contractBuilder/effects/returns/neg/4.txt new file mode 100644 index 00000000000..50e8b6549ed --- /dev/null +++ b/compiler/tests-spec/testData/diagnostics/notLinked/contracts/declarations/contractBuilder/effects/returns/neg/4.txt @@ -0,0 +1,5 @@ +package + +public fun case_1(/*0*/ x: kotlin.Any?): kotlin.Boolean +public fun case_2(/*0*/ x: kotlin.Any?): kotlin.Unit +public fun case_3(/*0*/ x: kotlin.Any?): kotlin.Boolean diff --git a/compiler/tests-spec/testData/diagnostics/notLinked/contracts/declarations/contractBuilder/effects/returns/neg/5.kt b/compiler/tests-spec/testData/diagnostics/notLinked/contracts/declarations/contractBuilder/effects/returns/neg/5.kt new file mode 100644 index 00000000000..d679d6bb7b8 --- /dev/null +++ b/compiler/tests-spec/testData/diagnostics/notLinked/contracts/declarations/contractBuilder/effects/returns/neg/5.kt @@ -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, contractBuilder, effects, returns + NUMBER: 5 + DESCRIPTION: Contract on the extension function with Boolean upper bound (Boolean or Nothing) or smartcast to Boolean. + DISCUSSION + */ + +import kotlin.contracts.* + +fun Boolean>T.case_1(): Boolean? { + contract { returns(null) implies (!this@case_1) } + return if (!this) null else true +} diff --git a/compiler/tests-spec/testData/diagnostics/notLinked/contracts/declarations/contractBuilder/effects/returns/neg/5.txt b/compiler/tests-spec/testData/diagnostics/notLinked/contracts/declarations/contractBuilder/effects/returns/neg/5.txt new file mode 100644 index 00000000000..520897170f4 --- /dev/null +++ b/compiler/tests-spec/testData/diagnostics/notLinked/contracts/declarations/contractBuilder/effects/returns/neg/5.txt @@ -0,0 +1,3 @@ +package + +public fun T.case_1(): kotlin.Boolean? diff --git a/compiler/tests-spec/testData/diagnostics/notLinked/contracts/declarations/contractBuilder/effects/returns/neg/6.kt b/compiler/tests-spec/testData/diagnostics/notLinked/contracts/declarations/contractBuilder/effects/returns/neg/6.kt new file mode 100644 index 00000000000..2f4080e703a --- /dev/null +++ b/compiler/tests-spec/testData/diagnostics/notLinked/contracts/declarations/contractBuilder/effects/returns/neg/6.kt @@ -0,0 +1,49 @@ +// !LANGUAGE: +AllowContractsForCustomFunctions +UseCallsInPlaceEffect +// !DIAGNOSTICS: -INVISIBLE_REFERENCE -INVISIBLE_MEMBER +// !USE_EXPERIMENTAL: kotlin.contracts.ExperimentalContracts + +/* + KOTLIN DIAGNOSTICS NOT LINKED SPEC TEST (NEGATIVE) + + SECTION: contracts + CATEGORY: declarations, contractBuilder, effects, returns + NUMBER: 6 + DESCRIPTION: Contract on the extension function with smartcast to Boolean. + */ + +import kotlin.contracts.* + +fun Boolean?.case_1(): Boolean { + contract { returns(true) implies (this@case_1 != null && this@case_1) } + return this != null && this +} + +fun Boolean>T?.case_2(): Boolean { + contract { returns(true) implies (this@case_2 != null && this@case_2 !is Nothing && this@case_2) } + return this != null && this !is Nothing && this +} + +fun T?.case_3() { + contract { returns() implies (this@case_3 == null || this@case_3 is Boolean? && !this@case_3) } + if (!(this == null || this is Boolean? && !this)) throw Exception() +} + +fun case_4(value_1: Boolean?): Boolean { + contract { returns(true) implies (value_1 != null && !value_1) } + return value_1 != null && !value_1 +} + +fun Boolean.case_5(value_1: Any?): Boolean? { + contract { returnsNotNull() implies (value_1 is Boolean? && value_1 != null && value_1) } + return if (value_1 is Boolean? && value_1 != null && value_1) true else null +} + +fun Boolean?.case_6(): Boolean? { + contract { returnsNotNull() implies (this@case_6 != null && this@case_6) } + return if (this@case_6 != null && this@case_6) true else null +} + +fun T.case_7(value_1: Any?): Boolean? { + contract { returnsNotNull() implies (value_1 is Boolean? && value_1 != null && value_1 && this@case_7 != null && this@case_7) } + return if (value_1 is Boolean? && value_1 != null && value_1 && this@case_7 != null && this@case_7) true else null +} diff --git a/compiler/tests-spec/testData/diagnostics/notLinked/contracts/declarations/contractBuilder/effects/returns/neg/6.txt b/compiler/tests-spec/testData/diagnostics/notLinked/contracts/declarations/contractBuilder/effects/returns/neg/6.txt new file mode 100644 index 00000000000..5d8ac1d46ce --- /dev/null +++ b/compiler/tests-spec/testData/diagnostics/notLinked/contracts/declarations/contractBuilder/effects/returns/neg/6.txt @@ -0,0 +1,9 @@ +package + +public fun case_4(/*0*/ value_1: kotlin.Boolean?): kotlin.Boolean +public fun kotlin.Boolean?.case_1(): kotlin.Boolean +public fun T?.case_2(): kotlin.Boolean +public fun T?.case_3(): kotlin.Unit +public fun kotlin.Boolean.case_5(/*0*/ value_1: kotlin.Any?): kotlin.Boolean? +public fun kotlin.Boolean?.case_6(): kotlin.Boolean? +public fun T.case_7(/*0*/ value_1: kotlin.Any?): kotlin.Boolean? diff --git a/compiler/tests-spec/testData/diagnostics/notLinked/contracts/declarations/contractBuilder/effects/returns/neg/7.kt b/compiler/tests-spec/testData/diagnostics/notLinked/contracts/declarations/contractBuilder/effects/returns/neg/7.kt new file mode 100644 index 00000000000..fe7ad943fa8 --- /dev/null +++ b/compiler/tests-spec/testData/diagnostics/notLinked/contracts/declarations/contractBuilder/effects/returns/neg/7.kt @@ -0,0 +1,35 @@ +// !LANGUAGE: +AllowContractsForCustomFunctions +UseCallsInPlaceEffect +// !DIAGNOSTICS: -INVISIBLE_REFERENCE -INVISIBLE_MEMBER +// !USE_EXPERIMENTAL: kotlin.contracts.ExperimentalContracts + +/* + KOTLIN DIAGNOSTICS NOT LINKED SPEC TEST (NEGATIVE) + + SECTION: contracts + CATEGORY: declarations, contractBuilder, effects, returns + NUMBER: 7 + DESCRIPTION: Returns effect with type checking with generic parameter + ISSUES: KT-26296 + */ + +import kotlin.contracts.* + +fun T.case_1() { + contract { returns() implies (this@case_1 is T) } + if (!(this@case_1 is T)) throw Exception() +} + +fun String> T?.case_2(value_1: K?) { + contract { returns() implies (this@case_2 is T && value_1 is K) } + if (!(this@case_2 is T && value_1 is K)) throw Exception() +} + +inline fun T?.case_3(value_1: Any?) { + contract { returns() implies (value_1 is T) } + if (!(value_1 is T)) throw Exception() +} + +inline fun K?.case_4(value_1: Any?) { + contract { returns() implies (this@case_4 !is T || value_1 is T) } + if (!(this@case_4 !is T || value_1 is T)) throw Exception() +} diff --git a/compiler/tests-spec/testData/diagnostics/notLinked/contracts/declarations/contractBuilder/effects/returns/neg/7.txt b/compiler/tests-spec/testData/diagnostics/notLinked/contracts/declarations/contractBuilder/effects/returns/neg/7.txt new file mode 100644 index 00000000000..96fd7216fba --- /dev/null +++ b/compiler/tests-spec/testData/diagnostics/notLinked/contracts/declarations/contractBuilder/effects/returns/neg/7.txt @@ -0,0 +1,6 @@ +package + +public fun T.case_1(): kotlin.Unit +public fun T?.case_2(/*0*/ value_1: K?): kotlin.Unit +public inline fun T?.case_3(/*0*/ value_1: kotlin.Any?): kotlin.Unit +public inline fun K?.case_4(/*0*/ value_1: kotlin.Any?): kotlin.Unit diff --git a/compiler/tests-spec/testData/diagnostics/notLinked/contracts/declarations/contractBuilder/effects/returns/pos/1.kt b/compiler/tests-spec/testData/diagnostics/notLinked/contracts/declarations/contractBuilder/effects/returns/pos/1.kt new file mode 100644 index 00000000000..a5c8d057bbf --- /dev/null +++ b/compiler/tests-spec/testData/diagnostics/notLinked/contracts/declarations/contractBuilder/effects/returns/pos/1.kt @@ -0,0 +1,75 @@ +// !USE_EXPERIMENTAL: kotlin.contracts.ExperimentalContracts +// !LANGUAGE: +AllowContractsForCustomFunctions +UseCallsInPlaceEffect +// !DIAGNOSTICS: -INVISIBLE_REFERENCE -INVISIBLE_MEMBER -FINAL_UPPER_BOUND +// !WITH_CLASSES + +/* + KOTLIN DIAGNOSTICS NOT LINKED SPEC TEST (POSITIVE) + + SECTION: contracts + CATEGORY: declarations, contractBuilder, effects, returns + NUMBER: 1 + DESCRIPTION: Returns effect with simple conditions. + */ + +import kotlin.contracts.* + +fun case_1(value_1: Boolean) { + contract { returns() implies (value_1) } + if (!value_1) throw Exception() +} + +fun case_2(value_1: Boolean): Boolean { + contract { returns(false) implies (!value_1) } + return value_1 +} + +fun Boolean.case_3() { + contract { returns() implies (!this@case_3) } + if (this@case_3) throw Exception() +} + +fun case_5(value_1: Any?) { + contract { returns() implies (value_1 is String) } + if (value_1 !is String) throw Exception() +} + +fun case_6(value_1: Any?) { + contract { returns() implies (value_1 !is String?) } + if (value_1 is String?) throw Exception() +} + +fun Any?.case_7() { + contract { returns() implies (this@case_7 is Number) } + if (this !is Number) throw Exception() +} + +fun T?.case_8() { + contract { returns() implies (this@case_8 !is _ClassLevel3?) } + if (this is _ClassLevel3?) throw Exception() +} + +fun T.case_9(): Boolean? { + contract { returns(null) implies (this@case_9 is Byte?) } + return if (this is Byte?) null else true +} + +fun case_10(value_1: Any?) { + contract { returns() implies (value_1 == null) } + if (value_1 != null) throw Exception() +} + +fun case_11(value_1: Any?): Boolean? { + contract { returns(null) implies (value_1 != null) } + return if (value_1 != null) null else true +} + +fun Char.case_12() { + contract { returns() implies (this@case_12 == null) } + if (this@case_12 != null) throw Exception() +} + +fun T?.case_13() { + contract { returns() implies (this@case_13 == null) } + if (this != null) throw Exception() +} diff --git a/compiler/tests-spec/testData/diagnostics/notLinked/contracts/declarations/contractBuilder/effects/returns/pos/1.txt b/compiler/tests-spec/testData/diagnostics/notLinked/contracts/declarations/contractBuilder/effects/returns/pos/1.txt new file mode 100644 index 00000000000..b35b151b6ce --- /dev/null +++ b/compiler/tests-spec/testData/diagnostics/notLinked/contracts/declarations/contractBuilder/effects/returns/pos/1.txt @@ -0,0 +1,128 @@ +package + +public fun case_1(/*0*/ value_1: kotlin.Boolean): kotlin.Unit + Returns(WILDCARD) -> value_1 + +public fun case_10(/*0*/ value_1: kotlin.Any?): kotlin.Unit + Returns(WILDCARD) -> value_1 == null + +public fun case_11(/*0*/ value_1: kotlin.Any?): kotlin.Boolean? + Returns(NULL) -> value_1 != null + +public fun case_2(/*0*/ value_1: kotlin.Boolean): kotlin.Boolean + Returns(FALSE) -> !value_1 + +public fun case_5(/*0*/ value_1: kotlin.Any?): kotlin.Unit + Returns(WILDCARD) -> value_1 is String + +public fun case_6(/*0*/ value_1: kotlin.Any?): kotlin.Unit + Returns(WILDCARD) -> value_1 !is String? + +public fun kotlin.Char.case_12(): kotlin.Unit + Returns(WILDCARD) -> == null + +public fun T?.case_13(): kotlin.Unit + Returns(WILDCARD) -> == null + +public fun kotlin.Boolean.case_3(): kotlin.Unit + Returns(WILDCARD) -> ! + +public fun kotlin.Any?.case_7(): kotlin.Unit + Returns(WILDCARD) -> is Number + +public fun T?.case_8(): kotlin.Unit + Returns(WILDCARD) -> !is _ClassLevel3? + +public fun T.case_9(): kotlin.Boolean? + Returns(NULL) -> is Byte? + +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 +} diff --git a/compiler/tests-spec/testData/diagnostics/notLinked/contracts/declarations/contractBuilder/effects/returns/pos/2.kt b/compiler/tests-spec/testData/diagnostics/notLinked/contracts/declarations/contractBuilder/effects/returns/pos/2.kt new file mode 100644 index 00000000000..98e39d940fe --- /dev/null +++ b/compiler/tests-spec/testData/diagnostics/notLinked/contracts/declarations/contractBuilder/effects/returns/pos/2.kt @@ -0,0 +1,35 @@ +// !LANGUAGE: +AllowContractsForCustomFunctions +UseCallsInPlaceEffect +// !DIAGNOSTICS: -INVISIBLE_REFERENCE -INVISIBLE_MEMBER -FINAL_UPPER_BOUND +// !USE_EXPERIMENTAL: kotlin.contracts.ExperimentalContracts +// !WITH_CLASSES + +/* + KOTLIN DIAGNOSTICS NOT LINKED SPEC TEST (POSITIVE) + + SECTION: contracts + CATEGORY: declarations, contractBuilder, effects, returns + NUMBER: 2 + DESCRIPTION: Returns effect with complex conditions (using conjunction and disjuntion). + */ + +import kotlin.contracts.* + +fun case_1(cond1: Boolean, cond2: Boolean, cond3: Boolean) { + contract { returns() implies (cond1 && !cond2 || cond3) } + if (!(cond1 && !cond2 || cond3)) throw Exception() +} + +fun case_2(value_1: Any?, value_2: Any?, value_3: Any?) { + contract { returns() implies (value_1 is String? || value_2 !is Int && value_3 !is Nothing?) } + if (!(value_1 is String? || value_2 !is Int && value_3 !is Nothing?)) throw Exception() +} + +fun case_3(value_1: Any?, value_2: Any?, value_3: Any?) { + contract { returns() implies (value_1 == null || value_2 != null && value_3 == null) } + if (!(value_1 == null || value_2 != null && value_3 == null)) throw Exception() +} + +fun T.case_4(): Boolean { + contract { returns(false) implies (this@case_4 is Char || this@case_4 == null) } + return !(this is Char || this == null) +} diff --git a/compiler/tests-spec/testData/diagnostics/notLinked/contracts/declarations/contractBuilder/effects/returns/pos/2.txt b/compiler/tests-spec/testData/diagnostics/notLinked/contracts/declarations/contractBuilder/effects/returns/pos/2.txt new file mode 100644 index 00000000000..9bc9a2c52f6 --- /dev/null +++ b/compiler/tests-spec/testData/diagnostics/notLinked/contracts/declarations/contractBuilder/effects/returns/pos/2.txt @@ -0,0 +1,104 @@ +package + +public fun case_1(/*0*/ cond1: kotlin.Boolean, /*1*/ cond2: kotlin.Boolean, /*2*/ cond3: kotlin.Boolean): kotlin.Unit + Returns(WILDCARD) -> (cond1 && (!cond2)) || cond3 + +public fun case_2(/*0*/ value_1: kotlin.Any?, /*1*/ value_2: kotlin.Any?, /*2*/ value_3: kotlin.Any?): kotlin.Unit + Returns(WILDCARD) -> value_1 is String? || (value_2 !is Int && value_3 !is Nothing?) + +public fun case_3(/*0*/ value_1: kotlin.Any?, /*1*/ value_2: kotlin.Any?, /*2*/ value_3: kotlin.Any?): kotlin.Unit + Returns(WILDCARD) -> value_1 == null || (value_2 != null && value_3 == null) + +public fun T.case_4(): kotlin.Boolean + Returns(FALSE) -> is Char || == null + +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 +} diff --git a/compiler/tests-spec/testData/diagnostics/notLinked/contracts/declarations/contractBuilder/effects/returns/pos/3.kt b/compiler/tests-spec/testData/diagnostics/notLinked/contracts/declarations/contractBuilder/effects/returns/pos/3.kt new file mode 100644 index 00000000000..d2786dcd8db --- /dev/null +++ b/compiler/tests-spec/testData/diagnostics/notLinked/contracts/declarations/contractBuilder/effects/returns/pos/3.kt @@ -0,0 +1,29 @@ +// !LANGUAGE: +AllowContractsForCustomFunctions +UseCallsInPlaceEffect +// !DIAGNOSTICS: -INVISIBLE_REFERENCE -INVISIBLE_MEMBER +// !USE_EXPERIMENTAL: kotlin.contracts.ExperimentalContracts + +/* + KOTLIN DIAGNOSTICS NOT LINKED SPEC TEST (POSITIVE) + + SECTION: contracts + CATEGORY: declarations, contractBuilder, effects, returns + NUMBER: 3 + DESCRIPTION: Returns effect with conditions on receiver. + */ + +import kotlin.contracts.* + +fun Any?.case_1(): Boolean { + contract { returns(false) implies (this@case_1 != null) } + return this == null +} + +fun T?.case_2(value_1: Any?, value_2: Any?) { + contract { returns() implies (this@case_2 is String? || value_1 !is Int && value_2 !is Nothing?) } + if (!(this@case_2 is String? || value_1 !is Int && value_2 !is Nothing?)) throw Exception() +} + +inline fun T.case_3(value_1: Any?) { + contract { returns() implies (value_1 == null || this@case_3 != null && this@case_3 is Int) } + if (!(value_1 == null || this@case_3 != null && this@case_3 is Int)) throw Exception() +} diff --git a/compiler/tests-spec/testData/diagnostics/notLinked/contracts/declarations/contractBuilder/effects/returns/pos/3.txt b/compiler/tests-spec/testData/diagnostics/notLinked/contracts/declarations/contractBuilder/effects/returns/pos/3.txt new file mode 100644 index 00000000000..095e226cd94 --- /dev/null +++ b/compiler/tests-spec/testData/diagnostics/notLinked/contracts/declarations/contractBuilder/effects/returns/pos/3.txt @@ -0,0 +1,11 @@ +package + +public fun kotlin.Any?.case_1(): kotlin.Boolean + Returns(FALSE) -> != null + +public fun T?.case_2(/*0*/ value_1: kotlin.Any?, /*1*/ value_2: kotlin.Any?): kotlin.Unit + Returns(WILDCARD) -> is String? || (value_1 !is Int && value_2 !is Nothing?) + +public inline fun T.case_3(/*0*/ value_1: kotlin.Any?): kotlin.Unit + Returns(WILDCARD) -> value_1 == null || ( != null && is Int) + diff --git a/compiler/tests-spec/testData/diagnostics/notLinked/contracts/declarations/contractFunction/neg/1.kt b/compiler/tests-spec/testData/diagnostics/notLinked/contracts/declarations/contractFunction/neg/1.kt new file mode 100644 index 00000000000..6a04909a7c6 --- /dev/null +++ b/compiler/tests-spec/testData/diagnostics/notLinked/contracts/declarations/contractFunction/neg/1.kt @@ -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() + case_1(block) +} diff --git a/compiler/tests-spec/testData/diagnostics/notLinked/contracts/declarations/contractFunction/neg/1.txt b/compiler/tests-spec/testData/diagnostics/notLinked/contracts/declarations/contractFunction/neg/1.txt new file mode 100644 index 00000000000..be76acdde1d --- /dev/null +++ b/compiler/tests-spec/testData/diagnostics/notLinked/contracts/declarations/contractFunction/neg/1.txt @@ -0,0 +1,5 @@ +package + +public inline fun case_1(/*0*/ block: () -> kotlin.Unit): kotlin.Unit + CallsInPlace(block, EXACTLY_ONCE) + diff --git a/compiler/tests-spec/testData/diagnostics/notLinked/contracts/declarations/contractFunction/neg/2.kt b/compiler/tests-spec/testData/diagnostics/notLinked/contracts/declarations/contractFunction/neg/2.kt new file mode 100644 index 00000000000..f8d0de5dcc4 --- /dev/null +++ b/compiler/tests-spec/testData/diagnostics/notLinked/contracts/declarations/contractFunction/neg/2.kt @@ -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 { callsInPlace(block, InvocationKind.EXACTLY_ONCE) } + return block() + } + + fun_1 { throw Exception() } + println("1") +} + +fun case_2() { + val lambda_1 = { block: () -> Unit -> + contract { callsInPlace(block, InvocationKind.EXACTLY_ONCE) } + block() + } + + lambda_1 { throw Exception() } + println("1") +} + +object case_3 { + fun case_3(block: () -> Unit) { + contract { callsInPlace(block, InvocationKind.EXACTLY_ONCE) } + return block() + } +} + +/* + UNEXPECTED BEHAVIOUR + ISSUES: KT-26244 + */ +class case_4 : _ClassLevel3() { + fun T.case_4_1(): Boolean { + contract { returns(false) implies (this@case_4 !is _ClassLevel1) } + return this == null + } + + fun case_4_2(number: Int?): Boolean { + contract { returns(false) implies (number != null) } + return number == null + } + + fun T?.case_4_3(): Boolean { + contract { returns(false) implies (this@case_4_3 !is Number) } + return this@case_4_3 is Number + } + + fun Boolean>T.case_4_4() { + contract { returns() implies (!this@case_4_4) } + if (this) throw Exception() + } + + fun T.case_4_5_wrap() { + fun case_4_5_contract() { + 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 : _ClassLevel5() { + inner class case_5_1 { + fun K.case_5_1_1() { + contract { returns() implies (this@case_5_1 !is _ClassLevel1 && this@case_5_1 != null || this@case_5 is _ClassLevel1 && this@case_5_1_1 is Float) } + if (!(this@case_5_1 !is _ClassLevel1 && this@case_5_1 != null || this@case_5 is _ClassLevel1 && this is Float)) throw Exception() + } + + fun case_5_1_2() { + contract { returns() implies (this@case_5_1 !is _ClassLevel1 || this@case_5 is _ClassLevel1 || this@case_5_1 == null) } + if (!(this@case_5_1 !is _ClassLevel1 || this@case_5 is _ClassLevel1 || this@case_5_1 == null)) throw Exception() + } + } +} diff --git a/compiler/tests-spec/testData/diagnostics/notLinked/contracts/declarations/contractFunction/neg/2.txt b/compiler/tests-spec/testData/diagnostics/notLinked/contracts/declarations/contractFunction/neg/2.txt new file mode 100644 index 00000000000..56722e3ee11 --- /dev/null +++ b/compiler/tests-spec/testData/diagnostics/notLinked/contracts/declarations/contractFunction/neg/2.txt @@ -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 T.case_4_1(): kotlin.Boolean + public final fun T?.case_4_3(): kotlin.Boolean + public final fun T.case_4_4(): kotlin.Unit + public final fun T.case_4_5_wrap(): kotlin.Unit +} + +public final class case_5 : _ClassLevel5 { + public constructor case_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 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 K.case_5_1_1(): kotlin.Unit + } +} diff --git a/compiler/tests-spec/testData/diagnostics/notLinked/contracts/declarations/contractFunction/pos/1.kt b/compiler/tests-spec/testData/diagnostics/notLinked/contracts/declarations/contractFunction/pos/1.kt new file mode 100644 index 00000000000..012b3d49a2f --- /dev/null +++ b/compiler/tests-spec/testData/diagnostics/notLinked/contracts/declarations/contractFunction/pos/1.kt @@ -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() +} diff --git a/compiler/tests-spec/testData/diagnostics/notLinked/contracts/declarations/contractFunction/pos/1.txt b/compiler/tests-spec/testData/diagnostics/notLinked/contracts/declarations/contractFunction/pos/1.txt new file mode 100644 index 00000000000..e5749664606 --- /dev/null +++ b/compiler/tests-spec/testData/diagnostics/notLinked/contracts/declarations/contractFunction/pos/1.txt @@ -0,0 +1,5 @@ +package + +public fun case_1(): kotlin.Unit +public fun funWithContractExactlyOnce(/*0*/ block: () -> kotlin.Unit): kotlin.Unit + CallsInPlace(block, EXACTLY_ONCE) diff --git a/compiler/tests-spec/testData/diagnostics/notLinked/contracts/declarations/contractFunction/pos/2.kt b/compiler/tests-spec/testData/diagnostics/notLinked/contracts/declarations/contractFunction/pos/2.kt new file mode 100644 index 00000000000..e1ea2f58653 --- /dev/null +++ b/compiler/tests-spec/testData/diagnostics/notLinked/contracts/declarations/contractFunction/pos/2.kt @@ -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)) { + x.length + } +} + +fun case_2_1(x: Number?) { + case_2_2(x) + println(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() +} + + diff --git a/compiler/tests-spec/testData/diagnostics/notLinked/contracts/declarations/contractFunction/pos/2.txt b/compiler/tests-spec/testData/diagnostics/notLinked/contracts/declarations/contractFunction/pos/2.txt new file mode 100644 index 00000000000..365216337a8 --- /dev/null +++ b/compiler/tests-spec/testData/diagnostics/notLinked/contracts/declarations/contractFunction/pos/2.txt @@ -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 + diff --git a/compiler/tests-spec/tests/org/jetbrains/kotlin/checkers/DiagnosticsTestSpecGenerated.java b/compiler/tests-spec/tests/org/jetbrains/kotlin/checkers/DiagnosticsTestSpecGenerated.java index 33b41fb4714..16ef26d090c 100644 --- a/compiler/tests-spec/tests/org/jetbrains/kotlin/checkers/DiagnosticsTestSpecGenerated.java +++ b/compiler/tests-spec/tests/org/jetbrains/kotlin/checkers/DiagnosticsTestSpecGenerated.java @@ -584,4 +584,848 @@ public class DiagnosticsTestSpecGenerated extends AbstractDiagnosticsTestSpec { } } } + + @TestMetadata("compiler/tests-spec/testData/diagnostics/notLinked") + @TestDataPath("$PROJECT_ROOT") + @RunWith(JUnit3RunnerWithInners.class) + public static class NotLinked extends AbstractDiagnosticsTestSpec { + private void runTest(String testDataFilePath) throws Exception { + KotlinTestUtils.runTest(this::doTest, TargetBackend.ANY, testDataFilePath); + } + + public void testAllFilesPresentInNotLinked() throws Exception { + KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("compiler/tests-spec/testData/diagnostics/notLinked"), Pattern.compile("^(.+)\\.kt$"), TargetBackend.ANY, true); + } + + @TestMetadata("compiler/tests-spec/testData/diagnostics/notLinked/contracts") + @TestDataPath("$PROJECT_ROOT") + @RunWith(JUnit3RunnerWithInners.class) + public static class Contracts extends AbstractDiagnosticsTestSpec { + private void runTest(String testDataFilePath) throws Exception { + KotlinTestUtils.runTest(this::doTest, TargetBackend.ANY, testDataFilePath); + } + + public void testAllFilesPresentInContracts() throws Exception { + KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("compiler/tests-spec/testData/diagnostics/notLinked/contracts"), Pattern.compile("^(.+)\\.kt$"), TargetBackend.ANY, true); + } + + @TestMetadata("compiler/tests-spec/testData/diagnostics/notLinked/contracts/analysis") + @TestDataPath("$PROJECT_ROOT") + @RunWith(JUnit3RunnerWithInners.class) + public static class Analysis extends AbstractDiagnosticsTestSpec { + private void runTest(String testDataFilePath) throws Exception { + KotlinTestUtils.runTest(this::doTest, TargetBackend.ANY, testDataFilePath); + } + + public void testAllFilesPresentInAnalysis() throws Exception { + KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("compiler/tests-spec/testData/diagnostics/notLinked/contracts/analysis"), Pattern.compile("^(.+)\\.kt$"), TargetBackend.ANY, true); + } + + @TestMetadata("compiler/tests-spec/testData/diagnostics/notLinked/contracts/analysis/common") + @TestDataPath("$PROJECT_ROOT") + @RunWith(JUnit3RunnerWithInners.class) + public static class Common extends AbstractDiagnosticsTestSpec { + private void runTest(String testDataFilePath) throws Exception { + KotlinTestUtils.runTest(this::doTest, TargetBackend.ANY, testDataFilePath); + } + + public void testAllFilesPresentInCommon() throws Exception { + KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("compiler/tests-spec/testData/diagnostics/notLinked/contracts/analysis/common"), Pattern.compile("^(.+)\\.kt$"), TargetBackend.ANY, true); + } + + @TestMetadata("compiler/tests-spec/testData/diagnostics/notLinked/contracts/analysis/common/neg") + @TestDataPath("$PROJECT_ROOT") + @RunWith(JUnit3RunnerWithInners.class) + public static class Neg extends AbstractDiagnosticsTestSpec { + private void runTest(String testDataFilePath) throws Exception { + KotlinTestUtils.runTest(this::doTest, TargetBackend.ANY, testDataFilePath); + } + + @TestMetadata("1.kt") + public void test1() throws Exception { + runTest("compiler/tests-spec/testData/diagnostics/notLinked/contracts/analysis/common/neg/1.kt"); + } + + public void testAllFilesPresentInNeg() throws Exception { + KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("compiler/tests-spec/testData/diagnostics/notLinked/contracts/analysis/common/neg"), Pattern.compile("^(.+)\\.kt$"), TargetBackend.ANY, true); + } + } + + @TestMetadata("compiler/tests-spec/testData/diagnostics/notLinked/contracts/analysis/common/pos") + @TestDataPath("$PROJECT_ROOT") + @RunWith(JUnit3RunnerWithInners.class) + public static class Pos extends AbstractDiagnosticsTestSpec { + private void runTest(String testDataFilePath) throws Exception { + KotlinTestUtils.runTest(this::doTest, TargetBackend.ANY, testDataFilePath); + } + + @TestMetadata("1.kt") + public void test1() throws Exception { + runTest("compiler/tests-spec/testData/diagnostics/notLinked/contracts/analysis/common/pos/1.kt"); + } + + @TestMetadata("2.kt") + public void test2() throws Exception { + runTest("compiler/tests-spec/testData/diagnostics/notLinked/contracts/analysis/common/pos/2.kt"); + } + + public void testAllFilesPresentInPos() throws Exception { + KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("compiler/tests-spec/testData/diagnostics/notLinked/contracts/analysis/common/pos"), Pattern.compile("^(.+)\\.kt$"), TargetBackend.ANY, true); + } + } + } + + @TestMetadata("compiler/tests-spec/testData/diagnostics/notLinked/contracts/analysis/controlFlow") + @TestDataPath("$PROJECT_ROOT") + @RunWith(JUnit3RunnerWithInners.class) + public static class ControlFlow extends AbstractDiagnosticsTestSpec { + private void runTest(String testDataFilePath) throws Exception { + KotlinTestUtils.runTest(this::doTest, TargetBackend.ANY, testDataFilePath); + } + + public void testAllFilesPresentInControlFlow() throws Exception { + KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("compiler/tests-spec/testData/diagnostics/notLinked/contracts/analysis/controlFlow"), Pattern.compile("^(.+)\\.kt$"), TargetBackend.ANY, true); + } + + @TestMetadata("compiler/tests-spec/testData/diagnostics/notLinked/contracts/analysis/controlFlow/initialization") + @TestDataPath("$PROJECT_ROOT") + @RunWith(JUnit3RunnerWithInners.class) + public static class Initialization extends AbstractDiagnosticsTestSpec { + private void runTest(String testDataFilePath) throws Exception { + KotlinTestUtils.runTest(this::doTest, TargetBackend.ANY, testDataFilePath); + } + + public void testAllFilesPresentInInitialization() throws Exception { + KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("compiler/tests-spec/testData/diagnostics/notLinked/contracts/analysis/controlFlow/initialization"), Pattern.compile("^(.+)\\.kt$"), TargetBackend.ANY, true); + } + + @TestMetadata("compiler/tests-spec/testData/diagnostics/notLinked/contracts/analysis/controlFlow/initialization/neg") + @TestDataPath("$PROJECT_ROOT") + @RunWith(JUnit3RunnerWithInners.class) + public static class Neg extends AbstractDiagnosticsTestSpec { + private void runTest(String testDataFilePath) throws Exception { + KotlinTestUtils.runTest(this::doTest, TargetBackend.ANY, testDataFilePath); + } + + @TestMetadata("1.kt") + public void test1() throws Exception { + runTest("compiler/tests-spec/testData/diagnostics/notLinked/contracts/analysis/controlFlow/initialization/neg/1.kt"); + } + + @TestMetadata("2.kt") + public void test2() throws Exception { + runTest("compiler/tests-spec/testData/diagnostics/notLinked/contracts/analysis/controlFlow/initialization/neg/2.kt"); + } + + @TestMetadata("3.kt") + public void test3() throws Exception { + runTest("compiler/tests-spec/testData/diagnostics/notLinked/contracts/analysis/controlFlow/initialization/neg/3.kt"); + } + + @TestMetadata("4.kt") + public void test4() throws Exception { + runTest("compiler/tests-spec/testData/diagnostics/notLinked/contracts/analysis/controlFlow/initialization/neg/4.kt"); + } + + @TestMetadata("5.kt") + public void test5() throws Exception { + runTest("compiler/tests-spec/testData/diagnostics/notLinked/contracts/analysis/controlFlow/initialization/neg/5.kt"); + } + + @TestMetadata("6.kt") + public void test6() throws Exception { + runTest("compiler/tests-spec/testData/diagnostics/notLinked/contracts/analysis/controlFlow/initialization/neg/6.kt"); + } + + public void testAllFilesPresentInNeg() throws Exception { + KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("compiler/tests-spec/testData/diagnostics/notLinked/contracts/analysis/controlFlow/initialization/neg"), Pattern.compile("^(.+)\\.kt$"), TargetBackend.ANY, true); + } + } + + @TestMetadata("compiler/tests-spec/testData/diagnostics/notLinked/contracts/analysis/controlFlow/initialization/pos") + @TestDataPath("$PROJECT_ROOT") + @RunWith(JUnit3RunnerWithInners.class) + public static class Pos extends AbstractDiagnosticsTestSpec { + private void runTest(String testDataFilePath) throws Exception { + KotlinTestUtils.runTest(this::doTest, TargetBackend.ANY, testDataFilePath); + } + + @TestMetadata("1.kt") + public void test1() throws Exception { + runTest("compiler/tests-spec/testData/diagnostics/notLinked/contracts/analysis/controlFlow/initialization/pos/1.kt"); + } + + @TestMetadata("2.kt") + public void test2() throws Exception { + runTest("compiler/tests-spec/testData/diagnostics/notLinked/contracts/analysis/controlFlow/initialization/pos/2.kt"); + } + + @TestMetadata("3.kt") + public void test3() throws Exception { + runTest("compiler/tests-spec/testData/diagnostics/notLinked/contracts/analysis/controlFlow/initialization/pos/3.kt"); + } + + @TestMetadata("4.kt") + public void test4() throws Exception { + runTest("compiler/tests-spec/testData/diagnostics/notLinked/contracts/analysis/controlFlow/initialization/pos/4.kt"); + } + + @TestMetadata("5.kt") + public void test5() throws Exception { + runTest("compiler/tests-spec/testData/diagnostics/notLinked/contracts/analysis/controlFlow/initialization/pos/5.kt"); + } + + public void testAllFilesPresentInPos() throws Exception { + KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("compiler/tests-spec/testData/diagnostics/notLinked/contracts/analysis/controlFlow/initialization/pos"), Pattern.compile("^(.+)\\.kt$"), TargetBackend.ANY, true); + } + } + } + + @TestMetadata("compiler/tests-spec/testData/diagnostics/notLinked/contracts/analysis/controlFlow/unreachableCode") + @TestDataPath("$PROJECT_ROOT") + @RunWith(JUnit3RunnerWithInners.class) + public static class UnreachableCode extends AbstractDiagnosticsTestSpec { + private void runTest(String testDataFilePath) throws Exception { + KotlinTestUtils.runTest(this::doTest, TargetBackend.ANY, testDataFilePath); + } + + public void testAllFilesPresentInUnreachableCode() throws Exception { + KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("compiler/tests-spec/testData/diagnostics/notLinked/contracts/analysis/controlFlow/unreachableCode"), Pattern.compile("^(.+)\\.kt$"), TargetBackend.ANY, true); + } + + @TestMetadata("compiler/tests-spec/testData/diagnostics/notLinked/contracts/analysis/controlFlow/unreachableCode/neg") + @TestDataPath("$PROJECT_ROOT") + @RunWith(JUnit3RunnerWithInners.class) + public static class Neg extends AbstractDiagnosticsTestSpec { + private void runTest(String testDataFilePath) throws Exception { + KotlinTestUtils.runTest(this::doTest, TargetBackend.ANY, testDataFilePath); + } + + @TestMetadata("1.kt") + public void test1() throws Exception { + runTest("compiler/tests-spec/testData/diagnostics/notLinked/contracts/analysis/controlFlow/unreachableCode/neg/1.kt"); + } + + public void testAllFilesPresentInNeg() throws Exception { + KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("compiler/tests-spec/testData/diagnostics/notLinked/contracts/analysis/controlFlow/unreachableCode/neg"), Pattern.compile("^(.+)\\.kt$"), TargetBackend.ANY, true); + } + } + + @TestMetadata("compiler/tests-spec/testData/diagnostics/notLinked/contracts/analysis/controlFlow/unreachableCode/pos") + @TestDataPath("$PROJECT_ROOT") + @RunWith(JUnit3RunnerWithInners.class) + public static class Pos extends AbstractDiagnosticsTestSpec { + private void runTest(String testDataFilePath) throws Exception { + KotlinTestUtils.runTest(this::doTest, TargetBackend.ANY, testDataFilePath); + } + + @TestMetadata("1.kt") + public void test1() throws Exception { + runTest("compiler/tests-spec/testData/diagnostics/notLinked/contracts/analysis/controlFlow/unreachableCode/pos/1.kt"); + } + + @TestMetadata("2.kt") + public void test2() throws Exception { + runTest("compiler/tests-spec/testData/diagnostics/notLinked/contracts/analysis/controlFlow/unreachableCode/pos/2.kt"); + } + + @TestMetadata("3.kt") + public void test3() throws Exception { + runTest("compiler/tests-spec/testData/diagnostics/notLinked/contracts/analysis/controlFlow/unreachableCode/pos/3.kt"); + } + + @TestMetadata("4.kt") + public void test4() throws Exception { + runTest("compiler/tests-spec/testData/diagnostics/notLinked/contracts/analysis/controlFlow/unreachableCode/pos/4.kt"); + } + + @TestMetadata("5.kt") + public void test5() throws Exception { + runTest("compiler/tests-spec/testData/diagnostics/notLinked/contracts/analysis/controlFlow/unreachableCode/pos/5.kt"); + } + + @TestMetadata("6.kt") + public void test6() throws Exception { + runTest("compiler/tests-spec/testData/diagnostics/notLinked/contracts/analysis/controlFlow/unreachableCode/pos/6.kt"); + } + + @TestMetadata("7.kt") + public void test7() throws Exception { + runTest("compiler/tests-spec/testData/diagnostics/notLinked/contracts/analysis/controlFlow/unreachableCode/pos/7.kt"); + } + + public void testAllFilesPresentInPos() throws Exception { + KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("compiler/tests-spec/testData/diagnostics/notLinked/contracts/analysis/controlFlow/unreachableCode/pos"), Pattern.compile("^(.+)\\.kt$"), TargetBackend.ANY, true); + } + } + } + } + + @TestMetadata("compiler/tests-spec/testData/diagnostics/notLinked/contracts/analysis/smartcasts") + @TestDataPath("$PROJECT_ROOT") + @RunWith(JUnit3RunnerWithInners.class) + public static class Smartcasts extends AbstractDiagnosticsTestSpec { + private void runTest(String testDataFilePath) throws Exception { + KotlinTestUtils.runTest(this::doTest, TargetBackend.ANY, testDataFilePath); + } + + public void testAllFilesPresentInSmartcasts() throws Exception { + KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("compiler/tests-spec/testData/diagnostics/notLinked/contracts/analysis/smartcasts"), Pattern.compile("^(.+)\\.kt$"), TargetBackend.ANY, true); + } + + @TestMetadata("compiler/tests-spec/testData/diagnostics/notLinked/contracts/analysis/smartcasts/neg") + @TestDataPath("$PROJECT_ROOT") + @RunWith(JUnit3RunnerWithInners.class) + public static class Neg extends AbstractDiagnosticsTestSpec { + private void runTest(String testDataFilePath) throws Exception { + KotlinTestUtils.runTest(this::doTest, TargetBackend.ANY, testDataFilePath); + } + + @TestMetadata("1.kt") + public void test1() throws Exception { + runTest("compiler/tests-spec/testData/diagnostics/notLinked/contracts/analysis/smartcasts/neg/1.kt"); + } + + @TestMetadata("10.kt") + public void test10() throws Exception { + runTest("compiler/tests-spec/testData/diagnostics/notLinked/contracts/analysis/smartcasts/neg/10.kt"); + } + + @TestMetadata("11.kt") + public void test11() throws Exception { + runTest("compiler/tests-spec/testData/diagnostics/notLinked/contracts/analysis/smartcasts/neg/11.kt"); + } + + @TestMetadata("12.kt") + public void test12() throws Exception { + runTest("compiler/tests-spec/testData/diagnostics/notLinked/contracts/analysis/smartcasts/neg/12.kt"); + } + + @TestMetadata("13.kt") + public void test13() throws Exception { + runTest("compiler/tests-spec/testData/diagnostics/notLinked/contracts/analysis/smartcasts/neg/13.kt"); + } + + @TestMetadata("14.kt") + public void test14() throws Exception { + runTest("compiler/tests-spec/testData/diagnostics/notLinked/contracts/analysis/smartcasts/neg/14.kt"); + } + + @TestMetadata("2.kt") + public void test2() throws Exception { + runTest("compiler/tests-spec/testData/diagnostics/notLinked/contracts/analysis/smartcasts/neg/2.kt"); + } + + @TestMetadata("3.kt") + public void test3() throws Exception { + runTest("compiler/tests-spec/testData/diagnostics/notLinked/contracts/analysis/smartcasts/neg/3.kt"); + } + + @TestMetadata("4.kt") + public void test4() throws Exception { + runTest("compiler/tests-spec/testData/diagnostics/notLinked/contracts/analysis/smartcasts/neg/4.kt"); + } + + @TestMetadata("5.kt") + public void test5() throws Exception { + runTest("compiler/tests-spec/testData/diagnostics/notLinked/contracts/analysis/smartcasts/neg/5.kt"); + } + + @TestMetadata("6.kt") + public void test6() throws Exception { + runTest("compiler/tests-spec/testData/diagnostics/notLinked/contracts/analysis/smartcasts/neg/6.kt"); + } + + @TestMetadata("7.kt") + public void test7() throws Exception { + runTest("compiler/tests-spec/testData/diagnostics/notLinked/contracts/analysis/smartcasts/neg/7.kt"); + } + + @TestMetadata("8.kt") + public void test8() throws Exception { + runTest("compiler/tests-spec/testData/diagnostics/notLinked/contracts/analysis/smartcasts/neg/8.kt"); + } + + @TestMetadata("9.kt") + public void test9() throws Exception { + runTest("compiler/tests-spec/testData/diagnostics/notLinked/contracts/analysis/smartcasts/neg/9.kt"); + } + + public void testAllFilesPresentInNeg() throws Exception { + KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("compiler/tests-spec/testData/diagnostics/notLinked/contracts/analysis/smartcasts/neg"), Pattern.compile("^(.+)\\.kt$"), TargetBackend.ANY, true); + } + } + + @TestMetadata("compiler/tests-spec/testData/diagnostics/notLinked/contracts/analysis/smartcasts/pos") + @TestDataPath("$PROJECT_ROOT") + @RunWith(JUnit3RunnerWithInners.class) + public static class Pos extends AbstractDiagnosticsTestSpec { + private void runTest(String testDataFilePath) throws Exception { + KotlinTestUtils.runTest(this::doTest, TargetBackend.ANY, testDataFilePath); + } + + @TestMetadata("1.kt") + public void test1() throws Exception { + runTest("compiler/tests-spec/testData/diagnostics/notLinked/contracts/analysis/smartcasts/pos/1.kt"); + } + + @TestMetadata("10.kt") + public void test10() throws Exception { + runTest("compiler/tests-spec/testData/diagnostics/notLinked/contracts/analysis/smartcasts/pos/10.kt"); + } + + @TestMetadata("2.kt") + public void test2() throws Exception { + runTest("compiler/tests-spec/testData/diagnostics/notLinked/contracts/analysis/smartcasts/pos/2.kt"); + } + + @TestMetadata("3.kt") + public void test3() throws Exception { + runTest("compiler/tests-spec/testData/diagnostics/notLinked/contracts/analysis/smartcasts/pos/3.kt"); + } + + @TestMetadata("4.kt") + public void test4() throws Exception { + runTest("compiler/tests-spec/testData/diagnostics/notLinked/contracts/analysis/smartcasts/pos/4.kt"); + } + + @TestMetadata("5.kt") + public void test5() throws Exception { + runTest("compiler/tests-spec/testData/diagnostics/notLinked/contracts/analysis/smartcasts/pos/5.kt"); + } + + @TestMetadata("6.kt") + public void test6() throws Exception { + runTest("compiler/tests-spec/testData/diagnostics/notLinked/contracts/analysis/smartcasts/pos/6.kt"); + } + + @TestMetadata("7.kt") + public void test7() throws Exception { + runTest("compiler/tests-spec/testData/diagnostics/notLinked/contracts/analysis/smartcasts/pos/7.kt"); + } + + @TestMetadata("8.kt") + public void test8() throws Exception { + runTest("compiler/tests-spec/testData/diagnostics/notLinked/contracts/analysis/smartcasts/pos/8.kt"); + } + + @TestMetadata("9.kt") + public void test9() throws Exception { + runTest("compiler/tests-spec/testData/diagnostics/notLinked/contracts/analysis/smartcasts/pos/9.kt"); + } + + public void testAllFilesPresentInPos() throws Exception { + KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("compiler/tests-spec/testData/diagnostics/notLinked/contracts/analysis/smartcasts/pos"), Pattern.compile("^(.+)\\.kt$"), TargetBackend.ANY, true); + } + } + } + } + + @TestMetadata("compiler/tests-spec/testData/diagnostics/notLinked/contracts/declarations") + @TestDataPath("$PROJECT_ROOT") + @RunWith(JUnit3RunnerWithInners.class) + public static class Declarations extends AbstractDiagnosticsTestSpec { + private void runTest(String testDataFilePath) throws Exception { + KotlinTestUtils.runTest(this::doTest, TargetBackend.ANY, testDataFilePath); + } + + public void testAllFilesPresentInDeclarations() throws Exception { + KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("compiler/tests-spec/testData/diagnostics/notLinked/contracts/declarations"), Pattern.compile("^(.+)\\.kt$"), TargetBackend.ANY, true); + } + + @TestMetadata("compiler/tests-spec/testData/diagnostics/notLinked/contracts/declarations/contractBuilder") + @TestDataPath("$PROJECT_ROOT") + @RunWith(JUnit3RunnerWithInners.class) + public static class ContractBuilder extends AbstractDiagnosticsTestSpec { + private void runTest(String testDataFilePath) throws Exception { + KotlinTestUtils.runTest(this::doTest, TargetBackend.ANY, testDataFilePath); + } + + public void testAllFilesPresentInContractBuilder() throws Exception { + KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("compiler/tests-spec/testData/diagnostics/notLinked/contracts/declarations/contractBuilder"), Pattern.compile("^(.+)\\.kt$"), TargetBackend.ANY, true); + } + + @TestMetadata("compiler/tests-spec/testData/diagnostics/notLinked/contracts/declarations/contractBuilder/common") + @TestDataPath("$PROJECT_ROOT") + @RunWith(JUnit3RunnerWithInners.class) + public static class Common extends AbstractDiagnosticsTestSpec { + private void runTest(String testDataFilePath) throws Exception { + KotlinTestUtils.runTest(this::doTest, TargetBackend.ANY, testDataFilePath); + } + + public void testAllFilesPresentInCommon() throws Exception { + KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("compiler/tests-spec/testData/diagnostics/notLinked/contracts/declarations/contractBuilder/common"), Pattern.compile("^(.+)\\.kt$"), TargetBackend.ANY, true); + } + + @TestMetadata("compiler/tests-spec/testData/diagnostics/notLinked/contracts/declarations/contractBuilder/common/neg") + @TestDataPath("$PROJECT_ROOT") + @RunWith(JUnit3RunnerWithInners.class) + public static class Neg extends AbstractDiagnosticsTestSpec { + private void runTest(String testDataFilePath) throws Exception { + KotlinTestUtils.runTest(this::doTest, TargetBackend.ANY, testDataFilePath); + } + + @TestMetadata("1.kt") + public void test1() throws Exception { + runTest("compiler/tests-spec/testData/diagnostics/notLinked/contracts/declarations/contractBuilder/common/neg/1.kt"); + } + + @TestMetadata("10.kt") + public void test10() throws Exception { + runTest("compiler/tests-spec/testData/diagnostics/notLinked/contracts/declarations/contractBuilder/common/neg/10.kt"); + } + + @TestMetadata("11.kt") + public void test11() throws Exception { + runTest("compiler/tests-spec/testData/diagnostics/notLinked/contracts/declarations/contractBuilder/common/neg/11.kt"); + } + + @TestMetadata("12.kt") + public void test12() throws Exception { + runTest("compiler/tests-spec/testData/diagnostics/notLinked/contracts/declarations/contractBuilder/common/neg/12.kt"); + } + + @TestMetadata("2.kt") + public void test2() throws Exception { + runTest("compiler/tests-spec/testData/diagnostics/notLinked/contracts/declarations/contractBuilder/common/neg/2.kt"); + } + + @TestMetadata("3.kt") + public void test3() throws Exception { + runTest("compiler/tests-spec/testData/diagnostics/notLinked/contracts/declarations/contractBuilder/common/neg/3.kt"); + } + + @TestMetadata("4.kt") + public void test4() throws Exception { + runTest("compiler/tests-spec/testData/diagnostics/notLinked/contracts/declarations/contractBuilder/common/neg/4.kt"); + } + + @TestMetadata("5.kt") + public void test5() throws Exception { + runTest("compiler/tests-spec/testData/diagnostics/notLinked/contracts/declarations/contractBuilder/common/neg/5.kt"); + } + + @TestMetadata("6.kt") + public void test6() throws Exception { + runTest("compiler/tests-spec/testData/diagnostics/notLinked/contracts/declarations/contractBuilder/common/neg/6.kt"); + } + + @TestMetadata("7.kt") + public void test7() throws Exception { + runTest("compiler/tests-spec/testData/diagnostics/notLinked/contracts/declarations/contractBuilder/common/neg/7.kt"); + } + + @TestMetadata("8.kt") + public void test8() throws Exception { + runTest("compiler/tests-spec/testData/diagnostics/notLinked/contracts/declarations/contractBuilder/common/neg/8.kt"); + } + + @TestMetadata("9.kt") + public void test9() throws Exception { + runTest("compiler/tests-spec/testData/diagnostics/notLinked/contracts/declarations/contractBuilder/common/neg/9.kt"); + } + + public void testAllFilesPresentInNeg() throws Exception { + KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("compiler/tests-spec/testData/diagnostics/notLinked/contracts/declarations/contractBuilder/common/neg"), Pattern.compile("^(.+)\\.kt$"), TargetBackend.ANY, true); + } + } + + @TestMetadata("compiler/tests-spec/testData/diagnostics/notLinked/contracts/declarations/contractBuilder/common/pos") + @TestDataPath("$PROJECT_ROOT") + @RunWith(JUnit3RunnerWithInners.class) + public static class Pos extends AbstractDiagnosticsTestSpec { + private void runTest(String testDataFilePath) throws Exception { + KotlinTestUtils.runTest(this::doTest, TargetBackend.ANY, testDataFilePath); + } + + @TestMetadata("1.kt") + public void test1() throws Exception { + runTest("compiler/tests-spec/testData/diagnostics/notLinked/contracts/declarations/contractBuilder/common/pos/1.kt"); + } + + @TestMetadata("2.kt") + public void test2() throws Exception { + runTest("compiler/tests-spec/testData/diagnostics/notLinked/contracts/declarations/contractBuilder/common/pos/2.kt"); + } + + @TestMetadata("4.kt") + public void test4() throws Exception { + runTest("compiler/tests-spec/testData/diagnostics/notLinked/contracts/declarations/contractBuilder/common/pos/4.kt"); + } + + @TestMetadata("7.kt") + public void test7() throws Exception { + runTest("compiler/tests-spec/testData/diagnostics/notLinked/contracts/declarations/contractBuilder/common/pos/7.kt"); + } + + @TestMetadata("8.kt") + public void test8() throws Exception { + runTest("compiler/tests-spec/testData/diagnostics/notLinked/contracts/declarations/contractBuilder/common/pos/8.kt"); + } + + @TestMetadata("9.kt") + public void test9() throws Exception { + runTest("compiler/tests-spec/testData/diagnostics/notLinked/contracts/declarations/contractBuilder/common/pos/9.kt"); + } + + public void testAllFilesPresentInPos() throws Exception { + KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("compiler/tests-spec/testData/diagnostics/notLinked/contracts/declarations/contractBuilder/common/pos"), Pattern.compile("^(.+)\\.kt$"), TargetBackend.ANY, true); + } + } + } + + @TestMetadata("compiler/tests-spec/testData/diagnostics/notLinked/contracts/declarations/contractBuilder/effects") + @TestDataPath("$PROJECT_ROOT") + @RunWith(JUnit3RunnerWithInners.class) + public static class Effects extends AbstractDiagnosticsTestSpec { + private void runTest(String testDataFilePath) throws Exception { + KotlinTestUtils.runTest(this::doTest, TargetBackend.ANY, testDataFilePath); + } + + public void testAllFilesPresentInEffects() throws Exception { + KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("compiler/tests-spec/testData/diagnostics/notLinked/contracts/declarations/contractBuilder/effects"), Pattern.compile("^(.+)\\.kt$"), TargetBackend.ANY, true); + } + + @TestMetadata("compiler/tests-spec/testData/diagnostics/notLinked/contracts/declarations/contractBuilder/effects/callsInPlace") + @TestDataPath("$PROJECT_ROOT") + @RunWith(JUnit3RunnerWithInners.class) + public static class CallsInPlace extends AbstractDiagnosticsTestSpec { + private void runTest(String testDataFilePath) throws Exception { + KotlinTestUtils.runTest(this::doTest, TargetBackend.ANY, testDataFilePath); + } + + public void testAllFilesPresentInCallsInPlace() throws Exception { + KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("compiler/tests-spec/testData/diagnostics/notLinked/contracts/declarations/contractBuilder/effects/callsInPlace"), Pattern.compile("^(.+)\\.kt$"), TargetBackend.ANY, true); + } + + @TestMetadata("compiler/tests-spec/testData/diagnostics/notLinked/contracts/declarations/contractBuilder/effects/callsInPlace/neg") + @TestDataPath("$PROJECT_ROOT") + @RunWith(JUnit3RunnerWithInners.class) + public static class Neg extends AbstractDiagnosticsTestSpec { + private void runTest(String testDataFilePath) throws Exception { + KotlinTestUtils.runTest(this::doTest, TargetBackend.ANY, testDataFilePath); + } + + @TestMetadata("1.kt") + public void test1() throws Exception { + runTest("compiler/tests-spec/testData/diagnostics/notLinked/contracts/declarations/contractBuilder/effects/callsInPlace/neg/1.kt"); + } + + public void testAllFilesPresentInNeg() throws Exception { + KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("compiler/tests-spec/testData/diagnostics/notLinked/contracts/declarations/contractBuilder/effects/callsInPlace/neg"), Pattern.compile("^(.+)\\.kt$"), TargetBackend.ANY, true); + } + } + + @TestMetadata("compiler/tests-spec/testData/diagnostics/notLinked/contracts/declarations/contractBuilder/effects/callsInPlace/pos") + @TestDataPath("$PROJECT_ROOT") + @RunWith(JUnit3RunnerWithInners.class) + public static class Pos extends AbstractDiagnosticsTestSpec { + private void runTest(String testDataFilePath) throws Exception { + KotlinTestUtils.runTest(this::doTest, TargetBackend.ANY, testDataFilePath); + } + + @TestMetadata("1.kt") + public void test1() throws Exception { + runTest("compiler/tests-spec/testData/diagnostics/notLinked/contracts/declarations/contractBuilder/effects/callsInPlace/pos/1.kt"); + } + + @TestMetadata("2.kt") + public void test2() throws Exception { + runTest("compiler/tests-spec/testData/diagnostics/notLinked/contracts/declarations/contractBuilder/effects/callsInPlace/pos/2.kt"); + } + + @TestMetadata("3.kt") + public void test3() throws Exception { + runTest("compiler/tests-spec/testData/diagnostics/notLinked/contracts/declarations/contractBuilder/effects/callsInPlace/pos/3.kt"); + } + + public void testAllFilesPresentInPos() throws Exception { + KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("compiler/tests-spec/testData/diagnostics/notLinked/contracts/declarations/contractBuilder/effects/callsInPlace/pos"), Pattern.compile("^(.+)\\.kt$"), TargetBackend.ANY, true); + } + } + } + + @TestMetadata("compiler/tests-spec/testData/diagnostics/notLinked/contracts/declarations/contractBuilder/effects/common") + @TestDataPath("$PROJECT_ROOT") + @RunWith(JUnit3RunnerWithInners.class) + public static class Common extends AbstractDiagnosticsTestSpec { + private void runTest(String testDataFilePath) throws Exception { + KotlinTestUtils.runTest(this::doTest, TargetBackend.ANY, testDataFilePath); + } + + public void testAllFilesPresentInCommon() throws Exception { + KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("compiler/tests-spec/testData/diagnostics/notLinked/contracts/declarations/contractBuilder/effects/common"), Pattern.compile("^(.+)\\.kt$"), TargetBackend.ANY, true); + } + + @TestMetadata("compiler/tests-spec/testData/diagnostics/notLinked/contracts/declarations/contractBuilder/effects/common/neg") + @TestDataPath("$PROJECT_ROOT") + @RunWith(JUnit3RunnerWithInners.class) + public static class Neg extends AbstractDiagnosticsTestSpec { + private void runTest(String testDataFilePath) throws Exception { + KotlinTestUtils.runTest(this::doTest, TargetBackend.ANY, testDataFilePath); + } + + @TestMetadata("1.kt") + public void test1() throws Exception { + runTest("compiler/tests-spec/testData/diagnostics/notLinked/contracts/declarations/contractBuilder/effects/common/neg/1.kt"); + } + + public void testAllFilesPresentInNeg() throws Exception { + KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("compiler/tests-spec/testData/diagnostics/notLinked/contracts/declarations/contractBuilder/effects/common/neg"), Pattern.compile("^(.+)\\.kt$"), TargetBackend.ANY, true); + } + } + } + + @TestMetadata("compiler/tests-spec/testData/diagnostics/notLinked/contracts/declarations/contractBuilder/effects/returns") + @TestDataPath("$PROJECT_ROOT") + @RunWith(JUnit3RunnerWithInners.class) + public static class Returns extends AbstractDiagnosticsTestSpec { + private void runTest(String testDataFilePath) throws Exception { + KotlinTestUtils.runTest(this::doTest, TargetBackend.ANY, testDataFilePath); + } + + public void testAllFilesPresentInReturns() throws Exception { + KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("compiler/tests-spec/testData/diagnostics/notLinked/contracts/declarations/contractBuilder/effects/returns"), Pattern.compile("^(.+)\\.kt$"), TargetBackend.ANY, true); + } + + @TestMetadata("compiler/tests-spec/testData/diagnostics/notLinked/contracts/declarations/contractBuilder/effects/returns/neg") + @TestDataPath("$PROJECT_ROOT") + @RunWith(JUnit3RunnerWithInners.class) + public static class Neg extends AbstractDiagnosticsTestSpec { + private void runTest(String testDataFilePath) throws Exception { + KotlinTestUtils.runTest(this::doTest, TargetBackend.ANY, testDataFilePath); + } + + @TestMetadata("1.kt") + public void test1() throws Exception { + runTest("compiler/tests-spec/testData/diagnostics/notLinked/contracts/declarations/contractBuilder/effects/returns/neg/1.kt"); + } + + @TestMetadata("2.kt") + public void test2() throws Exception { + runTest("compiler/tests-spec/testData/diagnostics/notLinked/contracts/declarations/contractBuilder/effects/returns/neg/2.kt"); + } + + @TestMetadata("3.kt") + public void test3() throws Exception { + runTest("compiler/tests-spec/testData/diagnostics/notLinked/contracts/declarations/contractBuilder/effects/returns/neg/3.kt"); + } + + @TestMetadata("4.kt") + public void test4() throws Exception { + runTest("compiler/tests-spec/testData/diagnostics/notLinked/contracts/declarations/contractBuilder/effects/returns/neg/4.kt"); + } + + @TestMetadata("5.kt") + public void test5() throws Exception { + runTest("compiler/tests-spec/testData/diagnostics/notLinked/contracts/declarations/contractBuilder/effects/returns/neg/5.kt"); + } + + @TestMetadata("6.kt") + public void test6() throws Exception { + runTest("compiler/tests-spec/testData/diagnostics/notLinked/contracts/declarations/contractBuilder/effects/returns/neg/6.kt"); + } + + @TestMetadata("7.kt") + public void test7() throws Exception { + runTest("compiler/tests-spec/testData/diagnostics/notLinked/contracts/declarations/contractBuilder/effects/returns/neg/7.kt"); + } + + public void testAllFilesPresentInNeg() throws Exception { + KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("compiler/tests-spec/testData/diagnostics/notLinked/contracts/declarations/contractBuilder/effects/returns/neg"), Pattern.compile("^(.+)\\.kt$"), TargetBackend.ANY, true); + } + } + + @TestMetadata("compiler/tests-spec/testData/diagnostics/notLinked/contracts/declarations/contractBuilder/effects/returns/pos") + @TestDataPath("$PROJECT_ROOT") + @RunWith(JUnit3RunnerWithInners.class) + public static class Pos extends AbstractDiagnosticsTestSpec { + private void runTest(String testDataFilePath) throws Exception { + KotlinTestUtils.runTest(this::doTest, TargetBackend.ANY, testDataFilePath); + } + + @TestMetadata("3.kt") + public void test3() throws Exception { + runTest("compiler/tests-spec/testData/diagnostics/notLinked/contracts/declarations/contractBuilder/effects/returns/pos/3.kt"); + } + + @TestMetadata("4.kt") + public void test4() throws Exception { + runTest("compiler/tests-spec/testData/diagnostics/notLinked/contracts/declarations/contractBuilder/effects/returns/pos/4.kt"); + } + + @TestMetadata("5.kt") + public void test5() throws Exception { + runTest("compiler/tests-spec/testData/diagnostics/notLinked/contracts/declarations/contractBuilder/effects/returns/pos/5.kt"); + } + + public void testAllFilesPresentInPos() throws Exception { + KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("compiler/tests-spec/testData/diagnostics/notLinked/contracts/declarations/contractBuilder/effects/returns/pos"), Pattern.compile("^(.+)\\.kt$"), TargetBackend.ANY, true); + } + } + } + } + } + + @TestMetadata("compiler/tests-spec/testData/diagnostics/notLinked/contracts/declarations/contractFunction") + @TestDataPath("$PROJECT_ROOT") + @RunWith(JUnit3RunnerWithInners.class) + public static class ContractFunction extends AbstractDiagnosticsTestSpec { + private void runTest(String testDataFilePath) throws Exception { + KotlinTestUtils.runTest(this::doTest, TargetBackend.ANY, testDataFilePath); + } + + public void testAllFilesPresentInContractFunction() throws Exception { + KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("compiler/tests-spec/testData/diagnostics/notLinked/contracts/declarations/contractFunction"), Pattern.compile("^(.+)\\.kt$"), TargetBackend.ANY, true); + } + + @TestMetadata("compiler/tests-spec/testData/diagnostics/notLinked/contracts/declarations/contractFunction/neg") + @TestDataPath("$PROJECT_ROOT") + @RunWith(JUnit3RunnerWithInners.class) + public static class Neg extends AbstractDiagnosticsTestSpec { + private void runTest(String testDataFilePath) throws Exception { + KotlinTestUtils.runTest(this::doTest, TargetBackend.ANY, testDataFilePath); + } + + @TestMetadata("1.kt") + public void test1() throws Exception { + runTest("compiler/tests-spec/testData/diagnostics/notLinked/contracts/declarations/contractFunction/neg/1.kt"); + } + + @TestMetadata("2.kt") + public void test2() throws Exception { + runTest("compiler/tests-spec/testData/diagnostics/notLinked/contracts/declarations/contractFunction/neg/2.kt"); + } + + public void testAllFilesPresentInNeg() throws Exception { + KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("compiler/tests-spec/testData/diagnostics/notLinked/contracts/declarations/contractFunction/neg"), Pattern.compile("^(.+)\\.kt$"), TargetBackend.ANY, true); + } + } + + @TestMetadata("compiler/tests-spec/testData/diagnostics/notLinked/contracts/declarations/contractFunction/pos") + @TestDataPath("$PROJECT_ROOT") + @RunWith(JUnit3RunnerWithInners.class) + public static class Pos extends AbstractDiagnosticsTestSpec { + private void runTest(String testDataFilePath) throws Exception { + KotlinTestUtils.runTest(this::doTest, TargetBackend.ANY, testDataFilePath); + } + + @TestMetadata("1.kt") + public void test1() throws Exception { + runTest("compiler/tests-spec/testData/diagnostics/notLinked/contracts/declarations/contractFunction/pos/1.kt"); + } + + @TestMetadata("2.kt") + public void test2() throws Exception { + runTest("compiler/tests-spec/testData/diagnostics/notLinked/contracts/declarations/contractFunction/pos/2.kt"); + } + + public void testAllFilesPresentInPos() throws Exception { + KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("compiler/tests-spec/testData/diagnostics/notLinked/contracts/declarations/contractFunction/pos"), Pattern.compile("^(.+)\\.kt$"), TargetBackend.ANY, true); + } + } + } + } + } + } }