public inbox for libstdc++-cvs@sourceware.org help / color / mirror / Atom feed
From: Jonathan Wakely <redi@gcc.gnu.org> To: gcc-cvs@gcc.gnu.org, libstdc++-cvs@gcc.gnu.org Subject: [gcc r14-5146] libstdc++: Improve static assert messages for monadic operations Date: Mon, 6 Nov 2023 10:16:25 +0000 (GMT) [thread overview] Message-ID: <20231106101625.08C4D3858D28@sourceware.org> (raw) https://gcc.gnu.org/g:3d654f96d4c583c35e4f73aa8f11371730671534 commit r14-5146-g3d654f96d4c583c35e4f73aa8f11371730671534 Author: Jonathan Wakely <jwakely@redhat.com> Date: Wed Nov 1 14:28:55 2023 +0000 libstdc++: Improve static assert messages for monadic operations 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. Diff: --- 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<typename _Up::error_type, _Er>); + static_assert(__expected::__is_expected<_Up>, + "the function passed to std::expected<T, E>::and_then " + "must return a std::expected"); + static_assert(is_same_v<typename _Up::error_type, _Er>, + "the function passed to std::expected<T, E>::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<typename _Up::error_type, _Er>); + static_assert(__expected::__is_expected<_Up>, + "the function passed to std::expected<T, E>::and_then " + "must return a std::expected"); + static_assert(is_same_v<typename _Up::error_type, _Er>, + "the function passed to std::expected<T, E>::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<typename _Up::error_type, _Er>); + static_assert(__expected::__is_expected<_Up>, + "the function passed to std::expected<T, E>::and_then " + "must return a std::expected"); + static_assert(is_same_v<typename _Up::error_type, _Er>, + "the function passed to std::expected<T, E>::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<typename _Up::error_type, _Er>); + static_assert(__expected::__is_expected<_Up>, + "the function passed to std::expected<T, E>::and_then " + "must return a std::expected"); + static_assert(is_same_v<typename _Up::error_type, _Er>, + "the function passed to std::expected<T, E>::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<typename _Gr::value_type, _Tp>); + static_assert(__expected::__is_expected<_Gr>, + "the function passed to std::expected<T, E>::or_else " + "must return a std::expected"); + static_assert(is_same_v<typename _Gr::value_type, _Tp>, + "the function passed to std::expected<T, E>::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<typename _Gr::value_type, _Tp>); + static_assert(__expected::__is_expected<_Gr>, + "the function passed to std::expected<T, E>::or_else " + "must return a std::expected"); + static_assert(is_same_v<typename _Gr::value_type, _Tp>, + "the function passed to std::expected<T, E>::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<typename _Gr::value_type, _Tp>); + static_assert(__expected::__is_expected<_Gr>, + "the function passed to std::expected<T, E>::or_else " + "must return a std::expected"); + static_assert(is_same_v<typename _Gr::value_type, _Tp>, + "the function passed to std::expected<T, E>::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<typename _Gr::value_type, _Tp>); + static_assert(__expected::__is_expected<_Gr>, + "the function passed to std::expected<T, E>::or_else " + "must return a std::expected"); + static_assert(is_same_v<typename _Gr::value_type, _Tp>, + "the function passed to std::expected<T, E>::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<invoke_result_t<_Fn, _Tp&>>; - static_assert(__is_optional_v<remove_cvref_t<_Up>>); + static_assert(__is_optional_v<remove_cvref_t<_Up>>, + "the function passed to std::optional<T>::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<invoke_result_t<_Fn, const _Tp&>>; - static_assert(__is_optional_v<_Up>); + static_assert(__is_optional_v<_Up>, + "the function passed to std::optional<T>::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<invoke_result_t<_Fn, _Tp>>; - static_assert(__is_optional_v<remove_cvref_t<_Up>>); + static_assert(__is_optional_v<remove_cvref_t<_Up>>, + "the function passed to std::optional<T>::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<invoke_result_t<_Fn, const _Tp>>; - static_assert(__is_optional_v<remove_cvref_t<_Up>>); + static_assert(__is_optional_v<remove_cvref_t<_Up>>, + "the function passed to std::optional<T>::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<remove_cvref_t<_Up>, optional> ); + static_assert(is_same_v<remove_cvref_t<_Up>, optional>, + "the function passed to std::optional<T>::or_else " + "must return a std::optional<T>"); 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<remove_cvref_t<_Up>, optional> ); + static_assert(is_same_v<remove_cvref_t<_Up>, optional>, + "the function passed to std::optional<T>::or_else " + "must return a std::optional<T>"); if (has_value()) return std::move(*this);
reply other threads:[~2023-11-06 10:16 UTC|newest] Thread overview: [no followups] expand[flat|nested] mbox.gz Atom feed
Reply instructions: You may reply publicly to this message via plain-text email using any one of the following methods: * Save the following mbox file, import it into your mail client, and reply-to-all from there: mbox Avoid top-posting and favor interleaved quoting: https://en.wikipedia.org/wiki/Posting_style#Interleaved_style * Reply using the --to, --cc, and --in-reply-to switches of git-send-email(1): git send-email \ --in-reply-to=20231106101625.08C4D3858D28@sourceware.org \ --to=redi@gcc.gnu.org \ --cc=gcc-cvs@gcc.gnu.org \ --cc=libstdc++-cvs@gcc.gnu.org \ /path/to/YOUR_REPLY https://kernel.org/pub/software/scm/git/docs/git-send-email.html * If your mail client supports setting the In-Reply-To header via mailto: links, try the mailto: linkBe sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox; as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).