Skip to content

Conversation

kazutakahirata
Copy link
Contributor

We have two implementations of IsResizableBase that are selected with
a boolean template parameter. This patch consolidates them into one
with "constexpr if". The "constexpr if" condition uses
llvm::is_detected to check the availability of resize().

We have two implementations of IsResizableBase that are selected with
a boolean template parameter.  This patch consolidates them into one
with "constexpr if".  The "constexpr if" condition uses
llvm::is_detected to check the availability of resize().
@llvmbot
Copy link
Member

llvmbot commented Sep 7, 2025

@llvm/pr-subscribers-llvm-support

Author: Kazu Hirata (kazutakahirata)

Changes

We have two implementations of IsResizableBase that are selected with
a boolean template parameter. This patch consolidates them into one
with "constexpr if". The "constexpr if" condition uses
llvm::is_detected to check the availability of resize().


Full diff: https://github.com/llvm/llvm-project/pull/157326.diff

1 Files Affected:

  • (modified) llvm/include/llvm/Support/YAMLTraits.h (+12-22)
diff --git a/llvm/include/llvm/Support/YAMLTraits.h b/llvm/include/llvm/Support/YAMLTraits.h
index 00f645545a2be..27af2d60c837f 100644
--- a/llvm/include/llvm/Support/YAMLTraits.h
+++ b/llvm/include/llvm/Support/YAMLTraits.h
@@ -1850,39 +1850,29 @@ template <> struct IsFlowSequenceBase<true> {
   static const bool flow = true;
 };
 
-template <typename T, typename U = void>
-struct IsResizable : std::false_type {};
-
 template <typename T>
-struct IsResizable<T, std::void_t<decltype(std::declval<T>().resize(0))>>
-    : public std::true_type {};
-
-template <typename T, bool B> struct IsResizableBase {
-  using type = typename T::value_type;
-
-  static type &element(IO &io, T &seq, size_t index) {
-    if (index >= seq.size())
-      seq.resize(index + 1);
-    return seq[index];
-  }
-};
+using check_resize_t = decltype(std::declval<T>().resize(0));
 
-template <typename T> struct IsResizableBase<T, false> {
+template <typename T> struct IsResizableBase {
   using type = typename T::value_type;
 
   static type &element(IO &io, T &seq, size_t index) {
-    if (index >= seq.size()) {
-      io.setError(Twine("value sequence extends beyond static size (") +
-                  Twine(seq.size()) + ")");
-      return seq[0];
+    if constexpr (is_detected<check_resize_t, T>::value) {
+      if (index >= seq.size())
+        seq.resize(index + 1);
+    } else {
+      if (index >= seq.size()) {
+        io.setError(Twine("value sequence extends beyond static size (") +
+                    Twine(seq.size()) + ")");
+        return seq[0];
+      }
     }
     return seq[index];
   }
 };
 
 template <typename T, bool Flow>
-struct SequenceTraitsImpl : IsFlowSequenceBase<Flow>,
-                            IsResizableBase<T, IsResizable<T>::value> {
+struct SequenceTraitsImpl : IsFlowSequenceBase<Flow>, IsResizableBase<T> {
   static size_t size(IO &io, T &seq) { return seq.size(); }
 };
 

@kazutakahirata kazutakahirata merged commit fc01a55 into llvm:main Sep 7, 2025
11 checks passed
@kazutakahirata kazutakahirata deleted the cleanup_20250906pm_Support_YAML_IsResizableBase_constexpr_if branch September 7, 2025 17:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants