From patchwork Fri Jan 14 10:16:10 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jonathan Wakely X-Patchwork-Id: 50017 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id BE6963857825 for ; Fri, 14 Jan 2022 10:19:08 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org BE6963857825 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1642155548; bh=WjnRJn5pa51PMCSMH1FDi6OT0Hq5pQmU6W16Se4uoSs=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=DH/LNE8NiBmYXEy/RsM1XD+L6LPmVzaxwzA+Ws8U3quIfJcI1eDrrlZ/SEAvn+Xhm 2V6c80/DcYc2DMKetrARNmLDQovTg5IZQGVWGaGFY/a839Cr6lqF8sQmOsF3yX+Jrm VYx3ipRmZAnRz8dxOWjz0uxrdbEryrc7mrB4+onc= 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 0FE2B385783B for ; Fri, 14 Jan 2022 10:16:15 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 0FE2B385783B Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-317-cX1YrdPLN2idtOUTk2bOrA-1; Fri, 14 Jan 2022 05:16:13 -0500 X-MC-Unique: cX1YrdPLN2idtOUTk2bOrA-1 Received: from smtp.corp.redhat.com (int-mx07.intmail.prod.int.phx2.redhat.com [10.5.11.22]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id C61D11083F61; Fri, 14 Jan 2022 10:16:11 +0000 (UTC) Received: from localhost (unknown [10.33.36.252]) by smtp.corp.redhat.com (Postfix) with ESMTP id 759C010595B1; Fri, 14 Jan 2022 10:16:11 +0000 (UTC) To: libstdc++@gcc.gnu.org, gcc-patches@gcc.gnu.org Subject: [committed] libstdc++: Ignore cv-quals when std::allocator constructs Date: Fri, 14 Jan 2022 10:16:10 +0000 Message-Id: <20220114101610.2975321-1-jwakely@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.84 on 10.5.11.22 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-13.9 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_H3, 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: 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+patchwork=sourceware.org@gcc.gnu.org Sender: "Gcc-patches" Tested powerpc64le-linux, pushed to trunk. When I added the std::allocator_traits> specialization it broke code like this: std::allocate_shared(std::allocator()); The problem is that allocator_traits>::construct(a, p) now uses std::_Construct(p), which only does a static_cast(p) and so fails if the pointer has cv-quals. This changes std::_Construct (and the related std::_Construct_novalue) to use a C-style cast to (void*) which matches the effects of the "voidify" helper in the C++20 standard. libstdc++-v3/ChangeLog: * include/bits/stl_construct.h (_Construct, _Construct_novalue): Also cast away cv-qualifiers when converting pointer to void. * testsuite/20_util/allocator/void.cc: Test construct function with cv-qualified types. --- libstdc++-v3/include/bits/stl_construct.h | 4 ++-- libstdc++-v3/testsuite/20_util/allocator/void.cc | 15 +++++++++++++++ 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/libstdc++-v3/include/bits/stl_construct.h b/libstdc++-v3/include/bits/stl_construct.h index 7c5fd4c9cf7..9531222809c 100644 --- a/libstdc++-v3/include/bits/stl_construct.h +++ b/libstdc++-v3/include/bits/stl_construct.h @@ -116,7 +116,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION return; } #endif - ::new(static_cast(__p)) _Tp(std::forward<_Args>(__args)...); + ::new((void*)__p) _Tp(std::forward<_Args>(__args)...); } #else template @@ -132,7 +132,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION template inline void _Construct_novalue(_T1* __p) - { ::new(static_cast(__p)) _T1; } + { ::new((void*)__p) _T1; } template _GLIBCXX20_CONSTEXPR void diff --git a/libstdc++-v3/testsuite/20_util/allocator/void.cc b/libstdc++-v3/testsuite/20_util/allocator/void.cc index 52e1fef3700..5cdf0be012c 100644 --- a/libstdc++-v3/testsuite/20_util/allocator/void.cc +++ b/libstdc++-v3/testsuite/20_util/allocator/void.cc @@ -87,8 +87,23 @@ static_assert( std::is_same::const_pointer, const void*>(), "const_pointer is const void*" ); #endif // C++20 +void +test02() +{ + std::allocator av; + int* p = std::allocator().allocate(1); + const int* c = p; + std::allocator_traits>::construct(av, c, 0); + volatile int* v = p; + std::allocator_traits>::construct(av, v, 0); + const volatile int* cv = p; + std::allocator_traits>::construct(av, cv, 0); + std::allocator().deallocate(p, 1); +} + int main() { test01(); + test02(); }