From e5d596c9910e779426afe585cc7e11d08105302f Mon Sep 17 00:00:00 2001 From: PyXiion Date: Sun, 8 Mar 2026 20:08:14 +0300 Subject: [PATCH 1/2] C++ 23 --- CMakeLists.txt | 2 +- include/korka/compiler/compiler.hpp | 3 ++- include/korka/shared/error.hpp | 14 +++++++++++++ include/korka/utils/const_format.hpp | 30 ++++++++++++++++++++++++++-- include/korka/utils/string.hpp | 2 +- include/korka/utils/utils.hpp | 2 +- 6 files changed, 47 insertions(+), 6 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 1d641d2..e2e624a 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -5,7 +5,7 @@ project(pxkorka ) -set(CMAKE_CXX_STANDARD 26) +set(CMAKE_CXX_STANDARD 23) set(CMAKE_CXX_STANDARD_REQUIRED ON) set(CMAKE_CXX_EXTENSIONS OFF) diff --git a/include/korka/compiler/compiler.hpp b/include/korka/compiler/compiler.hpp index e887d6e..add5d3a 100644 --- a/include/korka/compiler/compiler.hpp +++ b/include/korka/compiler/compiler.hpp @@ -484,7 +484,8 @@ struct unique_type{}; }; if constexpr (not expected()) { - report_error<[]{return expected.error();}>(); + report_error<[]{return expected().error();}>(); + return expected().error(); } else { return compilation_result_to_const<[] constexpr { return expected().value(); }>(); } diff --git a/include/korka/shared/error.hpp b/include/korka/shared/error.hpp index 1fdf92d..f512aab 100644 --- a/include/korka/shared/error.hpp +++ b/include/korka/shared/error.hpp @@ -50,6 +50,7 @@ namespace korka { struct redeclaration { std::string_view identifier; }; + constexpr auto report(const redeclaration &err) -> std::string { return korka::format("Compiler Error: ~ was redeclared", err.identifier); } @@ -57,6 +58,7 @@ namespace korka { struct unknown_type { std::string_view identifier; }; + constexpr auto report(const unknown_type &err) -> std::string { return korka::format("Compiler Error: unknown type `~`", err.identifier); } @@ -64,6 +66,7 @@ namespace korka { struct undefined_symbol { std::string_view identifier; }; + constexpr auto report(const undefined_symbol &err) -> std::string { return korka::format("Compiler Error: symbol `~` not defined", err.identifier); } @@ -72,6 +75,7 @@ namespace korka { std::string_view return_type; std::string_view actual_type; }; + constexpr auto report(const function_return_type_mismatch &err) -> std::string { return korka::format("Compiler Error: expected ~ type to be returned, got ~", err.return_type, err.actual_type); } @@ -110,8 +114,18 @@ namespace korka { }, err); } + template + struct ErrorMessage { + static_assert(false, "Check the template parameter for details"); + }; + template consteval auto report_error() -> void { + #if __cpp_static_assert >= 202306L static_assert(false, to_string(err_getter())); + #else + constexpr auto msg = const_string_from_string_view<[]{return to_string(err_getter());}>(); + std::ignore = ErrorMessage{}; + #endif } } \ No newline at end of file diff --git a/include/korka/utils/const_format.hpp b/include/korka/utils/const_format.hpp index 0f894fc..429a247 100644 --- a/include/korka/utils/const_format.hpp +++ b/include/korka/utils/const_format.hpp @@ -9,6 +9,32 @@ namespace korka { namespace detail { +// // Custom std::to_chars, since it's not constexpr in C++ 20 +// template +// constexpr auto internal_to_chars(char* first, char* last, T value) -> char* { +// if (value == 0) { +// *first = '0'; +// return first + 1; +// } +// +// if constexpr (std::is_signed_v) { +// if (value < 0) { +// *first++ = '-'; +// // Handle potential overflow for minimum values +// unsigned long long v = static_cast(-(value + 1)) + 1; +// return internal_to_chars(first, last, v); +// } +// } +// +// char* start = first; +// while (value > 0) { +// *first++ = static_cast('0' + (value % 10)); +// value /= 10; +// } +// std::reverse(start, first); +// return first; +// } + constexpr auto to_string_helper(const auto &value) -> std::string { using T = std::decay_t; @@ -18,8 +44,8 @@ namespace korka { return std::string(1, value); } else if constexpr (requires { std::to_chars(std::declval(), std::declval(), value); }) { std::array buffer{}; - auto [ptr, ec] = std::to_chars(buffer.data(), buffer.data() + buffer.size(), value); - return std::string(buffer.data(), ptr); + auto [end, _] = std::to_chars(buffer.data(), buffer.data() + buffer.size(), value); + return std::string(buffer.data(), end ); } else { return "?"; } diff --git a/include/korka/utils/string.hpp b/include/korka/utils/string.hpp index efae322..69bae52 100644 --- a/include/korka/utils/string.hpp +++ b/include/korka/utils/string.hpp @@ -26,7 +26,7 @@ namespace korka { template constexpr auto const_string_from_string_view() { - const_string str; + const_string str; std::copy_n(sv_getter().data(), str.length, str.value); return str; } diff --git a/include/korka/utils/utils.hpp b/include/korka/utils/utils.hpp index 4270127..76df5d4 100644 --- a/include/korka/utils/utils.hpp +++ b/include/korka/utils/utils.hpp @@ -8,7 +8,7 @@ namespace korka { template constexpr auto to_array() { using value_type = typename decltype(data_getter())::value_type; - constexpr static std::size_t size = data_getter().size(); + constexpr std::size_t size = data_getter().size(); std::array out; auto in = data_getter(); From 6add65b018d33a90e7fdc53f1472aea8bbc2684b Mon Sep 17 00:00:00 2001 From: PyXiion Date: Sun, 8 Mar 2026 20:12:05 +0300 Subject: [PATCH 2/2] Fix clang --- include/korka/shared/error.hpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/include/korka/shared/error.hpp b/include/korka/shared/error.hpp index f512aab..d70ea90 100644 --- a/include/korka/shared/error.hpp +++ b/include/korka/shared/error.hpp @@ -121,7 +121,8 @@ namespace korka { template consteval auto report_error() -> void { - #if __cpp_static_assert >= 202306L + // Idk, __cpp_static_assert check is not enough for clang + #if __cplusplus >= 202400L && __cpp_static_assert >= 202306L static_assert(false, to_string(err_getter())); #else constexpr auto msg = const_string_from_string_view<[]{return to_string(err_getter());}>();