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.133.124]) by sourceware.org (Postfix) with ESMTP id 9E710383D009 for ; Fri, 16 Jul 2021 22:08:34 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 9E710383D009 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-488-8SHXgOYqPyOHsOlEmaDTHg-1; Fri, 16 Jul 2021 18:08:32 -0400 X-MC-Unique: 8SHXgOYqPyOHsOlEmaDTHg-1 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.phx2.redhat.com [10.5.11.13]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id B0D751842154; Fri, 16 Jul 2021 22:08:31 +0000 (UTC) Received: from localhost (unknown [10.33.37.12]) by smtp.corp.redhat.com (Postfix) with ESMTP id 4E93E60864; Fri, 16 Jul 2021 22:08:31 +0000 (UTC) Date: Fri, 16 Jul 2021 23:08:30 +0100 From: Jonathan Wakely To: libstdc++@gcc.gnu.org, gcc-patches@gcc.gnu.org Subject: [committed] libstdc++: Improve diagnostics for std::get with invalid tuple index Message-ID: References: MIME-Version: 1.0 In-Reply-To: X-Clacks-Overhead: GNU Terry Pratchett X-Scanned-By: MIMEDefang 2.79 on 10.5.11.13 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: multipart/mixed; boundary="TFYNmlVRUjXWUwrQ" Content-Disposition: inline X-Spam-Status: No, score=-14.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, RCVD_IN_MSPIKE_H4, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_NONE, TXREP 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: 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: Fri, 16 Jul 2021 22:08:36 -0000 --TFYNmlVRUjXWUwrQ Content-Type: text/plain; charset=us-ascii; format=flowed Content-Disposition: inline The recent fix for std::get uses a deleted overload to give better diagnostics for out-of-range indices. This does something similar for std::get. Tested powerpc64le-linux. Committed to trunk. This adds a deleted overload of std::get(const tuple&). Invalid calls with an out of range index will match the deleted overload and give a single, clear error about calling a deleted function, instead of overload resolution errors for every std::get overload in the library. This changes the current output of 15+ errors (plus notes and associated header context) into just two errors (plus context): error: static assertion failed: tuple index must be in range error: use of deleted function 'constexpr std::__enable_if_t<(__i >= sizeof... (_Types))> std::get(const std::tuple<_Types ...>&) [with long unsigned int __i = 1; _Elements = {int}; std::__enable_if_t<(__i >= sizeof... (_Types))> = void]' This seems like a nice improvement, although PR c++/66968 means that "_Types" is printed in the signature rather than "_Elements". Signed-off-by: Jonathan Wakely libstdc++-v3/ChangeLog: * include/std/tuple (get): Add deleted overload for bad index. * testsuite/20_util/tuple/element_access/get_neg.cc: Adjust expected errors. --TFYNmlVRUjXWUwrQ Content-Type: text/x-patch; charset=us-ascii Content-Disposition: attachment; filename="patch.txt" commit 3dbc7b809a62167b36f217ab5f43207be19e5908 Author: Jonathan Wakely Date: Fri Jul 16 20:59:43 2021 libstdc++: Improve diagnostics for std::get with invalid tuple index This adds a deleted overload of std::get(const tuple&). Invalid calls with an out of range index will match the deleted overload and give a single, clear error about calling a deleted function, instead of overload resolution errors for every std::get overload in the library. This changes the current output of 15+ errors (plus notes and associated header context) into just two errors (plus context): error: static assertion failed: tuple index must be in range error: use of deleted function 'constexpr std::__enable_if_t<(__i >= sizeof... (_Types))> std::get(const std::tuple<_Types ...>&) [with long unsigned int __i = 1; _Elements = {int}; std::__enable_if_t<(__i >= sizeof... (_Types))> = void]' This seems like a nice improvement, although PR c++/66968 means that "_Types" is printed in the signature rather than "_Elements". Signed-off-by: Jonathan Wakely libstdc++-v3/ChangeLog: * include/std/tuple (get): Add deleted overload for bad index. * testsuite/20_util/tuple/element_access/get_neg.cc: Adjust expected errors. diff --git a/libstdc++-v3/include/std/tuple b/libstdc++-v3/include/std/tuple index 6953f8715d7..8ee0d2f1ef5 100644 --- a/libstdc++-v3/include/std/tuple +++ b/libstdc++-v3/include/std/tuple @@ -1406,6 +1406,13 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION return std::forward(std::__get_helper<__i>(__t)); } + /// @cond undocumented + // Deleted overload chosen for invalid indices. + template + constexpr __enable_if_t<(__i >= sizeof...(_Elements))> + get(const tuple<_Elements...>&) = delete; + /// @endcond + #if __cplusplus >= 201402L #define __cpp_lib_tuples_by_type 201304 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 cd850fdc21b..225bb6245a6 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 @@ -25,12 +25,12 @@ test01() { using test_type = std::tuple<>; test_type t; - std::get<0>(t); // { dg-error "no match" } - std::get<0>(const_cast(t)); // { dg-error "no match" } - std::get<0>(static_cast(t)); // { dg-error "no match" } - std::get<5>(t); // { dg-error "no match" } - std::get<5>(const_cast(t)); // { dg-error "no match" } - std::get<5>(static_cast(t)); // { dg-error "no match" } + std::get<0>(t); // { dg-error "deleted" } + std::get<0>(const_cast(t)); // { dg-error "deleted" } + std::get<0>(static_cast(t)); // { dg-error "deleted" } + std::get<5>(t); // { dg-error "deleted" } + std::get<5>(const_cast(t)); // { dg-error "deleted" } + std::get<5>(static_cast(t)); // { dg-error "deleted" } } void @@ -38,12 +38,12 @@ test02() { using test_type = std::tuple; test_type t; - std::get<1>(t); // { dg-error "no match" } - std::get<1>(const_cast(t)); // { dg-error "no match" } - std::get<1>(static_cast(t)); // { dg-error "no match" } - std::get<5>(t); // { dg-error "no match" } - std::get<5>(const_cast(t)); // { dg-error "no match" } - std::get<5>(static_cast(t)); // { dg-error "no match" } + std::get<1>(t); // { dg-error "deleted" } + std::get<1>(const_cast(t)); // { dg-error "deleted" } + std::get<1>(static_cast(t)); // { dg-error "deleted" } + std::get<5>(t); // { dg-error "deleted" } + std::get<5>(const_cast(t)); // { dg-error "deleted" } + std::get<5>(static_cast(t)); // { dg-error "deleted" } } void @@ -51,15 +51,12 @@ test03() { using test_type = std::tuple; test_type t; - std::get<5>(t); // { dg-error "no match" } - std::get<5>(const_cast(t)); // { dg-error "no match" } - std::get<5>(static_cast(t)); // { dg-error "no match" } - std::get<6>(t); // { dg-error "no match" } - std::get<6>(const_cast(t)); // { dg-error "no match" } - std::get<6>(static_cast(t)); // { dg-error "no match" } + std::get<5>(t); // { dg-error "deleted" } + std::get<5>(const_cast(t)); // { dg-error "deleted" } + std::get<5>(static_cast(t)); // { dg-error "deleted" } + std::get<6>(t); // { dg-error "deleted" } + std::get<6>(const_cast(t)); // { dg-error "deleted" } + std::get<6>(static_cast(t)); // { dg-error "deleted" } } -// { 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" } +// { dg-error "tuple index must be in range" "" { target *-*-* } 0 } --TFYNmlVRUjXWUwrQ--