Add the EXACTLY_ONCE contract to suspendCancellableContinuation - #4574
Merged
dkhalanskyjb merged 1 commit intoNov 25, 2025
Merged
Conversation
`suspendCancellableContinuation` invokes its block exactly once. `suspendCoroutineUninterceptedOrReturn`, which is used for implementing `suspendCancellableContinuation`, already has a contract stating just that, so for consistency and completeness, we add a contract to `suspendCancellableContinuation` itself as well. This will let the compiler recognize that `val`-variables can be safely assigned inside the lambda, as they won't be reassigned or left uninitialized (which would be the case if the lambda executed more than once or possibly not execute at all). There were no use cases actually reported for this.
murfel
approved these changes
Nov 25, 2025
| finish(5) | ||
| } | ||
|
|
||
| /** Tests that the compiler recognizes that [suspendCancellableCoroutine] invokes its block exactly once. */ |
Contributor
There was a problem hiding this comment.
by allowing the val variable
(the point of focus could be not immediately clear in future)
Collaborator
Author
There was a problem hiding this comment.
Given that the whole test is five lines, there isn't a lot of room for misinterpreting what the test does, especially given that the KDoc describes that we're testing the EXACTLY_ONCE contract.
| } | ||
| assertEquals(1, i) | ||
| } | ||
| } No newline at end of file |
Contributor
There was a problem hiding this comment.
Could you set your editor to add newlines?
Collaborator
Author
There was a problem hiding this comment.
It's not at all straightforward, I'm afraid. Of course, I will make sure to do that if I learn that the lack of newlines in Kotlin files leads to some actual problems.
dkhalanskyjb
deleted the
dkhalanskyjb/suspendCancellableContinuation-contract
branch
November 25, 2025 13:02
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
suspendCancellableContinuationinvokes its block exactly once.suspendCoroutineUninterceptedOrReturn,which is used for implementing
suspendCancellableContinuation, already has a contract stating just that,so for consistency and completeness, we add a contract to
suspendCancellableContinuationitself as well.This will let the compiler recognize that
val-variables can be safely assigned inside the lambda, as they won't be reassigned or left uninitialized (which would be the case if the lambda executed more than once or possibly not execute at all).There were no use cases actually reported for this.