Allow contracts on final non-override members since 1.4

This commit is contained in:
Dmitriy Novozhilov
2019-12-26 11:53:27 +03:00
parent 5a9070b6da
commit f083edfac2
34 changed files with 682 additions and 45 deletions
@@ -1,4 +1,4 @@
// !LANGUAGE: +AllowContractsForCustomFunctions +UseReturnsEffect
// !LANGUAGE: +AllowContractsForCustomFunctions +UseReturnsEffect -AllowContractsForNonOverridableMembers
// !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
@@ -1,4 +1,4 @@
// !LANGUAGE: +AllowContractsForCustomFunctions +UseReturnsEffect
// !LANGUAGE: +AllowContractsForCustomFunctions +UseReturnsEffect -AllowContractsForNonOverridableMembers
// !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
@@ -0,0 +1,101 @@
// !LANGUAGE: +AllowContractsForCustomFunctions +UseReturnsEffect +AllowContractsForNonOverridableMembers
// !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) }
}
}
open class Inheritor : Class() {
override fun openMemeber(x: Boolean) {
contract { returns() implies (x) }
}
final override fun abstractMember(x: Boolean) {
contract { returns() implies (x) }
}
}
interface Interface {
fun implicitlyOpenMember(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,101 @@
// !LANGUAGE: +AllowContractsForCustomFunctions +UseReturnsEffect +AllowContractsForNonOverridableMembers
// !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_NOT_ALLOWED!>contract<!> { returns() implies (x) }
}
open fun openMemeber(x: Boolean) {
<!CONTRACT_NOT_ALLOWED!>contract<!> { returns() implies (x) }
}
suspend fun suspendMember(x: Boolean) {
contract { returns() implies (x) }
}
}
open class Inheritor : Class() {
override fun openMemeber(x: Boolean) {
<!CONTRACT_NOT_ALLOWED!>contract<!> { returns() implies (x) }
}
final override fun abstractMember(x: Boolean) {
<!CONTRACT_NOT_ALLOWED!>contract<!> { returns() implies (x) }
}
}
interface Interface {
fun implicitlyOpenMember(x: Boolean) {
<!CONTRACT_NOT_ALLOWED!>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_NOT_ALLOWED!>contract<!> { returns() implies (x) }
return x
}
val topLevelLambda: (Boolean) -> Unit = { x: Boolean ->
<!CONTRACT_NOT_ALLOWED!>contract<!> { returns() implies (x) }
}
val topLevelAnonymousFunction = fun (x: Boolean) {
<!CONTRACT_NOT_ALLOWED!>contract<!> { returns() implies (x) }
}
var topLevelPropertyAccessors: Int? = 42
get() {
<!CONTRACT_NOT_ALLOWED, CONTRACT_NOT_ALLOWED!>contract<!> { returns() implies (field != null) }
return 42
}
set(value) {
<!CONTRACT_NOT_ALLOWED, CONTRACT_NOT_ALLOWED!>contract<!> { returns() implies (field != null) }
}
// ============= Local =====================
fun test() {
fun localDeclaration(x: Boolean) {
<!CONTRACT_NOT_ALLOWED!>contract<!> { returns() implies (x) }
}
suspend fun suspendlocalDeclaration(x: Boolean) {
<!CONTRACT_NOT_ALLOWED!>contract<!> { returns() implies (x) }
}
val localAnonymousFunction = fun (x: Boolean) {
<!CONTRACT_NOT_ALLOWED!>contract<!> { returns() implies (x) }
}
val localLambda: (Boolean) -> Unit = { x: Boolean ->
<!CONTRACT_NOT_ALLOWED!>contract<!> { returns() implies (x) }
}
}
@@ -0,0 +1,53 @@
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
public open class Class {
public constructor Class()
public abstract fun abstractMember(/*0*/ x: kotlin.Boolean): 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 final inline fun inlineMember(/*0*/ x: kotlin.Boolean): kotlin.Unit
public final fun member(/*0*/ x: kotlin.Boolean): kotlin.Unit
public open fun openMemeber(/*0*/ x: kotlin.Boolean): kotlin.Unit
public final suspend fun suspendMember(/*0*/ x: kotlin.Boolean): kotlin.Unit
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
}
public open class Inheritor : Class {
public constructor Inheritor()
public final override /*1*/ fun abstractMember(/*0*/ x: kotlin.Boolean): 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 final override /*1*/ inline /*fake_override*/ fun inlineMember(/*0*/ x: kotlin.Boolean): kotlin.Unit
Returns(WILDCARD) -> x
public final override /*1*/ /*fake_override*/ fun member(/*0*/ x: kotlin.Boolean): kotlin.Unit
Returns(WILDCARD) -> x
public open override /*1*/ fun openMemeber(/*0*/ x: kotlin.Boolean): kotlin.Unit
public final override /*1*/ suspend /*fake_override*/ fun suspendMember(/*0*/ x: kotlin.Boolean): kotlin.Unit
Returns(WILDCARD) -> x
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
}
public interface Interface {
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 fun implicitlyOpenMember(/*0*/ x: kotlin.Boolean): kotlin.Unit
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
}
@@ -1,4 +1,4 @@
// !LANGUAGE: +AllowContractsForCustomFunctions +UseReturnsEffect
// !LANGUAGE: +AllowContractsForCustomFunctions +UseReturnsEffect -AllowContractsForNonOverridableMembers
// !USE_EXPERIMENTAL: kotlin.contracts.ExperimentalContracts
// !DIAGNOSTICS: -INVISIBLE_REFERENCE -INVISIBLE_MEMBER
@@ -1,4 +1,4 @@
// !LANGUAGE: +AllowContractsForCustomFunctions +UseReturnsEffect
// !LANGUAGE: +AllowContractsForCustomFunctions +UseReturnsEffect -AllowContractsForNonOverridableMembers
// !USE_EXPERIMENTAL: kotlin.contracts.ExperimentalContracts
// !DIAGNOSTICS: -INVISIBLE_REFERENCE -INVISIBLE_MEMBER
@@ -0,0 +1,14 @@
// !LANGUAGE: +AllowContractsForCustomFunctions +UseReturnsEffect +AllowContractsForNonOverridableMembers
// !USE_EXPERIMENTAL: kotlin.contracts.ExperimentalContracts
// !DIAGNOSTICS: -INVISIBLE_REFERENCE -INVISIBLE_MEMBER
import kotlin.contracts.*
class Foo(val x: Int?) {
fun isXNull(): Boolean {
contract {
returns(false) implies (x != null)
}
return x != null
}
}
@@ -0,0 +1,14 @@
// !LANGUAGE: +AllowContractsForCustomFunctions +UseReturnsEffect +AllowContractsForNonOverridableMembers
// !USE_EXPERIMENTAL: kotlin.contracts.ExperimentalContracts
// !DIAGNOSTICS: -INVISIBLE_REFERENCE -INVISIBLE_MEMBER
import kotlin.contracts.*
class Foo(val x: Int?) {
fun isXNull(): Boolean {
contract {
returns(false) implies (<!ERROR_IN_CONTRACT_DESCRIPTION!>x<!> != null)
}
return x != null
}
}
@@ -0,0 +1,10 @@
package
public final class Foo {
public constructor Foo(/*0*/ x: kotlin.Int?)
public final val x: kotlin.Int?
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 isXNull(): kotlin.Boolean
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
}
@@ -1,4 +1,4 @@
// !LANGUAGE: +AllowContractsForCustomFunctions +UseReturnsEffect
// !LANGUAGE: +AllowContractsForCustomFunctions +UseReturnsEffect -AllowContractsForNonOverridableMembers
// !USE_EXPERIMENTAL: kotlin.contracts.ExperimentalContracts
// !DIAGNOSTICS: -INVISIBLE_REFERENCE -INVISIBLE_MEMBER
@@ -1,4 +1,4 @@
// !LANGUAGE: +AllowContractsForCustomFunctions +UseReturnsEffect
// !LANGUAGE: +AllowContractsForCustomFunctions +UseReturnsEffect -AllowContractsForNonOverridableMembers
// !USE_EXPERIMENTAL: kotlin.contracts.ExperimentalContracts
// !DIAGNOSTICS: -INVISIBLE_REFERENCE -INVISIBLE_MEMBER
@@ -0,0 +1,53 @@
// !LANGUAGE: +AllowContractsForCustomFunctions +UseReturnsEffect +AllowContractsForNonOverridableMembers
// !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 T)
}
}
}
fun referToSubstituted(x: Any?) {
contract {
returns() implies (x is 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 GenericString)
}
}
fun referToAliasedFunctionType(x: Any?) {
contract {
returns() implies (x is FunctionalType)
}
}
fun referToAliasedSimpleType(x: Any?) {
contract {
returns() implies (x is SimpleType)
}
}
@@ -0,0 +1,53 @@
// !LANGUAGE: +AllowContractsForCustomFunctions +UseReturnsEffect +AllowContractsForNonOverridableMembers
// !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 <!ERROR_IN_CONTRACT_DESCRIPTION("references to type parameters are forbidden in contracts")!>T<!>)
}
}
class Generic<T> {
fun referToCaptured(x: Any?) {
contract {
returns() implies (x is <!CANNOT_CHECK_FOR_ERASED, ERROR_IN_CONTRACT_DESCRIPTION!>T<!>)
}
}
}
fun referToSubstituted(x: Any?) {
<!ERROR_IN_CONTRACT_DESCRIPTION("Error in contract description")!>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?) {
<!ERROR_IN_CONTRACT_DESCRIPTION("Error in contract description")!>contract<!> {
returns() implies (x is <!CANNOT_CHECK_FOR_ERASED!>GenericString<!>)
}
}
fun referToAliasedFunctionType(x: Any?) {
<!ERROR_IN_CONTRACT_DESCRIPTION("Error in contract description")!>contract<!> {
returns() implies (x is <!CANNOT_CHECK_FOR_ERASED!>FunctionalType<!>)
}
}
fun referToAliasedSimpleType(x: Any?) {
contract {
returns() implies (x is SimpleType)
}
}
@@ -0,0 +1,22 @@
package
public fun referToAliasedFunctionType(/*0*/ x: kotlin.Any?): kotlin.Unit
public fun referToAliasedGeneric(/*0*/ x: kotlin.Any?): kotlin.Unit
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
public fun referToSubstituted(/*0*/ x: kotlin.Any?): kotlin.Unit
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
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
@@ -0,0 +1,31 @@
// !LANGUAGE: +AllowContractsForCustomFunctions +UseReturnsEffect +AllowContractsForNonOverridableMembers
// !USE_EXPERIMENTAL: kotlin.contracts.ExperimentalContracts
// !DIAGNOSTICS: -INVISIBLE_REFERENCE -INVISIBLE_MEMBER
import kotlin.contracts.*
class Foo {
fun myRun(block: () -> Unit) {
contract {
<!INAPPLICABLE_CANDIDATE!>callsInPlace<!>(block, InvocationKind.EXACTLY_ONCE)
}
block()
}
fun require(x: Boolean) {
contract { returns() implies (x) }
}
}
fun test_1(foo: Foo, x: Any) {
foo.require(x is String)
x.<!UNRESOLVED_REFERENCE!>length<!>
}
fun test_2(foo: Foo): Int {
val x: Int
foo.myRun {
x = 1
}
return x + 1
}
@@ -0,0 +1,31 @@
// !LANGUAGE: +AllowContractsForCustomFunctions +UseReturnsEffect +AllowContractsForNonOverridableMembers
// !USE_EXPERIMENTAL: kotlin.contracts.ExperimentalContracts
// !DIAGNOSTICS: -INVISIBLE_REFERENCE -INVISIBLE_MEMBER
import kotlin.contracts.*
class Foo {
fun myRun(block: () -> Unit) {
contract {
callsInPlace(block, InvocationKind.EXACTLY_ONCE)
}
block()
}
fun require(x: Boolean) {
contract { returns() implies (x) }
}
}
fun test_1(foo: Foo, x: Any) {
foo.require(x is String)
<!DEBUG_INFO_SMARTCAST!>x<!>.length
}
fun test_2(foo: Foo): Int {
val x: Int
foo.myRun {
x = 1
}
return x + 1
}
@@ -0,0 +1,17 @@
package
public fun test_1(/*0*/ foo: Foo, /*1*/ x: kotlin.Any): kotlin.Unit
public fun test_2(/*0*/ foo: Foo): kotlin.Int
public final class Foo {
public constructor Foo()
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 myRun(/*0*/ block: () -> kotlin.Unit): kotlin.Unit
CallsInPlace(block, EXACTLY_ONCE)
public final fun require(/*0*/ x: kotlin.Boolean): kotlin.Unit
Returns(WILDCARD) -> x
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
}