[llvm-branch-commits] [llvm] release/23.x: [ADT] Compute end() after removal in llvm::erase and llvm::erase_if (#243189) (PR #244652)

via llvm-branch-commits llvm-branch-commits at lists.llvm.org
Fri Nov 20 08:52:18 PST 2026


llvmorg-github-actions[bot] wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-llvm-adt

Author: llvmbot

<details>
<summary>Changes</summary>

Backport 27825e751662497914dac447753f6d8daca974ff

Requested by: @<!-- -->nikic

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


2 Files Affected:

- (modified) llvm/include/llvm/ADT/STLExtras.h (+10-5) 
- (modified) llvm/unittests/ADT/STLExtrasTest.cpp (+38) 


``````````diff
diff --git a/llvm/include/llvm/ADT/STLExtras.h b/llvm/include/llvm/ADT/STLExtras.h
index 0081ce2da106b9..8353aacdee681a 100644
--- a/llvm/include/llvm/ADT/STLExtras.h
+++ b/llvm/include/llvm/ADT/STLExtras.h
@@ -2184,21 +2184,26 @@ template <typename T> constexpr auto not_equal_to(T &&Arg) {
 /// Provide a container algorithm similar to C++ Library Fundamentals v2's
 /// `erase_if` which is equivalent to:
 ///
-///   C.erase(remove_if(C, pred), C.end());
+///   auto I = remove_if(C, pred);
+///   C.erase(I, C.end());
 ///
 /// This version works for any container with an erase method call accepting
-/// two iterators.
+/// two iterators. Compute end() after removal, since removing elements can
+/// change it for containers such as TinyPtrVector.
 template <typename Container, typename UnaryPredicate>
 void erase_if(Container &C, UnaryPredicate P) {
-  C.erase(remove_if(C, P), C.end());
+  auto I = remove_if(C, P);
+  C.erase(I, C.end());
 }
 
 /// Wrapper function to remove a value from a container:
 ///
-/// C.erase(remove(C.begin(), C.end(), V), C.end());
+/// auto I = std::remove(C.begin(), C.end(), V);
+/// C.erase(I, C.end());
 template <typename Container, typename ValueType>
 void erase(Container &C, ValueType V) {
-  C.erase(std::remove(C.begin(), C.end(), V), C.end());
+  auto I = std::remove(C.begin(), C.end(), V);
+  C.erase(I, C.end());
 }
 
 /// Wrapper function to append range `R` to container `C`.
diff --git a/llvm/unittests/ADT/STLExtrasTest.cpp b/llvm/unittests/ADT/STLExtrasTest.cpp
index 00c062ae1ca291..95ed147c08d87a 100644
--- a/llvm/unittests/ADT/STLExtrasTest.cpp
+++ b/llvm/unittests/ADT/STLExtrasTest.cpp
@@ -660,6 +660,44 @@ TEST(STLExtrasTest, EraseIf) {
   EXPECT_EQ(7, V[3]);
 }
 
+struct ContainerWithChangingEnd {
+  int Value = 0;
+  int *End = &Value + 1;
+
+  int *begin() { return &Value; }
+  int *end() { return End; }
+  void erase(int *First, int *Last) {
+    EXPECT_EQ(First, begin());
+    EXPECT_EQ(Last, End);
+  }
+};
+
+// Model removal changing end() without relying on the standard library's
+// treatment of removed elements.
+struct RemoveAndUpdateEnd {
+  ContainerWithChangingEnd &C;
+
+  bool operator()(int) const {
+    // Change the end() of the container.
+    C.End = C.begin();
+    return true;
+  }
+  friend bool operator==(int V, const RemoveAndUpdateEnd &P) {
+    // Forward to the operator above.
+    return P(V);
+  }
+};
+
+TEST(STLExtrasTest, EraseWithChangingEnd) {
+  ContainerWithChangingEnd C;
+  llvm::erase(C, RemoveAndUpdateEnd{C});
+}
+
+TEST(STLExtrasTest, EraseIfWithChangingEnd) {
+  ContainerWithChangingEnd C;
+  llvm::erase_if(C, RemoveAndUpdateEnd{C});
+}
+
 TEST(STLExtrasTest, AppendRange) {
   std::vector<int> V = {1, 2};
   auto AppendVals1 = {3};

``````````

</details>


https://github.com/llvm/llvm-project/pull/244652


More information about the llvm-branch-commits mailing list