try
diff --git a/kotlin-native/runtime/src/alloc/custom/cpp/AllocationSize.hpp b/kotlin-native/runtime/src/alloc/custom/cpp/AllocationSize.hpp
index 4dcbf5b..5114955 100644
--- a/kotlin-native/runtime/src/alloc/custom/cpp/AllocationSize.hpp
+++ b/kotlin-native/runtime/src/alloc/custom/cpp/AllocationSize.hpp
@@ -33,9 +33,7 @@
}
static constexpr AllocationSize bytesExactly(uint64_t bytes) {
AllocationSize atLeast = bytesAtLeast(bytes);
- if (atLeast.inBytes() != bytes) {
- RuntimeFail("The allocations size %" PRIu64 " must be a multiple of Cell size", bytes);
- }
+ RuntimeAssert(atLeast.inBytes() == bytes, "The allocations size %" PRIu64 " must be a multiple of Cell size", bytes);
return atLeast;
}
@@ -61,12 +59,12 @@
constexpr AllocationSize operator+(const AllocationSize& other) const noexcept { return AllocationSize{*this} += other; }
- AllocationSize& operator-=(const AllocationSize& other) noexcept {
+ constexpr AllocationSize& operator-=(const AllocationSize& other) noexcept {
RuntimeAssert(cells_ >= other.cells_, "Subtraction would cause a negative value");
cells_ -= other.cells_;
return *this;
}
- AllocationSize operator-(const AllocationSize& other) const noexcept { return AllocationSize{*this} -= other; }
+ constexpr AllocationSize operator-(const AllocationSize& other) const noexcept { return AllocationSize{*this} -= other; }
constexpr AllocationSize& operator*=(uint32_t multiplier) noexcept {
cells_ *= multiplier;
@@ -93,4 +91,6 @@
static_assert(AllocationSize::cells(3) + AllocationSize::cells(7) == AllocationSize::cells(10));
static_assert(AllocationSize::cells(3) * 7 == AllocationSize::cells(21));
+static_assert((AllocationSize::cells(37) - AllocationSize::cells(37)).inBytes() == 0);
+
}
\ No newline at end of file
diff --git a/kotlin-native/runtime/src/main/cpp/KAssert.h b/kotlin-native/runtime/src/main/cpp/KAssert.h
index 9da933d..b0e6ec6 100644
--- a/kotlin-native/runtime/src/main/cpp/KAssert.h
+++ b/kotlin-native/runtime/src/main/cpp/KAssert.h
@@ -19,6 +19,7 @@
#include "Common.h"
#include "CompilerConstants.hpp"
+#include "std_support/TypeTraits.hpp"
#define STRINGIFY(x) #x
#define TOSTRING(x) STRINGIFY(x)
@@ -45,19 +46,21 @@
// Use RuntimeAssert() in internal state checks, which could be ignored in production.
#define RuntimeAssert(condition, format, ...) \
- do { \
- switch (::kotlin::compiler::runtimeAssertsMode()) { \
- case ::kotlin::compiler::RuntimeAssertsMode::kIgnore: break; \
- case ::kotlin::compiler::RuntimeAssertsMode::kLog: \
- if (!(condition)) { \
- ::kotlin::internal::RuntimeAssertFailedLog(true, CURRENT_SOURCE_LOCATION, format, ##__VA_ARGS__); \
- } \
- break; \
- case ::kotlin::compiler::RuntimeAssertsMode::kPanic: \
- if (!(condition)) { \
- ::kotlin::internal::RuntimeAssertFailedPanic(true, CURRENT_SOURCE_LOCATION, format, ##__VA_ARGS__); \
- } \
- break; \
+ do { \
+ if (!::kotlin::std_support::is_constant_evaluated() || !(condition)) { \
+ switch (::kotlin::compiler::runtimeAssertsMode()) { \
+ case ::kotlin::compiler::RuntimeAssertsMode::kIgnore: break; \
+ case ::kotlin::compiler::RuntimeAssertsMode::kLog: \
+ if (!(condition)) { \
+ ::kotlin::internal::RuntimeAssertFailedLog(true, CURRENT_SOURCE_LOCATION, format, ##__VA_ARGS__); \
+ } \
+ break; \
+ case ::kotlin::compiler::RuntimeAssertsMode::kPanic: \
+ if (!(condition)) { \
+ ::kotlin::internal::RuntimeAssertFailedPanic(true, CURRENT_SOURCE_LOCATION, format, ##__VA_ARGS__); \
+ } \
+ break; \
+ } \
} \
} while (false)
diff --git a/kotlin-native/runtime/src/main/cpp/std_support/TypeTraits.hpp b/kotlin-native/runtime/src/main/cpp/std_support/TypeTraits.hpp
new file mode 100644
index 0000000..3a09835
--- /dev/null
+++ b/kotlin-native/runtime/src/main/cpp/std_support/TypeTraits.hpp
@@ -0,0 +1,16 @@
+/*
+ * Copyright 2010-2024 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license
+ * that can be found in the LICENSE file.
+ */
+
+#pragma once
+
+namespace kotlin::std_support {
+
+constexpr bool is_constant_evaluated() {
+ return __builtin_is_constant_evaluated();
+}
+
+static_assert(is_constant_evaluated());
+
+}