From patchwork Thu Nov 2 14:54:59 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jonathan Wakely X-Patchwork-Id: 160972 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:8f47:0:b0:403:3b70:6f57 with SMTP id j7csp455201vqu; Thu, 2 Nov 2023 08:59:19 -0700 (PDT) X-Google-Smtp-Source: AGHT+IF1bCZtQDsYGamuTbtA8E1DsdMJQYLRlVPlGrQE72102NznwIWSLPZxOo9ctIFokNXaajRx X-Received: by 2002:a05:620a:4011:b0:774:32f0:e2b5 with SMTP id h17-20020a05620a401100b0077432f0e2b5mr414444qko.9.1698940759009; Thu, 02 Nov 2023 08:59:19 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1698940758; cv=pass; d=google.com; s=arc-20160816; b=JjU3G/rmlkEkqoIQdQYGWww6snMiHVUb4Fw+wSBsycFphugeEaZ81+dP/ggw2nMBRH Rxt5aw+jrop52qHmiQUBwlz3rDQZta/Kqqr87YOxACENhw8T6r6v7d2C78K1MyXxQGgC +BJ2B7jNbgg4QY9l75JygRSfdbDo9vKUpjQsyVMlgyqZ5EFzUfHciSGYgYJ12TdQG/iM OaliDpJ6E06RrnLQv+Ci3Vgvj8c27Fh/JnuOZYO3hPFHVK00PnwLNA5wSjtXP7Ke8k2+ Q0PsKwsVTDXynzLeTb6FNFPi+X0Kp2xZE30qH7aWOjZK+en+Sm490aLw3Z/S8bVoksw1 8UJg== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:content-transfer-encoding :mime-version:message-id:date:subject:to:from:dkim-signature :arc-filter:dmarc-filter:delivered-to; bh=GPFe54Sw3W59P2JbsQehqxnCQrNDVncgSmC5JTC2/9Y=; fh=sJ+2/4g29YdyXkoRrFZSpsL2zxijepB7X/1rB0LDDh8=; b=dCFr1qk1V5rXx84jGsQyZ3GAe/2IoWdu9H9LoN0UlVhIbJUu3gJnyPwMDTQOOPh1JU hmp8DAfH9Xldaz1Zem5wU9W/HZoEKSYoPFfBDurkYc4LDYxb3cGguXrspXyosMysxOkB +L/SS83O1V1/XbNc9gISM5J1+Z6a/Ib5Bjd5rVmTnQZc8BvQSWKgJmF+ePVXjPxEkyqm mF76fpu89MCW0msE9JsBNaHQYWbTLKe6m4oAd8XrHaMOIaJo1US/L8aP9zmHQr02sTnE u1OwUXrwTgIZwR1YkWgKsc9nycMlRuGGvHIb64ChmuPmx6JO7/MWiX1rJvwISbTz8c0K 6yXQ== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=GleBY9y7; arc=pass (i=1); 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=redhat.com Received: from server2.sourceware.org (server2.sourceware.org. [2620:52:3:1:0:246e:9693:128c]) by mx.google.com with ESMTPS id y11-20020a05620a25cb00b007671804681asi141333qko.15.2023.11.02.08.59.18 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 02 Nov 2023 08:59:18 -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=@redhat.com header.s=mimecast20190719 header.b=GleBY9y7; arc=pass (i=1); 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=redhat.com Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 9D4D8385E021 for ; Thu, 2 Nov 2023 14:55:53 +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 ESMTPS id 560EB3858C36 for ; Thu, 2 Nov 2023 14:55:28 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 560EB3858C36 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 560EB3858C36 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=1698936930; cv=none; b=qyDblMBx7Q4eKQvsT7ozjaK4jNFb52x/ji0/NGoseWIfOH/vTlQ5WAXQrI9IMWVfxLVvgamvt8W7uHcCKXcf41cLiM3aBQ73xaP8Ofb0dL7Wc/PhXRE/cTbzIKFrv4L40DBJcjoOLgne5KkBtNE9dibdR5mK7I7NMB83V3vDldU= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1698936930; c=relaxed/simple; bh=aDK+iub9wj+MshkH3YT4kX83j0cIsriXSdfIk/954ek=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=TmQyye5tuTeiblGcoSCgEYoTJ30hfSzz9GO00XRvIAahsyfP6mV7OBOn/d5PwpN4umIG3V7nORprbD6L/KJvmDosGnBAx4eDhqaxIbldvn/jTZZXssHnJ2gH13WeadMEGJ1rDa4vvYgKE0JzMEH/t7b6xmX4fG4g1gBzcxdIA64= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1698936928; 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=GPFe54Sw3W59P2JbsQehqxnCQrNDVncgSmC5JTC2/9Y=; b=GleBY9y7A9kStHAxJ17fOXvm1eR4TAUpa1RYa7SkELFjpUQvtYtlPXvxdcQeOjA/B5Ml5x 8Vf/IE/nZi7rMBpIlTKYAGBE5Ezo9+J9vFf9FxN8fv2ET7249YdLZcWSVcws1627JaNYCw x7End6DHa4XWuaoU5v1hClC0vWDYzu8= Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-516-pmZ0VuoPPja2QW23vhWwbg-1; Thu, 02 Nov 2023 10:55:23 -0400 X-MC-Unique: pmZ0VuoPPja2QW23vhWwbg-1 Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.rdu2.redhat.com [10.11.54.1]) (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 mimecast-mx02.redhat.com (Postfix) with ESMTPS id A4170852683; Thu, 2 Nov 2023 14:55:23 +0000 (UTC) Received: from localhost (unknown [10.42.28.65]) by smtp.corp.redhat.com (Postfix) with ESMTP id 6E19B25C0; Thu, 2 Nov 2023 14:55:23 +0000 (UTC) From: Jonathan Wakely To: libstdc++@gcc.gnu.org, gcc-patches@gcc.gnu.org Subject: [PATCH] libstdc++: Improve static assert messages for monadic operations Date: Thu, 2 Nov 2023 14:54:59 +0000 Message-ID: <20231102145522.2813330-1-jwakely@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.1 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, T_SCC_BODY_TEXT_LINE 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+ouuuleilei=gmail.com@gcc.gnu.org X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1781468504991303982 X-GMAIL-MSGID: 1781468504991303982 Any objections or suggestions for better wording? Tested x86_64-linux. -- >8 -- The monadic operations for std::optional and std::expected make use of internal helper traits __is_optional nad __is_expected, which are not very user-friendly when shown in diagnostics. Add messages to the assertions explaining the problem more clearly. libstdc++-v3/ChangeLog: * include/std/expected (expected::and_then, expected::or_else): Add string literals to static assertions. * include/std/optional (optional::and_then, optional::or_else): Likewise. --- libstdc++-v3/include/std/expected | 64 +++++++++++++++++++++++-------- libstdc++-v3/include/std/optional | 24 +++++++++--- 2 files changed, 66 insertions(+), 22 deletions(-) diff --git a/libstdc++-v3/include/std/expected b/libstdc++-v3/include/std/expected index a796f0b6f27..a176d4c3a78 100644 --- a/libstdc++-v3/include/std/expected +++ b/libstdc++-v3/include/std/expected @@ -843,8 +843,12 @@ namespace __expected and_then(_Fn&& __f) & { using _Up = __expected::__result<_Fn, _Tp&>; - static_assert(__expected::__is_expected<_Up>); - static_assert(is_same_v); + static_assert(__expected::__is_expected<_Up>, + "the function passed to std::expected::and_then " + "must return a std::expected"); + static_assert(is_same_v, + "the function passed to std::expected::and_then " + "must return a std::expected with the same error_type"); if (has_value()) return std::__invoke(std::forward<_Fn>(__f), _M_val); @@ -857,8 +861,12 @@ namespace __expected and_then(_Fn&& __f) const & { using _Up = __expected::__result<_Fn, const _Tp&>; - static_assert(__expected::__is_expected<_Up>); - static_assert(is_same_v); + static_assert(__expected::__is_expected<_Up>, + "the function passed to std::expected::and_then " + "must return a std::expected"); + static_assert(is_same_v, + "the function passed to std::expected::and_then " + "must return a std::expected with the same error_type"); if (has_value()) return std::__invoke(std::forward<_Fn>(__f), _M_val); @@ -871,8 +879,12 @@ namespace __expected and_then(_Fn&& __f) && { using _Up = __expected::__result<_Fn, _Tp&&>; - static_assert(__expected::__is_expected<_Up>); - static_assert(is_same_v); + static_assert(__expected::__is_expected<_Up>, + "the function passed to std::expected::and_then " + "must return a std::expected"); + static_assert(is_same_v, + "the function passed to std::expected::and_then " + "must return a std::expected with the same error_type"); if (has_value()) return std::__invoke(std::forward<_Fn>(__f), std::move(_M_val)); @@ -886,8 +898,12 @@ namespace __expected and_then(_Fn&& __f) const && { using _Up = __expected::__result<_Fn, const _Tp&&>; - static_assert(__expected::__is_expected<_Up>); - static_assert(is_same_v); + static_assert(__expected::__is_expected<_Up>, + "the function passed to std::expected::and_then " + "must return a std::expected"); + static_assert(is_same_v, + "the function passed to std::expected::and_then " + "must return a std::expected with the same error_type"); if (has_value()) return std::__invoke(std::forward<_Fn>(__f), std::move(_M_val)); @@ -900,8 +916,12 @@ namespace __expected or_else(_Fn&& __f) & { using _Gr = __expected::__result<_Fn, _Er&>; - static_assert(__expected::__is_expected<_Gr>); - static_assert(is_same_v); + static_assert(__expected::__is_expected<_Gr>, + "the function passed to std::expected::or_else " + "must return a std::expected"); + static_assert(is_same_v, + "the function passed to std::expected::or_else " + "must return a std::expected with the same value_type"); if (has_value()) return _Gr(in_place, _M_val); @@ -914,8 +934,12 @@ namespace __expected or_else(_Fn&& __f) const & { using _Gr = __expected::__result<_Fn, const _Er&>; - static_assert(__expected::__is_expected<_Gr>); - static_assert(is_same_v); + static_assert(__expected::__is_expected<_Gr>, + "the function passed to std::expected::or_else " + "must return a std::expected"); + static_assert(is_same_v, + "the function passed to std::expected::or_else " + "must return a std::expected with the same value_type"); if (has_value()) return _Gr(in_place, _M_val); @@ -929,8 +953,12 @@ namespace __expected or_else(_Fn&& __f) && { using _Gr = __expected::__result<_Fn, _Er&&>; - static_assert(__expected::__is_expected<_Gr>); - static_assert(is_same_v); + static_assert(__expected::__is_expected<_Gr>, + "the function passed to std::expected::or_else " + "must return a std::expected"); + static_assert(is_same_v, + "the function passed to std::expected::or_else " + "must return a std::expected with the same value_type"); if (has_value()) return _Gr(in_place, std::move(_M_val)); @@ -943,8 +971,12 @@ namespace __expected or_else(_Fn&& __f) const && { using _Gr = __expected::__result<_Fn, const _Er&&>; - static_assert(__expected::__is_expected<_Gr>); - static_assert(is_same_v); + static_assert(__expected::__is_expected<_Gr>, + "the function passed to std::expected::or_else " + "must return a std::expected"); + static_assert(is_same_v, + "the function passed to std::expected::or_else " + "must return a std::expected with the same value_type"); if (has_value()) return _Gr(in_place, std::move(_M_val)); diff --git a/libstdc++-v3/include/std/optional b/libstdc++-v3/include/std/optional index 4f75eb96f97..53450c760d9 100644 --- a/libstdc++-v3/include/std/optional +++ b/libstdc++-v3/include/std/optional @@ -1048,7 +1048,9 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION and_then(_Fn&& __f) & { using _Up = remove_cvref_t>; - static_assert(__is_optional_v>); + static_assert(__is_optional_v>, + "the function passed to std::optional::and_then " + "must return a std::optional"); if (has_value()) return std::__invoke(std::forward<_Fn>(__f), **this); else @@ -1060,7 +1062,9 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION and_then(_Fn&& __f) const & { using _Up = remove_cvref_t>; - static_assert(__is_optional_v<_Up>); + static_assert(__is_optional_v<_Up>, + "the function passed to std::optional::and_then " + "must return a std::optional"); if (has_value()) return std::__invoke(std::forward<_Fn>(__f), **this); else @@ -1072,7 +1076,9 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION and_then(_Fn&& __f) && { using _Up = remove_cvref_t>; - static_assert(__is_optional_v>); + static_assert(__is_optional_v>, + "the function passed to std::optional::and_then " + "must return a std::optional"); if (has_value()) return std::__invoke(std::forward<_Fn>(__f), std::move(**this)); else @@ -1084,7 +1090,9 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION and_then(_Fn&& __f) const && { using _Up = remove_cvref_t>; - static_assert(__is_optional_v>); + static_assert(__is_optional_v>, + "the function passed to std::optional::and_then " + "must return a std::optional"); if (has_value()) return std::__invoke(std::forward<_Fn>(__f), std::move(**this)); else @@ -1140,7 +1148,9 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION or_else(_Fn&& __f) const& { using _Up = invoke_result_t<_Fn>; - static_assert( is_same_v, optional> ); + static_assert(is_same_v, optional>, + "the function passed to std::optional::or_else " + "must return a std::optional"); if (has_value()) return *this; @@ -1153,7 +1163,9 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION or_else(_Fn&& __f) && { using _Up = invoke_result_t<_Fn>; - static_assert( is_same_v, optional> ); + static_assert(is_same_v, optional>, + "the function passed to std::optional::or_else " + "must return a std::optional"); if (has_value()) return std::move(*this);