https://kotlinlang.org logo
#k2-early-adopters
Title
# k2-early-adopters
o

Oliver.O

11/12/2023, 6:13 PM
Leaked in-place lambda: block: suspend (T) -> R
What is the compiler trying to tell me? (Code in 🧵)
Copy code
suspend fun <T : ExecutorCoroutineDispatcher, R> T.useWithBlockHound(block: suspend (T) -> R): R {
    contract {
        callsInPlace(block, InvocationKind.EXACTLY_ONCE)
    }
    var exception: Throwable? = null
    try {
        return block(this)
    } catch (e: Throwable) {
        exception = e
        throw e
    } finally {
        withContext(NonCancellable) {
            withBlockHoundMode(BlockHoundMode.DISABLED) {
                closeFinally(exception)
            }
        }
    }
}
One warning message is referring to the line
contract {
. A second one refers to
return block(this)
.
And then there's
block: suspend (T) -> R has wrong invocation kind: given EXACTLY_ONCE case, but ZERO case is possible
warning at line
contract {
, but for that I've already filed KT-63414.
d

dmitriy.novozhilov

11/12/2023, 7:06 PM
Usually this diagnostic means that lambda declared as calls-in-place is leaked into some not in-place context (e.g. send to some executor or saved into variable) But this particular case seems like a bug, so n please report another issue
👍 1
o

Oliver.O

11/12/2023, 8:13 PM
Thanks for the explanation. It seems the warning is basically addressing KT-15514. Will report the above case.
Seems that the compiler has a point, though: The docs say that functions declaring
callsInPlace
must be
inline
. If I change the above accordingly, the "leaked in-place lambda" warnings disappear. Created KT-63416.