Skip to content

Commit bd47910

Browse files
sandwwraithSpace Team
authored andcommitted
[Stdlib] Change lambda signature inside assertFails(With).
This is required so RVC won't report "Unused return value" on Unit coercions in the places where this function is called. Despite lambda return type being a generic type arg, this change is still binary incompatible for Klibs, so we apply the usual trick with Deprecated(HIDDEN). #KT-79094 Fixed
1 parent 2b64be4 commit bd47910

File tree

16 files changed

+126
-24
lines changed

16 files changed

+126
-24
lines changed

compiler/testData/codegen/box/casts/kt55005.kt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
// TARGET_BACKEND: JVM_IR
22
// CHECK_BYTECODE_TEXT
33
// WITH_STDLIB
4+
// IGNORE_BACKEND_K1: JVM_IR
5+
// ^ K1 does not support coercing assigment to Any?
46

57
import kotlin.test.*
68

@@ -24,4 +26,4 @@ fun box(): String {
2426
return "OK"
2527
}
2628

27-
// 0 CHECKCAST
29+
// 0 CHECKCAST

compiler/testData/codegen/box/javaInterop/notNullAssertions/destructuringAssignmentWithNullabilityAssertionOnExtensionReceiver_lv12.kt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
// LANGUAGE: +NameBasedDestructuring +DeprecateNameMismatchInShortDestructuringWithParentheses +EnableNameBasedDestructuringShortForm
22
// TARGET_BACKEND: JVM
33
// WITH_STDLIB
4+
// IGNORE_BACKEND_K1: JVM_IR
5+
// ^ K1 does not support coercing assigment to Any?
6+
47
// FILE: destructuringAssignmentWithNullabilityAssertionOnExtensionReceiver_lv12.kt
58

69
import kotlin.test.*

compiler/testData/codegen/bytecodeText/nullCheckOptimization/nullabilityAssertionOnDispatchReceiver.kt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
// TARGET_BACKEND: JVM
22
// WITH_STDLIB
3+
// IGNORE_BACKEND_K1: JVM_IR
4+
// ^ K1 does not support coercing assigment to Any?
5+
36
// FILE: test.kt
47
import kotlin.test.*
58

kotlin-native/runtime/src/main/kotlin/kotlin/test/Assertions.kt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,10 @@ public actual inline fun todo(block: () -> Unit) {
2323
}
2424

2525
@PublishedApi
26-
internal actual fun <T : Throwable> checkResultIsFailure(exceptionClass: KClass<T>, message: String?, blockResult: Result<Unit>): T {
26+
internal actual fun <T : Throwable> checkResultIsFailure(exceptionClass: KClass<T>, message: String?, blockResult: Result<Any?>): T {
2727
blockResult.fold(
28-
onSuccess = {
29-
asserter.fail(messagePrefix(message) + "Expected an exception of ${exceptionClass.qualifiedName} to be thrown, but was completed successfully.")
28+
onSuccess = { v ->
29+
asserter.fail(messagePrefix(message) + "Expected an exception of ${exceptionClass.qualifiedName} to be thrown, ${formatResultMessage(v)}.")
3030
},
3131
onFailure = { e ->
3232
if (exceptionClass.isInstance(e)) {
@@ -44,4 +44,4 @@ internal actual inline fun AssertionErrorWithCause(message: String?, cause: Thro
4444
AssertionError(message, cause)
4545

4646

47-
internal actual fun lookupAsserter(): Asserter = DefaultAsserter
47+
internal actual fun lookupAsserter(): Asserter = DefaultAsserter

libraries/kotlin.test/common/src/main/kotlin/kotlin/test/Assertions.kt

Lines changed: 69 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -589,6 +589,17 @@ public inline fun <@OnlyInputTypes T> expect(expected: T, message: String?, bloc
589589
assertEquals(expected, block(), message)
590590
}
591591

592+
/**
593+
* Asserts that given function [block] fails by throwing an exception.
594+
*
595+
* @return An exception that was expected to be thrown and was successfully caught.
596+
* The returned exception can be inspected further, for example by asserting its property values.
597+
*/
598+
@InlineOnly
599+
@JvmName("assertFailsAny")
600+
public inline fun assertFails(block: () -> Any?): Throwable =
601+
checkResultIsFailure(null, runCatching(block))
602+
592603
/**
593604
* Asserts that given function [block] fails by throwing an exception.
594605
*
@@ -597,9 +608,24 @@ public inline fun <@OnlyInputTypes T> expect(expected: T, message: String?, bloc
597608
*/
598609
@InlineOnly
599610
@JvmName("assertFailsInline")
611+
@Deprecated(ASSERT_FAILS_NOTE, level = DeprecationLevel.HIDDEN)
600612
public inline fun assertFails(block: () -> Unit): Throwable =
601613
checkResultIsFailure(null, runCatching(block))
602614

615+
/**
616+
* Asserts that given function [block] fails by throwing an exception.
617+
*
618+
* If the assertion fails, the specified [message] is used unless it is null as a prefix for the failure message.
619+
*
620+
* @return An exception that was expected to be thrown and was successfully caught.
621+
* The returned exception can be inspected further, for example by asserting its property values.
622+
*/
623+
@SinceKotlin("1.1")
624+
@InlineOnly
625+
@JvmName("assertFailsAny")
626+
public inline fun assertFails(message: String?, block: () -> Any?): Throwable =
627+
checkResultIsFailure(message, runCatching(block))
628+
603629
/**
604630
* Asserts that given function [block] fails by throwing an exception.
605631
*
@@ -611,14 +637,15 @@ public inline fun assertFails(block: () -> Unit): Throwable =
611637
@SinceKotlin("1.1")
612638
@InlineOnly
613639
@JvmName("assertFailsInline")
640+
@Deprecated(ASSERT_FAILS_NOTE, level = DeprecationLevel.HIDDEN)
614641
public inline fun assertFails(message: String?, block: () -> Unit): Throwable =
615642
checkResultIsFailure(message, runCatching(block))
616643

617644
@PublishedApi
618-
internal fun checkResultIsFailure(message: String?, blockResult: Result<Unit>): Throwable {
645+
internal fun checkResultIsFailure(message: String?, blockResult: Result<Any?>): Throwable {
619646
blockResult.fold(
620-
onSuccess = {
621-
asserter.fail(messagePrefix(message) + "Expected an exception to be thrown, but was completed successfully.")
647+
onSuccess = { v ->
648+
asserter.fail(messagePrefix(message) + "Expected an exception to be thrown, ${formatResultMessage(v)}")
622649
},
623650
onFailure = { e ->
624651
return e
@@ -634,9 +661,32 @@ internal fun checkResultIsFailure(message: String?, blockResult: Result<Unit>):
634661
* The returned exception can be inspected further, for example by asserting its property values.
635662
*/
636663
@InlineOnly
664+
@JvmName("assertFailsWithAny")
665+
public inline fun <reified T : Throwable> assertFailsWith(message: String? = null, block: () -> Any?): T =
666+
assertFailsWith(T::class, message, block)
667+
668+
/** Asserts that a [block] fails with a specific exception of type [T] being thrown.
669+
*
670+
* If the assertion fails, the specified [message] is used unless it is null as a prefix for the failure message.
671+
*
672+
* @return An exception of the expected exception type [T] that successfully caught.
673+
* The returned exception can be inspected further, for example by asserting its property values.
674+
*/
675+
@InlineOnly
676+
@Deprecated(ASSERT_FAILS_NOTE, level = DeprecationLevel.HIDDEN)
637677
public inline fun <reified T : Throwable> assertFailsWith(message: String? = null, block: () -> Unit): T =
638678
assertFailsWith(T::class, message, block)
639679

680+
/**
681+
* Asserts that a [block] fails with a specific exception of type [exceptionClass] being thrown.
682+
*
683+
* @return An exception of the expected exception type [T] that successfully caught.
684+
* The returned exception can be inspected further, for example by asserting its property values.
685+
*/
686+
@InlineOnly
687+
@JvmName("assertFailsWithAny")
688+
public inline fun <T : Throwable> assertFailsWith(exceptionClass: KClass<T>, block: () -> Any?): T = assertFailsWith(exceptionClass, null, block)
689+
640690
/**
641691
* Asserts that a [block] fails with a specific exception of type [exceptionClass] being thrown.
642692
*
@@ -645,8 +695,22 @@ public inline fun <reified T : Throwable> assertFailsWith(message: String? = nul
645695
*/
646696
@InlineOnly
647697
@JvmName("assertFailsWithInline")
698+
@Deprecated(ASSERT_FAILS_NOTE, level = DeprecationLevel.HIDDEN)
648699
public inline fun <T : Throwable> assertFailsWith(exceptionClass: KClass<T>, block: () -> Unit): T = assertFailsWith(exceptionClass, null, block)
649700

701+
/**
702+
* Asserts that a [block] fails with a specific exception of type [exceptionClass] being thrown.
703+
*
704+
* If the assertion fails, the specified [message] is used unless it is null as a prefix for the failure message.
705+
*
706+
* @return An exception of the expected exception type [T] that successfully caught.
707+
* The returned exception can be inspected further, for example by asserting its property values.
708+
*/
709+
@InlineOnly
710+
@JvmName("assertFailsWithAny")
711+
public inline fun <T : Throwable> assertFailsWith(exceptionClass: KClass<T>, message: String?, block: () -> Any?): T =
712+
checkResultIsFailure(exceptionClass, message, runCatching(block))
713+
650714
/**
651715
* Asserts that a [block] fails with a specific exception of type [exceptionClass] being thrown.
652716
*
@@ -657,6 +721,7 @@ public inline fun <T : Throwable> assertFailsWith(exceptionClass: KClass<T>, blo
657721
*/
658722
@InlineOnly
659723
@JvmName("assertFailsWithInline")
724+
@Deprecated(ASSERT_FAILS_NOTE, level = DeprecationLevel.HIDDEN)
660725
public inline fun <T : Throwable> assertFailsWith(exceptionClass: KClass<T>, message: String?, block: () -> Unit): T =
661726
checkResultIsFailure(exceptionClass, message, runCatching(block))
662727

@@ -772,3 +837,4 @@ public interface AsserterContributor {
772837
public fun contribute(): Asserter?
773838
}
774839

840+
private const val ASSERT_FAILS_NOTE = "Provided for binary compatibility. assertFails(With) overload with () -> Any? should be used instead."

libraries/kotlin.test/common/src/main/kotlin/kotlin/test/AssertionsH.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,4 @@ public expect fun todo(block: () -> Unit)
1616

1717
/** Asserts that a [blockResult] is a failure with the specific exception type being thrown. */
1818
@PublishedApi
19-
internal expect fun <T : Throwable> checkResultIsFailure(exceptionClass: KClass<T>, message: String?, blockResult: Result<Unit>): T
19+
internal expect fun <T : Throwable> checkResultIsFailure(exceptionClass: KClass<T>, message: String?, blockResult: Result<Any?>): T

libraries/kotlin.test/common/src/main/kotlin/kotlin/test/Utils.kt

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,11 @@ internal expect fun lookupAsserter(): Asserter
1313
@PublishedApi // required to get stable name as it's called from box tests
1414
internal fun overrideAsserter(value: Asserter?): Asserter? = _asserter.also { _asserter = value }
1515

16+
internal fun formatResultMessage(value: Any?) = when (value) {
17+
is Unit -> "but was completed successfully."
18+
else -> "but was completed successfully with the result: <$value>."
19+
}
20+
1621

1722
private fun checkAbsoluteTolerance(absoluteTolerance: Double) {
1823
require(absoluteTolerance >= 0.0) { "Illegal negative absolute tolerance <$absoluteTolerance>." }
@@ -49,4 +54,4 @@ internal fun checkFloatsAreEqual(
4954
{ messagePrefix(message) + "Expected <$expected> with absolute tolerance <$absoluteTolerance>, actual <$actual>." },
5055
equal != shouldFail
5156
)
52-
}
57+
}

libraries/kotlin.test/js/src/main/kotlin/kotlin/test/JsImpl.kt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,10 @@ internal actual inline fun AssertionErrorWithCause(message: String?, cause: Thro
2424

2525

2626
@PublishedApi
27-
internal actual fun <T : Throwable> checkResultIsFailure(exceptionClass: KClass<T>, message: String?, blockResult: Result<Unit>): T {
27+
internal actual fun <T : Throwable> checkResultIsFailure(exceptionClass: KClass<T>, message: String?, blockResult: Result<Any?>): T {
2828
blockResult.fold(
29-
onSuccess = {
30-
asserter.fail(messagePrefix(message) + "Expected an exception of $exceptionClass to be thrown, but was completed successfully.")
29+
onSuccess = { v ->
30+
asserter.fail(messagePrefix(message) + "Expected an exception of $exceptionClass to be thrown, ${formatResultMessage(v)}")
3131
},
3232
onFailure = { e ->
3333
if (exceptionClass.isInstance(e)) {
@@ -43,4 +43,4 @@ internal actual fun <T : Throwable> checkResultIsFailure(exceptionClass: KClass<
4343
/**
4444
* Provides the JS implementation of asserter
4545
*/
46-
internal actual fun lookupAsserter(): Asserter = DefaultJsAsserter
46+
internal actual fun lookupAsserter(): Asserter = DefaultJsAsserter

libraries/kotlin.test/jvm/src/main/kotlin/AssertionsImpl.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,11 @@ import kotlin.reflect.*
1515

1616
/** Asserts that a [blockResult] is a failure with the specific exception type being thrown. */
1717
@PublishedApi
18-
internal actual fun <T : Throwable> checkResultIsFailure(exceptionClass: KClass<T>, message: String?, blockResult: Result<Unit>): T {
18+
internal actual fun <T : Throwable> checkResultIsFailure(exceptionClass: KClass<T>, message: String?, blockResult: Result<Any?>): T {
1919
blockResult.fold(
20-
onSuccess = {
20+
onSuccess = { v ->
2121
val msg = messagePrefix(message)
22-
asserter.fail(msg + "Expected an exception of ${exceptionClass.java} to be thrown, but was completed successfully.")
22+
asserter.fail(msg + "Expected an exception of ${exceptionClass.java} to be thrown, ${formatResultMessage(v)}")
2323
},
2424
onFailure = { e ->
2525
if (exceptionClass.java.isInstance(e)) {

libraries/kotlin.test/wasm/src/main/kotlin/kotlin/test/WasmImpl.kt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,10 @@ internal actual inline fun AssertionErrorWithCause(message: String?, cause: Thro
2323

2424

2525
@PublishedApi
26-
internal actual fun <T : Throwable> checkResultIsFailure(exceptionClass: KClass<T>, message: String?, blockResult: Result<Unit>): T {
26+
internal actual fun <T : Throwable> checkResultIsFailure(exceptionClass: KClass<T>, message: String?, blockResult: Result<Any?>): T {
2727
blockResult.fold(
28-
onSuccess = {
29-
asserter.fail(messagePrefix(message) + "Expected an exception of $exceptionClass to be thrown, but was completed successfully.")
28+
onSuccess = { v ->
29+
asserter.fail(messagePrefix(message) + "Expected an exception of $exceptionClass to be thrown, ${formatResultMessage(v)}")
3030
},
3131
onFailure = { e ->
3232
if (exceptionClass.isInstance(e)) {
@@ -42,4 +42,4 @@ internal actual fun <T : Throwable> checkResultIsFailure(exceptionClass: KClass<
4242
/**
4343
* Provides the JS implementation of asserter
4444
*/
45-
internal actual fun lookupAsserter(): Asserter = DefaultWasmAsserter
45+
internal actual fun lookupAsserter(): Asserter = DefaultWasmAsserter

0 commit comments

Comments
 (0)