[llvm] [ADT] Simplify ResolveUnderlyingType (NFC) (PR #164114)

Kazu Hirata via llvm-commits llvm-commits at lists.llvm.org
Sat Oct 18 12:33:51 PDT 2025


https://github.com/kazutakahirata created https://github.com/llvm/llvm-project/pull/164114

We have three implementations of ResolveUnderlyingType:

- enum
- bool
- neither

This patch combines the latter two with std::conditional_t.

Without this patch, we use "void" to trigger a compilation failure
downstream when sizeof(bool) != 1, which is not very friendly.  This
patch instead uses static_assert to catch the case where the user
chooses to use bool but sizeof(bool) != 1.


>From f671f1a3e55e2ca26e77a625560e681f9a43b528 Mon Sep 17 00:00:00 2001
From: Kazu Hirata <kazu at google.com>
Date: Mon, 13 Oct 2025 09:13:19 -0700
Subject: [PATCH] [ADT] Simplify ResolveUnderlyingType (NFC)

We have three implementations of ResolveUnderlyingType:

- enum
- bool
- neither

This patch combines the latter two with std::conditional_t.

Without this patch, we use "void" to trigger a compilation failure
downstream when sizeof(bool) != 1, which is not very friendly.  This
patch instead uses static_assert to catch the case where the user
chooses to use bool but sizeof(bool) != 1.
---
 llvm/include/llvm/ADT/Bitfields.h | 9 +++------
 1 file changed, 3 insertions(+), 6 deletions(-)

diff --git a/llvm/include/llvm/ADT/Bitfields.h b/llvm/include/llvm/ADT/Bitfields.h
index 1af276145ba48..1fbc41c472581 100644
--- a/llvm/include/llvm/ADT/Bitfields.h
+++ b/llvm/include/llvm/ADT/Bitfields.h
@@ -154,12 +154,9 @@ struct ResolveUnderlyingType {
   using type = std::underlying_type_t<T>;
 };
 template <typename T> struct ResolveUnderlyingType<T, false> {
-  using type = T;
-};
-template <> struct ResolveUnderlyingType<bool, false> {
-  /// In case sizeof(bool) != 1, replace `void` by an additionnal
-  /// std::conditional.
-  using type = std::conditional_t<sizeof(bool) == 1, uint8_t, void>;
+  static_assert(!std::is_same_v<T, bool> || sizeof(bool) == 1,
+                "T being bool requires sizeof(bool) == 1.");
+  using type = std::conditional_t<std::is_same_v<T, bool>, uint8_t, T>;
 };
 
 } // namespace bitfields_details



More information about the llvm-commits mailing list