Skip to content

Commit 344183d

Browse files
more valueless_by_exception cases
1 parent 1685b76 commit 344183d

File tree

6 files changed

+64
-17
lines changed

6 files changed

+64
-17
lines changed

libcxx/include/__ranges/concat_view.h

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -522,7 +522,9 @@ class concat_view<_Views...>::__iterator {
522522
...))
523523

524524
{
525-
return std::visit(
525+
_LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(
526+
!__it.__it_.valueless_by_exception(), "Trying to convert from a valueless iterator of concat_view.");
527+
return __variant_detail::__visitation::__variant::__visit_value(
526528
[](const auto& __i) -> __concat_rvalue_reference_t<__maybe_const<_Const, _Views>...> {
527529
return ranges::iter_move(__i);
528530
},
@@ -539,7 +541,11 @@ class concat_view<_Views...>::__iterator {
539541
requires swappable_with<iter_reference_t<__iterator>, iter_reference_t<__iterator>> &&
540542
(... && indirectly_swappable<iterator_t<__maybe_const<_Const, _Views>>>)
541543
{
542-
std::visit(
544+
_LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(
545+
!__x.__it_.valueless_by_exception(), "Trying to convert from a valueless iterator of concat_view.");
546+
_LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(
547+
!__y.__it_.valueless_by_exception(), "Trying to convert from a valueless iterator of concat_view.");
548+
__variant_detail::__visitation::__variant::__visit_value(
543549
[&](const auto& __it1, const auto& __it2) {
544550
if constexpr (is_same_v<decltype(__it1), decltype(__it2)>) {
545551
ranges::iter_swap(__it1, __it2);

libcxx/test/std/ranges/range.adaptors/range.concat/iterator/ctor.pass.cpp

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -67,21 +67,6 @@ struct IterNoDefaultInitView : std::ranges::view_base {
6767
int* end();
6868
};
6969

70-
struct ThrowOnCopyView : std::ranges::view_base {
71-
int start_;
72-
int* ptr_;
73-
constexpr explicit ThrowOnCopyView(int* ptr = globalBuff, int start = 0) : start_(start), ptr_(ptr) {}
74-
constexpr ThrowOnCopyView(ThrowOnCopyView&&) = default;
75-
constexpr ThrowOnCopyView(const ThrowOnCopyView&) { throw 42; };
76-
constexpr ThrowOnCopyView& operator=(ThrowOnCopyView&&) = default;
77-
constexpr ThrowOnCopyView& operator=(const ThrowOnCopyView&) {
78-
throw 42;
79-
return *this;
80-
};
81-
constexpr int* begin() const { return ptr_ + start_; }
82-
constexpr int* end() const { return ptr_ + 8; }
83-
};
84-
8570
constexpr bool test() {
8671
std::ranges::concat_view<MoveOnlyView> concatView;
8772
auto iter = std::move(concatView).begin();

libcxx/test/std/ranges/range.adaptors/range.concat/iterator/decrement.pass.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
//===----------------------------------------------------------------------===//
88

99
// REQUIRES: std-at-least-c++26
10+
// UNSUPPORTED: libcpp-hardening-mode=none, no-exceptions
1011

1112
#include <ranges>
1213

@@ -16,6 +17,7 @@
1617
#include <iterator>
1718
#include <utility>
1819
#include <vector>
20+
#include "check_assertion.h"
1921
#include "test_iterators.h"
2022
#include "test_macros.h"
2123
#include "../types.h"
@@ -81,6 +83,17 @@ constexpr void test() {
8183
assert(result == view.end());
8284
assert(it == (result - 1));
8385
}
86+
87+
{
88+
//valueless by exception test
89+
std::ranges::concat_view<ThrowOnCopyView> concatView_2;
90+
std::ranges::iterator_t<std::ranges::concat_view<ThrowOnCopyView>> it1;
91+
try {
92+
it1 = concatView_2.begin();
93+
} catch (...) {
94+
TEST_LIBCPP_ASSERT_FAILURE([&] { (void)--*it1; }(), "valueless by exception");
95+
}
96+
}
8497
}
8598

8699
int main(int, char**) {

libcxx/test/std/ranges/range.adaptors/range.concat/iterator/deref.pass.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
//===----------------------------------------------------------------------===//
88

99
// REQUIRES: std-at-least-c++26
10+
// UNSUPPORTED: libcpp-hardening-mode=none, no-exceptions
1011

1112
#include <ranges>
1213

@@ -15,6 +16,7 @@
1516
#include <concepts>
1617
#include <cstddef>
1718
#include <utility>
19+
#include "check_assertion.h"
1820
#include "test_iterators.h"
1921
#include "test_macros.h"
2022
#include "../types.h"
@@ -36,6 +38,17 @@ constexpr void test() {
3638
int& result = *iter;
3739
ASSERT_SAME_TYPE(int&, decltype(*iter));
3840
assert(&result == array.data());
41+
42+
{
43+
//valueless by exception test
44+
std::ranges::concat_view<ThrowOnCopyView> concatView_2;
45+
std::ranges::iterator_t<std::ranges::concat_view<ThrowOnCopyView>> it1;
46+
try {
47+
it1 = concatView_2.begin();
48+
} catch (...) {
49+
TEST_LIBCPP_ASSERT_FAILURE([&] { (void)*it1; }(), "valueless by exception");
50+
}
51+
}
3952
}
4053

4154
constexpr bool tests() {

libcxx/test/std/ranges/range.adaptors/range.concat/iterator/increment.pass.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
//===----------------------------------------------------------------------===//
88

99
// REQUIRES: std-at-least-c++26
10+
// UNSUPPORTED: libcpp-hardening-mode=none, no-exceptions
1011

1112
#include <ranges>
1213

@@ -16,6 +17,7 @@
1617
#include <concepts>
1718
#include <type_traits>
1819
#include <utility>
20+
#include "check_assertion.h"
1921
#include "test_iterators.h"
2022
#include "test_macros.h"
2123
#include "../types.h"
@@ -61,6 +63,17 @@ constexpr void test() {
6163
++it;
6264
assert(*it == array[4]);
6365
}
66+
67+
{
68+
//valueless by exception test
69+
std::ranges::concat_view<ThrowOnCopyView> concatView_2;
70+
std::ranges::iterator_t<std::ranges::concat_view<ThrowOnCopyView>> it1;
71+
try {
72+
it1 = concatView_2.begin();
73+
} catch (...) {
74+
TEST_LIBCPP_ASSERT_FAILURE([&] { (void)++*it1; }(), "valueless by exception");
75+
}
76+
}
6477
}
6578

6679
constexpr bool tests() {

libcxx/test/std/ranges/range.adaptors/range.concat/types.h

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
#include <ranges>
1313
#include <utility>
1414

15+
int globalArray[8] = {0, 1, 2, 3, 4, 5, 6, 7};
16+
1517
struct TrackInitialization {
1618
constexpr explicit TrackInitialization(bool* moved, bool* copied) : moved_(moved), copied_(copied) {}
1719
constexpr TrackInitialization(TrackInitialization const& other) : moved_(other.moved_), copied_(other.copied_) {
@@ -41,4 +43,19 @@ struct minimal_view : std::ranges::view_base {
4143
decltype(base(std::declval<Sent>())) sent_;
4244
};
4345

46+
struct ThrowOnCopyView : std::ranges::view_base {
47+
int start_;
48+
int* ptr_;
49+
constexpr explicit ThrowOnCopyView(int* ptr = globalArray, int start = 0) : start_(start), ptr_(ptr) {}
50+
constexpr ThrowOnCopyView(ThrowOnCopyView&&) = default;
51+
constexpr ThrowOnCopyView(const ThrowOnCopyView&) { throw 42; };
52+
constexpr ThrowOnCopyView& operator=(ThrowOnCopyView&&) = default;
53+
constexpr ThrowOnCopyView& operator=(const ThrowOnCopyView&) {
54+
throw 42;
55+
return *this;
56+
};
57+
constexpr int* begin() const { return ptr_ + start_; }
58+
constexpr int* end() const { return ptr_ + 8; }
59+
};
60+
4461
#endif // TEST_STD_RANGES_RANGE_ADAPTORS_CONCAT_FILTER_TYPES_H

0 commit comments

Comments
 (0)