From a81fc9e9bcdd7b7ef9b6bb25c394b5d9424f2507 Mon Sep 17 00:00:00 2001 From: Dmitry Savvinov Date: Thu, 26 Jul 2018 13:32:29 +0300 Subject: [PATCH] Add contract to checkNotNull overload without message ^KT-25303 Fixed --- .../testsWithStdLib/contracts/fromStdlib/check.kt | 9 +++++++-- .../testsWithStdLib/contracts/fromStdlib/check.txt | 3 ++- libraries/stdlib/src/kotlin/util/Preconditions.kt | 7 ++++++- 3 files changed, 15 insertions(+), 4 deletions(-) diff --git a/compiler/testData/diagnostics/testsWithStdLib/contracts/fromStdlib/check.kt b/compiler/testData/diagnostics/testsWithStdLib/contracts/fromStdlib/check.kt index 873062fc774..df534621847 100644 --- a/compiler/testData/diagnostics/testsWithStdLib/contracts/fromStdlib/check.kt +++ b/compiler/testData/diagnostics/testsWithStdLib/contracts/fromStdlib/check.kt @@ -22,7 +22,12 @@ fun testCheckWithFailingMessage(x: Any?) { x.length } -fun tesCheckNotNullWithMessage(x: Int?) { - checkNotNull(x) { "x is null!"} +fun testCheckNotNullWithMessage(x: Int?) { + checkNotNull(x) { "x is null!" } + x.inc() +} + +fun testCheckNotNull(x: Int?) { + checkNotNull(x) x.inc() } \ No newline at end of file diff --git a/compiler/testData/diagnostics/testsWithStdLib/contracts/fromStdlib/check.txt b/compiler/testData/diagnostics/testsWithStdLib/contracts/fromStdlib/check.txt index e62fe7822c1..b102d8eb8f4 100644 --- a/compiler/testData/diagnostics/testsWithStdLib/contracts/fromStdlib/check.txt +++ b/compiler/testData/diagnostics/testsWithStdLib/contracts/fromStdlib/check.txt @@ -1,6 +1,7 @@ package -public fun tesCheckNotNullWithMessage(/*0*/ x: kotlin.Int?): kotlin.Unit +public fun testCheckNotNull(/*0*/ x: kotlin.Int?): kotlin.Unit +public fun testCheckNotNullWithMessage(/*0*/ x: kotlin.Int?): kotlin.Unit public fun testCheckSmartcast(/*0*/ x: kotlin.Any?): kotlin.Unit public fun testCheckUnreachableCode(): kotlin.Unit public fun testCheckWithFailingMessage(/*0*/ x: kotlin.Any?): kotlin.Unit diff --git a/libraries/stdlib/src/kotlin/util/Preconditions.kt b/libraries/stdlib/src/kotlin/util/Preconditions.kt index 97511d75f78..7f87628b38d 100644 --- a/libraries/stdlib/src/kotlin/util/Preconditions.kt +++ b/libraries/stdlib/src/kotlin/util/Preconditions.kt @@ -106,7 +106,12 @@ public inline fun check(value: Boolean, lazyMessage: () -> Any): Unit { * @sample samples.misc.Preconditions.failCheckWithLazyMessage */ @kotlin.internal.InlineOnly -public inline fun checkNotNull(value: T?): T = checkNotNull(value) { "Required value was null." } +public inline fun checkNotNull(value: T?): T { + contract { + returns() implies (value != null) + } + return checkNotNull(value) { "Required value was null." } +} /** * Throws an [IllegalStateException] with the result of calling [lazyMessage] if the [value] is null. Otherwise