From patchwork Wed Oct 2 19:37:51 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jonathan Wakely X-Patchwork-Id: 1992164 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=GQSCRaSh; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=2620:52:3:1:0:246e:9693:128c; helo=server2.sourceware.org; envelope-from=gcc-patches-bounces~incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=patchwork.ozlabs.org) Received: from server2.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 ECDSA (secp384r1) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4XJm5y026Bz1xtY for ; Thu, 3 Oct 2024 06:06:14 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id D6C6D385C6D3 for ; Wed, 2 Oct 2024 20:06:11 +0000 (GMT) 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.133.124]) by sourceware.org (Postfix) with ESMTP id F18C63858C50 for ; Wed, 2 Oct 2024 20:05:13 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org F18C63858C50 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org F18C63858C50 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1727899515; cv=none; b=EW2INXQcnvTGPyU8y9F7+tNO5SBO+KQ4H9Ie7RXDzIcFUBDM+GJv/7t0BHNerLI/+mifFRQ2GCnmZ37LkTOS8fw+6+b0UsP+XAyAWcb8PLi62G/7AClsy2B7s2tsy2WxHQCATB9rB1VT3j77OjwEjPUQHe0zJzwXjdLQVC1tJlM= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1727899515; c=relaxed/simple; bh=x3SUyvx8z5vRf3NNKlEMTgvEh8Ajm9mwL7D0VTsuehU=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=heN1aYfMPP4RINCMsJmS8xER/Lzh9DmboXkbXbWJsg65iHqGapCNLATse2Z0EwqifOf0+j4Hh9tVxoqkl4RSIaGo9SgiDgy+BfwdKhHeRWcTy7F3J1NgoMaAE2qyq0slmudvil7fZcjlNqFG37fQ7wyKdmB1N4zSRaD/kAPiZY0= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1727899513; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=2kFMnEwWw7QlfAjPHBSLIUcyWvScpZJb3dQqkKwGf/s=; b=GQSCRaShSANz3gcYXXJRDJ6JL6nq2Qed1sphK8kQLdtsxgcieyYqj12QVftkxi8nY1fAew hY1CaGB0XgSOFluIAAV5nAUzI7f0HTWzlskWVFhWV43L4OEILzPhHIFGBwZVHQRN1cA7xG W/sXX7ZDuC55MYGX16Q+Stla6I8MNmA= Received: from mx-prod-mc-04.mail-002.prod.us-west-2.aws.redhat.com (ec2-54-186-198-63.us-west-2.compute.amazonaws.com [54.186.198.63]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-570-L5JfyH5fNVeqvhVEbWWOuA-1; Wed, 02 Oct 2024 16:05:10 -0400 X-MC-Unique: L5JfyH5fNVeqvhVEbWWOuA-1 Received: from mx-prod-int-05.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-05.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.17]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-04.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 788D21955F45; Wed, 2 Oct 2024 20:05:09 +0000 (UTC) Received: from localhost (unknown [10.42.28.136]) by mx-prod-int-05.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id D17AD19560A3; Wed, 2 Oct 2024 20:05:08 +0000 (UTC) From: Jonathan Wakely To: libstdc++@gcc.gnu.org, gcc-patches@gcc.gnu.org Subject: [PATCH 2/2] libstdc++: Do not cast away const-ness in std::construct_at (LWG 3870) Date: Wed, 2 Oct 2024 20:37:51 +0100 Message-ID: <20241002200503.3359969-2-jwakely@redhat.com> In-Reply-To: <20241002200503.3359969-1-jwakely@redhat.com> References: <20241002200503.3359969-1-jwakely@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.0 on 10.30.177.17 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-12.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_NONE, RCVD_IN_MSPIKE_H3, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_NONE, TXREP autolearn=unavailable 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.30 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gcc-patches-bounces~incoming=patchwork.ozlabs.org@gcc.gnu.org Tested x86_64-linux. -- >8 -- This change also requires implementing the proposed resolution of LWG 3216 so that std::make_shared and std::allocate_shared still work, and the proposed resolution of LWG 3891 so that std::expected still works. libstdc++-v3/ChangeLog: * include/bits/shared_ptr_base.h: Remove cv-qualifiers from type managed by _Sp_counted_ptr_inplace, as per LWG 3210. * include/bits/stl_construct.h: Do not cast away cv-qualifiers when passing pointer to placement new. * include/std/expected: Use remove_cv_t for union member, as per LWG 3891. * testsuite/20_util/allocator/void.cc: Do not test construction via const pointer. --- libstdc++-v3/include/bits/shared_ptr_base.h | 15 ++++++++------- libstdc++-v3/include/bits/stl_construct.h | 6 +++--- libstdc++-v3/include/std/expected | 2 +- libstdc++-v3/testsuite/20_util/allocator/void.cc | 15 --------------- 4 files changed, 12 insertions(+), 26 deletions(-) diff --git a/libstdc++-v3/include/bits/shared_ptr_base.h b/libstdc++-v3/include/bits/shared_ptr_base.h index 3d0b74ba1c6..ef0658f6182 100644 --- a/libstdc++-v3/include/bits/shared_ptr_base.h +++ b/libstdc++-v3/include/bits/shared_ptr_base.h @@ -591,7 +591,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION _Alloc& _M_alloc() noexcept { return _A_base::_S_get(*this); } - __gnu_cxx::__aligned_buffer<_Tp> _M_storage; + __gnu_cxx::__aligned_buffer<__remove_cv_t<_Tp>> _M_storage; }; public: @@ -633,7 +633,6 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION virtual void* _M_get_deleter(const std::type_info& __ti) noexcept override { - auto __ptr = const_cast::type*>(_M_ptr()); // Check for the fake type_info first, so we don't try to access it // as a real type_info object. Otherwise, check if it's the real // type_info for this class. With RTTI enabled we can check directly, @@ -646,11 +645,12 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION _Sp_make_shared_tag::_S_eq(__ti) #endif ) - return __ptr; + return _M_ptr(); return nullptr; } - _Tp* _M_ptr() noexcept { return _M_impl._M_storage._M_ptr(); } + __remove_cv_t<_Tp>* + _M_ptr() noexcept { return _M_impl._M_storage._M_ptr(); } _Impl _M_impl; }; @@ -674,13 +674,13 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION [[no_unique_address]] _Alloc _M_alloc; union { - _Tp _M_obj; + remove_cv_t<_Tp> _M_obj; char _M_unused; }; friend class __shared_count<_Lp>; // To be able to call _M_ptr(). - _Tp* _M_ptr() noexcept { return std::__addressof(_M_obj); } + auto _M_ptr() noexcept { return std::__addressof(_M_obj); } public: using __allocator_type = __alloc_rebind<_Alloc, _Sp_counted_ptr_inplace>; @@ -962,7 +962,8 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION __shared_count(_Tp*& __p, _Sp_alloc_shared_tag<_Alloc> __a, _Args&&... __args) { - typedef _Sp_counted_ptr_inplace<_Tp, _Alloc, _Lp> _Sp_cp_type; + using _Tp2 = __remove_cv_t<_Tp>; + using _Sp_cp_type = _Sp_counted_ptr_inplace<_Tp2, _Alloc, _Lp>; typename _Sp_cp_type::__allocator_type __a2(__a._M_a); auto __guard = std::__allocate_guarded(__a2); _Sp_cp_type* __mem = __guard.get(); diff --git a/libstdc++-v3/include/bits/stl_construct.h b/libstdc++-v3/include/bits/stl_construct.h index 146ea14e99a..9d6111396e1 100644 --- a/libstdc++-v3/include/bits/stl_construct.h +++ b/libstdc++-v3/include/bits/stl_construct.h @@ -96,7 +96,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION construct_at(_Tp* __location, _Args&&... __args) noexcept(noexcept(::new((void*)0) _Tp(std::declval<_Args>()...))) { - void* __loc = const_cast*>(__location); + void* __loc = __location; // _GLIBCXX_RESOLVE_LIB_DEFECTS // 3436. std::construct_at should support arrays if constexpr (is_array_v<_Tp>) @@ -130,7 +130,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION return; } #endif - ::new((void*)__p) _Tp(std::forward<_Args>(__args)...); + ::new(static_cast(__p)) _Tp(std::forward<_Args>(__args)...); } #else template @@ -146,7 +146,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION template inline void _Construct_novalue(_T1* __p) - { ::new((void*)__p) _T1; } + { ::new(static_cast(__p)) _T1; } template _GLIBCXX20_CONSTEXPR void diff --git a/libstdc++-v3/include/std/expected b/libstdc++-v3/include/std/expected index 9e92339e406..d4a4bc17541 100644 --- a/libstdc++-v3/include/std/expected +++ b/libstdc++-v3/include/std/expected @@ -1261,7 +1261,7 @@ namespace __expected { } union { - _Tp _M_val; + remove_cv_t<_Tp> _M_val; _Er _M_unex; }; diff --git a/libstdc++-v3/testsuite/20_util/allocator/void.cc b/libstdc++-v3/testsuite/20_util/allocator/void.cc index e50bf993294..04cd2094776 100644 --- a/libstdc++-v3/testsuite/20_util/allocator/void.cc +++ b/libstdc++-v3/testsuite/20_util/allocator/void.cc @@ -88,23 +88,8 @@ 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(); }