diff --git a/mirai-console/backend/codegen/src/MessageScopeCodegen.kt b/mirai-console/backend/codegen/src/MessageScopeCodegen.kt index 8fff5ae04..39409ee8a 100644 --- a/mirai-console/backend/codegen/src/MessageScopeCodegen.kt +++ b/mirai-console/backend/codegen/src/MessageScopeCodegen.kt @@ -1,10 +1,10 @@ /* - * Copyright 2019-2021 Mamoe Technologies and contributors. + * Copyright 2019-2022 Mamoe Technologies and contributors. * - * 此源代码的使用受 GNU AFFERO GENERAL PUBLIC LICENSE version 3 许可证的约束, 可以在以下链接找到该许可证. - * Use of this source code is governed by the GNU AGPLv3 license that can be found through the following link. + * 此源代码的使用受 GNU AFFERO GENERAL PUBLIC LICENSE version 3 许可证的约束, 可以在以下链接找到该许可证. + * Use of this source code is governed by the GNU AGPLv3 license that can be found through the following link. * - * https://github.com/mamoe/mirai/blob/master/LICENSE + * https://github.com/mamoe/mirai/blob/dev/LICENSE */ package net.mamoe.mirai.console.codegen @@ -151,6 +151,7 @@ internal object MessageScopeCodegen { appendKCode( """ public inline fun <R> ${a}?.scopeWith(vararg others: ${b}?, action: MessageScope.() -> R): R { + contract { callsInPlace(action, InvocationKind.EXACTLY_ONCE) } return scopeWith(*others).invoke(action) } """ @@ -182,7 +183,10 @@ internal object MessageScopeCodegen { "net.mamoe.mirai.console.util.invoke", ) ) // diagnostic deprecation - public inline fun <R> ${a}.scopeWith(action: MessageScope.() -> R): R = asMessageScope()(action) + public inline fun <R> ${a}.scopeWith(action: MessageScope.() -> R): R { + contract { callsInPlace(action, InvocationKind.EXACTLY_ONCE) } + return asMessageScope()(action) + } """ ) appendLine() diff --git a/mirai-console/backend/mirai-console/src/util/MessageScope.kt b/mirai-console/backend/mirai-console/src/util/MessageScope.kt index 017a7fd34..d2d7add4c 100644 --- a/mirai-console/backend/mirai-console/src/util/MessageScope.kt +++ b/mirai-console/backend/mirai-console/src/util/MessageScope.kt @@ -1,10 +1,10 @@ /* - * Copyright 2019-2021 Mamoe Technologies and contributors. + * Copyright 2019-2022 Mamoe Technologies and contributors. * - * 此源代码的使用受 GNU AFFERO GENERAL PUBLIC LICENSE version 3 许可证的约束, 可以在以下链接找到该许可证. - * Use of this source code is governed by the GNU AGPLv3 license that can be found through the following link. + * 此源代码的使用受 GNU AFFERO GENERAL PUBLIC LICENSE version 3 许可证的约束, 可以在以下链接找到该许可证. + * Use of this source code is governed by the GNU AGPLv3 license that can be found through the following link. * - * https://github.com/mamoe/mirai/blob/master/LICENSE + * https://github.com/mamoe/mirai/blob/dev/LICENSE */ @file:Suppress( @@ -22,6 +22,8 @@ import net.mamoe.mirai.console.command.CommandSender import net.mamoe.mirai.contact.Contact import net.mamoe.mirai.contact.User import net.mamoe.mirai.message.data.Message +import kotlin.contracts.InvocationKind +import kotlin.contracts.contract import kotlin.internal.LowPriorityInOverloadResolution /** @@ -290,38 +292,47 @@ public fun MessageScope?.scopeWith(other: MessageScope?): MessageScope { } public inline fun <R> Contact?.scopeWith(vararg others: Contact?, action: MessageScope.() -> R): R { + contract { callsInPlace(action, InvocationKind.EXACTLY_ONCE) } return scopeWith(*others).invoke(action) } public inline fun <R> Contact?.scopeWith(vararg others: CommandSender?, action: MessageScope.() -> R): R { + contract { callsInPlace(action, InvocationKind.EXACTLY_ONCE) } return scopeWith(*others).invoke(action) } public inline fun <R> Contact?.scopeWith(vararg others: MessageScope?, action: MessageScope.() -> R): R { + contract { callsInPlace(action, InvocationKind.EXACTLY_ONCE) } return scopeWith(*others).invoke(action) } public inline fun <R> CommandSender?.scopeWith(vararg others: Contact?, action: MessageScope.() -> R): R { + contract { callsInPlace(action, InvocationKind.EXACTLY_ONCE) } return scopeWith(*others).invoke(action) } public inline fun <R> CommandSender?.scopeWith(vararg others: CommandSender?, action: MessageScope.() -> R): R { + contract { callsInPlace(action, InvocationKind.EXACTLY_ONCE) } return scopeWith(*others).invoke(action) } public inline fun <R> CommandSender?.scopeWith(vararg others: MessageScope?, action: MessageScope.() -> R): R { + contract { callsInPlace(action, InvocationKind.EXACTLY_ONCE) } return scopeWith(*others).invoke(action) } public inline fun <R> MessageScope?.scopeWith(vararg others: Contact?, action: MessageScope.() -> R): R { + contract { callsInPlace(action, InvocationKind.EXACTLY_ONCE) } return scopeWith(*others).invoke(action) } public inline fun <R> MessageScope?.scopeWith(vararg others: CommandSender?, action: MessageScope.() -> R): R { + contract { callsInPlace(action, InvocationKind.EXACTLY_ONCE) } return scopeWith(*others).invoke(action) } public inline fun <R> MessageScope?.scopeWith(vararg others: MessageScope?, action: MessageScope.() -> R): R { + contract { callsInPlace(action, InvocationKind.EXACTLY_ONCE) } return scopeWith(*others).invoke(action) } @@ -351,7 +362,10 @@ public inline fun MessageScope.scopeWith(): MessageScope = asMessageScope() "net.mamoe.mirai.console.util.invoke", ) ) // diagnostic deprecation -public inline fun <R> Contact.scopeWith(action: MessageScope.() -> R): R = asMessageScope()(action) +public inline fun <R> Contact.scopeWith(action: MessageScope.() -> R): R { + contract { callsInPlace(action, InvocationKind.EXACTLY_ONCE) } + return asMessageScope()(action) +} @Deprecated( "Senseless scopeWith. Use .asMessageScope().invoke.", @@ -361,7 +375,10 @@ public inline fun <R> Contact.scopeWith(action: MessageScope.() -> R): R = asMes "net.mamoe.mirai.console.util.invoke", ) ) // diagnostic deprecation -public inline fun <R> CommandSender.scopeWith(action: MessageScope.() -> R): R = asMessageScope()(action) +public inline fun <R> CommandSender.scopeWith(action: MessageScope.() -> R): R { + contract { callsInPlace(action, InvocationKind.EXACTLY_ONCE) } + return asMessageScope()(action) +} @Deprecated( "Senseless scopeWith. Use .asMessageScope().invoke.", @@ -371,7 +388,10 @@ public inline fun <R> CommandSender.scopeWith(action: MessageScope.() -> R): R = "net.mamoe.mirai.console.util.invoke", ) ) // diagnostic deprecation -public inline fun <R> MessageScope.scopeWith(action: MessageScope.() -> R): R = asMessageScope()(action) +public inline fun <R> MessageScope.scopeWith(action: MessageScope.() -> R): R { + contract { callsInPlace(action, InvocationKind.EXACTLY_ONCE) } + return asMessageScope()(action) +} //// endregion MessageScopeBuilders CODEGEN ////