[llvm] [ADT] Consolidate assertSafeToReferenceAfterClear with "if constexpr" (NFC) (PR #161042)

Kazu Hirata via llvm-commits llvm-commits at lists.llvm.org
Sat Sep 27 22:48:07 PDT 2025


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

This patch consolidates two implementations of
assertSafeToReferenceAfterClear into a single template function.


>From e50aa50bbad4eaffb6abae0fd99a30035bee0f8d Mon Sep 17 00:00:00 2001
From: Kazu Hirata <kazu at google.com>
Date: Fri, 19 Sep 2025 17:08:04 -0700
Subject: [PATCH] [ADT] Consolidate assertSafeToReferenceAfterClear with "if
 constexpr" (NFC)

This patch consolidates two implementations of
assertSafeToReferenceAfterClear into a single template function.
---
 llvm/include/llvm/ADT/SmallVector.h | 23 ++++++++++++-----------
 1 file changed, 12 insertions(+), 11 deletions(-)

diff --git a/llvm/include/llvm/ADT/SmallVector.h b/llvm/include/llvm/ADT/SmallVector.h
index 36b324355ee10..77805f5c03c14 100644
--- a/llvm/include/llvm/ADT/SmallVector.h
+++ b/llvm/include/llvm/ADT/SmallVector.h
@@ -199,17 +199,18 @@ class SmallVectorTemplateCommon
   }
 
   /// Check whether any part of the range will be invalidated by clearing.
-  void assertSafeToReferenceAfterClear(const T *From, const T *To) {
-    if (From == To)
-      return;
-    this->assertSafeToReferenceAfterResize(From, 0);
-    this->assertSafeToReferenceAfterResize(To - 1, 0);
-  }
-  template <
-      class ItTy,
-      std::enable_if_t<!std::is_same<std::remove_const_t<ItTy>, T *>::value,
-                       bool> = false>
-  void assertSafeToReferenceAfterClear(ItTy, ItTy) {}
+  template <class ItTy>
+  void assertSafeToReferenceAfterClear(ItTy From, ItTy To) {
+    if constexpr (std::is_pointer_v<ItTy> &&
+                  std::is_same_v<
+                      std::remove_const_t<std::remove_pointer_t<ItTy>>,
+                      std::remove_const_t<T>>) {
+      if (From == To)
+        return;
+      this->assertSafeToReferenceAfterResize(From, 0);
+      this->assertSafeToReferenceAfterResize(To - 1, 0);
+    }
+  }
 
   /// Check whether any part of the range will be invalidated by growing.
   template <class ItTy> void assertSafeToAddRange(ItTy From, ItTy To) {



More information about the llvm-commits mailing list