From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by sourceware.org (Postfix) with ESMTPS id A8F4A3857427 for ; Wed, 11 May 2022 12:46:10 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org A8F4A3857427 Received: from mail-qv1-f72.google.com (mail-qv1-f72.google.com [209.85.219.72]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-623-Hxhq0UrWPLWkoSn4hAKU_g-1; Wed, 11 May 2022 08:46:09 -0400 X-MC-Unique: Hxhq0UrWPLWkoSn4hAKU_g-1 Received: by mail-qv1-f72.google.com with SMTP id j1-20020a0cf9c1000000b0045abd139f01so1853030qvo.23 for ; Wed, 11 May 2022 05:46:09 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=IU/WbQiNhlV655MgSYH4CmQ6ON0xbGiL4YeklH4Nbxg=; b=iRGP+2JJVwDBPlcLfGYfhZwnXFdhqtI7slPcaW/thNfQ7Mt4EJ7cJeaGhxGP1wH0hE Pl2AAWEz8q/3KOkb9we4TrCxXIgpreEie67QTleH1LKjhyycD7RolwmARPljj+Fppnbs G4G1JzEeOKBcd9/iDvFLGd0xXj01+oh4IAT0mYKAJGMUnpSTjWpXaKujgqNte0rsJUnB D7IFYEniKu9NmgTAgMTFPWb2/V9Vt7EvPAGOdJG/AFJeZarQK6vYyhUw1L9NbfyefnW9 ahqlnWnThFrjdkWMvQq1FqbTq7svPe+Q4qTcwxcKz9dbwK9PKvQ55xvLGoBeQ5JNBVn+ GPcQ== X-Gm-Message-State: AOAM5329wtHjHaoKuyEaHEo953dLxziXZ+BFXGCRmOBD5SR6GXc1AB1j sPzAOn6lGsHirTt6KwzHM3JsSb9/ALsiigbWUSfP+k71MRgkXZydlPuJCs54wHTUn0gOyDrN7je 4r1eu2zdRS0szQHM= X-Received: by 2002:a05:620a:1135:b0:69f:9986:be40 with SMTP id p21-20020a05620a113500b0069f9986be40mr18744200qkk.351.1652273167945; Wed, 11 May 2022 05:46:07 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzgOdhV54a9YDnp7oHUaLTQAjsFuyyi+wfKOwipSdU3U6HPumjDyB8JmmQ9YLbIFaw1+uKO2Q== X-Received: by 2002:a05:620a:1135:b0:69f:9986:be40 with SMTP id p21-20020a05620a113500b0069f9986be40mr18744178qkk.351.1652273167514; Wed, 11 May 2022 05:46:07 -0700 (PDT) Received: from localhost.localdomain (ool-457670bb.dyn.optonline.net. [69.118.112.187]) by smtp.gmail.com with ESMTPSA id t20-20020a05622a181400b002f39b99f66fsm1114179qtc.9.2022.05.11.05.46.06 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 11 May 2022 05:46:07 -0700 (PDT) From: Patrick Palka To: gcc-patches@gcc.gnu.org Cc: libstdc++@gcc.gnu.org, Patrick Palka Subject: [PATCH, 10 backport] libstdc++: Implement P2325 changes to default-constructibility of views Date: Wed, 11 May 2022 08:45:58 -0400 Message-Id: <20220511124558.935858-1-ppalka@redhat.com> X-Mailer: git-send-email 2.36.0.63.gf5aaf72f1b MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: 8bit Content-Type: text/plain; charset="US-ASCII"; x-default=true X-Spam-Status: No, score=-15.1 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_LOW, SPF_HELO_NONE, SPF_NONE, TXREP, T_SCC_BODY_TEXT_LINE autolearn=unavailable autolearn_force=no version=3.4.4 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on server2.sourceware.org X-BeenThere: libstdc++@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Libstdc++ mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 11 May 2022 12:46:13 -0000 Tested on x86_64-pc-linux-gnu, does this look OK for the 10 branch? NB: This backport of r12-1606 to the 10 branch deliberately omits parts of P2325R3 so as to maximize backward compatibility with pre-P2325R3 code. In particular, we don't remove the default ctors for back_insert_iterator, front_insert_iterator, ostream_iterator, ref_view and basic_istream_view. And in the 10 branch we we don't have __non_propagating_cache or the partial specialization of __box, so the changes to them are omitted too. Finally, we don't update __cpp_lib_ranges to 202106L because many significant 202106 Ranges changes still aren't implemented in the 10 branch, e.g. P2281R1 and P2210R2. === This implements the wording changes of P2325R3 "Views should not be required to be default constructible". Changes are relatively straightforward, besides perhaps those to __box (which now stands for copyable-box instead of semiregular-box) and __non_propagating_cache. For __box, this patch implements the recommended practice to also avoid std::optional when the boxed type is nothrow_move/copy_constructible. For __non_propagating_cache, now that it's used by split_view::_M_current, we need to add assignment from a value of the underlying type to the subset of the std::optional API implemented for the cache (needed by split_view::begin()). Hence the new __non_propagating_cache::operator= overload. In passing, this fixes the undesirable list-init in the constructors of the partial specialization of __box as reported in PR100475 comment #7. PR libstdc++/103904 libstdc++-v3/ChangeLog: * include/bits/iterator_concepts.h (weakly_incrementable): Remove default_initializable requirement. * include/bits/stl_iterator.h (common_iterator): Constrain the default ctor. (counted_iterator): Likewise. * include/std/ranges (ranges::view): Remove default_initializable requirement. (subrange): Constrain the default ctor. (__detail::__box::operator=): Handle self-assignment. (single_view): Constraint the default ctor. (iota_view): Relax semiregular constraint to copyable. Constrain the default ctor. (iota_view::_Iterator): Constraint the default ctor. (filter_view): Likewise. (filter_view::_Iterator): Likewise. (transform_view): Likewise. (transform_view::_Iterator): Likewise. (take_view): Likewise. (take_view::_Iterator): Likewise. (take_while_view): Likewise. (take_while_view::_Iterator): Likewise. (drop_while_view): Likewise. (drop_while_view::_Iterator): Likewise. (join_view): Likewise. (split_view): Constrain the default ctor. (common_view): Likewise. (reverse_view): Likewise. (elements_view): Likewise. (elements_view::_Iterator): Likewise. * include/std/span (enable_view>): Define this partial specialization to true unconditionally. * testsuite/std/ranges/p2325.cc: New test. * testsuite/std/ranges/single_view.cc (test06): New test. * testsuite/std/ranges/view.cc: Adjust now that view doesn't require default_initializable. (cherry picked from commit 4b4f5666b4c2f3aab2a9f3d53d394e390b9b682d) --- libstdc++-v3/include/bits/iterator_concepts.h | 3 +- libstdc++-v3/include/bits/stl_iterator.h | 3 +- libstdc++-v3/include/std/ranges | 78 +++++--- libstdc++-v3/include/std/span | 3 +- libstdc++-v3/testsuite/std/ranges/p2325.cc | 181 ++++++++++++++++++ .../testsuite/std/ranges/single_view.cc | 15 ++ libstdc++-v3/testsuite/std/ranges/view.cc | 2 +- 7 files changed, 249 insertions(+), 36 deletions(-) create mode 100644 libstdc++-v3/testsuite/std/ranges/p2325.cc diff --git a/libstdc++-v3/include/bits/iterator_concepts.h b/libstdc++-v3/include/bits/iterator_concepts.h index 4f118df6b02..a533ae2d279 100644 --- a/libstdc++-v3/include/bits/iterator_concepts.h +++ b/libstdc++-v3/include/bits/iterator_concepts.h @@ -573,8 +573,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION /// Requirements on types that can be incremented with ++. template - concept weakly_incrementable = default_initializable<_Iter> - && movable<_Iter> + concept weakly_incrementable = movable<_Iter> && requires(_Iter __i) { typename iter_difference_t<_Iter>; diff --git a/libstdc++-v3/include/bits/stl_iterator.h b/libstdc++-v3/include/bits/stl_iterator.h index 91cfeccecf3..a46872fc3c2 100644 --- a/libstdc++-v3/include/bits/stl_iterator.h +++ b/libstdc++-v3/include/bits/stl_iterator.h @@ -1729,6 +1729,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION constexpr common_iterator() noexcept(is_nothrow_default_constructible_v<_It>) + requires default_initializable<_It> : _M_it(), _M_index(0) { } @@ -2106,7 +2107,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION // iterator_concept defined in __counted_iter_concept // iterator_category defined in __counted_iter_cat - constexpr counted_iterator() = default; + constexpr counted_iterator() requires default_initializable<_It> = default; constexpr counted_iterator(_It __i, iter_difference_t<_It> __n) diff --git a/libstdc++-v3/include/std/ranges b/libstdc++-v3/include/std/ranges index cec775db3b1..60ae3882c10 100644 --- a/libstdc++-v3/include/std/ranges +++ b/libstdc++-v3/include/std/ranges @@ -70,8 +70,7 @@ namespace ranges template concept view - = range<_Tp> && movable<_Tp> && default_initializable<_Tp> - && enable_view<_Tp>; + = range<_Tp> && movable<_Tp> && enable_view<_Tp>; /// A range which can be safely converted to a view. template @@ -251,7 +250,7 @@ namespace ranges [[no_unique_address]] _Size> _M_size = {}; public: - subrange() = default; + subrange() requires default_initializable<_It> = default; constexpr subrange(__detail::__convertible_to_non_slicing<_It> auto __i, _Sent __s) @@ -486,10 +485,13 @@ namespace ranges noexcept(is_nothrow_copy_constructible_v<_Tp>) requires (!copyable<_Tp>) { - if ((bool)__that) - this->emplace(*__that); - else - this->reset(); + if (this != std::__addressof(__that)) + { + if ((bool)__that) + this->emplace(*__that); + else + this->reset(); + } return *this; } @@ -498,10 +500,13 @@ namespace ranges noexcept(is_nothrow_move_constructible_v<_Tp>) requires (!movable<_Tp>) { - if ((bool)__that) - this->emplace(std::move(*__that)); - else - this->reset(); + if (this != std::__addressof(__that)) + { + if ((bool)__that) + this->emplace(std::move(*__that)); + else + this->reset(); + } return *this; } }; @@ -513,7 +518,7 @@ namespace ranges class single_view : public view_interface> { public: - single_view() = default; + single_view() requires default_initializable<_Tp> = default; constexpr explicit single_view(const _Tp& __t) @@ -622,7 +627,7 @@ namespace ranges template requires std::__detail::__weakly_eq_cmp_with<_Winc, _Bound> - && semiregular<_Winc> + && copyable<_Winc> class iota_view : public view_interface> { private: @@ -651,7 +656,7 @@ namespace ranges using value_type = _Winc; using difference_type = __detail::__iota_diff_t<_Winc>; - _Iterator() = default; + _Iterator() requires default_initializable<_Winc> = default; constexpr explicit _Iterator(_Winc __value) @@ -848,7 +853,7 @@ namespace ranges _Bound _M_bound = _Bound(); public: - iota_view() = default; + iota_view() requires default_initializable<_Winc> = default; constexpr explicit iota_view(_Winc __value) @@ -1475,7 +1480,7 @@ namespace views using value_type = range_value_t<_Vp>; using difference_type = range_difference_t<_Vp>; - _Iterator() = default; + _Iterator() requires default_initializable<_Vp_iter> = default; constexpr _Iterator(filter_view* __parent, _Vp_iter __current) @@ -1587,7 +1592,9 @@ namespace views [[no_unique_address]] __detail::_CachedPosition<_Vp> _M_cached_begin; public: - filter_view() = default; + filter_view() requires (default_initializable<_Vp> + && default_initializable<_Pred>) + = default; constexpr filter_view(_Vp __base, _Pred __pred) @@ -1718,7 +1725,7 @@ namespace views = remove_cvref_t>>; using difference_type = range_difference_t<_Base>; - _Iterator() = default; + _Iterator() requires default_initializable<_Base_iter> = default; constexpr _Iterator(_Parent* __parent, _Base_iter __current) @@ -1933,7 +1940,9 @@ namespace views __detail::__box<_Fp> _M_fun; public: - transform_view() = default; + transform_view() requires (default_initializable<_Vp> + && default_initializable<_Fp>) + = default; constexpr transform_view(_Vp __base, _Fp __fun) @@ -2050,7 +2059,7 @@ namespace views range_difference_t<_Vp> _M_count = 0; public: - take_view() = default; + take_view() requires default_initializable<_Vp> = default; constexpr take_view(_Vp base, range_difference_t<_Vp> __count) @@ -2211,7 +2220,9 @@ namespace views __detail::__box<_Pred> _M_pred; public: - take_while_view() = default; + take_while_view() requires (default_initializable<_Vp> + && default_initializable<_Pred>) + = default; constexpr take_while_view(_Vp base, _Pred __pred) @@ -2282,7 +2293,7 @@ namespace views _M_cached_begin; public: - drop_view() = default; + drop_view() requires default_initializable<_Vp> = default; constexpr drop_view(_Vp __base, range_difference_t<_Vp> __count) @@ -2378,7 +2389,9 @@ namespace views [[no_unique_address]] __detail::_CachedPosition<_Vp> _M_cached_begin; public: - drop_while_view() = default; + drop_while_view() requires (default_initializable<_Vp> + && default_initializable<_Pred>) + = default; constexpr drop_while_view(_Vp __base, _Pred __pred) @@ -2553,7 +2566,9 @@ namespace views = common_type_t, range_difference_t>>; - _Iterator() = default; + _Iterator() requires (default_initializable<_Outer_iter> + && default_initializable<_Inner_iter>) + = default; constexpr _Iterator(_Parent* __parent, _Outer_iter __outer) @@ -2709,7 +2724,7 @@ namespace views views::all_t<_InnerRange>> _M_inner; public: - join_view() = default; + join_view() requires default_initializable<_Vp> = default; constexpr explicit join_view(_Vp __base) @@ -3123,7 +3138,10 @@ namespace views public: - split_view() = default; + split_view() requires (default_initializable<_Vp> + && default_initializable<_Pattern>, + && default_initializable>) + = default; constexpr split_view(_Vp __base, _Pattern __pattern) @@ -3229,7 +3247,7 @@ namespace views _Vp _M_base = _Vp(); public: - common_view() = default; + common_view() requires default_initializable<_Vp> = default; constexpr explicit common_view(_Vp __r) @@ -3339,7 +3357,7 @@ namespace views _M_cached_begin; public: - reverse_view() = default; + reverse_view() requires default_initializable<_Vp> = default; constexpr explicit reverse_view(_Vp __r) @@ -3466,7 +3484,7 @@ namespace views class elements_view : public view_interface> { public: - elements_view() = default; + elements_view() requires default_initializable<_Vp> = default; constexpr explicit elements_view(_Vp base) @@ -3587,7 +3605,7 @@ namespace views = remove_cvref_t>>; using difference_type = range_difference_t<_Base>; - _Iterator() = default; + _Iterator() requires default_initializable> = default; constexpr explicit _Iterator(iterator_t<_Base> current) diff --git a/libstdc++-v3/include/std/span b/libstdc++-v3/include/std/span index 4b389a46af7..35df1a64c0e 100644 --- a/libstdc++-v3/include/std/span +++ b/libstdc++-v3/include/std/span @@ -453,8 +453,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION // Opt-in to view concept template inline constexpr bool - enable_view> - = _Extent == 0 || _Extent == dynamic_extent; + enable_view> = true; } _GLIBCXX_END_NAMESPACE_VERSION } // namespace std diff --git a/libstdc++-v3/testsuite/std/ranges/p2325.cc b/libstdc++-v3/testsuite/std/ranges/p2325.cc new file mode 100644 index 00000000000..aff3bd6ca28 --- /dev/null +++ b/libstdc++-v3/testsuite/std/ranges/p2325.cc @@ -0,0 +1,181 @@ +// { dg-options "-std=gnu++20" } +// { dg-do compile { target c++20 } } +// P2325R3 "Views should not be required to be default constructible" + +// Parts of P2325R3 are deliberately omitted in libstdc++ 10, in particular the +// removal of default ctors for back_/front_insert_iterator, ostream_iterator, +// ref_view and basic_istream_view/::iterator, so as to maximize backward +// compatibility with pre-P2325R3 code. So most static_asserts in this test fail, +// see the xfails at the end of this file. + +#include +#include +#include +#include +#include +#include + +using namespace std; + +template void f(); +template requires weakly_incrementable || ranges::view void f(); + +void +test01() +{ + // Verify neither std::weakly_incrementable nor ranges::view require + // default_initializable. + f(); // { dg-error "ambiguous" } +} + +void +test02() +{ + // Verify these iterators are not default constructible. + static_assert(!default_initializable>>); + static_assert(!default_initializable>>); + static_assert(!default_initializable>>); + static_assert(!default_initializable>); + + using iter = ostream_iterator; + + // Verify common_iterator is conditionally default constructible. + static_assert(!default_initializable>); + static_assert(default_initializable>); + + // Verify counted_iterator is conditionally default constructible. + static_assert(!default_initializable>); + static_assert(default_initializable>); +} + +void +test03() +{ + using iter = ostream_iterator; + + // Verify iota_view is conditionally default constructible. + static_assert(!default_initializable>); + static_assert(!default_initializable>().begin())>); + static_assert(default_initializable>); + static_assert(default_initializable>().begin())>); + + // Verify subrange is conditionally default constructible. + static_assert(!default_initializable>); + static_assert(default_initializable>); + + // Verify single_view is conditionally default constructible. + static_assert(!default_initializable>); + static_assert(default_initializable>); +} + +void +test04() +{ + // Verify basic_istream_view is not default constructible. + using type = ranges::basic_istream_view>; + static_assert(!default_initializable); + static_assert(!default_initializable().begin())>); +} + +void +test05() +{ + // Verify ref_view is not default constructible. + static_assert(!default_initializable>); +} + +template +void +test06() +{ + auto f1 = [] (auto) { return true; }; + auto f2 = [i=0] (auto) { return true; }; + static_assert(default_initializable); + static_assert(!default_initializable); + + struct S { S() = delete; }; + static_assert(!default_initializable()) | adaptor(f1))>); + static_assert(!default_initializable()) | adaptor(f2))>); +} + +// Verify filter_view, transform_view, take_while_view and drop_while_view are +// conditionally default constructible. +template void test06(); +template void test06(); +template void test06(); +template void test06(); + +void +test07() +{ + // Verify join_view is conditionally default constructible. + struct S { S() = delete; }; + using type1 = ranges::join_view>>; + static_assert(!default_initializable); + using type2 = ranges::join_view>>; + static_assert(default_initializable); +} + +void +test08() +{ + // Verify split_view is conditionally default constructible. + using type1 = ranges::split_view, ranges::single_view>; + static_assert(!default_initializable); + using type2 = ranges::split_view, ranges::ref_view>; + static_assert(!default_initializable); + using type3 = ranges::split_view, ranges::ref_view>; + static_assert(!default_initializable); + using type4 = ranges::split_view, ranges::single_view>; + static_assert(default_initializable); +} + +void +test09() +{ + // Verify common_view is conditionally default constructible. + using type1 = ranges::common_view>>; + static_assert(!default_initializable); + using type2 = ranges::common_view>; + static_assert(default_initializable); +} + +void +test10() +{ + // Verify reverse_view is conditionally default constructible. + using type1 = ranges::reverse_view>; + static_assert(!default_initializable); + using type2 = ranges::reverse_view>; + static_assert(default_initializable); +} + +void +test11() +{ + // Verify elements_view is conditionally default constructible. + using type1 = ranges::elements_view[2]>, 0>; + static_assert(!default_initializable); + using type2 = ranges::elements_view>, 0>; + static_assert(default_initializable); +} + +// { dg-bogus "static assertion failed" "" { xfail *-*-* } 35 } +// { dg-bogus "static assertion failed" "" { xfail *-*-* } 36 } +// { dg-bogus "static assertion failed" "" { xfail *-*-* } 37 } +// { dg-bogus "static assertion failed" "" { xfail *-*-* } 38 } +// { dg-bogus "static assertion failed" "" { xfail *-*-* } 43 } +// { dg-bogus "static assertion failed" "" { xfail *-*-* } 47 } +// { dg-bogus "static assertion failed" "" { xfail *-*-* } 57 } +// { dg-bogus "static assertion failed" "" { xfail *-*-* } 58 } +// { dg-bogus "static assertion failed" "" { xfail *-*-* } 63 } +// { dg-bogus "static assertion failed" "" { xfail *-*-* } 67 } +// { dg-bogus "static assertion failed" "" { xfail *-*-* } 76 } +// { dg-bogus "static assertion failed" "" { xfail *-*-* } 77 } +// { dg-bogus "static assertion failed" "" { xfail *-*-* } 84 } +// { dg-bogus "static assertion failed" "" { xfail *-*-* } 124 } +// { dg-bogus "static assertion failed" "" { xfail *-*-* } 126 } +// { dg-bogus "static assertion failed" "" { xfail *-*-* } 128 } +// { dg-bogus "static assertion failed" "" { xfail *-*-* } 138 } +// { dg-bogus "static assertion failed" "" { xfail *-*-* } 148 } +// { dg-bogus "static assertion failed" "" { xfail *-*-* } 158 } diff --git a/libstdc++-v3/testsuite/std/ranges/single_view.cc b/libstdc++-v3/testsuite/std/ranges/single_view.cc index 658e0a0a32d..48406447060 100644 --- a/libstdc++-v3/testsuite/std/ranges/single_view.cc +++ b/libstdc++-v3/testsuite/std/ranges/single_view.cc @@ -58,9 +58,24 @@ test03() VERIFY(*std::ranges::begin(s3) == 'a'); } +void +test06() +{ + // PR libstdc++/100475 comment #7 + struct S { + S() = default; + S(std::initializer_list) = delete; + S(const S&) {} + }; + S obj; + auto x = std::views::single(obj); + auto y = std::views::single(std::move(obj)); +} + int main() { test01(); test02(); test03(); + test06(); } diff --git a/libstdc++-v3/testsuite/std/ranges/view.cc b/libstdc++-v3/testsuite/std/ranges/view.cc index 72e8bcb50d7..095a92e466f 100644 --- a/libstdc++-v3/testsuite/std/ranges/view.cc +++ b/libstdc++-v3/testsuite/std/ranges/view.cc @@ -31,7 +31,7 @@ static_assert(std::ranges::view>); static_assert(std::ranges::view>); -static_assert(!std::ranges::view>); +static_assert(std::ranges::view>); // Changed with P2325R3 static_assert(std::ranges::view); static_assert(std::ranges::view); -- 2.36.0.63.gf5aaf72f1b