From patchwork Wed May 17 19:05:52 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jonathan Wakely X-Patchwork-Id: 95495 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b0ea:0:b0:3b6:4342:cba0 with SMTP id b10csp1359902vqo; Wed, 17 May 2023 12:07:40 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ4zkR/iOdQZGZForkLf0S+Rl7Ja8t4+AiPBWGHCp1Gya78EdVjPtux8US7kVoYuZdwvMWvD X-Received: by 2002:a17:907:1c85:b0:92b:6b6d:2daf with SMTP id nb5-20020a1709071c8500b0092b6b6d2dafmr38936732ejc.77.1684350460036; Wed, 17 May 2023 12:07:40 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1684350460; cv=none; d=google.com; s=arc-20160816; b=BToP09aBZ35I90q/cpwDQOdx09rOHwN3HkVd5Sl53J6y5pYQMFu5VZNGNgWctLkJ34 HmVgIxCPbQOlqX4H+1unMZPnI4bX9pYm7p8uP7kiyWyG9yjw1P44IF1yENgI29HKYkIc WMd87X9gDjqouRpTcgZKjK2jv2eDBuMNsCYTV2qnXFMDxPomKsiygoSulB16fU260Di5 Q3iK3CQm3/K/i4JX/2Szbt2qEcPtFx7WEisvi58NKoabPQ9vuTOJp1mzGveK4DHqNDbT dMLflHnthCzSwTIp/1D51St9HJk8OUarxg3brDfn+1f5zT74oCxFbK+PvVkw1Pyot0Fx mIPA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:reply-to:from:list-subscribe:list-help:list-post :list-archive:list-unsubscribe:list-id:precedence :content-transfer-encoding:mime-version:message-id:date:subject:to :dmarc-filter:delivered-to:dkim-signature:dkim-filter; bh=HzFYONIGp/c0Ttwlj58EE/TpIMR8CqLpgaU3ZNTbO84=; b=KdZALeu4qE0JGHafDftvXE/c/nve6HniXw6cb1ZxiprhNk4HwjiVGSyswRxLbSl7t+ nA+x+ryoX56ZAF87tdKRE/Y6HPVXWFiRe7SBf0INgfS556v6brrFsuESGIIVntkOpafB nfAGINAdudws1JxQ/QedTFUNoUBSSi5TM8fUyIxHZJ2BGj9718Zon3e1f/TNEj+mT8JC XmmR3FYWBPcEg1KBdr8a2Hou88LPuA+YYHkCXXMN/IZudb0ge//Kxmp5tYvVjpQQ/iEz 7Q0pXBXBq7ydYMSzsu0N+F22oJF5ERFojkCqwJd+mxd+QxlMbs5pgEZEGLSypleGDhKs FYVQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=kPrBVakf; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnu.org Received: from sourceware.org (server2.sourceware.org. [2620:52:3:1:0:246e:9693:128c]) by mx.google.com with ESMTPS id lj13-20020a170906f9cd00b009662bcf493asi17078936ejb.795.2023.05.17.12.07.39 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 17 May 2023 12:07:40 -0700 (PDT) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) client-ip=2620:52:3:1:0:246e:9693:128c; Authentication-Results: mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=kPrBVakf; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnu.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 44F1B383803A for ; Wed, 17 May 2023 19:06:51 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 44F1B383803A DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1684350411; bh=HzFYONIGp/c0Ttwlj58EE/TpIMR8CqLpgaU3ZNTbO84=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=kPrBVakfgoygvkcqxVZyD9sxxkZvkGWXE1K1QxWgnLtkjEMx/CAEKh/MGnZkwYphD mCna22RVe9OFvFjWwZUv/bicwbD87rXy6SBLs8dOGubwWcHHc+JMs4d6Bq5VXu64gC crO6Emy73pbmmLqF85F5yrjC2i1BppDCUur/0uvg= 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 1E9C33858D1E for ; Wed, 17 May 2023 19:05:56 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 1E9C33858D1E Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-299--7S0EAlLOp6lxrKCde26Ew-1; Wed, 17 May 2023 15:05:54 -0400 X-MC-Unique: -7S0EAlLOp6lxrKCde26Ew-1 Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.rdu2.redhat.com [10.11.54.2]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id AADAC805F58; Wed, 17 May 2023 19:05:53 +0000 (UTC) Received: from localhost (unknown [10.42.28.178]) by smtp.corp.redhat.com (Postfix) with ESMTP id 71C5C40C6EC4; Wed, 17 May 2023 19:05:53 +0000 (UTC) To: libstdc++@gcc.gnu.org, gcc-patches@gcc.gnu.org Subject: [committed] libstdc++: Implement LWG 3877 for std::expected monadic ops Date: Wed, 17 May 2023 20:05:52 +0100 Message-Id: <20230517190552.2023422-1-jwakely@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.2 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-11.8 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, SPF_HELO_NONE, SPF_NONE, TXREP, T_SCC_BODY_TEXT_LINE 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.29 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Jonathan Wakely via Gcc-patches From: Jonathan Wakely Reply-To: Jonathan Wakely Errors-To: gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org Sender: "Gcc-patches" X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1766169467951143504?= X-GMAIL-MSGID: =?utf-8?q?1766169467951143504?= Tested powerpc64le-linux. Pushed to trunk. I'll probably backport this to gcc-13 too. There are no tests for the changes to and_then and transform, because LWG 3843 makes that hard to test. Both and_then and transform call value(), which requires a copy constructible error_type. So we can test that certain types can never be used as the error-type, but it's not very useful to test. -- >8 -- This was approved in Issaquah 2023. As well as fixing the value categories, this fixes the fact that we were incorrectly testing E instead of T in the or_else constraints. libstdc++-v3/ChangeLog: * include/std/expected (expected::and_then, expected::or_else) (expected::transform, expected::transform_error): Fix exception specifications as per LWG 3877. (expected::and_then, expected::transform): Likewise. * testsuite/20_util/expected/lwg3877.cc: New test. --- libstdc++-v3/include/std/expected | 48 +++++++------- .../testsuite/20_util/expected/lwg3877.cc | 64 +++++++++++++++++++ 2 files changed, 88 insertions(+), 24 deletions(-) create mode 100644 libstdc++-v3/testsuite/20_util/expected/lwg3877.cc diff --git a/libstdc++-v3/include/std/expected b/libstdc++-v3/include/std/expected index c6d26b0d224..5ea0d6a7cb9 100644 --- a/libstdc++-v3/include/std/expected +++ b/libstdc++-v3/include/std/expected @@ -840,7 +840,7 @@ namespace __expected // monadic operations - template requires is_copy_constructible_v<_Er> + template requires is_constructible_v<_Er, _Er&> constexpr auto and_then(_Fn&& __f) & { @@ -854,7 +854,7 @@ namespace __expected return _Up(unexpect, error()); } - template requires is_copy_constructible_v<_Er> + template requires is_constructible_v<_Er, const _Er&> constexpr auto and_then(_Fn&& __f) const & { @@ -868,7 +868,7 @@ namespace __expected return _Up(unexpect, error()); } - template requires is_move_constructible_v<_Er> + template requires is_constructible_v<_Er, _Er> constexpr auto and_then(_Fn&& __f) && { @@ -883,7 +883,7 @@ namespace __expected } - template requires is_move_constructible_v<_Er> + template requires is_constructible_v<_Er, const _Er> constexpr auto and_then(_Fn&& __f) const && { @@ -897,7 +897,7 @@ namespace __expected return _Up(unexpect, std::move(error())); } - template requires is_copy_constructible_v<_Er> + template requires is_constructible_v<_Tp, _Tp&> constexpr auto or_else(_Fn&& __f) & { @@ -911,7 +911,7 @@ namespace __expected return std::__invoke(std::forward<_Fn>(__f), error()); } - template requires is_copy_constructible_v<_Er> + template requires is_constructible_v<_Tp, const _Tp&> constexpr auto or_else(_Fn&& __f) const & { @@ -926,7 +926,7 @@ namespace __expected } - template requires is_move_constructible_v<_Er> + template requires is_constructible_v<_Tp, _Tp> constexpr auto or_else(_Fn&& __f) && { @@ -940,7 +940,7 @@ namespace __expected return std::__invoke(std::forward<_Fn>(__f), std::move(error())); } - template requires is_move_constructible_v<_Er> + template requires is_constructible_v<_Tp, const _Tp> constexpr auto or_else(_Fn&& __f) const && { @@ -954,7 +954,7 @@ namespace __expected return std::__invoke(std::forward<_Fn>(__f), std::move(error())); } - template requires is_copy_constructible_v<_Er> + template requires is_constructible_v<_Er, _Er&> constexpr auto transform(_Fn&& __f) & { @@ -970,7 +970,7 @@ namespace __expected return _Res(unexpect, std::move(error())); } - template requires is_copy_constructible_v<_Er> + template requires is_constructible_v<_Er, const _Er&> constexpr auto transform(_Fn&& __f) const & { @@ -986,7 +986,7 @@ namespace __expected return _Res(unexpect, std::move(error())); } - template requires is_move_constructible_v<_Er> + template requires is_constructible_v<_Er, _Er> constexpr auto transform(_Fn&& __f) && { @@ -1002,7 +1002,7 @@ namespace __expected return _Res(unexpect, std::move(error())); } - template requires is_move_constructible_v<_Er> + template requires is_constructible_v<_Er, const _Er> constexpr auto transform(_Fn&& __f) const && { @@ -1018,7 +1018,7 @@ namespace __expected return _Res(unexpect, std::move(error())); } - template requires is_copy_constructible_v<_Tp> + template requires is_constructible_v<_Tp, _Tp&> constexpr auto transform_error(_Fn&& __f) & { @@ -1034,7 +1034,7 @@ namespace __expected }); } - template requires is_copy_constructible_v<_Tp> + template requires is_constructible_v<_Tp, const _Tp&> constexpr auto transform_error(_Fn&& __f) const & { @@ -1050,7 +1050,7 @@ namespace __expected }); } - template requires is_move_constructible_v<_Tp> + template requires is_constructible_v<_Tp, _Tp> constexpr auto transform_error(_Fn&& __f) && { @@ -1066,7 +1066,7 @@ namespace __expected }); } - template requires is_move_constructible_v<_Tp> + template requires is_constructible_v<_Tp, const _Tp> constexpr auto transform_error(_Fn&& __f) const && { @@ -1519,7 +1519,7 @@ namespace __expected // monadic operations - template requires is_copy_constructible_v<_Er> + template requires is_constructible_v<_Er, _Er&> constexpr auto and_then(_Fn&& __f) & { @@ -1533,7 +1533,7 @@ namespace __expected return _Up(unexpect, error()); } - template requires is_copy_constructible_v<_Er> + template requires is_constructible_v<_Er, const _Er&> constexpr auto and_then(_Fn&& __f) const & { @@ -1547,7 +1547,7 @@ namespace __expected return _Up(unexpect, error()); } - template requires is_move_constructible_v<_Er> + template requires is_constructible_v<_Er, _Er> constexpr auto and_then(_Fn&& __f) && { @@ -1561,7 +1561,7 @@ namespace __expected return _Up(unexpect, std::move(error())); } - template requires is_move_constructible_v<_Er> + template requires is_constructible_v<_Er, const _Er> constexpr auto and_then(_Fn&& __f) const && { @@ -1631,7 +1631,7 @@ namespace __expected return std::__invoke(std::forward<_Fn>(__f), std::move(error())); } - template requires is_copy_constructible_v<_Er> + template requires is_constructible_v<_Er, _Er&> constexpr auto transform(_Fn&& __f) & { @@ -1644,7 +1644,7 @@ namespace __expected return _Res(unexpect, error()); } - template requires is_copy_constructible_v<_Er> + template requires is_constructible_v<_Er, const _Er&> constexpr auto transform(_Fn&& __f) const & { @@ -1657,7 +1657,7 @@ namespace __expected return _Res(unexpect, error()); } - template requires is_move_constructible_v<_Er> + template requires is_constructible_v<_Er, _Er> constexpr auto transform(_Fn&& __f) && { @@ -1670,7 +1670,7 @@ namespace __expected return _Res(unexpect, std::move(error())); } - template requires is_move_constructible_v<_Er> + template requires is_constructible_v<_Er, const _Er> constexpr auto transform(_Fn&& __f) const && { diff --git a/libstdc++-v3/testsuite/20_util/expected/lwg3877.cc b/libstdc++-v3/testsuite/20_util/expected/lwg3877.cc new file mode 100644 index 00000000000..556d8d5dc9e --- /dev/null +++ b/libstdc++-v3/testsuite/20_util/expected/lwg3877.cc @@ -0,0 +1,64 @@ +// { dg-options "-std=gnu++23" } +// { dg-do compile { target c++23 } } + +#include + +struct T1 +{ +}; + +struct T2 +{ + T2(const T2&) = delete; +}; + +struct T3 +{ + T3(const T3&) = delete; + T3(T3&&) = delete; + T3(T3&) { } + T3(const T3&&) { } +}; + +template +concept Has_or_else = requires(Exp&& exp, F f) { + std::forward(exp).or_else(f); +}; + +using E1 = std::expected; +static_assert( Has_or_else ); +static_assert( Has_or_else ); +static_assert( Has_or_else ); +static_assert( Has_or_else ); + +using E2 = std::expected; +static_assert( !Has_or_else ); +static_assert( !Has_or_else ); +static_assert( !Has_or_else ); +static_assert( !Has_or_else ); + +using E3 = std::expected; +static_assert( Has_or_else ); +static_assert( !Has_or_else ); +static_assert( Has_or_else ); // uses or_else(F) const && +static_assert( Has_or_else ); + +template +concept Has_transform_error = requires(Exp&& exp, F f) { + std::forward(exp).transform_error(f); +}; + +static_assert( Has_transform_error ); +static_assert( Has_transform_error ); +static_assert( Has_transform_error ); +static_assert( Has_transform_error ); + +static_assert( !Has_transform_error ); +static_assert( !Has_transform_error ); +static_assert( !Has_transform_error ); +static_assert( !Has_transform_error ); + +static_assert( Has_transform_error ); +static_assert( !Has_transform_error ); +static_assert( Has_transform_error ); // uses transform_error(F) const && +static_assert( Has_transform_error );