Skip to content

Commit 0fde0e3

Browse files
authored
fix: Literals not implementing strict weak ordering correctly (#188)
* Not working. Committed before I stuff shit up further. * Undo lunacy * Note to self * This is actually starting to find issues * Fix sort bug (ImHex, not checker) * Move variant type index code into header * DRY * Tidy up * Make sort checks optional * tidy up * Put checks in other container types * Disable sort checks * Make enable macro cover more code * Remove namespace qual * disable sort checks * Add static_assert. Fiddle around * Remove sort check code * Remove formatting change * Adjust codeing style * More style changes
1 parent 716dca5 commit 0fde0e3

File tree

3 files changed

+37
-3
lines changed

3 files changed

+37
-3
lines changed

lib/include/pl/core/token.hpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -220,8 +220,10 @@ namespace pl::core {
220220
constexpr bool operator==(const Comment &) const = default;
221221
};
222222

223+
using LiteralVariantType = std::variant<char, bool, u128, i128, double, std::string, std::shared_ptr<ptrn::Pattern>>;
223224

224-
struct Literal : std::variant<char, bool, u128, i128, double, std::string, std::shared_ptr<ptrn::Pattern>> {
225+
226+
struct Literal : LiteralVariantType {
225227
using variant::variant;
226228

227229
[[nodiscard]] std::shared_ptr<ptrn::Pattern> toPattern() const;
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#pragma once
2+
3+
#include <variant>
4+
#include <type_traits>
5+
#include <cstddef>
6+
7+
namespace pl::hlp {
8+
9+
template<typename T, typename Variant>
10+
struct VariantTypeIndexImpl;
11+
12+
template<typename T, typename... Ts>
13+
struct VariantTypeIndexImpl<T, std::variant<Ts...>> {
14+
static constexpr auto value = [] -> std::size_t {
15+
static_assert(
16+
(std::is_same_v<std::remove_cvref_t<T>, Ts> || ...),
17+
"T not in std::variant");
18+
constexpr bool matches[] = { std::is_same_v<std::remove_cvref_t<T>, Ts>... };
19+
20+
for (std::size_t i=0; i<sizeof...(Ts); ++i) {
21+
if (matches[i]) { return i; }
22+
}
23+
24+
return -1;
25+
}();
26+
};
27+
28+
template<class T, class Variant>
29+
inline constexpr std::size_t VariantTypeIndex = VariantTypeIndexImpl<T, Variant>::value;
30+
31+
} // namespace pl::hlp

lib/source/pl/core/token.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
#include <pl/helpers/utils.hpp>
77
#include <pl/helpers/concepts.hpp>
8+
#include <pl/helpers/variant_type_index.hpp>
89
#include <variant>
910

1011
namespace pl::core {
@@ -160,8 +161,8 @@ namespace pl::core {
160161
return std::strong_ordering::greater;
161162
}
162163
},
163-
[](auto, auto) -> std::strong_ordering {
164-
return std::strong_ordering::equal;
164+
[]<typename TL, typename TR>(TL, TR) -> std::strong_ordering {
165+
return hlp::VariantTypeIndex<TL, LiteralVariantType> <=> hlp::VariantTypeIndex<TR, LiteralVariantType>;
165166
}
166167
}, *this, other);
167168
}

0 commit comments

Comments
 (0)