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
@@ -1188,9 +1188,14 @@ public class FirOldFrontendDiagnosticsTestWithStdlibGenerated extends AbstractFi
runTest("compiler/testData/diagnostics/testsWithStdLib/contracts/dsl/errors/callInContractDescription.kt");
}
@TestMetadata("contractCallSites.kt")
public void testContractCallSites() throws Exception {
runTest("compiler/testData/diagnostics/testsWithStdLib/contracts/dsl/errors/contractCallSites.kt");
@TestMetadata("contractCallSites.1.3.kt")
public void testContractCallSites_1_3() throws Exception {
runTest("compiler/testData/diagnostics/testsWithStdLib/contracts/dsl/errors/contractCallSites.1.3.kt");
}
@TestMetadata("contractCallSites.1.4.kt")
public void testContractCallSites_1_4() throws Exception {
runTest("compiler/testData/diagnostics/testsWithStdLib/contracts/dsl/errors/contractCallSites.1.4.kt");
}
@TestMetadata("emptyContract.kt")
@@ -1233,14 +1238,24 @@ public class FirOldFrontendDiagnosticsTestWithStdlibGenerated extends AbstractFi
runTest("compiler/testData/diagnostics/testsWithStdLib/contracts/dsl/errors/recursiveContractCustomContractFunction.kt");
}
@TestMetadata("referenceToProperty.kt")
public void testReferenceToProperty() throws Exception {
runTest("compiler/testData/diagnostics/testsWithStdLib/contracts/dsl/errors/referenceToProperty.kt");
@TestMetadata("referenceToProperty.1.3.kt")
public void testReferenceToProperty_1_3() throws Exception {
runTest("compiler/testData/diagnostics/testsWithStdLib/contracts/dsl/errors/referenceToProperty.1.3.kt");
}
@TestMetadata("typeReferences.kt")
public void testTypeReferences() throws Exception {
runTest("compiler/testData/diagnostics/testsWithStdLib/contracts/dsl/errors/typeReferences.kt");
@TestMetadata("referenceToProperty.1.4.kt")
public void testReferenceToProperty_1_4() throws Exception {
runTest("compiler/testData/diagnostics/testsWithStdLib/contracts/dsl/errors/referenceToProperty.1.4.kt");
}
@TestMetadata("typeReferences.1.3.kt")
public void testTypeReferences_1_3() throws Exception {
runTest("compiler/testData/diagnostics/testsWithStdLib/contracts/dsl/errors/typeReferences.1.3.kt");
}
@TestMetadata("typeReferences.1.4.kt")
public void testTypeReferences_1_4() throws Exception {
runTest("compiler/testData/diagnostics/testsWithStdLib/contracts/dsl/errors/typeReferences.1.4.kt");
}
@TestMetadata("unlabeledReceiver.kt")
@@ -1320,6 +1335,11 @@ public class FirOldFrontendDiagnosticsTestWithStdlibGenerated extends AbstractFi
runTest("compiler/testData/diagnostics/testsWithStdLib/contracts/smartcasts/compositions.kt");
}
@TestMetadata("contractsOnMembers.kt")
public void testContractsOnMembers() throws Exception {
runTest("compiler/testData/diagnostics/testsWithStdLib/contracts/smartcasts/contractsOnMembers.kt");
}
@TestMetadata("deeplyNested.kt")
public void testDeeplyNested() throws Exception {
runTest("compiler/testData/diagnostics/testsWithStdLib/contracts/smartcasts/deeplyNested.kt");
@@ -23,7 +23,6 @@ import org.jetbrains.kotlin.builtins.KotlinBuiltIns
import org.jetbrains.kotlin.builtins.getReceiverTypeFromFunctionType
import org.jetbrains.kotlin.builtins.getValueParameterTypesFromFunctionType
import org.jetbrains.kotlin.builtins.isBuiltinFunctionalType
import org.jetbrains.kotlin.config.AnalysisFlags
import org.jetbrains.kotlin.config.LanguageFeature
import org.jetbrains.kotlin.config.LanguageVersionSettings
import org.jetbrains.kotlin.contracts.description.ContractProviderKey
@@ -259,8 +258,9 @@ class FunctionDescriptorResolver(
if (function !is KtNamedFunction) return null
val isContractsEnabled = languageVersionSettings.supportsFeature(LanguageFeature.AllowContractsForCustomFunctions)
val isAllowedOnMembers = languageVersionSettings.supportsFeature(LanguageFeature.AllowContractsForNonOverridableMembers)
if (!isContractsEnabled || !function.mayHaveContract()) return null
if (!isContractsEnabled || !function.mayHaveContract(isAllowedOnMembers)) return null
return LazyContractProvider(storageManager) {
AstLoadingFilter.forceAllowTreeLoading(function.containingFile, ThrowableComputable {
@@ -6,11 +6,9 @@
package org.jetbrains.kotlin.resolve.calls.checkers
import com.intellij.psi.PsiElement
import org.jetbrains.kotlin.config.LanguageFeature
import org.jetbrains.kotlin.contracts.parsing.isContractCallDescriptor
import org.jetbrains.kotlin.descriptors.FunctionDescriptor
import org.jetbrains.kotlin.descriptors.PackageFragmentDescriptor
import org.jetbrains.kotlin.descriptors.PropertyAccessorDescriptor
import org.jetbrains.kotlin.descriptors.isOverridable
import org.jetbrains.kotlin.descriptors.*
import org.jetbrains.kotlin.diagnostics.Errors
import org.jetbrains.kotlin.psi.KtElement
import org.jetbrains.kotlin.psi.psiUtil.isFirstStatement
@@ -25,6 +23,8 @@ object ContractNotAllowedCallChecker : CallChecker {
val descriptor = resolvedCall.resultingDescriptor as? FunctionDescriptor ?: return
if (!descriptor.isContractCallDescriptor()) return
val allowedOnMembers = context.languageVersionSettings.supportsFeature(LanguageFeature.AllowContractsForNonOverridableMembers)
val callElement = resolvedCall.call.callElement
var hasErrors = false
@@ -40,7 +40,24 @@ object ContractNotAllowedCallChecker : CallChecker {
contractNotAllowed("Contracts are allowed only for functions")
var inFunctionBodyBlock = true
if (scope.ownerDescriptor.containingDeclaration !is PackageFragmentDescriptor
val declarationOwner = scope.ownerDescriptor.containingDeclaration
val acceptableParent = if (allowedOnMembers) {
var owner = declarationOwner
var result = true
while (owner !is PackageFragmentDescriptor) {
if (owner !is ClassDescriptor) {
result = false
break
}
owner = owner.containingDeclaration
}
result
} else {
declarationOwner is PackageFragmentDescriptor
}
if (!acceptableParent
|| scope.kind != LexicalScopeKind.CODE_BLOCK
|| (scope.parent as? LexicalScope)?.kind != LexicalScopeKind.FUNCTION_INNER_SCOPE
) {
@@ -48,13 +65,23 @@ object ContractNotAllowedCallChecker : CallChecker {
contractNotAllowed("Contracts are allowed only in function body block")
inFunctionBodyBlock = false
} else {
contractNotAllowed("Contracts are allowed only for top-level functions")
val message = if (allowedOnMembers)
"Contracts are allowed only for functions"
else
"Contracts are allowed only for top-level functions"
contractNotAllowed(message)
}
}
if (functionDescriptor?.isOperator == true) contractNotAllowed("Contracts are not allowed for operator functions")
if (functionDescriptor?.isOverridable == true) contractNotAllowed("Contracts are not allowed for open functions")
if (!allowedOnMembers && functionDescriptor?.isOverridable == true) {
contractNotAllowed("Contracts are not allowed for open functions")
}
if (allowedOnMembers && functionDescriptor?.isOverridableOrOverrides == true) {
contractNotAllowed("Contracts are not allowed for open or override functions")
}
if (!callElement.isFirstStatement() && inFunctionBodyBlock) {
contractNotAllowed("Contract should be the first statement")
@@ -246,11 +246,15 @@ public class KtNamedFunction extends KtTypeParameterListOwnerStub<KotlinFunction
}
public boolean mayHaveContract() {
return mayHaveContract(true);
}
public boolean mayHaveContract(boolean isAllowedOnMembers) {
KotlinFunctionStub stub = getStub();
if (stub != null) {
return stub.mayHaveContract();
}
return KtPsiUtilKt.isContractPresentPsiCheck(this);
return KtPsiUtilKt.isContractPresentPsiCheck(this, isAllowedOnMembers);
}
}
@@ -296,9 +296,9 @@ inline fun <reified T : KtElement, R> flatMapDescendantsOfTypeVisitor(
// ----------- Contracts -------------------------------------------------------------------------------------------------------------------
fun KtNamedFunction.isContractPresentPsiCheck(): Boolean {
fun KtNamedFunction.isContractPresentPsiCheck(isAllowedOnMembers: Boolean): Boolean {
val contractAllowedHere =
isTopLevel &&
(isAllowedOnMembers || isTopLevel) &&
hasBlockBody() &&
!hasModifier(KtTokens.OPERATOR_KEYWORD)
if (!contractAllowedHere) return false
@@ -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
}
@@ -0,0 +1,19 @@
// !LANGUAGE: +AllowContractsForCustomFunctions +ReadDeserializedContracts +AllowContractsForNonOverridableMembers
// !USE_EXPERIMENTAL: kotlin.contracts.ExperimentalContracts
@file:Suppress("INVISIBLE_MEMBER", "INVISIBLE_REFERENCE")
package test
import kotlin.contracts.*
class Foo {
fun <X, Y, Z, R> callsEffectWithKind(block: (X, Y, Z) -> R) {
contract {
callsInPlace(block, InvocationKind.EXACTLY_ONCE)
}
}
fun returnsImplies(x: Boolean) {
contract { returns() implies (x) }
}
}
@@ -0,0 +1,11 @@
package test
public final class Foo {
/*primary*/ public constructor Foo()
public final fun </*0*/ X, /*1*/ Y, /*2*/ Z, /*3*/ R> callsEffectWithKind(/*0*/ block: (X, Y, Z) -> R): kotlin.Unit
CallsInPlace(block, EXACTLY_ONCE)
public final fun returnsImplies(/*0*/ x: kotlin.Boolean): kotlin.Unit
Returns(WILDCARD) -> x
}
@@ -1189,9 +1189,14 @@ public class DiagnosticsTestWithStdLibGenerated extends AbstractDiagnosticsTestW
runTest("compiler/testData/diagnostics/testsWithStdLib/contracts/dsl/errors/callInContractDescription.kt");
}
@TestMetadata("contractCallSites.kt")
public void testContractCallSites() throws Exception {
runTest("compiler/testData/diagnostics/testsWithStdLib/contracts/dsl/errors/contractCallSites.kt");
@TestMetadata("contractCallSites.1.3.kt")
public void testContractCallSites_1_3() throws Exception {
runTest("compiler/testData/diagnostics/testsWithStdLib/contracts/dsl/errors/contractCallSites.1.3.kt");
}
@TestMetadata("contractCallSites.1.4.kt")
public void testContractCallSites_1_4() throws Exception {
runTest("compiler/testData/diagnostics/testsWithStdLib/contracts/dsl/errors/contractCallSites.1.4.kt");
}
@TestMetadata("emptyContract.kt")
@@ -1234,14 +1239,24 @@ public class DiagnosticsTestWithStdLibGenerated extends AbstractDiagnosticsTestW
runTest("compiler/testData/diagnostics/testsWithStdLib/contracts/dsl/errors/recursiveContractCustomContractFunction.kt");
}
@TestMetadata("referenceToProperty.kt")
public void testReferenceToProperty() throws Exception {
runTest("compiler/testData/diagnostics/testsWithStdLib/contracts/dsl/errors/referenceToProperty.kt");
@TestMetadata("referenceToProperty.1.3.kt")
public void testReferenceToProperty_1_3() throws Exception {
runTest("compiler/testData/diagnostics/testsWithStdLib/contracts/dsl/errors/referenceToProperty.1.3.kt");
}
@TestMetadata("typeReferences.kt")
public void testTypeReferences() throws Exception {
runTest("compiler/testData/diagnostics/testsWithStdLib/contracts/dsl/errors/typeReferences.kt");
@TestMetadata("referenceToProperty.1.4.kt")
public void testReferenceToProperty_1_4() throws Exception {
runTest("compiler/testData/diagnostics/testsWithStdLib/contracts/dsl/errors/referenceToProperty.1.4.kt");
}
@TestMetadata("typeReferences.1.3.kt")
public void testTypeReferences_1_3() throws Exception {
runTest("compiler/testData/diagnostics/testsWithStdLib/contracts/dsl/errors/typeReferences.1.3.kt");
}
@TestMetadata("typeReferences.1.4.kt")
public void testTypeReferences_1_4() throws Exception {
runTest("compiler/testData/diagnostics/testsWithStdLib/contracts/dsl/errors/typeReferences.1.4.kt");
}
@TestMetadata("unlabeledReceiver.kt")
@@ -1321,6 +1336,11 @@ public class DiagnosticsTestWithStdLibGenerated extends AbstractDiagnosticsTestW
runTest("compiler/testData/diagnostics/testsWithStdLib/contracts/smartcasts/compositions.kt");
}
@TestMetadata("contractsOnMembers.kt")
public void testContractsOnMembers() throws Exception {
runTest("compiler/testData/diagnostics/testsWithStdLib/contracts/smartcasts/contractsOnMembers.kt");
}
@TestMetadata("deeplyNested.kt")
public void testDeeplyNested() throws Exception {
runTest("compiler/testData/diagnostics/testsWithStdLib/contracts/smartcasts/deeplyNested.kt");
@@ -1189,9 +1189,14 @@ public class DiagnosticsTestWithStdLibUsingJavacGenerated extends AbstractDiagno
runTest("compiler/testData/diagnostics/testsWithStdLib/contracts/dsl/errors/callInContractDescription.kt");
}
@TestMetadata("contractCallSites.kt")
public void testContractCallSites() throws Exception {
runTest("compiler/testData/diagnostics/testsWithStdLib/contracts/dsl/errors/contractCallSites.kt");
@TestMetadata("contractCallSites.1.3.kt")
public void testContractCallSites_1_3() throws Exception {
runTest("compiler/testData/diagnostics/testsWithStdLib/contracts/dsl/errors/contractCallSites.1.3.kt");
}
@TestMetadata("contractCallSites.1.4.kt")
public void testContractCallSites_1_4() throws Exception {
runTest("compiler/testData/diagnostics/testsWithStdLib/contracts/dsl/errors/contractCallSites.1.4.kt");
}
@TestMetadata("emptyContract.kt")
@@ -1234,14 +1239,24 @@ public class DiagnosticsTestWithStdLibUsingJavacGenerated extends AbstractDiagno
runTest("compiler/testData/diagnostics/testsWithStdLib/contracts/dsl/errors/recursiveContractCustomContractFunction.kt");
}
@TestMetadata("referenceToProperty.kt")
public void testReferenceToProperty() throws Exception {
runTest("compiler/testData/diagnostics/testsWithStdLib/contracts/dsl/errors/referenceToProperty.kt");
@TestMetadata("referenceToProperty.1.3.kt")
public void testReferenceToProperty_1_3() throws Exception {
runTest("compiler/testData/diagnostics/testsWithStdLib/contracts/dsl/errors/referenceToProperty.1.3.kt");
}
@TestMetadata("typeReferences.kt")
public void testTypeReferences() throws Exception {
runTest("compiler/testData/diagnostics/testsWithStdLib/contracts/dsl/errors/typeReferences.kt");
@TestMetadata("referenceToProperty.1.4.kt")
public void testReferenceToProperty_1_4() throws Exception {
runTest("compiler/testData/diagnostics/testsWithStdLib/contracts/dsl/errors/referenceToProperty.1.4.kt");
}
@TestMetadata("typeReferences.1.3.kt")
public void testTypeReferences_1_3() throws Exception {
runTest("compiler/testData/diagnostics/testsWithStdLib/contracts/dsl/errors/typeReferences.1.3.kt");
}
@TestMetadata("typeReferences.1.4.kt")
public void testTypeReferences_1_4() throws Exception {
runTest("compiler/testData/diagnostics/testsWithStdLib/contracts/dsl/errors/typeReferences.1.4.kt");
}
@TestMetadata("unlabeledReceiver.kt")
@@ -1321,6 +1336,11 @@ public class DiagnosticsTestWithStdLibUsingJavacGenerated extends AbstractDiagno
runTest("compiler/testData/diagnostics/testsWithStdLib/contracts/smartcasts/compositions.kt");
}
@TestMetadata("contractsOnMembers.kt")
public void testContractsOnMembers() throws Exception {
runTest("compiler/testData/diagnostics/testsWithStdLib/contracts/smartcasts/contractsOnMembers.kt");
}
@TestMetadata("deeplyNested.kt")
public void testDeeplyNested() throws Exception {
runTest("compiler/testData/diagnostics/testsWithStdLib/contracts/smartcasts/deeplyNested.kt");
@@ -4650,6 +4650,11 @@ public class LoadJavaTestGenerated extends AbstractLoadJavaTest {
runTest("compiler/testData/loadJava/compiledKotlinWithStdlib/contracts/callsEffect.kt");
}
@TestMetadata("contractsOnMembers.kt")
public void testContractsOnMembers() throws Exception {
runTest("compiler/testData/loadJava/compiledKotlinWithStdlib/contracts/contractsOnMembers.kt");
}
@TestMetadata("deeplyNestedExpression.kt")
public void testDeeplyNestedExpression() throws Exception {
runTest("compiler/testData/loadJava/compiledKotlinWithStdlib/contracts/deeplyNestedExpression.kt");
@@ -4651,6 +4651,11 @@ public class IrLoadJavaTestGenerated extends AbstractIrLoadJavaTest {
runTest("compiler/testData/loadJava/compiledKotlinWithStdlib/contracts/callsEffect.kt");
}
@TestMetadata("contractsOnMembers.kt")
public void testContractsOnMembers() throws Exception {
runTest("compiler/testData/loadJava/compiledKotlinWithStdlib/contracts/contractsOnMembers.kt");
}
@TestMetadata("deeplyNestedExpression.kt")
public void testDeeplyNestedExpression() throws Exception {
runTest("compiler/testData/loadJava/compiledKotlinWithStdlib/contracts/deeplyNestedExpression.kt");
@@ -4650,6 +4650,11 @@ public class LoadJavaUsingJavacTestGenerated extends AbstractLoadJavaUsingJavacT
runTest("compiler/testData/loadJava/compiledKotlinWithStdlib/contracts/callsEffect.kt");
}
@TestMetadata("contractsOnMembers.kt")
public void testContractsOnMembers() throws Exception {
runTest("compiler/testData/loadJava/compiledKotlinWithStdlib/contracts/contractsOnMembers.kt");
}
@TestMetadata("deeplyNestedExpression.kt")
public void testDeeplyNestedExpression() throws Exception {
runTest("compiler/testData/loadJava/compiledKotlinWithStdlib/contracts/deeplyNestedExpression.kt");
@@ -121,6 +121,7 @@ enum class LanguageFeature(
AllowNullOperatorsForResult(KOTLIN_1_4),
AllowResultInReturnType(KOTLIN_1_4),
PreferJavaFieldOverload(KOTLIN_1_4),
AllowContractsForNonOverridableMembers(KOTLIN_1_4),
ProperVisibilityForCompanionObjectInstanceField(sinceVersion = null, kind = BUG_FIX),
// Temporarily disabled, see KT-27084/KT-22379