libstdc++: Use std::remove_cv_t in std::optional::transform [PR109340]
authorJonathan Wakely <jwakely@redhat.com>
Wed, 29 Mar 2023 21:16:55 +0000 (22:16 +0100)
committerJonathan Wakely <jwakely@redhat.com>
Wed, 29 Mar 2023 23:06:25 +0000 (00:06 +0100)
We need to strip cv-qualifiers from the result of the callable passed to
std::optional::transform.

For std::expected::transform and std::expected::transform_error I
noticed we were stripping cv-qualifiers but were also incorrectly
stripping references.

libstdc++-v3/ChangeLog:

PR libstdc++/109340
* include/std/expected (expected::transform): Use
std::remove_cv_t instead of std::remove_cvref_t.
(expected::transform_error): Likewise.
(expected<cv void, E>::transform): Likewise.
(expected<cv void, E>::transform_error): Likewise.
* include/std/optional (transform): Use std::remove_cv_t.
* testsuite/20_util/optional/monadic/pr109340.cc: New test.

libstdc++-v3/include/std/expected
libstdc++-v3/include/std/optional
libstdc++-v3/testsuite/20_util/optional/monadic/pr109340.cc [new file with mode: 0644]

index 567a519..cb5754e 100644 (file)
@@ -154,8 +154,12 @@ namespace __expected
 
   template<typename _Fn, typename _Tp>
     using __result = remove_cvref_t<invoke_result_t<_Fn&&, _Tp&&>>;
+  template<typename _Fn, typename _Tp>
+    using __result_xform = remove_cv_t<invoke_result_t<_Fn&&, _Tp&&>>;
   template<typename _Fn>
     using __result0 = remove_cvref_t<invoke_result_t<_Fn&&>>;
+  template<typename _Fn>
+    using __result0_xform = remove_cv_t<invoke_result_t<_Fn&&>>;
 
   template<typename _Er>
     concept __can_be_unexpected
@@ -953,7 +957,7 @@ namespace __expected
        constexpr auto
        transform(_Fn&& __f) &
        {
-         using _Up = __expected::__result<_Fn, _Tp&>;
+         using _Up = __expected::__result_xform<_Fn, _Tp&>;
          using _Res = expected<_Up, _Er>;
 
          if (has_value())
@@ -969,7 +973,7 @@ namespace __expected
        constexpr auto
        transform(_Fn&& __f) const &
        {
-         using _Up = __expected::__result<_Fn, const _Tp&>;
+         using _Up = __expected::__result_xform<_Fn, const _Tp&>;
          using _Res = expected<_Up, _Er>;
 
          if (has_value())
@@ -985,7 +989,7 @@ namespace __expected
        constexpr auto
        transform(_Fn&& __f) &&
        {
-         using _Up = __expected::__result<_Fn, _Tp>;
+         using _Up = __expected::__result_xform<_Fn, _Tp>;
          using _Res = expected<_Up, _Er>;
 
          if (has_value())
@@ -1001,7 +1005,7 @@ namespace __expected
        constexpr auto
        transform(_Fn&& __f) const &&
        {
-         using _Up = __expected::__result<_Fn, const _Tp>;
+         using _Up = __expected::__result_xform<_Fn, const _Tp>;
          using _Res = expected<_Up, _Er>;
 
          if (has_value())
@@ -1017,7 +1021,7 @@ namespace __expected
        constexpr auto
        transform_error(_Fn&& __f) &
        {
-         using _Gr = __expected::__result<_Fn, _Er&>;
+         using _Gr = __expected::__result_xform<_Fn, _Er&>;
          using _Res = expected<_Tp, _Gr>;
 
          if (has_value())
@@ -1033,7 +1037,7 @@ namespace __expected
        constexpr auto
        transform_error(_Fn&& __f) const &
        {
-         using _Gr = __expected::__result<_Fn, const _Er&>;
+         using _Gr = __expected::__result_xform<_Fn, const _Er&>;
          using _Res = expected<_Tp, _Gr>;
 
          if (has_value())
@@ -1049,7 +1053,7 @@ namespace __expected
        constexpr auto
        transform_error(_Fn&& __f) &&
        {
-         using _Gr = __expected::__result<_Fn, _Er&&>;
+         using _Gr = __expected::__result_xform<_Fn, _Er&&>;
          using _Res = expected<_Tp, _Gr>;
 
          if (has_value())
@@ -1065,7 +1069,7 @@ namespace __expected
        constexpr auto
        transform_error(_Fn&& __f) const &&
        {
-         using _Gr = __expected::__result<_Fn, const _Er&&>;
+         using _Gr = __expected::__result_xform<_Fn, const _Er&&>;
          using _Res = expected<_Tp, _Gr>;
 
          if (has_value())
@@ -1630,7 +1634,7 @@ namespace __expected
        constexpr auto
        transform(_Fn&& __f) &
        {
-         using _Up = __expected::__result0<_Fn>;
+         using _Up = __expected::__result0_xform<_Fn>;
          using _Res = expected<_Up, _Er>;
 
          if (has_value())
@@ -1643,7 +1647,7 @@ namespace __expected
        constexpr auto
        transform(_Fn&& __f) const &
        {
-         using _Up = __expected::__result0<_Fn>;
+         using _Up = __expected::__result0_xform<_Fn>;
          using _Res = expected<_Up, _Er>;
 
          if (has_value())
@@ -1656,7 +1660,7 @@ namespace __expected
        constexpr auto
        transform(_Fn&& __f) &&
        {
-         using _Up = __expected::__result0<_Fn>;
+         using _Up = __expected::__result0_xform<_Fn>;
          using _Res = expected<_Up, _Er>;
 
          if (has_value())
@@ -1669,7 +1673,7 @@ namespace __expected
        constexpr auto
        transform(_Fn&& __f) const &&
        {
-         using _Up = __expected::__result0<_Fn>;
+         using _Up = __expected::__result0_xform<_Fn>;
          using _Res = expected<_Up, _Er>;
 
          if (has_value())
@@ -1682,7 +1686,7 @@ namespace __expected
        constexpr auto
        transform_error(_Fn&& __f) &
        {
-         using _Gr = __expected::__result<_Fn, _Er&>;
+         using _Gr = __expected::__result_xform<_Fn, _Er&>;
          using _Res = expected<_Tp, _Gr>;
 
          if (has_value())
@@ -1698,7 +1702,7 @@ namespace __expected
        constexpr auto
        transform_error(_Fn&& __f) const &
        {
-         using _Gr = __expected::__result<_Fn, const _Er&>;
+         using _Gr = __expected::__result_xform<_Fn, const _Er&>;
          using _Res = expected<_Tp, _Gr>;
 
          if (has_value())
@@ -1714,7 +1718,7 @@ namespace __expected
        constexpr auto
        transform_error(_Fn&& __f) &&
        {
-         using _Gr = __expected::__result<_Fn, _Er&&>;
+         using _Gr = __expected::__result_xform<_Fn, _Er&&>;
          using _Res = expected<_Tp, _Gr>;
 
          if (has_value())
@@ -1730,7 +1734,7 @@ namespace __expected
        constexpr auto
        transform_error(_Fn&& __f) const &&
        {
-         using _Gr = __expected::__result<_Fn, const _Er&&>;
+         using _Gr = __expected::__result_xform<_Fn, const _Er&&>;
          using _Res = expected<_Tp, _Gr>;
 
          if (has_value())
index 90bf741..bcac152 100644 (file)
@@ -1100,7 +1100,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
        constexpr auto
        transform(_Fn&& __f) &
        {
-         using _Up = invoke_result_t<_Fn, _Tp&>;
+         using _Up = remove_cv_t<invoke_result_t<_Fn, _Tp&>>;
          if (has_value())
            return optional<_Up>(_Optional_func<_Fn>{__f}, **this);
          else
@@ -1111,7 +1111,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
        constexpr auto
        transform(_Fn&& __f) const &
        {
-         using _Up = invoke_result_t<_Fn, const _Tp&>;
+         using _Up = remove_cv_t<invoke_result_t<_Fn, const _Tp&>>;
          if (has_value())
            return optional<_Up>(_Optional_func<_Fn>{__f}, **this);
          else
@@ -1122,7 +1122,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
        constexpr auto
        transform(_Fn&& __f) &&
        {
-         using _Up = invoke_result_t<_Fn, _Tp>;
+         using _Up = remove_cv_t<invoke_result_t<_Fn, _Tp>>;
          if (has_value())
            return optional<_Up>(_Optional_func<_Fn>{__f}, std::move(**this));
          else
@@ -1133,7 +1133,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
        constexpr auto
        transform(_Fn&& __f) const &&
        {
-         using _Up = invoke_result_t<_Fn, const _Tp>;
+         using _Up = remove_cv_t<invoke_result_t<_Fn, const _Tp>>;
          if (has_value())
            return optional<_Up>(_Optional_func<_Fn>{__f}, std::move(**this));
          else
diff --git a/libstdc++-v3/testsuite/20_util/optional/monadic/pr109340.cc b/libstdc++-v3/testsuite/20_util/optional/monadic/pr109340.cc
new file mode 100644 (file)
index 0000000..a25b625
--- /dev/null
@@ -0,0 +1,35 @@
+// { dg-options "-std=gnu++23" }
+// { dg-do compile { target c++23 } }
+
+#include <optional>
+
+// PR libstdc++/109242
+// transform omits required std::remove_cv_t from return optional type
+
+struct A { };
+struct B { };
+struct C { };
+struct D { };
+
+struct F
+{
+  const A operator()(int&);
+  const B operator()(const int&);
+  const C operator()(int&&);
+  const D operator()(const int&&);
+} f;
+
+std::optional<int> o;
+const auto& co = o;
+
+auto o1 = o.transform(f);
+static_assert(std::is_same_v<decltype(o1), std::optional<A>>);
+
+auto o2 = co.transform(f);
+static_assert(std::is_same_v<decltype(o2), std::optional<B>>);
+
+auto o3 = std::move(o).transform(f);
+static_assert(std::is_same_v<decltype(o3), std::optional<C>>);
+
+auto o4 = std::move(co).transform(f);
+static_assert(std::is_same_v<decltype(o4), std::optional<D>>);