From patchwork Wed Jun 19 16:37:44 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jonathan Wakely X-Patchwork-Id: 1949777 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=MJQsZ7xK; 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 4W48Sy40Psz20Wb for ; Thu, 20 Jun 2024 02:38:42 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id D2D013886C6E for ; Wed, 19 Jun 2024 16:38:40 +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.129.124]) by sourceware.org (Postfix) with ESMTPS id 9655E3885C1B for ; Wed, 19 Jun 2024 16:38:17 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 9655E3885C1B 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 9655E3885C1B Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=170.10.129.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1718815099; cv=none; b=eyC1hDekYPgzszpBkyexdAdqyO5zceC92GeFEdBULKe1H76MVIMavcC5y0vMFtg4xWhtMaOv34RvSVyM82Mc0umHflHMMFKOCwWQUst/9izvw+P288rWhWc+VeEIiT517pGpfO10kcEGUZ8Q5K/mHPVsyxcSj4MPMoWKiNM6ttg= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1718815099; c=relaxed/simple; bh=hFnBpBl3nwQgr4+AD1fdhBiMuZKbFYj0kicx8ncNeM8=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=RJYi2hrXjyQk9oGPemPcg3NFnQV+6Rh3ndlHAOLnN+NAzxUZ9B9PVprgT5IvUCPDphJntFcL3ICb+gqJOmmDmfm64JIfKd6KOo32i9QnY9pivDr1gICP0QZVu/Mgfpw6rN5hmNuKC3co3Aub/igwbjIRTvfdvLjkx0nXJGuXj0Y= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1718815097; 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; bh=jx+OLohNYzptjJ3qpwg5WBqvmQZWIBadbzk+w7mgAYs=; b=MJQsZ7xKMR229iyNlb/9fBE9tmdtPp5Ao+zDHh2yoCsJvdNTapJIELiz6sEDv+nRBQdEge LJFqV3oofpbncJLxQoyjNue7mmAPCIf6QFZ+y/jQ3FjyGf/U7TT1M2AtvdtdUOIY2RtHAE KiUbE5QG012DlwWINr5pFRw1U0XIaa0= Received: from mx-prod-mc-02.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-255-JEwZ6xlWNPKOwLH7a5kH0g-1; Wed, 19 Jun 2024 12:38:14 -0400 X-MC-Unique: JEwZ6xlWNPKOwLH7a5kH0g-1 Received: from mx-prod-int-02.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-02.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.15]) (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-02.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 2657419560AE; Wed, 19 Jun 2024 16:38:13 +0000 (UTC) Received: from localhost (unknown [10.42.28.182]) by mx-prod-int-02.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id 9E9D71955E80; Wed, 19 Jun 2024 16:38:11 +0000 (UTC) From: Jonathan Wakely To: gcc-patches@gcc.gnu.org, libstdc++@gcc.gnu.org Subject: [PATCH] libstdc++: Fix std::to_array for trivial-ish types [PR115522] Date: Wed, 19 Jun 2024 17:37:44 +0100 Message-ID: <20240619163810.1792381-1-jwakely@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.0 on 10.30.177.15 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-10.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, RCVD_IN_MSPIKE_H4, RCVD_IN_MSPIKE_WL, RCVD_IN_SBL_CSS, SPF_HELO_NONE, SPF_NONE, TXREP, T_SCC_BODY_TEXT_LINE, URI_HEX 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. Not pushed yet. backports will be needed too. -- >8 -- Due to PR c++/85723 the std::is_trivial trait is true for types with a deleted default constructor, so the use of std::is_trivial in std::to_array is not sufficient to ensure the type can be trivially default constructed then filled using memcpy. I also forgot that a type with a deleted assignment operator can still be trivial, so we also need to check that it's assignable because the is_constant_evaluated() path can't use memcpy. Replace the uses of std::is_trivial with std::is_trivially_copyable (needed for memcpy), std::is_trivially_default_constructible (needed so that the default construction is valid and does no work) and std::is_copy_assignable (needed for the constant evaluation case). libstdc++-v3/ChangeLog: PR libstdc++/115522 * include/std/array (to_array): Workaround the fact that std::is_trivial is not sufficient to check that a type is trivially default constructible and assignable. * testsuite/23_containers/array/creation/115522.cc: New test. --- libstdc++-v3/include/std/array | 8 +++-- .../23_containers/array/creation/115522.cc | 33 +++++++++++++++++++ 2 files changed, 39 insertions(+), 2 deletions(-) create mode 100644 libstdc++-v3/testsuite/23_containers/array/creation/115522.cc diff --git a/libstdc++-v3/include/std/array b/libstdc++-v3/include/std/array index 39695471e24..8710bf75924 100644 --- a/libstdc++-v3/include/std/array +++ b/libstdc++-v3/include/std/array @@ -431,7 +431,9 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION static_assert(is_constructible_v<_Tp, _Tp&>); if constexpr (is_constructible_v<_Tp, _Tp&>) { - if constexpr (is_trivial_v<_Tp>) + if constexpr (is_trivially_copyable_v<_Tp> + && is_trivially_default_constructible_v<_Tp> + && is_copy_assignable_v<_Tp>) { array, _Nm> __arr; if (!__is_constant_evaluated() && _Nm != 0) @@ -460,7 +462,9 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION static_assert(is_move_constructible_v<_Tp>); if constexpr (is_move_constructible_v<_Tp>) { - if constexpr (is_trivial_v<_Tp>) + if constexpr (is_trivially_copyable_v<_Tp> + && is_trivially_default_constructible_v<_Tp> + && is_copy_assignable_v<_Tp>) { array, _Nm> __arr; if (!__is_constant_evaluated() && _Nm != 0) diff --git a/libstdc++-v3/testsuite/23_containers/array/creation/115522.cc b/libstdc++-v3/testsuite/23_containers/array/creation/115522.cc new file mode 100644 index 00000000000..37073e002bd --- /dev/null +++ b/libstdc++-v3/testsuite/23_containers/array/creation/115522.cc @@ -0,0 +1,33 @@ +// { dg-do compile { target c++20 } } + +// PR libstdc++/115522 std::to_array no longer works for struct which is +// trivial but not default constructible + +#include + +void +test_deleted_ctor() +{ + struct S + { + S() = delete; + S(int) { } + }; + + S arr[1] = {{1}}; + auto arr1 = std::to_array(arr); + auto arr2 = std::to_array(std::move(arr)); +} + +void +test_deleted_assignment() +{ + struct S + { + void operator=(const S&) = delete; + }; + + S arr[1] = {}; + auto a1 = std::to_array(arr); + auto a2 = std::to_array(std::move(arr)); +}