[libcxx-commits] [libcxx] [libc++][NFC] Add optional bounded iterator tests (PR #176597)

William Tran-Viet via libcxx-commits libcxx-commits at lists.llvm.org
Sat Jan 17 14:13:04 PST 2026


https://github.com/smallp-o-p created https://github.com/llvm/llvm-project/pull/176597

- These were missed when implementing `optional` iterator.

>From 85f9ce35e8e4656abef8d66bc5f7facf68aeba35 Mon Sep 17 00:00:00 2001
From: William Tran-Viet <wtranviet at proton.me>
Date: Sat, 17 Jan 2026 17:11:40 -0500
Subject: [PATCH] Add optional bounded iterator tests

---
 .../assert.bounded_iterator.pass.cpp          | 86 +++++++++++++++++++
 .../libcxx/test/features/libcxx_macros.py     |  1 +
 2 files changed, 87 insertions(+)
 create mode 100644 libcxx/test/libcxx/utilities/optional/optional.object/optional.iterator/assert.bounded_iterator.pass.cpp

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..e549d95859e30
--- /dev/null
+++ b/libcxx/test/libcxx/utilities/optional/optional.object/optional.iterator/assert.bounded_iterator.pass.cpp
@@ -0,0 +1,86 @@
+//===----------------------------------------------------------------------===//
+//
+// 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, libcpp-has-no-experimental-optional-iterator
+
+// 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 7a465f2e87866..e1f2b68837d73 100644
--- a/libcxx/utils/libcxx/test/features/libcxx_macros.py
+++ b/libcxx/utils/libcxx/test/features/libcxx_macros.py
@@ -32,6 +32,7 @@
     "_LIBCPP_DEPRECATED_ABI_DISABLE_PAIR_TRIVIAL_COPY_CTOR": "libcpp-deprecated-abi-disable-pair-trivial-copy-ctor",
     "_LIBCPP_ABI_NO_COMPRESSED_PAIR_PADDING": "libcpp-abi-no-compressed-pair-padding",
     "_LIBCPP_PSTL_BACKEND_LIBDISPATCH": "libcpp-pstl-backend-libdispatch",
+    "_LIBCPP_ABI_BOUNDED_ITERATORS_IN_OPTIONAL" : "libcpp-has-abi-bounded-iterators-in-optional",
 }
 for macro, feature in macros.items():
     features.append(



More information about the libcxx-commits mailing list