From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: 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 B8D513858C2B for ; Mon, 6 Nov 2023 10:17:00 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org B8D513858C2B 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 B8D513858C2B 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=1699265824; cv=none; b=qTDlN5W2LKmnSjZ52oQEuomEb2mU5hhoIfxcRcYZxZux9LPAaHY/86pYqlEVgRtB+80OncJ3bIJ67waEcNJjMchSCoiua/WI/spjvVzfNseuRGmXmAp+XAyCaKxeYTbdL3BjvlHUtoA1dxy9kNtPGIYG2HOIpnfyVGb97fBOnMU= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1699265824; c=relaxed/simple; bh=WhgAvfz0pJHHXg2ffu5i4II2p/lC8DR25lJPyjOnJmg=; h=DKIM-Signature:MIME-Version:From:Date:Message-ID:Subject:To; b=L6Yy9Iya53rjHbUxlEsFi9AFmxgvJ386jOVWQn6M/gSsPIjhtm6lAFUWswl4+BGtgt4VthhqU2dLZRJFWTmptnUza0rRpzshapoJq3V3kiYIqjQ2tXHPXUfudtm0YEDjYsTMe2Ra6awRzK/0NG4VZsOgRLhwQv9lyRn9AIFOm+4= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1699265820; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=VBvH/ak8k35umS74dROfV6YkJCOlX99UbT2aGywsEwU=; b=K9Dm8v9PZmeJO9ZcGMW/io/606OVV/zIobQqhQerpeJdSfFbo7GTS0StgBd6S99dpq1d01 ea1wIHgHpe1TQPX6LI3NTcPaGOzOgY0fcqLIP3sjUXeVSaPhk40LEHXxTqqCF842FzyBkq Df4z16dSgXAAlm2ujUAJfOImwP0dQs0= Received: from mail-yw1-f200.google.com (mail-yw1-f200.google.com [209.85.128.200]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-445-h-pmS2UHPgCIgzkeEz9rJg-1; Mon, 06 Nov 2023 05:16:55 -0500 X-MC-Unique: h-pmS2UHPgCIgzkeEz9rJg-1 Received: by mail-yw1-f200.google.com with SMTP id 00721157ae682-5a7af69a4baso58742857b3.0 for ; Mon, 06 Nov 2023 02:16:55 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1699265815; x=1699870615; h=to:subject:message-id:date:from:in-reply-to:references:mime-version :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=VBvH/ak8k35umS74dROfV6YkJCOlX99UbT2aGywsEwU=; b=FxsM9ue2T0vsycLPOuz+8NVXm5HHI0BjSVxMBxv20IU9BdsMX1DPLPBsWj8QJu2Bpl VwEM1uJRRCYW1rJzK8dpU9dgjinDZE7/tSj9XKSZ/Zme74cmfPMAoexWqdHcaD5VD2MN L6h/sGc+cmTe3kmlIXSyp1OIY71ujI0PWlk97gvfFkuwXHnTyb2aoxO2evki4JxWvpiC kv6Jj9Y77Ek6FIpUFFU5/6ao72UKHwcWwpKHj+ks7PIza3OSqG/kkuH/itA4H0ROLWJm IwC9+j+8nyke68m24ftPefHMmkG4vqr8K6BxaiQrElWPL2ySoXmM1VhnXw32asdss9t3 bmcA== X-Gm-Message-State: AOJu0YwNM7/hvRJmSo4r3TwmvBTkNqmKDbVz/Img/RnWo3j1oGkPO3SR 4F3VJd7TItl+LVL0R0Zo0m0y6OAtvhZeieahQKuzSk2tl8I+GHL/9n5PVLPgmkZQGsmYzF9WVXE sheBH8YyUOwdizI8r6fFVxYjQH6phr/1E910VwKg= X-Received: by 2002:a0d:d58b:0:b0:5b0:6326:75a4 with SMTP id x133-20020a0dd58b000000b005b0632675a4mr10466526ywd.7.1699265814808; Mon, 06 Nov 2023 02:16:54 -0800 (PST) X-Google-Smtp-Source: AGHT+IFTTVY1nHRTsPOu5HDwIVgUSlRGIrakXdbQIql0NXhrft1cZPnBeSLV0dPrt7g3qCjkUkSLevWCwhMAqrOmhHc= X-Received: by 2002:a0d:d58b:0:b0:5b0:6326:75a4 with SMTP id x133-20020a0dd58b000000b005b0632675a4mr10466518ywd.7.1699265814548; Mon, 06 Nov 2023 02:16:54 -0800 (PST) MIME-Version: 1.0 References: <20231102145522.2813330-1-jwakely@redhat.com> In-Reply-To: <20231102145522.2813330-1-jwakely@redhat.com> From: Jonathan Wakely Date: Mon, 6 Nov 2023 10:16:43 +0000 Message-ID: Subject: Re: [PATCH] libstdc++: Improve static assert messages for monadic operations To: libstdc++@gcc.gnu.org, gcc-patches@gcc.gnu.org X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=-12.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_H4,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 List-Id: On Thu, 2 Nov 2023 at 14:55, Jonathan Wakely wrote: > > Any objections or suggestions for better wording? > > Tested x86_64-linux. Pushed to trunk. > > -- >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); > -- > 2.41.0 >