From patchwork Tue Jun 6 11:46:08 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jonathan Wakely X-Patchwork-Id: 103741 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:994d:0:b0:3d9:f83d:47d9 with SMTP id k13csp3330985vqr; Tue, 6 Jun 2023 04:50:13 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ4TEdyFJsBrJDlZ/jTW/YSaTrqdma/Av6M0RROPv1N+V2fBbfeOpBTppxXOh1p/atLlgCmE X-Received: by 2002:a17:907:9688:b0:974:62bc:4f42 with SMTP id hd8-20020a170907968800b0097462bc4f42mr2244230ejc.37.1686052213223; Tue, 06 Jun 2023 04:50:13 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1686052213; cv=none; d=google.com; s=arc-20160816; b=w/4XZIGXCOVLjMNne3QKsNfqJ2D9/xoHmHCLXHbH1bt2j4A95GkhB8qG+Fj/vifrPr CLDtMCwXODLw4FiwSjLSoWPiGoyGKREMA4N4w6ETA3xyt4ALoqyo23e8hpxqyoAC21xH LRqT8qy5ukJevT4NwdVGWEMEuBAGZ8mT5xVjy7UpDoy1TeNi8qY/snrhsm6fjNGY2RTM PjXdD+s2OI9pUISGgt5tiZiPUSNMizB+y+06LavVNDnWF5PsQW7gv9Ex2+fxA5KpMeAN 3RelxRiTDwjUSPMyNffHZc8nNtMT25g7Wj06/Okl8w67gwN5FGBDeikctnO0bP6fHXbZ qfqw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:reply-to:from:list-subscribe:list-help:list-post :list-archive:list-unsubscribe:list-id:precedence :content-transfer-encoding:mime-version:message-id:date:subject:to :dmarc-filter:delivered-to:dkim-signature:dkim-filter; bh=Z+BUWnNKogyMCOaD5CphBmC1r4uOcAsWpA7tHByzxDc=; b=uxq1jQi4fJhEsuVpJtIOL1oFI8TXBWb5JN7NsJDZPgPp/uqxhc0ImVk24r32NI6GBv K2xo1c4g0wF6yymAknoEcuh4cc27ClSrC5hzU1/1EmKug6zAp3u7Xa7ZxLOy/pWi0r4l ykgo+lpnlxEkw8U9KiiT7Q8kXyDE9euU3j7VixTw8mCZRnCvjAxjS/K8Bkm8fwQXKCNR o6fThXv90Gy+PKrIuTsvPxNzFNHJHiYGUdENmjhtPo6DSlTabU9uu0qncKWWEuKfH2i0 WtoOPwLs0WCQKwGihCj2zdcFROqc+nueilMaoqDYSIpPYwGEm8uKmgt2ua0NgKe7mfmQ rTjw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=rA4JshL5; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnu.org Received: from sourceware.org (ip-8-43-85-97.sourceware.org. [8.43.85.97]) by mx.google.com with ESMTPS id ay7-20020a170906d28700b00973a80fb4basi6223845ejb.488.2023.06.06.04.50.12 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 06 Jun 2023 04:50:13 -0700 (PDT) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) client-ip=8.43.85.97; Authentication-Results: mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=rA4JshL5; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnu.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 3D8153856624 for ; Tue, 6 Jun 2023 11:47:56 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 3D8153856624 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1686052076; bh=Z+BUWnNKogyMCOaD5CphBmC1r4uOcAsWpA7tHByzxDc=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=rA4JshL5zb2jE9Ttieam7qbTs826obJu3IAEx+SL0jrFc1B5nmf2z5Lb0UZ9e4Zbp yw/ZMqv0zwTh1xDCAvWyTj8R8FTHPm0Zon+lz+G71Wko8qz8UiZaeOzpgGqup6n0JN WFk+uvvDX3Y9PlCpKqU4S7pdXHNTISZcak4mJgho= 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 [170.10.129.124]) by sourceware.org (Postfix) with ESMTPS id 6CC263858023 for ; Tue, 6 Jun 2023 11:46:12 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 6CC263858023 Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-377-JklFaCv8MkCyRrxMKaBKig-1; Tue, 06 Jun 2023 07:46:10 -0400 X-MC-Unique: JklFaCv8MkCyRrxMKaBKig-1 Received: from smtp.corp.redhat.com (int-mx10.intmail.prod.int.rdu2.redhat.com [10.11.54.10]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 861D2101A585; Tue, 6 Jun 2023 11:46:10 +0000 (UTC) Received: from localhost (unknown [10.42.28.30]) by smtp.corp.redhat.com (Postfix) with ESMTP id 449DF400F5F; Tue, 6 Jun 2023 11:46:10 +0000 (UTC) To: libstdc++@gcc.gnu.org, gcc-patches@gcc.gnu.org Subject: [committed] libstdc++: Fix ambiguous expression in std::array::front() [PR110139] Date: Tue, 6 Jun 2023 12:46:08 +0100 Message-Id: <20230606114608.868760-1-jwakely@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.10 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-11.7 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_NONE, SPF_HELO_NONE, SPF_NONE, TXREP, T_SCC_BODY_TEXT_LINE, URI_HEX autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) 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+ouuuleilei=gmail.com@gcc.gnu.org Sender: "Gcc-patches" X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1767953885141455098?= X-GMAIL-MSGID: =?utf-8?q?1767953885141455098?= Tested x86_64-linux. Pushed to trunk. Backport needed for gcc-13 too. -- >8 -- For 32-bit targets using -pedantic (or using Clang) makes the expression _M_elems[0] ambiguous. The overloaded operator[] that we want to call has a size_t parameter, but 0 is type ptrdiff_t for many ILP32 targets, so using the implicit conversion from _M_elems to T* and then subscripting that is also viable. Change the 0 to (size_type)0 and also make the conversion to T* explicit, so that's it's not viable here. The latter change requires a static_cast in data() where we really do want to convert _M_elems to a pointer. libstdc++-v3/ChangeLog: PR libstdc++/110139 * include/std/array (__array_traits::operator T*()): Make conversion operator explicit. (array::front): Use size_type as subscript operand. (array::data): Use static_cast to make conversion explicit. * testsuite/23_containers/array/element_access/110139.cc: New test. --- libstdc++-v3/include/std/array | 10 +++++----- .../23_containers/array/element_access/110139.cc | 5 +++++ 2 files changed, 10 insertions(+), 5 deletions(-) create mode 100644 libstdc++-v3/testsuite/23_containers/array/element_access/110139.cc diff --git a/libstdc++-v3/include/std/array b/libstdc++-v3/include/std/array index 27354d9e3fb..70280c1beeb 100644 --- a/libstdc++-v3/include/std/array +++ b/libstdc++-v3/include/std/array @@ -69,7 +69,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION // Conversion to a pointer produces a null pointer. __attribute__((__always_inline__)) - constexpr operator _Tp*() const noexcept { return nullptr; } + constexpr explicit operator _Tp*() const noexcept { return nullptr; } }; using _Is_swappable = true_type; @@ -240,7 +240,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION front() noexcept { __glibcxx_requires_nonempty(); - return _M_elems[0]; + return _M_elems[(size_type)0]; } [[__nodiscard__]] @@ -250,7 +250,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION #if __cplusplus >= 201402L __glibcxx_requires_nonempty(); #endif - return _M_elems[0]; + return _M_elems[(size_type)0]; } [[__nodiscard__]] @@ -274,12 +274,12 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION [[__nodiscard__, __gnu__::__const__, __gnu__::__always_inline__]] _GLIBCXX17_CONSTEXPR pointer data() noexcept - { return _M_elems; } + { return static_cast(_M_elems); } [[__nodiscard__]] _GLIBCXX17_CONSTEXPR const_pointer data() const noexcept - { return _M_elems; } + { return static_cast(_M_elems); } }; #if __cpp_deduction_guides >= 201606 diff --git a/libstdc++-v3/testsuite/23_containers/array/element_access/110139.cc b/libstdc++-v3/testsuite/23_containers/array/element_access/110139.cc new file mode 100644 index 00000000000..6f09aacf0a7 --- /dev/null +++ b/libstdc++-v3/testsuite/23_containers/array/element_access/110139.cc @@ -0,0 +1,5 @@ +// { dg-options "-pedantic" } +// { dg-do compile { target c++11 } } +#include +template class std::array; +// { dg-bogus "ambiguous overload" "" { target *-*-* } 0 }