Skip to content

Commit f24b60c

Browse files
committed
Narrow down return type of tryResumeSend/Receive
They return Symbol?
1 parent 2f8bff1 commit f24b60c

File tree

2 files changed

+26
-16
lines changed

2 files changed

+26
-16
lines changed

kotlinx-coroutines-core/common/src/channels/AbstractChannel.kt

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -450,8 +450,8 @@ internal abstract class AbstractSendChannel<E> : SendChannel<E> {
450450
@JvmField val select: SelectInstance<R>,
451451
@JvmField val block: suspend (SendChannel<E>) -> R
452452
) : Send(), DisposableHandle {
453-
override fun tryResumeSend(otherOp: PrepareOp?): Any? =
454-
select.trySelectOther(otherOp)
453+
override fun tryResumeSend(otherOp: PrepareOp?): Symbol? =
454+
select.trySelectOther(otherOp) as Symbol? // must return symbol
455455

456456
override fun completeResumeSend() {
457457
block.startCoroutine(receiver = channel, completion = select.completion)
@@ -473,7 +473,7 @@ internal abstract class AbstractSendChannel<E> : SendChannel<E> {
473473
@JvmField val element: E
474474
) : Send() {
475475
override val pollResult: Any? get() = element
476-
override fun tryResumeSend(otherOp: PrepareOp?): Any? = RESUME_TOKEN.also { otherOp?.finishPrepare() }
476+
override fun tryResumeSend(otherOp: PrepareOp?): Symbol? = RESUME_TOKEN.also { otherOp?.finishPrepare() }
477477
override fun completeResumeSend() {}
478478
override fun resumeSendClosed(closed: Closed<*>) {}
479479
}
@@ -898,9 +898,11 @@ internal abstract class AbstractChannel<E> : AbstractSendChannel<E>(), Channel<E
898898
}
899899

900900
@Suppress("IMPLICIT_CAST_TO_ANY")
901-
override fun tryResumeReceive(value: E, otherOp: PrepareOp?): Any? {
901+
override fun tryResumeReceive(value: E, otherOp: PrepareOp?): Symbol? {
902902
otherOp?.finishPrepare()
903-
return cont.tryResume(resumeValue(value), otherOp?.desc)
903+
val token = cont.tryResume(resumeValue(value), otherOp?.desc) ?: return null
904+
assert { token === RESUME_TOKEN } // the only other possible result
905+
return RESUME_TOKEN
904906
}
905907

906908
override fun completeResumeReceive(value: E) = cont.completeResume(RESUME_TOKEN)
@@ -919,9 +921,11 @@ internal abstract class AbstractChannel<E> : AbstractSendChannel<E>(), Channel<E
919921
@JvmField val iterator: Itr<E>,
920922
@JvmField val cont: CancellableContinuation<Boolean>
921923
) : Receive<E>() {
922-
override fun tryResumeReceive(value: E, otherOp: PrepareOp?): Any? {
924+
override fun tryResumeReceive(value: E, otherOp: PrepareOp?): Symbol? {
923925
otherOp?.finishPrepare()
924-
return cont.tryResume(true, otherOp?.desc)
926+
val token = cont.tryResume(true, otherOp?.desc) ?: return null
927+
assert { token === RESUME_TOKEN } // the only other possible result
928+
return RESUME_TOKEN
925929
}
926930

927931
override fun completeResumeReceive(value: E) {
@@ -953,8 +957,8 @@ internal abstract class AbstractChannel<E> : AbstractSendChannel<E>(), Channel<E
953957
@JvmField val block: suspend (Any?) -> R,
954958
@JvmField val receiveMode: Int
955959
) : Receive<E>(), DisposableHandle {
956-
override fun tryResumeReceive(value: E, otherOp: PrepareOp?): Any? =
957-
select.trySelectOther(otherOp)
960+
override fun tryResumeReceive(value: E, otherOp: PrepareOp?): Symbol? =
961+
select.trySelectOther(otherOp) as Symbol?
958962

959963
@Suppress("UNCHECKED_CAST")
960964
override fun completeResumeReceive(value: E) {
@@ -1019,7 +1023,7 @@ internal abstract class Send : LockFreeLinkedListNode() {
10191023
// RETRY_ATOMIC for retry (only when otherOp != null),
10201024
// RESUME_TOKEN on success (call completeResumeSend)
10211025
// Must call otherOp?.finishPrepare() before deciding on result other than RETRY_ATOMIC
1022-
abstract fun tryResumeSend(otherOp: PrepareOp?): Any?
1026+
abstract fun tryResumeSend(otherOp: PrepareOp?): Symbol?
10231027
abstract fun completeResumeSend()
10241028
abstract fun resumeSendClosed(closed: Closed<*>)
10251029
}
@@ -1033,7 +1037,7 @@ internal interface ReceiveOrClosed<in E> {
10331037
// RETRY_ATOMIC for retry (only when otherOp != null),
10341038
// RESUME_TOKEN on success (call completeResumeReceive)
10351039
// Must call otherOp?.finishPrepare() before deciding on result other than RETRY_ATOMIC
1036-
fun tryResumeReceive(value: E, otherOp: PrepareOp?): Any?
1040+
fun tryResumeReceive(value: E, otherOp: PrepareOp?): Symbol?
10371041
fun completeResumeReceive(value: E)
10381042
}
10391043

@@ -1045,9 +1049,11 @@ internal class SendElement(
10451049
override val pollResult: Any?,
10461050
@JvmField val cont: CancellableContinuation<Unit>
10471051
) : Send() {
1048-
override fun tryResumeSend(otherOp: PrepareOp?): Any? {
1052+
override fun tryResumeSend(otherOp: PrepareOp?): Symbol? {
10491053
otherOp?.finishPrepare()
1050-
return cont.tryResume(Unit, otherOp?.desc)
1054+
val token = cont.tryResume(Unit, otherOp?.desc)
1055+
assert { token === RESUME_TOKEN } // the only other possible result
1056+
return RESUME_TOKEN
10511057
}
10521058
override fun completeResumeSend() = cont.completeResume(RESUME_TOKEN)
10531059
override fun resumeSendClosed(closed: Closed<*>) = cont.resumeWithException(closed.sendException)
@@ -1065,9 +1071,9 @@ internal class Closed<in E>(
10651071

10661072
override val offerResult get() = this
10671073
override val pollResult get() = this
1068-
override fun tryResumeSend(otherOp: PrepareOp?): Any? = RESUME_TOKEN.also { otherOp?.finishPrepare() }
1074+
override fun tryResumeSend(otherOp: PrepareOp?): Symbol? = RESUME_TOKEN.also { otherOp?.finishPrepare() }
10691075
override fun completeResumeSend() {}
1070-
override fun tryResumeReceive(value: E, otherOp: PrepareOp?): Any? = RESUME_TOKEN.also { otherOp?.finishPrepare() }
1076+
override fun tryResumeReceive(value: E, otherOp: PrepareOp?): Symbol? = RESUME_TOKEN.also { otherOp?.finishPrepare() }
10711077
override fun completeResumeReceive(value: E) {}
10721078
override fun resumeSendClosed(closed: Closed<*>) = assert { false } // "Should be never invoked"
10731079
override fun toString(): String = "Closed[$closeCause]"

kotlinx-coroutines-core/common/src/selects/Select.kt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ public interface SelectClause2<in P, out Q> {
9393
*
9494
* @suppress **This is unstable API and it is subject to change.**
9595
*/
96-
@InternalCoroutinesApi
96+
@InternalCoroutinesApi // todo: sealed interface https://youtrack.jetbrains.com/issue/KT-22286
9797
public interface SelectInstance<in R> {
9898
/**
9999
* Returns `true` when this [select] statement had already picked a clause to execute.
@@ -112,6 +112,10 @@ public interface SelectInstance<in R> {
112112
* * `null` on failure to select (already selected).
113113
* [otherOp] is not null when trying to rendezvous with this select from inside of another select.
114114
* In this case, [PrepareOp.finishPrepare] must be called before deciding on any value other than [RETRY_ATOMIC].
115+
*
116+
* Note, that this method's actual return type is `Symbol?` but we cannot declare it as such, because this
117+
* member is public, but [Symbol] is internal. When [SelectInstance] becomes a `sealed interface`
118+
* (see KT-222860) we can declare this method as internal.
115119
*/
116120
public fun trySelectOther(otherOp: PrepareOp?): Any?
117121

0 commit comments

Comments
 (0)