From patchwork Mon Oct 14 15:50:13 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jonathan Wakely X-Patchwork-Id: 1997050 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=Hsbczymb; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=8.43.85.97; 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 [8.43.85.97]) (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 4XS1tN6mnZz1xvm for ; Tue, 15 Oct 2024 02:51:24 +1100 (AEDT) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 2B415385AE74 for ; Mon, 14 Oct 2024 15:51:23 +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 15CCA3856DFA for ; Mon, 14 Oct 2024 15:50:40 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 15CCA3856DFA 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 15CCA3856DFA 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=1728921049; cv=none; b=vE4VFmSL5dCN4HO4T0aHH85VE6Hrcgps+N40IKCCLUAw2F4bC/Ij32TaJaukegAwJ43KHJA9oWW/BbRQb1PpM/1PEB9dDFpcXWxfx5PRFBf1CNmpOGFSvkOgmQEQKg4HgXR/WUr4yZanV+SmOhNmiCC+Pu1AmZyf1xNdQa4+uFc= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1728921049; c=relaxed/simple; bh=VgA4u5tPuzbfz8ekjb/yg1DIfKzbFbPSBLpfBopESJ4=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=g+x9ym7OqIYHUHIpJLdzTXcaVte86s/eBft3R//QNjSjRwATFBIUNzzojfyFfImqT6Bjq+t82J618q1I/8dfi+xzqsT1uuFKnE4GR2lQOZMo+QGrMTzhqlXTFaLjYeR3kYDPpB+lwPzlLIezxAdvOSF911v4HKgZuZQk2mseTZY= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1728921039; 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=VTb7lOpm0WiWlpgdapsBkj5LNL89uVIfpcrujdXKpUg=; b=HsbczymbDOiMGBDTOz9F7Rp8Xcn+Va5zLTf6Iu4amzqE7Td148FQ393HyWilKG/tgbmUQX 06P8uXuojyYb88H2iDvBMT/RwwVfq80gSL6Oy3pMhhb8HgT1cd1aGSOfAkP/edgodIl0Lg mWSntsi87Vrip4hw4gNpaRe9u2x1zH8= Received: from mx-prod-mc-03.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-84-Po6YeJESMVi7Vrdxchf1Bw-1; Mon, 14 Oct 2024 11:50:36 -0400 X-MC-Unique: Po6YeJESMVi7Vrdxchf1Bw-1 Received: from mx-prod-int-04.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-04.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.40]) (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-03.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id B941B1955F54; Mon, 14 Oct 2024 15:50:34 +0000 (UTC) Received: from localhost (unknown [10.42.28.16]) by mx-prod-int-04.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id AAA8C19560AE; Mon, 14 Oct 2024 15:50:33 +0000 (UTC) From: Jonathan Wakely To: libstdc++@gcc.gnu.org, gcc-patches@gcc.gnu.org Subject: [committed] libstdc++: Constrain std::expected comparisons (P3379R0) Date: Mon, 14 Oct 2024 16:50:13 +0100 Message-ID: <20241014155031.1901606-1-jwakely@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.0 on 10.30.177.40 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-10.4 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, RCVD_IN_SBL_CSS, 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 Tested x86_64-linux. Pushed to trunk. -- >8 -- This proposal of mine has been approved by LEWG and forwarded to LWG. I expect it to be voted into the draft without significant changes. libstdc++-v3/ChangeLog: * include/bits/version.def (constrained_equality): Bump value. * include/bits/version.h: Regenerate. * include/std/expected (operator==): Add constraints and noexcept specifiers. * testsuite/20_util/optional/relops/constrained.cc: Adjust check for feature test macro. * testsuite/20_util/pair/comparison_operators/constrained.cc: Likewise. * testsuite/20_util/tuple/comparison_operators/constrained.cc: Likewise. * testsuite/20_util/variant/relops/constrained.cc: Likewise. * testsuite/20_util/expected/equality_constrained.cc: New test. --- libstdc++-v3/include/bits/version.def | 2 +- libstdc++-v3/include/bits/version.h | 4 +- libstdc++-v3/include/std/expected | 31 ++- .../20_util/expected/equality_constrained.cc | 181 ++++++++++++++++++ .../20_util/optional/relops/constrained.cc | 2 +- .../pair/comparison_operators/constrained.cc | 2 +- .../tuple/comparison_operators/constrained.cc | 2 +- .../20_util/variant/relops/constrained.cc | 2 +- 8 files changed, 213 insertions(+), 13 deletions(-) create mode 100644 libstdc++-v3/testsuite/20_util/expected/equality_constrained.cc diff --git a/libstdc++-v3/include/bits/version.def b/libstdc++-v3/include/bits/version.def index 477651f358a..4fa820a5a4a 100644 --- a/libstdc++-v3/include/bits/version.def +++ b/libstdc++-v3/include/bits/version.def @@ -1256,7 +1256,7 @@ ftms = { ftms = { name = constrained_equality; values = { - v = 202403; + v = 202411; // FIXME: 202403 for P2944R3, ??? for P3379R0 cxxmin = 20; extra_cond = "__glibcxx_three_way_comparison"; }; diff --git a/libstdc++-v3/include/bits/version.h b/libstdc++-v3/include/bits/version.h index 342ee6b4c7a..fdbee8161d9 100644 --- a/libstdc++-v3/include/bits/version.h +++ b/libstdc++-v3/include/bits/version.h @@ -1392,9 +1392,9 @@ #if !defined(__cpp_lib_constrained_equality) # if (__cplusplus >= 202002L) && (__glibcxx_three_way_comparison) -# define __glibcxx_constrained_equality 202403L +# define __glibcxx_constrained_equality 202411L # if defined(__glibcxx_want_all) || defined(__glibcxx_want_constrained_equality) -# define __cpp_lib_constrained_equality 202403L +# define __cpp_lib_constrained_equality 202411L # endif # endif #endif /* !defined(__cpp_lib_constrained_equality) && defined(__glibcxx_want_constrained_equality) */ diff --git a/libstdc++-v3/include/std/expected b/libstdc++-v3/include/std/expected index d4a4bc17541..7de2aeffc70 100644 --- a/libstdc++-v3/include/std/expected +++ b/libstdc++-v3/include/std/expected @@ -35,6 +35,7 @@ #define __glibcxx_want_expected #define __glibcxx_want_freestanding_expected +#define __glibcxx_want_constrained_equality #include #ifdef __cpp_lib_expected // C++ >= 23 && __cpp_concepts >= 202002L @@ -1152,10 +1153,15 @@ namespace __expected template requires (!is_void_v<_Up>) + && requires (const _Tp& __t, const _Up& __u, + const _Er& __e, const _Er2& __e2) { + { __t == __u } -> convertible_to; + { __e == __e2 } -> convertible_to; + } friend constexpr bool operator==(const expected& __x, const expected<_Up, _Er2>& __y) - // FIXME: noexcept(noexcept(bool(*__x == *__y)) - // && noexcept(bool(__x.error() == __y.error()))) + noexcept(noexcept(bool(*__x == *__y)) + && noexcept(bool(__x.error() == __y.error()))) { if (__x.has_value()) return __y.has_value() && bool(*__x == *__y); @@ -1164,15 +1170,22 @@ namespace __expected } template + requires (!__expected::__is_expected<_Up>) + && requires (const _Tp& __t, const _Up& __u) { + { __t == __u } -> convertible_to; + } friend constexpr bool operator==(const expected& __x, const _Up& __v) - // FIXME: noexcept(noexcept(bool(*__x == __v))) + noexcept(noexcept(bool(*__x == __v))) { return __x.has_value() && bool(*__x == __v); } template + requires requires (const _Er& __e, const _Er2& __e2) { + { __e == __e2 } -> convertible_to; + } friend constexpr bool operator==(const expected& __x, const unexpected<_Er2>& __e) - // FIXME: noexcept(noexcept(bool(__x.error() == __e.error()))) + noexcept(noexcept(bool(__x.error() == __e.error()))) { return !__x.has_value() && bool(__x.error() == __e.error()); } friend constexpr void @@ -1819,9 +1832,12 @@ namespace __expected template requires is_void_v<_Up> + && requires (const _Er& __e, const _Er2& __e2) { + { __e == __e2 } -> convertible_to; + } friend constexpr bool operator==(const expected& __x, const expected<_Up, _Er2>& __y) - // FIXME: noexcept(noexcept(bool(__x.error() == __y.error()))) + noexcept(noexcept(bool(__x.error() == __y.error()))) { if (__x.has_value()) return __y.has_value(); @@ -1830,9 +1846,12 @@ namespace __expected } template + requires requires (const _Er& __e, const _Er2& __e2) { + { __e == __e2 } -> convertible_to; + } friend constexpr bool operator==(const expected& __x, const unexpected<_Er2>& __e) - // FIXME: noexcept(noexcept(bool(__x.error() == __e.error()))) + noexcept(noexcept(bool(__x.error() == __e.error()))) { return !__x.has_value() && bool(__x.error() == __e.error()); } friend constexpr void diff --git a/libstdc++-v3/testsuite/20_util/expected/equality_constrained.cc b/libstdc++-v3/testsuite/20_util/expected/equality_constrained.cc new file mode 100644 index 00000000000..7f6cefae748 --- /dev/null +++ b/libstdc++-v3/testsuite/20_util/expected/equality_constrained.cc @@ -0,0 +1,181 @@ +// { dg-do compile { target c++23 } } + +#include + +#ifndef __cpp_lib_constrained_equality +# error "Feature-test macro for constrained_equality missing in " +#elif __cpp_lib_constrained_equality < 202411L // TODO: use final value +# error "Feature-test macro for constrained_equality has wrong value" +#endif + +template +concept eq_comparable += requires (const std::expected& t, const std::expected& u) { + t == u; +}; + +static_assert( eq_comparable ); +static_assert( eq_comparable ); +static_assert( eq_comparable ); +static_assert( eq_comparable ); +static_assert( eq_comparable ); + +struct A { }; +static_assert( ! eq_comparable ); +static_assert( ! eq_comparable ); +static_assert( ! eq_comparable ); +static_assert( ! eq_comparable ); +static_assert( ! eq_comparable ); +static_assert( ! eq_comparable ); + +struct B { }; +void operator==(B, B); +static_assert( ! eq_comparable ); +static_assert( ! eq_comparable ); +static_assert( ! eq_comparable ); +static_assert( ! eq_comparable ); +static_assert( ! eq_comparable ); +static_assert( ! eq_comparable ); + +struct C { }; +bool operator==(C, C); +static_assert( eq_comparable ); +static_assert( eq_comparable ); +static_assert( eq_comparable ); +static_assert( eq_comparable ); +static_assert( eq_comparable ); +static_assert( ! eq_comparable ); +static_assert( ! eq_comparable ); +static_assert( eq_comparable ); + +struct D { }; +int operator==(D, D); +bool operator!=(D, D) = delete; +static_assert( eq_comparable ); +static_assert( eq_comparable ); +static_assert( eq_comparable ); +static_assert( eq_comparable ); +static_assert( ! eq_comparable ); + +struct E { }; +bool operator==(/* not-const */ E&, const E&); +static_assert( ! eq_comparable ); +static_assert( ! eq_comparable ); +static_assert( ! eq_comparable ); +static_assert( ! eq_comparable ); + +bool operator==(A, B); +static_assert( eq_comparable ); +static_assert( eq_comparable ); +static_assert( eq_comparable ); + +int operator==(C, D); +static_assert( eq_comparable ); +static_assert( eq_comparable ); +static_assert( eq_comparable ); +static_assert( eq_comparable ); +static_assert( eq_comparable ); +static_assert( eq_comparable ); + +template +concept eq_comparable_val += requires (const std::expected& t, const U& u) { + t == u; +}; + +static_assert( eq_comparable_val ); +static_assert( eq_comparable_val ); +static_assert( eq_comparable_val ); +static_assert( eq_comparable_val ); +static_assert( ! eq_comparable_val ); + +static_assert( ! eq_comparable_val ); +static_assert( ! eq_comparable_val ); +static_assert( ! eq_comparable_val ); +static_assert( eq_comparable_val ); +static_assert( ! eq_comparable_val ); + +static_assert( ! eq_comparable_val ); +static_assert( ! eq_comparable_val ); +static_assert( ! eq_comparable_val ); +static_assert( eq_comparable_val ); +static_assert( ! eq_comparable_val ); + +static_assert( eq_comparable_val ); +static_assert( eq_comparable_val ); +static_assert( eq_comparable_val ); +static_assert( eq_comparable_val ); +static_assert( eq_comparable_val ); +static_assert( eq_comparable_val ); +static_assert( ! eq_comparable_val ); + +static_assert( eq_comparable_val ); +static_assert( eq_comparable_val ); +static_assert( ! eq_comparable_val ); +static_assert( eq_comparable_val ); +static_assert( eq_comparable_val ); + +static_assert( ! eq_comparable_val ); +static_assert( ! eq_comparable_val ); +static_assert( ! eq_comparable_val ); +static_assert( eq_comparable_val ); + +static_assert( eq_comparable_val ); +static_assert( eq_comparable_val ); + +static_assert( eq_comparable_val ); +static_assert( ! eq_comparable_val ); + +template +concept eq_comparable_unex += requires (const std::expected& t, const std::unexpected& u) { + t == u; +}; + +static_assert( eq_comparable_unex ); +static_assert( eq_comparable_unex ); +static_assert( eq_comparable_unex ); +static_assert( eq_comparable_unex ); +static_assert( eq_comparable_unex ); +static_assert( eq_comparable_unex ); + +static_assert( ! eq_comparable_unex ); +static_assert( ! eq_comparable_unex ); +static_assert( ! eq_comparable_unex ); +static_assert( ! eq_comparable_unex ); +static_assert( ! eq_comparable_unex ); +static_assert( ! eq_comparable_unex ); + +static_assert( ! eq_comparable_unex ); +static_assert( ! eq_comparable_unex ); +static_assert( ! eq_comparable_unex ); +static_assert( ! eq_comparable_unex ); +static_assert( ! eq_comparable_unex ); +static_assert( ! eq_comparable_unex ); + +static_assert( eq_comparable_unex ); +static_assert( eq_comparable_unex ); + +static_assert( eq_comparable_unex ); +static_assert( ! eq_comparable_unex ); +static_assert( ! eq_comparable_unex ); +static_assert( eq_comparable_unex ); +static_assert( ! eq_comparable_unex ); +static_assert( ! eq_comparable_unex ); + +static_assert( ! eq_comparable_unex ); +static_assert( ! eq_comparable_unex ); +static_assert( ! eq_comparable_unex ); +static_assert( ! eq_comparable_unex ); +static_assert( ! eq_comparable_unex ); +static_assert( ! eq_comparable_unex ); + +static_assert( eq_comparable_unex ); +static_assert( eq_comparable_unex ); +static_assert( eq_comparable_unex ); +static_assert( eq_comparable_unex ); + +static_assert( eq_comparable_unex ); +static_assert( ! eq_comparable_unex ); +static_assert( eq_comparable_unex ); +static_assert( ! eq_comparable_unex ); diff --git a/libstdc++-v3/testsuite/20_util/optional/relops/constrained.cc b/libstdc++-v3/testsuite/20_util/optional/relops/constrained.cc index 0e325618008..3e393257928 100644 --- a/libstdc++-v3/testsuite/20_util/optional/relops/constrained.cc +++ b/libstdc++-v3/testsuite/20_util/optional/relops/constrained.cc @@ -4,7 +4,7 @@ #ifndef __cpp_lib_constrained_equality # error "Feature-test macro for constrained_equality missing" -#elif __cpp_lib_constrained_equality != 202403 +#elif __cpp_lib_constrained_equality < 202403L # error "Feature-test macro for constrained_equality has wrong value" #endif diff --git a/libstdc++-v3/testsuite/20_util/pair/comparison_operators/constrained.cc b/libstdc++-v3/testsuite/20_util/pair/comparison_operators/constrained.cc index a35dbd265a7..dabe35ff746 100644 --- a/libstdc++-v3/testsuite/20_util/pair/comparison_operators/constrained.cc +++ b/libstdc++-v3/testsuite/20_util/pair/comparison_operators/constrained.cc @@ -4,7 +4,7 @@ #ifndef __cpp_lib_constrained_equality # error "Feature-test macro for constrained_equality missing" -#elif __cpp_lib_constrained_equality != 202403 +#elif __cpp_lib_constrained_equality < 202403L # error "Feature-test macro for constrained_equality has wrong value" #endif diff --git a/libstdc++-v3/testsuite/20_util/tuple/comparison_operators/constrained.cc b/libstdc++-v3/testsuite/20_util/tuple/comparison_operators/constrained.cc index 47035ab18ba..994d4a491a1 100644 --- a/libstdc++-v3/testsuite/20_util/tuple/comparison_operators/constrained.cc +++ b/libstdc++-v3/testsuite/20_util/tuple/comparison_operators/constrained.cc @@ -4,7 +4,7 @@ #ifndef __cpp_lib_constrained_equality # error "Feature-test macro for constrained_equality missing" -#elif __cpp_lib_constrained_equality != 202403 +#elif __cpp_lib_constrained_equality < 202403L # error "Feature-test macro for constrained_equality has wrong value" #endif diff --git a/libstdc++-v3/testsuite/20_util/variant/relops/constrained.cc b/libstdc++-v3/testsuite/20_util/variant/relops/constrained.cc index 95e8f754d1e..fbe10e0ba03 100644 --- a/libstdc++-v3/testsuite/20_util/variant/relops/constrained.cc +++ b/libstdc++-v3/testsuite/20_util/variant/relops/constrained.cc @@ -4,7 +4,7 @@ #ifndef __cpp_lib_constrained_equality # error "Feature-test macro for constrained_equality missing" -#elif __cpp_lib_constrained_equality != 202403 +#elif __cpp_lib_constrained_equality < 202403L # error "Feature-test macro for constrained_equality has wrong value" #endif