From patchwork Thu Jul 15 15:26:07 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jonathan Wakely X-Patchwork-Id: 1505765 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=2620:52:3:1:0:246e:9693:128c; helo=sourceware.org; envelope-from=gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=) Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org header.a=rsa-sha256 header.s=default header.b=IS8MeFr6; dkim-atps=neutral Received: from sourceware.org (server2.sourceware.org [IPv6:2620:52:3:1:0:246e:9693:128c]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 4GQdcf0K7Wz9sV8 for ; Fri, 16 Jul 2021 01:30:57 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id C655739890A8 for ; Thu, 15 Jul 2021 15:30:54 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org C655739890A8 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1626363054; bh=SxpE8kMpdodbD5HDbaYHX+5WdwSXaqF1S4G4w5jV+vE=; h=Date:To:Subject:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=IS8MeFr65L6bCMkNUotd7GhYrVExuqaIFeCzeHKQ5xc5drzcdc3VjmaG6CR3/eWaR 46yQg8Std2k+OI0FSnrR0TFfte9udkO6tG+MCzvhR+Ku6UZOhpOZG2YYTeFawaRXO/ NysfAyVX3YmNO2i+tDspxAEoY6RhkEzh4R4XQY/c= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [216.205.24.124]) by sourceware.org (Postfix) with ESMTP id 416903864831 for ; Thu, 15 Jul 2021 15:26:12 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 416903864831 Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-549-1P47TmLMPm6lNXjhuDBjSw-1; Thu, 15 Jul 2021 11:26:10 -0400 X-MC-Unique: 1P47TmLMPm6lNXjhuDBjSw-1 Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.12]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 183A11018720; Thu, 15 Jul 2021 15:26:09 +0000 (UTC) Received: from localhost (unknown [10.33.37.12]) by smtp.corp.redhat.com (Postfix) with ESMTP id B8A0F60BF1; Thu, 15 Jul 2021 15:26:08 +0000 (UTC) Date: Thu, 15 Jul 2021 16:26:07 +0100 To: libstdc++@gcc.gnu.org, gcc-patches@gcc.gnu.org Subject: [committed] libstdc++: Fix std::get for std::tuple [PR101427] Message-ID: MIME-Version: 1.0 X-Clacks-Overhead: GNU Terry Pratchett X-Scanned-By: MIMEDefang 2.79 on 10.5.11.12 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Disposition: inline X-Spam-Status: No, score=-15.0 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, RCVD_IN_MSPIKE_H4, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_NONE, TXREP, URI_HEX autolearn=ham autolearn_force=no version=3.4.4 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Jonathan Wakely via Gcc-patches From: Jonathan Wakely Reply-To: Jonathan Wakely Errors-To: gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org Sender: "Gcc-patches" 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. Tested powerpc64le-linux. Committed to trunk. commit 17855eed7fc76b2cee7fbbc26f84d3c8b99be13c Author: Jonathan Wakely Date: Wed Jul 14 20:14:14 2021 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. 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" }