From patchwork Wed Oct 2 19:37:50 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jonathan Wakely X-Patchwork-Id: 1992163 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=dF1OnVWI; 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 4XJm5K41Wnz1xtY for ; Thu, 3 Oct 2024 06:05:41 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id D63BF3861039 for ; Wed, 2 Oct 2024 20:05:38 +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 ESMTP id ADE473858D34 for ; Wed, 2 Oct 2024 20:05:09 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org ADE473858D34 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 ADE473858D34 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=1727899513; cv=none; b=ApnEiTnIBY54i8N5F9LFrlxztWTt7FXgvUes4wFz4GkJjbM+9RzLVpGqqcbayk6KVHwd2x2xaBf9/Ayew7cX62L36QmUaajhB0y6TY6upeQQlL5VrNSbv7IRBKRPggX5bkSOWy7WkiZvI/7eA7Yw9lgrnUpMFp2lU5U0sneg/tI= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1727899513; c=relaxed/simple; bh=C2E9eV6F59Y3e8VifA0uV5U60vvY/tivXsoFtGa3gX8=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=bo6a10+9da8x0cCtJIbQ6SlkjJEmIkCkoP1/OAQg2uTFLEQVBPjFPDYbgolVNR8K+yPzH4uVztTdgmWC+THAZ8o8eAfrL/mghE8HE1/z81R6eNvOzLdoq+7LR499VYkTMmhN2S3UFMm6CcBlsz4bmozLiGDt/QKxYxgduMg6KpE= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1727899509; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=1aSPzywGQTirD6VgrtlrFeGEhaN4YgC+6TMcsTEdQ2E=; b=dF1OnVWI4DNW6AqsbFX54RCoazRn1h4faQ38W96BXyNwLbhI6wDdGGKaPRxiASapA49Zdu FWrKWzOj5nmSDt4EypXL4decjFLmEeimCnlqQpnishCVDcwy+QvwzIK179dt+sslsJaZUA bEh072nwVcdyi3zeoXT6+S6jd003q2w= Received: from mx-prod-mc-01.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-657-FyIhMIAfMHSndWM1Aqw2ww-1; Wed, 02 Oct 2024 16:05:08 -0400 X-MC-Unique: FyIhMIAfMHSndWM1Aqw2ww-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-01.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 50B10195608F; Wed, 2 Oct 2024 20:05:07 +0000 (UTC) Received: from localhost (unknown [10.42.28.136]) by mx-prod-int-02.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id 97D781956088; Wed, 2 Oct 2024 20:05:05 +0000 (UTC) From: Jonathan Wakely To: libstdc++@gcc.gnu.org, gcc-patches@gcc.gnu.org Cc: Jason Merrill Subject: [PATCH 1/2] libstdc++: Make std::construct_at support arrays (LWG 3436) Date: Wed, 2 Oct 2024 20:37:50 +0100 Message-ID: <20241002200503.3359969-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=-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=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.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 Is the g++ test change OK? Tested x86_64-linux. -- >8 -- The issue was approved at the recent St. Louis meeting, requiring support for bounded arrays, but only without arguments to initialize the array elements. libstdc++-v3/ChangeLog: * include/bits/stl_construct.h (construct_at): Support array types (LWG 3436). * testsuite/20_util/specialized_algorithms/construct_at/array.cc: New test. * testsuite/20_util/specialized_algorithms/construct_at/array_neg.cc: New test. gcc/testsuite/ChangeLog: * g++.dg/cpp0x/initlist-opt1.C: Adjust for different diagnostics from std::construct_at by adding -fconcepts-diagnostics-depth=2. --- gcc/testsuite/g++.dg/cpp0x/initlist-opt1.C | 1 + libstdc++-v3/include/bits/stl_construct.h | 20 +++++++-- .../construct_at/array.cc | 41 +++++++++++++++++++ .../construct_at/array_neg.cc | 19 +++++++++ 4 files changed, 78 insertions(+), 3 deletions(-) create mode 100644 libstdc++-v3/testsuite/20_util/specialized_algorithms/construct_at/array.cc create mode 100644 libstdc++-v3/testsuite/20_util/specialized_algorithms/construct_at/array_neg.cc diff --git a/gcc/testsuite/g++.dg/cpp0x/initlist-opt1.C b/gcc/testsuite/g++.dg/cpp0x/initlist-opt1.C index 391b7c47d50..38c4f00cec0 100644 --- a/gcc/testsuite/g++.dg/cpp0x/initlist-opt1.C +++ b/gcc/testsuite/g++.dg/cpp0x/initlist-opt1.C @@ -1,5 +1,6 @@ // PR c++/110102 // { dg-do compile { target c++11 } } +// { dg-additional-options "-fconcepts-diagnostics-depth=2" { target c++20 } } // { dg-skip-if "requires hosted libstdc++ for list" { ! hostedlib } } // { dg-error "deleted|construct_at" "" { target *-*-* } 0 } diff --git a/libstdc++-v3/include/bits/stl_construct.h b/libstdc++-v3/include/bits/stl_construct.h index dc08fb7ea33..146ea14e99a 100644 --- a/libstdc++-v3/include/bits/stl_construct.h +++ b/libstdc++-v3/include/bits/stl_construct.h @@ -90,11 +90,25 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION #if __cpp_constexpr_dynamic_alloc // >= C++20 template - constexpr auto + requires (!is_unbounded_array_v<_Tp>) + && requires { ::new((void*)0) _Tp(std::declval<_Args>()...); } + constexpr _Tp* construct_at(_Tp* __location, _Args&&... __args) noexcept(noexcept(::new((void*)0) _Tp(std::declval<_Args>()...))) - -> decltype(::new((void*)0) _Tp(std::declval<_Args>()...)) - { return ::new((void*)__location) _Tp(std::forward<_Args>(__args)...); } + { + void* __loc = const_cast*>(__location); + // _GLIBCXX_RESOLVE_LIB_DEFECTS + // 3436. std::construct_at should support arrays + if constexpr (is_array_v<_Tp>) + { + static_assert(sizeof...(_Args) == 0, "std::construct_at for array " + "types must not use any arguments to initialize the " + "array"); + return ::new(__loc) _Tp[1](); + } + else + return ::new(__loc) _Tp(std::forward<_Args>(__args)...); + } #endif // C++20 #endif// C++17 diff --git a/libstdc++-v3/testsuite/20_util/specialized_algorithms/construct_at/array.cc b/libstdc++-v3/testsuite/20_util/specialized_algorithms/construct_at/array.cc new file mode 100644 index 00000000000..c3683462835 --- /dev/null +++ b/libstdc++-v3/testsuite/20_util/specialized_algorithms/construct_at/array.cc @@ -0,0 +1,41 @@ +// { dg-do compile { target c++20 } } + +// LWG 3436. std::construct_at should support arrays + +#include +#include + +constexpr void +test_array() +{ + int arr[1] { 99 }; + std::construct_at(&arr); + VERIFY( arr[0] == 0 ); + + union U { + long long x; + int arr[4]; + } u; + u.x = -1; + + auto p = std::construct_at(&u.arr); + VERIFY( (*p)[0] == 0 ); + VERIFY( (*p)[1] == 0 ); + VERIFY( (*p)[2] == 0 ); + VERIFY( (*p)[3] == 0 ); + + struct NonTrivial { + constexpr NonTrivial() : i(99) { } + int i; + }; + + union U2 { + char c = 'a'; + NonTrivial arr[2]; + } u2; + + auto p2 = std::construct_at(&u2.arr); + VERIFY( (*p2)[0].i == 99 ); +} + +static_assert( [] { test_array(); return true; }() ); diff --git a/libstdc++-v3/testsuite/20_util/specialized_algorithms/construct_at/array_neg.cc b/libstdc++-v3/testsuite/20_util/specialized_algorithms/construct_at/array_neg.cc new file mode 100644 index 00000000000..deb86930d1a --- /dev/null +++ b/libstdc++-v3/testsuite/20_util/specialized_algorithms/construct_at/array_neg.cc @@ -0,0 +1,19 @@ +// { dg-do compile { target c++20 } } + +// LWG 3436. std::construct_at should support arrays + +#include + +void +test_array_args() +{ + int arr[2]; + std::construct_at(&arr, 1, 2); // { dg-error "here" } + // { dg-error "must not use any arguments" "" { target *-*-* } 0 } +} + +void +test_unbounded_array(int (*p)[]) +{ + std::construct_at(p); // { dg-error "no matching function" } +}