File tree Expand file tree Collapse file tree 2 files changed +23
-4
lines changed
kotlinx-coroutines-core/common Expand file tree Collapse file tree 2 files changed +23
-4
lines changed Original file line number Diff line number Diff line change 11/*
2- * Copyright 2016-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
2+ * Copyright 2016-2021 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
33 */
44@file:JvmMultifileClass
55@file:JvmName(" ChannelsKt" )
66@file:Suppress(" DEPRECATION_ERROR" )
7+ @file:OptIn(ExperimentalContracts ::class )
78
89package kotlinx.coroutines.channels
910
1011import kotlinx.coroutines.*
1112import kotlinx.coroutines.selects.*
13+ import kotlin.contracts.*
1214import kotlin.coroutines.*
1315import kotlin.jvm.*
1416
@@ -164,6 +166,9 @@ public fun consumesAll(vararg channels: ReceiveChannel<*>): CompletionHandler =
164166 * The operation is _terminal_.
165167 */
166168public inline fun <E , R > ReceiveChannel<E>.consume (block : ReceiveChannel <E >.() -> R ): R {
169+ contract {
170+ callsInPlace(block, InvocationKind .EXACTLY_ONCE )
171+ }
167172 var cause: Throwable ? = null
168173 try {
169174 return block()
Original file line number Diff line number Diff line change 11/*
2- * Copyright 2016-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
2+ * Copyright 2016-2021 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
33 */
44
55package kotlinx.coroutines
66
7+ import kotlinx.coroutines.channels.*
78import kotlinx.coroutines.selects.*
89import kotlin.test.*
910
@@ -44,9 +45,22 @@ class BuilderContractsTest : TestBase() {
4445 Job ().apply { complete() }.onJoin {}
4546 }
4647 consume(s)
48+
49+
50+ val ch: Int
51+ val i = Channel <Int >()
52+ i.consume {
53+ ch = 321
54+ }
55+ consume(ch)
4756 }
4857
4958 private fun consume (a : Int ) {
50- a.hashCode() // BE codegen verification
59+ /*
60+ * Verify the value is actually set correctly
61+ * (non-zero, VerificationError is not triggered, can be read)
62+ */
63+ assertNotEquals(0 , a)
64+ assertEquals(a.hashCode(), a)
5165 }
52- }
66+ }
You can’t perform that action at this time.
0 commit comments