From f30fd81d8cf72ce93a901bc1d10026d5392145ef Mon Sep 17 00:00:00 2001 From: Sam Clegg Date: Mon, 30 Mar 2026 16:40:55 -0700 Subject: [PATCH] Use C++20 requires clauses in TypeNameGeneratorBase This addresses the TODOs in src/wasm-type-printing.h by utilizing C++20 concepts and requires clauses. - Replaced the manual SFINAE/macro-based check in TypeNameGeneratorBase with a static_assert(requires { ... }) to ensure subclasses implement getNames correctly. This is cleaner and more robust. - Updated the ModuleTypeNameGenerator constructor to use a requires clause instead of std::enable_if_t for its default constructor, improving readability. - Added #include as required. --- src/wasm-type-printing.h | 24 ++++++------------------ 1 file changed, 6 insertions(+), 18 deletions(-) diff --git a/src/wasm-type-printing.h b/src/wasm-type-printing.h index 483c48cf0a3..377cda2e6e6 100644 --- a/src/wasm-type-printing.h +++ b/src/wasm-type-printing.h @@ -17,6 +17,7 @@ #ifndef wasm_wasm_type_printing_h #define wasm_wasm_type_printing_h +#include #include #include #include @@ -34,9 +35,6 @@ namespace wasm { template struct TypeNameGeneratorBase { TypeNameGeneratorBase() { assertValidUsage(); } - TypeNames getNames(HeapType type) { - WASM_UNREACHABLE("Derived class must implement getNames"); - } HeapType::Printed operator()(HeapType type) { return type.print( [&](HeapType ht) { return static_cast(this)->getNames(ht); }); @@ -48,16 +46,9 @@ template struct TypeNameGeneratorBase { private: constexpr void assertValidUsage() { - // This check current causes a crash on MSVC - // TODO: Convert to C++20 requires check -#if !defined(_MSC_VER) && (!defined(__GNUC__) || __GNUC__ >= 14) - // Check that the subclass provides `getNames` with the correct type. - using Self = TypeNameGeneratorBase; - static_assert( - static_cast(&Self::getNames) != - static_cast(&Subclass::getNames), - "Derived class must implement getNames"); -#endif + static_assert(requires(Subclass& s, HeapType ht) { + { s.getNames(ht) } -> std::same_as; + }, "Derived class must implement getNames"); } }; @@ -123,11 +114,8 @@ struct ModuleTypeNameGenerator ModuleTypeNameGenerator(const Module& wasm, FallbackGenerator& fallback) : wasm(wasm), fallback(fallback) {} - // TODO: Use C++20 `requires` to clean this up. - template - ModuleTypeNameGenerator( - const Module& wasm, - std::enable_if_t>* = nullptr) + ModuleTypeNameGenerator(const Module& wasm) + requires std::is_same_v : ModuleTypeNameGenerator(wasm, defaultGenerator) {} TypeNames getNames(HeapType type) {