From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 2181) id C2AC838515FE; Mon, 19 Jul 2021 12:38:54 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org C2AC838515FE MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset="utf-8" From: Jonathan Wakely To: gcc-cvs@gcc.gnu.org, libstdc++-cvs@gcc.gnu.org Subject: [gcc r11-8777] libstdc++: Fix std::get for std::tuple [PR101427] X-Act-Checkin: gcc X-Git-Author: Jonathan Wakely X-Git-Refname: refs/heads/releases/gcc-11 X-Git-Oldrev: 87aa5a09eb09fb07351b5e6c3bda7c354ad8f50e X-Git-Newrev: 6dc150d9a036cbbed3c4dac6df1ce895b899d423 Message-Id: <20210719123854.C2AC838515FE@sourceware.org> Date: Mon, 19 Jul 2021 12:38:54 +0000 (GMT) X-BeenThere: libstdc++-cvs@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Libstdc++-cvs mailing list List-Unsubscribe: , List-Archive: List-Help: List-Subscribe: , X-List-Received-Date: Mon, 19 Jul 2021 12:38:54 -0000 https://gcc.gnu.org/g:6dc150d9a036cbbed3c4dac6df1ce895b899d423 commit r11-8777-g6dc150d9a036cbbed3c4dac6df1ce895b899d423 Author: Jonathan Wakely Date: Wed Jul 14 20:14:14 2021 +0100 libstdc++: Fix std::get for std::tuple [PR101427] The std::get functions relied on deduction failing if more than one base class existed for the type T. However the implementation of Core DR 2303 (in r11-4693) made deduction succeed (and select the more-derived base class). This rewrites the implementation of std::get to explicitly check for more than one occurrence of T in the tuple elements, making it ill-formed again. Additionally, the large wall of overload resolution errors described in PR c++/101460 is avoided by making std::get use __get_helper directly instead of calling std::get, and by adding a deleted overload of __get_helper for out-of-range N. Signed-off-by: Jonathan Wakely libstdc++-v3/ChangeLog: PR libstdc++/101427 * include/std/tuple (tuple_element): Improve static_assert text. (__get_helper): Add deleted overload. (get(tuple&&), get(const tuple&&)): Use __get_helper directly. (__get_helper2): Remove. (__find_uniq_type_in_pack): New constexpr helper function. (get): Use __find_uniq_type_in_pack and __get_helper instead of __get_helper2. * testsuite/20_util/tuple/element_access/get_neg.cc: Adjust expected errors. * testsuite/20_util/tuple/element_access/101427.cc: New test. (cherry picked from commit 17855eed7fc76b2cee7fbbc26f84d3c8b99be13c) Diff: --- libstdc++-v3/include/std/tuple | 69 +++++++++++++++++----- .../20_util/tuple/element_access/101427.cc | 23 ++++++++ .../20_util/tuple/element_access/get_neg.cc | 3 +- 3 files changed, 78 insertions(+), 17 deletions(-) diff --git a/libstdc++-v3/include/std/tuple b/libstdc++-v3/include/std/tuple index 2d562f8da77..6953f8715d7 100644 --- a/libstdc++-v3/include/std/tuple +++ b/libstdc++-v3/include/std/tuple @@ -1358,7 +1358,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION struct tuple_element<__i, tuple<>> { static_assert(__i < tuple_size>::value, - "tuple index is in range"); + "tuple index must be in range"); }; template @@ -1371,6 +1371,11 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION __get_helper(const _Tuple_impl<__i, _Head, _Tail...>& __t) noexcept { return _Tuple_impl<__i, _Head, _Tail...>::_M_head(__t); } + // Deleted overload to improve diagnostics for invalid indices + template + __enable_if_t<(__i >= sizeof...(_Types))> + __get_helper(const tuple<_Types...>&) = delete; + /// Return a reference to the ith element of a tuple. template constexpr __tuple_element_t<__i, tuple<_Elements...>>& @@ -1389,7 +1394,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION get(tuple<_Elements...>&& __t) noexcept { typedef __tuple_element_t<__i, tuple<_Elements...>> __element_type; - return std::forward<__element_type&&>(std::get<__i>(__t)); + return std::forward<__element_type>(std::__get_helper<__i>(__t)); } /// Return a const rvalue reference to the ith element of a const tuple rvalue. @@ -1398,47 +1403,79 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION get(const tuple<_Elements...>&& __t) noexcept { typedef __tuple_element_t<__i, tuple<_Elements...>> __element_type; - return std::forward(std::get<__i>(__t)); + return std::forward(std::__get_helper<__i>(__t)); } #if __cplusplus >= 201402L #define __cpp_lib_tuples_by_type 201304 - template - constexpr _Head& - __get_helper2(_Tuple_impl<__i, _Head, _Tail...>& __t) noexcept - { return _Tuple_impl<__i, _Head, _Tail...>::_M_head(__t); } - - template - constexpr const _Head& - __get_helper2(const _Tuple_impl<__i, _Head, _Tail...>& __t) noexcept - { return _Tuple_impl<__i, _Head, _Tail...>::_M_head(__t); } + // Return the index of _Tp in _Types, if it occurs exactly once. + // Otherwise, return sizeof...(_Types). + // TODO reuse this for __detail::__variant::__exactly_once. + template + constexpr size_t + __find_uniq_type_in_pack() + { + constexpr size_t __sz = sizeof...(_Types); + constexpr bool __found[__sz] = { __is_same(_Tp, _Types) ... }; + size_t __n = __sz; + for (size_t __i = 0; __i < __sz; ++__i) + { + if (__found[__i]) + { + if (__n < __sz) // more than one _Tp found + return __sz; + __n = __i; + } + } + return __n; + } /// Return a reference to the unique element of type _Tp of a tuple. template constexpr _Tp& get(tuple<_Types...>& __t) noexcept - { return std::__get_helper2<_Tp>(__t); } + { + constexpr size_t __idx = __find_uniq_type_in_pack<_Tp, _Types...>(); + static_assert(__idx < sizeof...(_Types), + "the type T in std::get must occur exactly once in the tuple"); + return std::__get_helper<__idx>(__t); + } /// Return a reference to the unique element of type _Tp of a tuple rvalue. template constexpr _Tp&& get(tuple<_Types...>&& __t) noexcept - { return std::forward<_Tp&&>(std::__get_helper2<_Tp>(__t)); } + { + constexpr size_t __idx = __find_uniq_type_in_pack<_Tp, _Types...>(); + static_assert(__idx < sizeof...(_Types), + "the type T in std::get must occur exactly once in the tuple"); + return std::forward<_Tp>(std::__get_helper<__idx>(__t)); + } /// Return a const reference to the unique element of type _Tp of a tuple. template constexpr const _Tp& get(const tuple<_Types...>& __t) noexcept - { return std::__get_helper2<_Tp>(__t); } + { + constexpr size_t __idx = __find_uniq_type_in_pack<_Tp, _Types...>(); + static_assert(__idx < sizeof...(_Types), + "the type T in std::get must occur exactly once in the tuple"); + return std::__get_helper<__idx>(__t); + } /// Return a const reference to the unique element of type _Tp of /// a const tuple rvalue. template constexpr const _Tp&& get(const tuple<_Types...>&& __t) noexcept - { return std::forward(std::__get_helper2<_Tp>(__t)); } + { + constexpr size_t __idx = __find_uniq_type_in_pack<_Tp, _Types...>(); + static_assert(__idx < sizeof...(_Types), + "the type T in std::get must occur exactly once in the tuple"); + return std::forward(std::__get_helper<__idx>(__t)); + } #endif // This class performs the comparison operations on tuples diff --git a/libstdc++-v3/testsuite/20_util/tuple/element_access/101427.cc b/libstdc++-v3/testsuite/20_util/tuple/element_access/101427.cc new file mode 100644 index 00000000000..6490afda193 --- /dev/null +++ b/libstdc++-v3/testsuite/20_util/tuple/element_access/101427.cc @@ -0,0 +1,23 @@ +// { dg-do compile { target c++14 } } +// PR libstdc++/101427 + +#include + +void test_pr101427() +{ + std::tuple tup1; + std::get(tup1); // { dg-error "here" } + + const std::tuple tup2; + std::get(tup2); // { dg-error "here" } + + std::tuple tup3; + std::get(std::move(tup3)); // { dg-error "here" } + + const std::tuple tup4; + std::get(std::move(tup4)); // { dg-error "here" } + + // { dg-error "must occur exactly once in the tuple" "" { target *-*-* } 0 } +} + +// { dg-prune-output "use of deleted function" } diff --git a/libstdc++-v3/testsuite/20_util/tuple/element_access/get_neg.cc b/libstdc++-v3/testsuite/20_util/tuple/element_access/get_neg.cc index 5a8c3033575..cd850fdc21b 100644 --- a/libstdc++-v3/testsuite/20_util/tuple/element_access/get_neg.cc +++ b/libstdc++-v3/testsuite/20_util/tuple/element_access/get_neg.cc @@ -17,7 +17,6 @@ // { dg-options "-fno-show-column" } // { dg-do compile { target c++14 } } -// { dg-prune-output "tuple index is in range" } #include @@ -60,5 +59,7 @@ test03() std::get<6>(static_cast(t)); // { dg-error "no match" } } +// { dg-prune-output "tuple index must be in range" } // { dg-prune-output "no type named .type" } // { dg-prune-output "type/value mismatch" } +// { dg-prune-output "use of deleted function" }