Skip to content

Conversation

@dkhalanskyjb
Copy link
Collaborator

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.

`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.
@dkhalanskyjb dkhalanskyjb requested a review from murfel November 25, 2025 11:37
finish(5)
}

/** Tests that the compiler recognizes that [suspendCancellableCoroutine] invokes its block exactly once. */
Copy link
Contributor

@murfel murfel Nov 25, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

by allowing the val variable

(the point of focus could be not immediately clear in future)

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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)
}
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you set your editor to add newlines?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 dkhalanskyjb merged commit d1c583a into develop Nov 25, 2025
@dkhalanskyjb dkhalanskyjb deleted the dkhalanskyjb/suspendCancellableContinuation-contract branch November 25, 2025 13:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants