[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:17:49 PST 2026
https://github.com/smallp-o-p updated https://github.com/llvm/llvm-project/pull/176597
>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 1/3] 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(
>From 94e18ab1b138e0fdc27e8b7b1c7cf8291aa502dd Mon Sep 17 00:00:00 2001
From: William Tran-Viet <wtranviet at proton.me>
Date: Sat, 17 Jan 2026 17:15:27 -0500
Subject: [PATCH 2/3] Formatting
---
.../optional.iterator/assert.bounded_iterator.pass.cpp | 5 +++--
1 file changed, 3 insertions(+), 2 deletions(-)
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
index e549d95859e30..338cd4411436e 100644
--- 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
@@ -63,7 +63,8 @@ int main(int, char**) {
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");
+ TEST_LIBCPP_ASSERT_FAILURE(
+ i.operator->(), "__bounded_iter::operator->: Attempt to dereference an iterator at the end");
}
{ // operator+=
@@ -78,7 +79,7 @@ int main(int, char**) {
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 -= 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");
}
>From a23b29e398d88dac0536e2b8d45238862a5afc8d Mon Sep 17 00:00:00 2001
From: William Tran-Viet <wtranviet at proton.me>
Date: Sat, 17 Jan 2026 17:17:41 -0500
Subject: [PATCH 3/3] Formatting 2
---
libcxx/utils/libcxx/test/features/libcxx_macros.py | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/libcxx/utils/libcxx/test/features/libcxx_macros.py b/libcxx/utils/libcxx/test/features/libcxx_macros.py
index e1f2b68837d73..bbcd0492d62b8 100644
--- a/libcxx/utils/libcxx/test/features/libcxx_macros.py
+++ b/libcxx/utils/libcxx/test/features/libcxx_macros.py
@@ -32,7 +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",
+ "_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