]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
libstdc++: Improve static assert messages for monadic operations
authorJonathan Wakely <jwakely@redhat.com>
Wed, 1 Nov 2023 14:28:55 +0000 (14:28 +0000)
committerJonathan Wakely <jwakely@redhat.com>
Mon, 6 Nov 2023 10:15:20 +0000 (10:15 +0000)
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
libstdc++-v3/include/std/optional

index a796f0b6f27e79fff12fca6a412a6a4a5153cc40..a176d4c3a780a13c96d20df844fd2d1f1c5fb200 100644 (file)
@@ -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));
index 4f75eb96f977261bd7d8e7717df4ae9f8937290b..53450c760d9fe11e5750a021bf4a0c4e26998037 100644 (file)
@@ -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);