From 450e66ccfd256b0d7c964c6d8c3aebb9c3d4e18a Mon Sep 17 00:00:00 2001 From: Him188 Date: Tue, 17 Nov 2020 09:29:53 +0800 Subject: [PATCH] Add internal UNREACHABLE_CLAUSE, assertUnreachable, ifNull --- .../src/internal/util/CommonUtils.kt | 23 +++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/backend/mirai-console/src/internal/util/CommonUtils.kt b/backend/mirai-console/src/internal/util/CommonUtils.kt index a28170631..d52b4a8b9 100644 --- a/backend/mirai-console/src/internal/util/CommonUtils.kt +++ b/backend/mirai-console/src/internal/util/CommonUtils.kt @@ -41,8 +41,27 @@ internal fun StackFrame.findLoader(): ClassLoader? { }.getOrNull() } +internal inline fun T?.ifNull(block: () -> T): T { + contract { callsInPlace(block, InvocationKind.AT_MOST_ONCE) } + return this ?: block() +} + @PublishedApi -internal inline fun assertionError(message: () -> String): Nothing { +internal inline fun assertionError(message: () -> String = { "Reached an unexpected branch." }): Nothing { contract { callsInPlace(message, InvocationKind.EXACTLY_ONCE) } throw AssertionError(message()) -} \ No newline at end of file +} + +@PublishedApi +internal inline fun assertUnreachable(message: () -> String = { "Reached an unexpected branch." }): Nothing { + contract { callsInPlace(message, InvocationKind.EXACTLY_ONCE) } + throw AssertionError(message()) +} + +@MarkerUnreachableClause +@PublishedApi +internal inline val UNREACHABLE_CLAUSE: Nothing + get() = assertUnreachable() + +@DslMarker +private annotation class MarkerUnreachableClause \ No newline at end of file