[libcxx-commits] [libcxx] c0ae6c9 - [libc++] Add assertion tests for optional bounded iterator (#176597)

via libcxx-commits libcxx-commits at lists.llvm.org
Mon Jan 26 10:04:32 PST 2026


Author: William Tran-Viet
Date: 2026-01-26T13:04:27-05:00
New Revision: c0ae6c91bfe7dc54ff4f19acd24719c258639c60

URL: https://github.com/llvm/llvm-project/commit/c0ae6c91bfe7dc54ff4f19acd24719c258639c60
DIFF: https://github.com/llvm/llvm-project/commit/c0ae6c91bfe7dc54ff4f19acd24719c258639c60.diff

LOG: [libc++] Add assertion tests for optional bounded iterator (#176597)

These were missed when implementing `optional` iterator.

Added: 
    libcxx/test/libcxx/utilities/optional/optional.object/optional.iterator/assert.bounded_iterator.pass.cpp

Modified: 
    libcxx/utils/libcxx/test/features/libcxx_macros.py

Removed: 
    


################################################################################
diff  --git a/libcxx/test/libcxx/utilities/optional/optional.object/optional.iterator/assert.bounded_iterator.pass.cpp b/libcxx/test/libcxx/utilities/optional/optional.object/optional.iterator/assert.bounded_iterator.pass.cpp
new file mode 100644
index 0000000000000..393b9e5f06476
--- /dev/null
+++ b/libcxx/test/libcxx/utilities/optional/optional.object/optional.iterator/assert.bounded_iterator.pass.cpp
@@ -0,0 +1,87 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+// <optional>
+
+// REQUIRES: std-at-least-c++26, libcpp-has-abi-bounded-iterators-in-optional
+// UNSUPPORTED: libcpp-hardening-mode=none
+
+// Test that an assertion fires for invalid uses of the following operators on a bounded iterator:
+
+// operator++()
+// operator++(int),
+// operator--(),
+// operator--(int),
+// operator*
+// operator[]
+// operator->
+// operator+=
+// operator-=
+
+#include <optional>
+
+#include "check_assertion.h"
+
+int main(int, char**) {
+  { // operator++
+    std::optional<int> o{1};
+    auto i = o.end();
+
+    TEST_LIBCPP_ASSERT_FAILURE(++i, "__bounded_iter::operator++: Attempt to advance an iterator past the end");
+    TEST_LIBCPP_ASSERT_FAILURE(i++, "__bounded_iter::operator++: Attempt to advance an iterator past the end");
+  }
+
+  { // operator--
+    std::optional<int> o{1};
+    auto i = o.begin();
+
+    TEST_LIBCPP_ASSERT_FAILURE(--i, "__bounded_iter::operator--: Attempt to rewind an iterator past the start");
+    TEST_LIBCPP_ASSERT_FAILURE(i--, "__bounded_iter::operator--: Attempt to rewind an iterator past the start");
+  }
+
+  { // operator*
+    std::optional<int> o;
+    auto i = o.begin();
+
+    TEST_LIBCPP_ASSERT_FAILURE(*i, "__bounded_iter::operator*: Attempt to dereference an iterator at the end");
+  }
+
+  { // operator[]
+    std::optional<int> o{1};
+    auto i = o.begin();
+
+    TEST_LIBCPP_ASSERT_FAILURE(i[1], "__bounded_iter::operator[]: Attempt to index an iterator at or past the end");
+    TEST_LIBCPP_ASSERT_FAILURE(i[-1], "__bounded_iter::operator[]: Attempt to index an iterator past the start");
+  }
+
+  { // operator->
+    std::optional<int> o{1};
+    auto i = o.end();
+
+    TEST_LIBCPP_ASSERT_FAILURE(
+        i.operator->(), "__bounded_iter::operator->: Attempt to dereference an iterator at the end");
+  }
+
+  { // operator+=
+    std::optional<int> o{1};
+    auto i = o.begin();
+
+    TEST_LIBCPP_ASSERT_FAILURE(i += 2, "__bounded_iter::operator+=: Attempt to advance an iterator past the end");
+    TEST_LIBCPP_ASSERT_FAILURE(i += -1, "__bounded_iter::operator+=: Attempt to rewind an iterator past the start");
+  }
+
+  { // operator-=
+    std::optional<int> o{1};
+    auto i = o.begin();
+
+    TEST_LIBCPP_ASSERT_FAILURE(i -= 1, "__bounded_iter::operator-=: Attempt to rewind an iterator past the start");
+    TEST_LIBCPP_ASSERT_FAILURE(i -= -2, "__bounded_iter::operator-=: Attempt to advance an iterator past the end");
+  }
+
+  return 0;
+}

diff  --git a/libcxx/utils/libcxx/test/features/libcxx_macros.py b/libcxx/utils/libcxx/test/features/libcxx_macros.py
index 92825a93ac0f1..ca5612fc2b5ea 100644
--- a/libcxx/utils/libcxx/test/features/libcxx_macros.py
+++ b/libcxx/utils/libcxx/test/features/libcxx_macros.py
@@ -24,6 +24,7 @@
     "_LIBCPP_NO_VCRUNTIME": "libcpp-no-vcruntime",
     "_LIBCPP_ABI_VERSION": "libcpp-abi-version",
     "_LIBCPP_ABI_BOUNDED_ITERATORS": "libcpp-has-abi-bounded-iterators",
+    "_LIBCPP_ABI_BOUNDED_ITERATORS_IN_OPTIONAL": "libcpp-has-abi-bounded-iterators-in-optional",
     "_LIBCPP_ABI_BOUNDED_ITERATORS_IN_STRING": "libcpp-has-abi-bounded-iterators-in-string",
     "_LIBCPP_ABI_BOUNDED_ITERATORS_IN_VECTOR": "libcpp-has-abi-bounded-iterators-in-vector",
     "_LIBCPP_ABI_BOUNDED_ITERATORS_IN_STD_ARRAY": "libcpp-has-abi-bounded-iterators-in-std-array",


        


More information about the libcxx-commits mailing list