Skip to content

Commit 542c293

Browse files
fujiijustinmichaud
authored andcommitted
EnumTraits.h: error: no matching function for call to 'enumName' with Clang 20
https://bugs.webkit.org/show_bug.cgi?id=289669 Reviewed by Keith Miller. Clang 20 couldn't compile EnumTraits.h. > wtf/EnumTraits.h:212:33: note: candidate template ignored: invalid explicitly-specified argument for template parameter 'V' An invalid enum value can't be specifed to the template parameter `V`. > template<auto V> constexpr std::span<const char> enumName() The upstream Magic Enum C++ has a template variable `is_enum_constexpr_static_cast_valid<E, V>` to check a enum value is valid. <https://github.com/Neargye/magic_enum/blob/a413fcc9c46a020a746907136a384c227f3cd095/include/magic_enum/magic_enum.hpp#L624-L634> Imported the template variable. * Source/WTF/wtf/EnumTraits.h: (WTF::enumName): (WTF::makeEnumNames): Canonical link: https://commits.webkit.org/292321@main
1 parent c255458 commit 542c293

1 file changed

Lines changed: 20 additions & 1 deletion

File tree

Source/WTF/wtf/EnumTraits.h

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,16 @@ constexpr bool isZeroBasedContiguousEnum()
161161
#pragma clang diagnostic ignored "-Wenum-constexpr-conversion"
162162
#endif
163163

164+
#if COMPILER(CLANG) && __clang_major__ >= 16
165+
template <typename E, auto V, typename = void>
166+
inline constexpr bool isEnumConstexprStaticCastValid = false;
167+
template <typename E, auto V>
168+
inline constexpr bool isEnumConstexprStaticCastValid<E, V, std::void_t<std::integral_constant<E, static_cast<E>(V)>>> = true;
169+
#else
170+
template <typename, auto>
171+
inline constexpr bool isEnumConstexprStaticCastValid = true;
172+
#endif
173+
164174
template<typename E>
165175
constexpr std::span<const char> enumTypeNameImpl()
166176
{
@@ -224,6 +234,15 @@ constexpr std::span<const char> enumName()
224234
return result;
225235
}
226236

237+
template<typename E, auto V>
238+
constexpr std::span<const char> enumName()
239+
{
240+
if constexpr (isEnumConstexprStaticCastValid<E, V>)
241+
return enumName<static_cast<E>(V)>();
242+
else
243+
return { };
244+
}
245+
227246
namespace detail {
228247

229248
template<size_t i, size_t end>
@@ -243,7 +262,7 @@ constexpr std::array<std::span<const char>, limit> enumNames()
243262
std::array<std::span<const char>, limit> names;
244263

245264
detail::forConstexpr<0, limit>([&] (auto i) {
246-
names[i] = enumName<static_cast<E>(static_cast<unsigned>(i))>();
265+
names[i] = enumName<E, std::underlying_type_t<E>(static_cast<unsigned>(i))>();
247266
});
248267
return names;
249268
}

0 commit comments

Comments
 (0)