Skip to content

Commit

Permalink
Fix issues with contracts found by building with K2
Browse files Browse the repository at this point in the history
  • Loading branch information
lukellmann committed Apr 9, 2024
1 parent 7350a21 commit 681b22d
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 6 deletions.
8 changes: 6 additions & 2 deletions core/src/commonMain/kotlin/Util.kt
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,10 @@ import kotlin.contracts.contract
import kotlin.reflect.KClass

internal inline fun <T> catchNotFound(block: () -> T): T? {
// block is called exactly once, but might not be executed fully, even if catchNotFound returns normally
// -> AT_MOST_ONCE, see https://youtrack.jetbrains.com/issue/KT-63414
contract {
callsInPlace(block, InvocationKind.EXACTLY_ONCE)
callsInPlace(block, InvocationKind.AT_MOST_ONCE)
}
return try {
block()
Expand All @@ -41,8 +43,10 @@ internal inline fun <T> catchNotFound(block: () -> T): T? {
}

internal inline fun <T> catchDiscordError(vararg codes: JsonErrorCode, block: () -> T): T? {
// block is called exactly once, but might not be executed fully, even if catchDiscordError returns normally
// -> AT_MOST_ONCE, see https://youtrack.jetbrains.com/issue/KT-63414
contract {
callsInPlace(block, InvocationKind.EXACTLY_ONCE)
callsInPlace(block, InvocationKind.AT_MOST_ONCE)
}
return try {
block()
Expand Down
3 changes: 0 additions & 3 deletions core/src/commonMain/kotlin/builder/kord/KordBuilder.kt
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,6 @@ import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.MutableSharedFlow
import kotlinx.serialization.json.Json
import kotlin.contracts.InvocationKind
import kotlin.contracts.contract

private val gatewayInfoJson = Json { ignoreUnknownKeys = true }

Expand Down Expand Up @@ -169,7 +167,6 @@ public abstract class BaseKordBuilder internal constructor(public val token: Str
* ```
*/
public fun cache(builder: KordCacheBuilder.(resources: ClientResources) -> Unit) {
contract { callsInPlace(builder, InvocationKind.EXACTLY_ONCE) }
val old = cacheBuilder
cacheBuilder = { resources: ClientResources ->
old(resources)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ public class MediaChannelModifyBuilder : PermissionOverwritesModifyBuilder,
private var _availableTags: Optional<MutableList<ForumTagRequest>> = Optional.Missing()
public var availableTags: MutableList<ForumTagRequest>? by ::_availableTags.delegate()

public fun tag(name: String, builder: ForumTagBuilder.() -> Unit = {}) {
public inline fun tag(name: String, builder: ForumTagBuilder.() -> Unit = {}) {
contract { callsInPlace(builder, InvocationKind.EXACTLY_ONCE) }
val tag = ForumTagBuilder(name).apply(builder).toRequest()
availableTags?.add(tag) ?: run { availableTags = mutableListOf(tag) }
Expand Down

0 comments on commit 681b22d

Please sign in to comment.