From patchwork Tue Jul 30 13:32:34 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jonathan Wakely X-Patchwork-Id: 1966543 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=HaCbfqi8; 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 4WYGRG2bkLz1ybX for ; Tue, 30 Jul 2024 23:34:16 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id B759A3858C56 for ; Tue, 30 Jul 2024 13:34:14 +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 717353858C78 for ; Tue, 30 Jul 2024 13:33:47 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 717353858C78 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 717353858C78 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=1722346429; cv=none; b=aS0zWsOcEKa0fchhKhV+SKImpPaCvg2vN836vxgZZCI2wjUyeyhprR6KJIFefVQQV5wJGjovwfbwRTX2FC7ElMqNGn7/NXHvf+xcPzhYx+IFGhu7tWrGvDkhP0BTNPywRieDBXJc230sKjaFcdahJm8K2sZS7Zf982+Zj/wn8ac= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1722346429; c=relaxed/simple; bh=lawzfB/dadbMZHFwos1t2IO/gDDKrsNLqlIc9+FX6OM=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=jKN4BDPvuRihQqd/xzcKZX9mzs2hycUQPi4Sawyy+HNjQ74Bvs1Rsvxyf+nMcY9T4bXtlD5cewB5SMox+RL16bzs2L896kCJ3lXAuO+ZvmFYcwEGKM10+ARVGIaIgEcVslpS85TGKxhXZEDEC6eECnZSN3nnr1uowsKylMuz9Bc= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1722346427; 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=2MXPzobZT4hAOL7K73UJCpOmTJvlgM/HQRvmX0kOr4E=; b=HaCbfqi8dvUsA4V8lHm02aMRWgWxeuzpV44h0gkLMoreFl5hhBdrGVypTn9vtA+saPsvG+ rtNpPiGgRLPBkwXZ2xygAvmEDR6eqrR0rhefvjIRTmzNKm5GjQ47lx0+2pPb0C3RpKw8vC EwrQWFB9KQ4UFHpc481IdDw5S7vfc1o= 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-325-1jvwpRhBPjKa5MOkw7M1wg-1; Tue, 30 Jul 2024 09:33:43 -0400 X-MC-Unique: 1jvwpRhBPjKa5MOkw7M1wg-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 35F3D1955D4F; Tue, 30 Jul 2024 13:33:40 +0000 (UTC) Received: from localhost (unknown [10.42.28.14]) by mx-prod-int-02.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id 590CD1955D47; Tue, 30 Jul 2024 13:33:38 +0000 (UTC) From: Jonathan Wakely To: libstdc++@gcc.gnu.org, gcc-patches@gcc.gnu.org Subject: [PATCH] libstdc++: Implement LWG 3886 for std::optional and std::expected Date: Tue, 30 Jul 2024 14:32:34 +0100 Message-ID: <20240730133337.1025999-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.1 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 This LWG issue is about to become Tentatively Ready. Tested x86_64-linux. -- >8 -- This uses remove_cv_t for the default template argument used for deducing a type for a braced-init-list used with std::optional and std::expected. libstdc++-v3/ChangeLog: * include/std/expected (expected(U&&), operator=(U&&)) (value_or): Use remove_cv_t on default template argument, as per LWG 3886. * include/std/optional (optional(U&&), operator=(U&&)) (value_or): Likewise. * testsuite/20_util/expected/lwg3886.cc: New test. * testsuite/20_util/optional/cons/lwg3886.cc: New test. --- libstdc++-v3/include/std/expected | 8 +-- libstdc++-v3/include/std/optional | 12 ++-- .../testsuite/20_util/expected/lwg3886.cc | 58 +++++++++++++++++++ .../20_util/optional/cons/lwg3886.cc | 58 +++++++++++++++++++ 4 files changed, 126 insertions(+), 10 deletions(-) create mode 100644 libstdc++-v3/testsuite/20_util/expected/lwg3886.cc create mode 100644 libstdc++-v3/testsuite/20_util/optional/cons/lwg3886.cc diff --git a/libstdc++-v3/include/std/expected b/libstdc++-v3/include/std/expected index 515a1e6ab8f..b8217e577fa 100644 --- a/libstdc++-v3/include/std/expected +++ b/libstdc++-v3/include/std/expected @@ -468,7 +468,7 @@ namespace __expected std::move(__x)._M_unex); } - template + template> requires (!is_same_v, expected>) && (!is_same_v, in_place_t>) && is_constructible_v<_Tp, _Up> @@ -582,7 +582,7 @@ namespace __expected return *this; } - template + template> requires (!is_same_v>) && (!__expected::__is_unexpected>) && is_constructible_v<_Tp, _Up> && is_assignable_v<_Tp&, _Up> @@ -818,7 +818,7 @@ namespace __expected return std::move(_M_unex); } - template + template> constexpr _Tp value_or(_Up&& __v) const & noexcept(__and_v, @@ -832,7 +832,7 @@ namespace __expected return static_cast<_Tp>(std::forward<_Up>(__v)); } - template + template> constexpr _Tp value_or(_Up&& __v) && noexcept(__and_v, diff --git a/libstdc++-v3/include/std/optional b/libstdc++-v3/include/std/optional index 4694d594f98..2c4cc260f90 100644 --- a/libstdc++-v3/include/std/optional +++ b/libstdc++-v3/include/std/optional @@ -868,7 +868,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION // Converting constructors for engaged optionals. #ifdef _GLIBCXX_USE_CONSTRAINTS_FOR_OPTIONAL - template + template> requires (!is_same_v>) && (!is_same_v>) && is_constructible_v<_Tp, _Up> @@ -919,7 +919,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION : _Base(std::in_place, __il, std::forward<_Args>(__args)...) { } #else - template, _Requires<__not_self<_Up>, __not_tag<_Up>, is_constructible<_Tp, _Up>, is_convertible<_Up, _Tp>, @@ -929,7 +929,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION noexcept(is_nothrow_constructible_v<_Tp, _Up>) : _Base(std::in_place, std::forward<_Up>(__t)) { } - template, _Requires<__not_self<_Up>, __not_tag<_Up>, is_constructible<_Tp, _Up>, __not_>, @@ -1017,7 +1017,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION return *this; } - template + template> #ifdef _GLIBCXX_USE_CONSTRAINTS_FOR_OPTIONAL requires (!is_same_v>) && (!(is_scalar_v<_Tp> && is_same_v<_Tp, decay_t<_Up>>)) @@ -1242,7 +1242,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION __throw_bad_optional_access(); } - template + template> constexpr _Tp value_or(_Up&& __u) const& { @@ -1255,7 +1255,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION return static_cast<_Tp>(std::forward<_Up>(__u)); } - template + template> constexpr _Tp value_or(_Up&& __u) && { diff --git a/libstdc++-v3/testsuite/20_util/expected/lwg3886.cc b/libstdc++-v3/testsuite/20_util/expected/lwg3886.cc new file mode 100644 index 00000000000..cf1a2ce4421 --- /dev/null +++ b/libstdc++-v3/testsuite/20_util/expected/lwg3886.cc @@ -0,0 +1,58 @@ +// { dg-do compile { target c++23 } } + +// LWG 3886. Monad mo' problems + +#include + +void +test_constructor() +{ + struct MoveOnly { + MoveOnly(int, int) { } + MoveOnly(MoveOnly&&) { } + }; + + // The {0,0} should be deduced as MoveOnly not const MoveOnly + [[maybe_unused]] std::expected e({0,0}); +} + +struct Tracker { + bool moved = false; + constexpr Tracker(int, int) { } + constexpr Tracker(const Tracker&) { } + constexpr Tracker(Tracker&&) : moved(true) { } + + // The follow means that is_assignable is true: + template constexpr void operator=(T&&) const { } + // This stops a copy assignment from being declared implicitly: + void operator=(Tracker&) = delete; +}; + +void +test_assignment() +{ + constexpr bool moved = [] { + std::expected e(std::unexpect); + // The {0,0} should be deduced as Tracker not const Tracker: + e = {0,0}; + // So the contained value should have been move constructed not copied: + return e->moved; + }(); + static_assert( moved ); +} + +void +test_value_or() +{ + constexpr bool moved = [] { + const std::expected e(std::unexpect, 1); + return e.value_or({0,0}).moved; + }(); + static_assert( moved ); + + constexpr bool moved_rval = [] { + std::expected e(std::unexpect, 1); + return std::move(e).value_or({0,0}).moved; + }(); + static_assert( moved_rval ); +} diff --git a/libstdc++-v3/testsuite/20_util/optional/cons/lwg3886.cc b/libstdc++-v3/testsuite/20_util/optional/cons/lwg3886.cc new file mode 100644 index 00000000000..f6323ca7cbc --- /dev/null +++ b/libstdc++-v3/testsuite/20_util/optional/cons/lwg3886.cc @@ -0,0 +1,58 @@ +// { dg-do compile { target c++17 } } + +// LWG 3886. Monad mo' problems + +#include + +void +test_cons() +{ + struct MoveOnly { + MoveOnly(int, int) { } + MoveOnly(MoveOnly&&) { } + }; + + // The {0,0} should be deduced as MoveOnly not const MoveOnly + [[maybe_unused]] std::optional o({0,0}); +} + +struct Tracker { + bool moved = false; + constexpr Tracker(int, int) { } + constexpr Tracker(const Tracker&) { } + constexpr Tracker(Tracker&&) : moved(true) { } + + // The follow means that is_assignable is true: + template constexpr void operator=(T&&) const { } +}; + +#if __cpp_lib_optional >= 202106L // for constexpr assignment +void +test_assignment() +{ + constexpr bool moved = [] { + std::optional o; + // The {0,0} should be deduced as Tracker not const Tracker: + o = {0,0}; + // So the contained value should have been move constructed not copied: + return o->moved; + }(); + static_assert( moved ); +} +#endif + +void +test_value_or() +{ + constexpr bool moved = [] { + std::optional o; + return o.value_or({0,0}).moved; + }(); + static_assert( moved ); + + constexpr bool moved_rval = [] { + std::optional o; + return std::move(o).value_or({0,0}).moved; + }(); + static_assert( moved_rval ); +}