From ef623b84910380272620cf6fe66af602550f3c47 Mon Sep 17 00:00:00 2001 From: Roman Efremov Date: Wed, 19 Jul 2023 15:41:11 +0200 Subject: [PATCH 1/2] Revert "Remove `@PublishedApi` from `unwrap` to comply with new compiler restriction (#3810)" This reverts commit 2bd0f2909a74cb2b2ba315c249b4f62166ca0268. --- .../common/src/internal/StackTraceRecovery.common.kt | 1 + 1 file changed, 1 insertion(+) diff --git a/kotlinx-coroutines-core/common/src/internal/StackTraceRecovery.common.kt b/kotlinx-coroutines-core/common/src/internal/StackTraceRecovery.common.kt index 2d00768d7c..2812b931de 100644 --- a/kotlinx-coroutines-core/common/src/internal/StackTraceRecovery.common.kt +++ b/kotlinx-coroutines-core/common/src/internal/StackTraceRecovery.common.kt @@ -40,6 +40,7 @@ internal expect suspend inline fun recoverAndThrow(exception: Throwable): Nothin * The opposite of [recoverStackTrace]. * It is guaranteed that `unwrap(recoverStackTrace(e)) === e` */ +@PublishedApi // published for the multiplatform implementation of kotlinx-coroutines-test internal expect fun unwrap(exception: E): E internal expect class StackTraceElement From 3c9e8564a914641980e6d2bcab88507b48522b43 Mon Sep 17 00:00:00 2001 From: Roman Efremov Date: Wed, 19 Jul 2023 13:20:11 +0200 Subject: [PATCH 2/2] Make annotations on expect declarations comply with new compiler restriction In KT-58551 we require all annotations from expect declaration to be present on actual. This commit fixes the following violations: 1. `DefaultDelay` has `@PublishedApi` only on expect, thus it must be copied to all actuals. 2. `unwrap` has the same problem. 3. expect `IgnoreJreRequirement` has `AnnotationTarget.TYPE` which is not present in actual. This is because Java target TYPE doesn't correspond to Kotlin target TYPE. Since `IgnoreJreRequirement` is internal, we can safely change targets on expect and make them equal to actual. --- kotlinx-coroutines-core/api/kotlinx-coroutines-core.api | 5 +++++ .../common/src/CoroutineContext.common.kt | 2 +- .../common/src/internal/InternalAnnotations.common.kt | 9 ++++++++- .../common/src/internal/StackTraceRecovery.common.kt | 2 +- kotlinx-coroutines-core/js/src/CoroutineContext.kt | 1 + .../js/src/internal/StackTraceRecovery.kt | 1 + kotlinx-coroutines-core/jvm/src/Debug.kt | 3 ++- kotlinx-coroutines-core/jvm/src/DefaultExecutor.kt | 1 + .../jvm/src/internal/StackTraceRecovery.kt | 2 ++ kotlinx-coroutines-core/native/src/CoroutineContext.kt | 1 + .../native/src/internal/StackTraceRecovery.kt | 2 ++ 11 files changed, 25 insertions(+), 4 deletions(-) diff --git a/kotlinx-coroutines-core/api/kotlinx-coroutines-core.api b/kotlinx-coroutines-core/api/kotlinx-coroutines-core.api index 9b548ac49f..f2975086a7 100644 --- a/kotlinx-coroutines-core/api/kotlinx-coroutines-core.api +++ b/kotlinx-coroutines-core/api/kotlinx-coroutines-core.api @@ -276,6 +276,11 @@ public final class kotlinx/coroutines/DebugKt { public static final field DEBUG_PROPERTY_VALUE_AUTO Ljava/lang/String; public static final field DEBUG_PROPERTY_VALUE_OFF Ljava/lang/String; public static final field DEBUG_PROPERTY_VALUE_ON Ljava/lang/String; + public static final fun getRECOVER_STACK_TRACES ()Z +} + +public final class kotlinx/coroutines/DefaultExecutorKt { + public static final fun getDefaultDelay ()Lkotlinx/coroutines/Delay; } public abstract interface class kotlinx/coroutines/Deferred : kotlinx/coroutines/Job { diff --git a/kotlinx-coroutines-core/common/src/CoroutineContext.common.kt b/kotlinx-coroutines-core/common/src/CoroutineContext.common.kt index 9153f39821..5f776335bf 100644 --- a/kotlinx-coroutines-core/common/src/CoroutineContext.common.kt +++ b/kotlinx-coroutines-core/common/src/CoroutineContext.common.kt @@ -20,7 +20,7 @@ public expect fun CoroutineScope.newCoroutineContext(context: CoroutineContext): @InternalCoroutinesApi public expect fun CoroutineContext.newCoroutineContext(addedContext: CoroutineContext): CoroutineContext -@PublishedApi +@PublishedApi // to have unmangled name when using from other modules via suppress @Suppress("PropertyName") internal expect val DefaultDelay: Delay diff --git a/kotlinx-coroutines-core/common/src/internal/InternalAnnotations.common.kt b/kotlinx-coroutines-core/common/src/internal/InternalAnnotations.common.kt index 1df81c9cc4..e0a4f24595 100644 --- a/kotlinx-coroutines-core/common/src/internal/InternalAnnotations.common.kt +++ b/kotlinx-coroutines-core/common/src/internal/InternalAnnotations.common.kt @@ -5,6 +5,13 @@ package kotlinx.coroutines.internal // Ignore JRE requirements for animal-sniffer, compileOnly dependency -@Target(AnnotationTarget.FUNCTION, AnnotationTarget.TYPE) +@Target( + AnnotationTarget.FUNCTION, + AnnotationTarget.PROPERTY_GETTER, + AnnotationTarget.PROPERTY_SETTER, + AnnotationTarget.CONSTRUCTOR, + AnnotationTarget.CLASS, + AnnotationTarget.FILE +) @OptionalExpectation internal expect annotation class IgnoreJreRequirement() diff --git a/kotlinx-coroutines-core/common/src/internal/StackTraceRecovery.common.kt b/kotlinx-coroutines-core/common/src/internal/StackTraceRecovery.common.kt index 2812b931de..ef0525e516 100644 --- a/kotlinx-coroutines-core/common/src/internal/StackTraceRecovery.common.kt +++ b/kotlinx-coroutines-core/common/src/internal/StackTraceRecovery.common.kt @@ -40,7 +40,7 @@ internal expect suspend inline fun recoverAndThrow(exception: Throwable): Nothin * The opposite of [recoverStackTrace]. * It is guaranteed that `unwrap(recoverStackTrace(e)) === e` */ -@PublishedApi // published for the multiplatform implementation of kotlinx-coroutines-test +@PublishedApi // Used from kotlinx-coroutines-test and reactor modules via suppress, not part of ABI internal expect fun unwrap(exception: E): E internal expect class StackTraceElement diff --git a/kotlinx-coroutines-core/js/src/CoroutineContext.kt b/kotlinx-coroutines-core/js/src/CoroutineContext.kt index 8036c88a10..232b3e271b 100644 --- a/kotlinx-coroutines-core/js/src/CoroutineContext.kt +++ b/kotlinx-coroutines-core/js/src/CoroutineContext.kt @@ -33,6 +33,7 @@ private fun isJsdom() = jsTypeOf(navigator) != UNDEFINED && jsTypeOf(navigator.userAgent.match) != UNDEFINED && navigator.userAgent.match("\\bjsdom\\b") +@PublishedApi // Used from kotlinx-coroutines-test via suppress, not part of ABI internal actual val DefaultDelay: Delay get() = Dispatchers.Default as Delay diff --git a/kotlinx-coroutines-core/js/src/internal/StackTraceRecovery.kt b/kotlinx-coroutines-core/js/src/internal/StackTraceRecovery.kt index 06107b8f49..2fb9527d68 100644 --- a/kotlinx-coroutines-core/js/src/internal/StackTraceRecovery.kt +++ b/kotlinx-coroutines-core/js/src/internal/StackTraceRecovery.kt @@ -10,6 +10,7 @@ internal actual fun recoverStackTrace(exception: E, continuation: internal actual fun recoverStackTrace(exception: E): E = exception internal actual suspend inline fun recoverAndThrow(exception: Throwable): Nothing = throw exception +@PublishedApi internal actual fun unwrap(exception: E): E = exception @Suppress("UNUSED") diff --git a/kotlinx-coroutines-core/jvm/src/Debug.kt b/kotlinx-coroutines-core/jvm/src/Debug.kt index 4a821a14bb..4e4c9cb2a5 100644 --- a/kotlinx-coroutines-core/jvm/src/Debug.kt +++ b/kotlinx-coroutines-core/jvm/src/Debug.kt @@ -78,7 +78,8 @@ internal actual val DEBUG = systemProp(DEBUG_PROPERTY_NAME).let { value -> // Note: stack-trace recovery is enabled only in debug mode // @JvmField: Don't use JvmField here to enable R8 optimizations via "assumenosideeffects" -internal actual val RECOVER_STACK_TRACES = +@PublishedApi +internal actual val RECOVER_STACK_TRACES: Boolean = DEBUG && systemProp(STACKTRACE_RECOVERY_PROPERTY_NAME, true) // It is used only in debug mode diff --git a/kotlinx-coroutines-core/jvm/src/DefaultExecutor.kt b/kotlinx-coroutines-core/jvm/src/DefaultExecutor.kt index c993bc2324..4d23aff3f7 100644 --- a/kotlinx-coroutines-core/jvm/src/DefaultExecutor.kt +++ b/kotlinx-coroutines-core/jvm/src/DefaultExecutor.kt @@ -10,6 +10,7 @@ import kotlin.coroutines.* private val defaultMainDelayOptIn = systemProp("kotlinx.coroutines.main.delay", false) +@PublishedApi internal actual val DefaultDelay: Delay = initializeDefaultDelay() private fun initializeDefaultDelay(): Delay { diff --git a/kotlinx-coroutines-core/jvm/src/internal/StackTraceRecovery.kt b/kotlinx-coroutines-core/jvm/src/internal/StackTraceRecovery.kt index afc9989646..d1833492ae 100644 --- a/kotlinx-coroutines-core/jvm/src/internal/StackTraceRecovery.kt +++ b/kotlinx-coroutines-core/jvm/src/internal/StackTraceRecovery.kt @@ -157,10 +157,12 @@ internal actual suspend inline fun recoverAndThrow(exception: Throwable): Nothin } } +@PublishedApi @Suppress("NOTHING_TO_INLINE") // Inline for better R8 optimizations internal actual inline fun unwrap(exception: E): E = if (!RECOVER_STACK_TRACES) exception else unwrapImpl(exception) +@PublishedApi internal fun unwrapImpl(exception: E): E { val cause = exception.cause // Fast-path to avoid array cloning diff --git a/kotlinx-coroutines-core/native/src/CoroutineContext.kt b/kotlinx-coroutines-core/native/src/CoroutineContext.kt index 51ffd7318a..d751f0672e 100644 --- a/kotlinx-coroutines-core/native/src/CoroutineContext.kt +++ b/kotlinx-coroutines-core/native/src/CoroutineContext.kt @@ -30,6 +30,7 @@ internal actual object DefaultExecutor : CoroutineDispatcher(), Delay { internal expect fun createDefaultDispatcher(): CoroutineDispatcher +@PublishedApi internal actual val DefaultDelay: Delay = DefaultExecutor public actual fun CoroutineScope.newCoroutineContext(context: CoroutineContext): CoroutineContext { diff --git a/kotlinx-coroutines-core/native/src/internal/StackTraceRecovery.kt b/kotlinx-coroutines-core/native/src/internal/StackTraceRecovery.kt index d93af9f4d2..903a6240a8 100644 --- a/kotlinx-coroutines-core/native/src/internal/StackTraceRecovery.kt +++ b/kotlinx-coroutines-core/native/src/internal/StackTraceRecovery.kt @@ -8,6 +8,8 @@ import kotlin.coroutines.* internal actual fun recoverStackTrace(exception: E, continuation: Continuation<*>): E = exception internal actual fun recoverStackTrace(exception: E): E = exception + +@PublishedApi internal actual fun unwrap(exception: E): E = exception internal actual suspend inline fun recoverAndThrow(exception: Throwable): Nothing = throw exception