Add tests on illegal usages of Contracts DSL

(currently with undesired behavior, it will be fixed in the next
commits)
This commit is contained in:
Dmitry Savvinov
2018-08-09 18:33:34 +03:00
committed by Ilya Gorbunov
parent 79476839c4
commit a6783176ea
14 changed files with 403 additions and 0 deletions
@@ -0,0 +1,9 @@
// !LANGUAGE: +AllowContractsForCustomFunctions +UseReturnsEffect
// !USE_EXPERIMENTAL: kotlin.contracts.ExperimentalContracts
// !DIAGNOSTICS: -INVISIBLE_REFERENCE -INVISIBLE_MEMBER
import kotlin.contracts.*
fun emptyContract() {
contract { }
}
@@ -0,0 +1,3 @@
package
public fun emptyContract(): kotlin.Unit
@@ -0,0 +1,86 @@
// !LANGUAGE: +AllowContractsForCustomFunctions +UseReturnsEffect
// !USE_EXPERIMENTAL: kotlin.contracts.ExperimentalContracts
// !DIAGNOSTICS: -INVISIBLE_REFERENCE -INVISIBLE_MEMBER -NOTHING_TO_INLINE -ABSTRACT_FUNCTION_IN_NON_ABSTRACT_CLASS -ABSTRACT_FUNCTION_WITH_BODY -UNUSED_PARAMETER -UNUSED_VARIABLE -EXPERIMENTAL_FEATURE_WARNING
import kotlin.contracts.*
// ============= Class =====================
open class Class {
fun member(x: Boolean) {
contract { returns() implies (x) }
}
inline fun inlineMember(x: Boolean) {
contract { returns() implies (x) }
}
abstract fun abstractMember(x: Boolean) {
contract { returns() implies (x) }
}
open fun openMemeber(x: Boolean) {
contract { returns() implies (x) }
}
suspend fun suspendMember(x: Boolean) {
contract { returns() implies (x) }
}
}
// ============= Top-level =====================
fun topLevel(x: Boolean) {
contract { returns() implies (x) }
}
inline fun inlineTopLevel(x: Boolean) {
contract { returns() implies (x) }
}
suspend fun suspendTopLevel(x: Boolean) {
contract { returns() implies (x) }
}
// Top-level operator
operator fun Boolean.plus(x: Boolean): Boolean {
contract { returns() implies (x) }
return x
}
val topLevelLambda: (Boolean) -> Unit = { x: Boolean ->
contract { returns() implies (x) }
}
val topLevelAnonymousFunction = fun (x: Boolean) {
contract { returns() implies (x) }
}
var topLevelPropertyAccessors: Int? = 42
get() {
contract { returns() implies (field != null) }
return 42
}
set(value) {
contract { returns() implies (field != null) }
}
// ============= Local =====================
fun test() {
fun localDeclaration(x: Boolean) {
contract { returns() implies (x) }
}
suspend fun suspendlocalDeclaration(x: Boolean) {
contract { returns() implies (x) }
}
val localAnonymousFunction = fun (x: Boolean) {
contract { returns() implies (x) }
}
val localLambda: (Boolean) -> Unit = { x: Boolean ->
contract { returns() implies (x) }
}
}
@@ -0,0 +1,39 @@
package
public val topLevelAnonymousFunction: (kotlin.Boolean) -> kotlin.Unit
public val topLevelLambda: (kotlin.Boolean) -> kotlin.Unit
public var topLevelPropertyAccessors: kotlin.Int?
public inline fun inlineTopLevel(/*0*/ x: kotlin.Boolean): kotlin.Unit
Returns(WILDCARD) -> x
public suspend fun suspendTopLevel(/*0*/ x: kotlin.Boolean): kotlin.Unit
Returns(WILDCARD) -> x
public fun test(): kotlin.Unit
public fun topLevel(/*0*/ x: kotlin.Boolean): kotlin.Unit
Returns(WILDCARD) -> x
public operator fun kotlin.Boolean.plus(/*0*/ x: kotlin.Boolean): kotlin.Boolean
Returns(WILDCARD) -> x
public open class Class {
public constructor Class()
public abstract fun abstractMember(/*0*/ x: kotlin.Boolean): kotlin.Unit
Returns(WILDCARD) -> x
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 final inline fun inlineMember(/*0*/ x: kotlin.Boolean): kotlin.Unit
Returns(WILDCARD) -> x
public final fun member(/*0*/ x: kotlin.Boolean): kotlin.Unit
Returns(WILDCARD) -> x
public open fun openMemeber(/*0*/ x: kotlin.Boolean): kotlin.Unit
Returns(WILDCARD) -> x
public final suspend fun suspendMember(/*0*/ x: kotlin.Boolean): kotlin.Unit
Returns(WILDCARD) -> x
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
}
@@ -0,0 +1,55 @@
// !LANGUAGE: +AllowContractsForCustomFunctions +UseReturnsEffect
// !USE_EXPERIMENTAL: kotlin.contracts.ExperimentalContracts
// !DIAGNOSTICS: -INVISIBLE_REFERENCE -INVISIBLE_MEMBER -NOTHING_TO_INLINE -ABSTRACT_FUNCTION_IN_NON_ABSTRACT_CLASS -ABSTRACT_FUNCTION_WITH_BODY -UNUSED_PARAMETER -UNUSED_VARIABLE -EXPERIMENTAL_FEATURE_WARNING
import kotlin.contracts.*
fun ifInContract(x: Any?, boolean: Boolean) {
contract {
if (boolean) {
returns() implies (x is String)
} else {
returns() implies (x is Int)
}
}
}
fun whenInContract(x: Any?, boolean: Boolean) {
contract {
when (boolean) {
true -> returns() implies (x is String)
else -> returns() implies (x is Int)
}
}
}
fun forInContract(x: Any?) {
contract {
for (i in 0..1) {
returns() implies (x is String)
}
}
}
fun whileInContract(x: Any?) {
contract {
while (false) {
returns() implies (x is String)
}
}
}
fun doWhileInContract(x: Any?) {
contract {
do {
returns() implies (x is String)
} while (false)
}
}
fun localValInContract(x: Any?) {
contract {
val y: Int = 42
returns() implies (x is String)
}
}
@@ -0,0 +1,10 @@
package
public fun doWhileInContract(/*0*/ x: kotlin.Any?): kotlin.Unit
public fun forInContract(/*0*/ x: kotlin.Any?): kotlin.Unit
public fun ifInContract(/*0*/ x: kotlin.Any?, /*1*/ boolean: kotlin.Boolean): kotlin.Unit
public fun localValInContract(/*0*/ x: kotlin.Any?): kotlin.Unit
Returns(WILDCARD) -> x is String
public fun whenInContract(/*0*/ x: kotlin.Any?, /*1*/ boolean: kotlin.Boolean): kotlin.Unit
public fun whileInContract(/*0*/ x: kotlin.Any?): kotlin.Unit
@@ -0,0 +1,30 @@
// !LANGUAGE: +AllowContractsForCustomFunctions +UseReturnsEffect
// !USE_EXPERIMENTAL: kotlin.contracts.ExperimentalContracts
// !DIAGNOSTICS: -INVISIBLE_REFERENCE -INVISIBLE_MEMBER
import kotlin.contracts.*
fun equalsWithVariables(x: Any?, y: Any?) {
contract {
returns() implies (<!ERROR_IN_CONTRACT_DESCRIPTION(only equality comparisons with 'null' allowed)!>x == y<!>)
}
}
fun identityEqualsWithVariables(x: Any?, y: Any?) {
contract {
returns() implies (x === y)
}
}
fun equalConstants() {
contract {
returns() implies (<!SENSELESS_COMPARISON, ERROR_IN_CONTRACT_DESCRIPTION(only equality comparisons with 'null' allowed)!>null == null<!>)
}
}
fun get(): Int? = null
fun equalNullWithCall() {
contract {
returns() implies (<!ERROR_IN_CONTRACT_DESCRIPTION(only references to parameters are allowed in contract description)!>get()<!> == null)
}
}
@@ -0,0 +1,7 @@
package
public fun equalConstants(): kotlin.Unit
public fun equalNullWithCall(): kotlin.Unit
public fun equalsWithVariables(/*0*/ x: kotlin.Any?, /*1*/ y: kotlin.Any?): kotlin.Unit
public fun get(): kotlin.Int?
public fun identityEqualsWithVariables(/*0*/ x: kotlin.Any?, /*1*/ y: kotlin.Any?): kotlin.Unit
@@ -0,0 +1,15 @@
// !LANGUAGE: +AllowContractsForCustomFunctions +UseReturnsEffect
// !USE_EXPERIMENTAL: kotlin.contracts.ExperimentalContracts
// !DIAGNOSTICS: -INVISIBLE_REFERENCE -INVISIBLE_MEMBER -EXPOSED_PARAMETER_TYPE
import kotlin.contracts.*
fun passLambdaValue(l: ContractBuilder.() -> Unit) {
contract(l)
}
fun passAnonymousFunction(x: Boolean) {
contract(fun ContractBuilder.() {
returns() implies x
})
}
@@ -0,0 +1,4 @@
package
public fun passAnonymousFunction(/*0*/ x: kotlin.Boolean): kotlin.Unit
public fun passLambdaValue(/*0*/ l: kotlin.contracts.ContractBuilder.() -> kotlin.Unit): kotlin.Unit
@@ -0,0 +1,53 @@
// !LANGUAGE: +AllowContractsForCustomFunctions +UseReturnsEffect
// !USE_EXPERIMENTAL: kotlin.contracts.ExperimentalContracts
// !DIAGNOSTICS: -INVISIBLE_REFERENCE -INVISIBLE_MEMBER
import kotlin.contracts.*
inline fun <reified T> referToReifiedGeneric(x: Any?) {
contract {
returns() implies (x is T)
}
}
class Generic<T> {
fun referToCaptured(x: Any?) {
contract {
returns() implies (x is <!CANNOT_CHECK_FOR_ERASED!>T<!>)
}
}
}
fun referToSubstituted(x: Any?) {
contract {
returns() implies (x is <!CANNOT_CHECK_FOR_ERASED!>Generic<String><!>)
}
}
fun referToSubstitutedWithStar(x: Any?) {
contract {
returns() implies (x is Generic<*>)
}
}
typealias GenericString = Generic<String>
typealias FunctionalType = () -> Unit
typealias SimpleType = Int
fun referToAliasedGeneric(x: Any?) {
contract {
returns() implies (x is <!CANNOT_CHECK_FOR_ERASED!>GenericString<!>)
}
}
fun referToAliasedFunctionType(x: Any?) {
contract {
returns() implies (x is <!CANNOT_CHECK_FOR_ERASED!>FunctionalType<!>)
}
}
fun referToAliasedSimpleType(x: Any?) {
contract {
returns() implies (x is SimpleType)
}
}
@@ -0,0 +1,32 @@
package
public fun referToAliasedFunctionType(/*0*/ x: kotlin.Any?): kotlin.Unit
Returns(WILDCARD) -> x is Function0<Unit>
public fun referToAliasedGeneric(/*0*/ x: kotlin.Any?): kotlin.Unit
Returns(WILDCARD) -> x is Generic<String>
public fun referToAliasedSimpleType(/*0*/ x: kotlin.Any?): kotlin.Unit
Returns(WILDCARD) -> x is Int
public inline fun </*0*/ reified T> referToReifiedGeneric(/*0*/ x: kotlin.Any?): kotlin.Unit
Returns(WILDCARD) -> x is T
public fun referToSubstituted(/*0*/ x: kotlin.Any?): kotlin.Unit
Returns(WILDCARD) -> x is Generic<String>
public fun referToSubstitutedWithStar(/*0*/ x: kotlin.Any?): kotlin.Unit
Returns(WILDCARD) -> x is Generic<*>
public final class Generic</*0*/ T> {
public constructor Generic</*0*/ T>()
public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int
public final fun referToCaptured(/*0*/ x: kotlin.Any?): kotlin.Unit
Returns(WILDCARD) -> x is T
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
}
public typealias FunctionalType = () -> kotlin.Unit
public typealias GenericString = Generic<kotlin.String>
public typealias SimpleType = kotlin.Int
@@ -1134,11 +1134,36 @@ public class DiagnosticsTestWithStdLibGenerated extends AbstractDiagnosticsTestW
runTest("compiler/testData/diagnostics/testsWithStdLib/contracts/dsl/errors/callInContractDescription.kt");
}
@TestMetadata("emptyContract.kt")
public void testEmptyContract() throws Exception {
runTest("compiler/testData/diagnostics/testsWithStdLib/contracts/dsl/errors/emptyContract.kt");
}
@TestMetadata("illegalCallSites.kt")
public void testIllegalCallSites() throws Exception {
runTest("compiler/testData/diagnostics/testsWithStdLib/contracts/dsl/errors/illegalCallSites.kt");
}
@TestMetadata("illegalConstructionInContractBlock.kt")
public void testIllegalConstructionInContractBlock() throws Exception {
runTest("compiler/testData/diagnostics/testsWithStdLib/contracts/dsl/errors/illegalConstructionInContractBlock.kt");
}
@TestMetadata("illegalEqualsCondition.kt")
public void testIllegalEqualsCondition() throws Exception {
runTest("compiler/testData/diagnostics/testsWithStdLib/contracts/dsl/errors/illegalEqualsCondition.kt");
}
@TestMetadata("nestedConditionalEffects.kt")
public void testNestedConditionalEffects() throws Exception {
runTest("compiler/testData/diagnostics/testsWithStdLib/contracts/dsl/errors/nestedConditionalEffects.kt");
}
@TestMetadata("nonLambdaLiteralAsArgument.kt")
public void testNonLambdaLiteralAsArgument() throws Exception {
runTest("compiler/testData/diagnostics/testsWithStdLib/contracts/dsl/errors/nonLambdaLiteralAsArgument.kt");
}
@TestMetadata("notFirstStatement.kt")
public void testNotFirstStatement() throws Exception {
runTest("compiler/testData/diagnostics/testsWithStdLib/contracts/dsl/errors/notFirstStatement.kt");
@@ -1149,6 +1174,11 @@ public class DiagnosticsTestWithStdLibGenerated extends AbstractDiagnosticsTestW
runTest("compiler/testData/diagnostics/testsWithStdLib/contracts/dsl/errors/referenceToProperty.kt");
}
@TestMetadata("typeReferences.kt")
public void testTypeReferences() throws Exception {
runTest("compiler/testData/diagnostics/testsWithStdLib/contracts/dsl/errors/typeReferences.kt");
}
@TestMetadata("unlabeledReceiver.kt")
public void testUnlabeledReceiver() throws Exception {
runTest("compiler/testData/diagnostics/testsWithStdLib/contracts/dsl/errors/unlabeledReceiver.kt");
@@ -1134,11 +1134,36 @@ public class DiagnosticsTestWithStdLibUsingJavacGenerated extends AbstractDiagno
runTest("compiler/testData/diagnostics/testsWithStdLib/contracts/dsl/errors/callInContractDescription.kt");
}
@TestMetadata("emptyContract.kt")
public void testEmptyContract() throws Exception {
runTest("compiler/testData/diagnostics/testsWithStdLib/contracts/dsl/errors/emptyContract.kt");
}
@TestMetadata("illegalCallSites.kt")
public void testIllegalCallSites() throws Exception {
runTest("compiler/testData/diagnostics/testsWithStdLib/contracts/dsl/errors/illegalCallSites.kt");
}
@TestMetadata("illegalConstructionInContractBlock.kt")
public void testIllegalConstructionInContractBlock() throws Exception {
runTest("compiler/testData/diagnostics/testsWithStdLib/contracts/dsl/errors/illegalConstructionInContractBlock.kt");
}
@TestMetadata("illegalEqualsCondition.kt")
public void testIllegalEqualsCondition() throws Exception {
runTest("compiler/testData/diagnostics/testsWithStdLib/contracts/dsl/errors/illegalEqualsCondition.kt");
}
@TestMetadata("nestedConditionalEffects.kt")
public void testNestedConditionalEffects() throws Exception {
runTest("compiler/testData/diagnostics/testsWithStdLib/contracts/dsl/errors/nestedConditionalEffects.kt");
}
@TestMetadata("nonLambdaLiteralAsArgument.kt")
public void testNonLambdaLiteralAsArgument() throws Exception {
runTest("compiler/testData/diagnostics/testsWithStdLib/contracts/dsl/errors/nonLambdaLiteralAsArgument.kt");
}
@TestMetadata("notFirstStatement.kt")
public void testNotFirstStatement() throws Exception {
runTest("compiler/testData/diagnostics/testsWithStdLib/contracts/dsl/errors/notFirstStatement.kt");
@@ -1149,6 +1174,11 @@ public class DiagnosticsTestWithStdLibUsingJavacGenerated extends AbstractDiagno
runTest("compiler/testData/diagnostics/testsWithStdLib/contracts/dsl/errors/referenceToProperty.kt");
}
@TestMetadata("typeReferences.kt")
public void testTypeReferences() throws Exception {
runTest("compiler/testData/diagnostics/testsWithStdLib/contracts/dsl/errors/typeReferences.kt");
}
@TestMetadata("unlabeledReceiver.kt")
public void testUnlabeledReceiver() throws Exception {
runTest("compiler/testData/diagnostics/testsWithStdLib/contracts/dsl/errors/unlabeledReceiver.kt");