[libcxx-commits] [libcxx] [libc++] P3798R1: The unexpected in std::expected (PR #219236)

Alex Kremer via libcxx-commits libcxx-commits at lists.llvm.org
Fri Aug 28 07:01:22 PDT 2026


https://github.com/godexsoft updated https://github.com/llvm/llvm-project/pull/219236

>From 68583dda408d21af79ff41d0374567d6782cff93 Mon Sep 17 00:00:00 2001
From: Alex Kremer <akremer at ripple.com>
Date: Thu, 27 Aug 2026 17:10:00 +0100
Subject: [PATCH 1/5] Initial implementation of has_error

---
 libcxx/docs/ReleaseNotes/23.rst               |  1 +
 libcxx/docs/Status/Cxx29Papers.csv            |  2 +-
 libcxx/include/__expected/expected.h          |  8 ++++
 .../utilities/expected/nodiscard.verify.cpp   |  4 +-
 .../observers/has_error.pass.cpp              | 42 +++++++++++++++++++
 .../observers/has_error.pass.cpp              | 42 +++++++++++++++++++
 .../generate_feature_test_macro_components.py |  2 +-
 7 files changed, 98 insertions(+), 3 deletions(-)
 create mode 100644 libcxx/test/std/utilities/expected/expected.expected/observers/has_error.pass.cpp
 create mode 100644 libcxx/test/std/utilities/expected/expected.void/observers/has_error.pass.cpp
 mode change 100644 => 100755 libcxx/utils/generate_feature_test_macro_components.py

diff --git a/libcxx/docs/ReleaseNotes/23.rst b/libcxx/docs/ReleaseNotes/23.rst
index e7eb42207db42..d077a72e623f9 100644
--- a/libcxx/docs/ReleaseNotes/23.rst
+++ b/libcxx/docs/ReleaseNotes/23.rst
@@ -52,6 +52,7 @@ Implemented Papers
 - P3383R3: ``mdspan.at()`` (`Github <https://llvm.org/PR175213>`__)
 - P3369R0: constexpr for ``uninitialized_default_construct`` (`Github <https://llvm.org/PR118380>`__)
 - P3508R0: Wording for "constexpr for specialized memory algorithms" (`Github <https://llvm.org/PR118379>`__)
+- P3798R1: The unexpected in ``std::expected`` (`Github <https://llvm.org/PR204394>`__)
 
 Improvements and New Features
 -----------------------------
diff --git a/libcxx/docs/Status/Cxx29Papers.csv b/libcxx/docs/Status/Cxx29Papers.csv
index a5d896f260657..d3cb0a0437a99 100644
--- a/libcxx/docs/Status/Cxx29Papers.csv
+++ b/libcxx/docs/Status/Cxx29Papers.csv
@@ -3,7 +3,7 @@
 "`P4101R1 <https://wg21.link/P4101R1>`__","Consteval-only Values for C++26","2026-06 (Brno)","","","`#204391 <https://github.com/llvm/llvm-project/issues/204391>`__","Voted as a Defect Report."
 "`P2414R12 <https://wg21.link/P2414R12>`__","Pointer lifetime-end zap proposed solutions","2026-06 (Brno)","","","`#204392 <https://github.com/llvm/llvm-project/issues/204392>`__","Voted as a Defect Report."
 "`P3319R6 <https://wg21.link/P3319R6>`__","Add an ``iota`` object for ``simd`` (and more)","2026-06 (Brno)","","","`#204393 <https://github.com/llvm/llvm-project/issues/204393>`__",""
-"`P3798R1 <https://wg21.link/P3798R1>`__","The unexpected in ``std::expected``","2026-06 (Brno)","","","`#204394 <https://github.com/llvm/llvm-project/issues/204394>`__",""
+"`P3798R1 <https://wg21.link/P3798R1>`__","The unexpected in ``std::expected``","2026-06 (Brno)","|Complete|","29","`#204394 <https://github.com/llvm/llvm-project/issues/204394>`__",""
 "`P3052R2 <https://wg21.link/P3052R2>`__","``view_interface::at()``","2026-06 (Brno)","","","`#204395 <https://github.com/llvm/llvm-project/issues/204395>`__",""
 "`P4206R0 <https://wg21.link/P4206R0>`__","Revert string support in ``std::constant_wrapper``","2026-06 (Brno)","","","`#203336 <https://github.com/llvm/llvm-project/issues/203336>`__","To be applied as a Defect Report."
 "`P3395R6 <https://wg21.link/P3395R6>`__","Fix encoding issues and add a formatter for ``std::error_code``","2026-06 (Brno)","","","`#204396 <https://github.com/llvm/llvm-project/issues/204396>`__",""
diff --git a/libcxx/include/__expected/expected.h b/libcxx/include/__expected/expected.h
index 32ed81a392702..9a374cb2410f2 100644
--- a/libcxx/include/__expected/expected.h
+++ b/libcxx/include/__expected/expected.h
@@ -825,6 +825,10 @@ class expected : private __expected_base<_Tp, _Err> {
 
   [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr bool has_value() const noexcept { return this->__has_val(); }
 
+#  if _LIBCPP_STD_VER >= 29
+  [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr bool has_error() const noexcept { return !this->has_value(); }
+#  endif
+
   [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr const _Tp& value() const& {
     static_assert(is_copy_constructible_v<_Err>, "error_type has to be copy constructible");
     if (!this->__has_val()) {
@@ -1599,6 +1603,10 @@ class expected<_Tp, _Err> : private __expected_void_base<_Err> {
 
   [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr bool has_value() const noexcept { return this->__has_val(); }
 
+#  if _LIBCPP_STD_VER >= 29
+  [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr bool has_error() const noexcept { return !this->has_value(); }
+#  endif
+
   _LIBCPP_HIDE_FROM_ABI constexpr void operator*() const noexcept {
     _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(
         this->__has_val(), "expected::operator* requires the expected to contain a value");
diff --git a/libcxx/test/libcxx/utilities/expected/nodiscard.verify.cpp b/libcxx/test/libcxx/utilities/expected/nodiscard.verify.cpp
index c9af7a91c67f2..dc0d9aa210934 100644
--- a/libcxx/test/libcxx/utilities/expected/nodiscard.verify.cpp
+++ b/libcxx/test/libcxx/utilities/expected/nodiscard.verify.cpp
@@ -47,7 +47,8 @@ void test() {
   *std::move(exp);  // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
 
   exp.has_value(); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
-
+  exp.has_error(); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
+                 
   cExp.value();            // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
   exp.value();             // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
   std::move(cExp).value(); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
@@ -110,6 +111,7 @@ void test() {
   const std::expected<void, int> cVExp{};
 
   vExp.has_value(); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
+  vExp.has_error(); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
 
   // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
   cVExp.error();
diff --git a/libcxx/test/std/utilities/expected/expected.expected/observers/has_error.pass.cpp b/libcxx/test/std/utilities/expected/expected.expected/observers/has_error.pass.cpp
new file mode 100644
index 0000000000000..98571857fff08
--- /dev/null
+++ b/libcxx/test/std/utilities/expected/expected.expected/observers/has_error.pass.cpp
@@ -0,0 +1,42 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+// REQUIRES: std-at-least-c++29
+
+// constexpr bool has_error() const noexcept;
+
+#include <cassert>
+#include <concepts>
+#include <expected>
+#include <type_traits>
+#include <utility>
+
+#include "../../types.h"
+
+constexpr bool test() {
+  {
+    const std::expected<int, int> e(std::unexpect, 5);
+    static_assert(noexcept(e.has_error()));
+    std::same_as<bool> decltype(auto) has_err = e.has_error();
+    assert(has_err);
+  }
+
+  {
+    const std::expected<int, int> e(5);
+    assert(!e.has_error());
+  }
+
+  return true;
+}
+
+int main(int, char**) {
+  test();
+  static_assert(test());
+
+  return 0;
+}
diff --git a/libcxx/test/std/utilities/expected/expected.void/observers/has_error.pass.cpp b/libcxx/test/std/utilities/expected/expected.void/observers/has_error.pass.cpp
new file mode 100644
index 0000000000000..35d39c2769f55
--- /dev/null
+++ b/libcxx/test/std/utilities/expected/expected.void/observers/has_error.pass.cpp
@@ -0,0 +1,42 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+// REQUIRES: std-at-least-c++29
+
+// constexpr bool has_error() const noexcept;
+
+#include <cassert>
+#include <concepts>
+#include <expected>
+#include <type_traits>
+#include <utility>
+
+#include "../../types.h"
+
+constexpr bool test() {
+  {
+    const std::expected<void, int> e(std::unexpect, 5);
+    static_assert(noexcept(e.has_error()));
+    std::same_as<bool> decltype(auto) has_err = e.has_error();
+    assert(has_err);
+  }
+
+  {
+    const std::expected<void, int> e;
+    assert(!e.has_error());
+  }
+
+  return true;
+}
+
+int main(int, char**) {
+  test();
+  static_assert(test());
+
+  return 0;
+}
diff --git a/libcxx/utils/generate_feature_test_macro_components.py b/libcxx/utils/generate_feature_test_macro_components.py
old mode 100644
new mode 100755
index d764a1f677ba6..b8cc9113e8fb0
--- a/libcxx/utils/generate_feature_test_macro_components.py
+++ b/libcxx/utils/generate_feature_test_macro_components.py
@@ -557,7 +557,7 @@ def add_version_header(tc):
         },
         {
             "name": "__cpp_lib_expected",
-            "values": {"c++23": 202211},
+            "values": {"c++23": 202211, "c++29": 202606},
             "headers": ["expected"],
         },
         {

>From b7b9eff8b2d1104e939e0b8fe548dea838c84505 Mon Sep 17 00:00:00 2001
From: Alex Kremer <akremer at ripple.com>
Date: Fri, 28 Aug 2026 12:40:00 +0100
Subject: [PATCH 2/5] Address review comments

---
 .../test/libcxx/utilities/expected/nodiscard.verify.cpp   | 8 +++++++-
 .../expected.expected/observers/has_error.pass.cpp        | 1 -
 .../expected/expected.void/observers/has_error.pass.cpp   | 1 -
 3 files changed, 7 insertions(+), 3 deletions(-)

diff --git a/libcxx/test/libcxx/utilities/expected/nodiscard.verify.cpp b/libcxx/test/libcxx/utilities/expected/nodiscard.verify.cpp
index dc0d9aa210934..2c5c831161d2c 100644
--- a/libcxx/test/libcxx/utilities/expected/nodiscard.verify.cpp
+++ b/libcxx/test/libcxx/utilities/expected/nodiscard.verify.cpp
@@ -15,6 +15,8 @@
 #include <expected>
 #include <utility>
 
+#include "test_macros.h"
+
 void test() {
   // [expected.bad.void]
 
@@ -47,8 +49,10 @@ void test() {
   *std::move(exp);  // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
 
   exp.has_value(); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
+#if TEST_STD_VER >= 29
   exp.has_error(); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
-                 
+#endif
+
   cExp.value();            // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
   exp.value();             // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
   std::move(cExp).value(); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
@@ -111,7 +115,9 @@ void test() {
   const std::expected<void, int> cVExp{};
 
   vExp.has_value(); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
+#if TEST_STD_VER >= 29
   vExp.has_error(); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
+#endif
 
   // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
   cVExp.error();
diff --git a/libcxx/test/std/utilities/expected/expected.expected/observers/has_error.pass.cpp b/libcxx/test/std/utilities/expected/expected.expected/observers/has_error.pass.cpp
index 98571857fff08..a0ff30673afb5 100644
--- a/libcxx/test/std/utilities/expected/expected.expected/observers/has_error.pass.cpp
+++ b/libcxx/test/std/utilities/expected/expected.expected/observers/has_error.pass.cpp
@@ -13,7 +13,6 @@
 #include <cassert>
 #include <concepts>
 #include <expected>
-#include <type_traits>
 #include <utility>
 
 #include "../../types.h"
diff --git a/libcxx/test/std/utilities/expected/expected.void/observers/has_error.pass.cpp b/libcxx/test/std/utilities/expected/expected.void/observers/has_error.pass.cpp
index 35d39c2769f55..7e99b37ec74db 100644
--- a/libcxx/test/std/utilities/expected/expected.void/observers/has_error.pass.cpp
+++ b/libcxx/test/std/utilities/expected/expected.void/observers/has_error.pass.cpp
@@ -13,7 +13,6 @@
 #include <cassert>
 #include <concepts>
 #include <expected>
-#include <type_traits>
 #include <utility>
 
 #include "../../types.h"

>From fcbe85c8ba8ccdadf10fe4259f7cb7637dd5a258 Mon Sep 17 00:00:00 2001
From: Alex Kremer <akremer at ripple.com>
Date: Fri, 28 Aug 2026 12:45:37 +0100
Subject: [PATCH 3/5] Move entry to 24

---
 libcxx/docs/ReleaseNotes/24.rst    | 1 +
 libcxx/docs/Status/Cxx29Papers.csv | 2 +-
 2 files changed, 2 insertions(+), 1 deletion(-)

diff --git a/libcxx/docs/ReleaseNotes/24.rst b/libcxx/docs/ReleaseNotes/24.rst
index 9cf08b5732970..b9e638213191a 100644
--- a/libcxx/docs/ReleaseNotes/24.rst
+++ b/libcxx/docs/ReleaseNotes/24.rst
@@ -47,6 +47,7 @@ Implemented Papers
 - P3961R1: Less double indirection in ``function_ref`` (RU-220) (`Github <https://llvm.org/PR189606>`__)
 - P1901R2: Enabling the Use of ``weak_ptr`` as Keys in Unordered Associative Containers (`Github <https://llvm.org/PR105372>`__)
 - P0528R3: The Curious Case of Padding Bits, Featuring Atomic Compare-and-Exchange (`Github <https://llvm.org/PR76180>`__)
+- P3798R1: The unexpected in ``std::expected`` (`Github <https://llvm.org/PR204394>`__)
 
 Improvements and New Features
 -----------------------------
diff --git a/libcxx/docs/Status/Cxx29Papers.csv b/libcxx/docs/Status/Cxx29Papers.csv
index a06cbfd09cd68..1a413c1da63f9 100644
--- a/libcxx/docs/Status/Cxx29Papers.csv
+++ b/libcxx/docs/Status/Cxx29Papers.csv
@@ -3,7 +3,7 @@
 "`P4101R1 <https://wg21.link/P4101R1>`__","Consteval-only Values for C++26","2026-06 (Brno)","","","`#204391 <https://github.com/llvm/llvm-project/issues/204391>`__","Voted as a Defect Report."
 "`P2414R12 <https://wg21.link/P2414R12>`__","Pointer lifetime-end zap proposed solutions","2026-06 (Brno)","","","`#204392 <https://github.com/llvm/llvm-project/issues/204392>`__","Voted as a Defect Report."
 "`P3319R6 <https://wg21.link/P3319R6>`__","Add an ``iota`` object for ``simd`` (and more)","2026-06 (Brno)","","","`#204393 <https://github.com/llvm/llvm-project/issues/204393>`__",""
-"`P3798R1 <https://wg21.link/P3798R1>`__","The unexpected in ``std::expected``","2026-06 (Brno)","|Complete|","29","`#204394 <https://github.com/llvm/llvm-project/issues/204394>`__",""
+"`P3798R1 <https://wg21.link/P3798R1>`__","The unexpected in ``std::expected``","2026-06 (Brno)","|Complete|","24","`#204394 <https://github.com/llvm/llvm-project/issues/204394>`__",""
 "`P3052R2 <https://wg21.link/P3052R2>`__","``view_interface::at()``","2026-06 (Brno)","","","`#204395 <https://github.com/llvm/llvm-project/issues/204395>`__",""
 "`P4206R0 <https://wg21.link/P4206R0>`__","Revert string support in ``std::constant_wrapper``","2026-06 (Brno)","|Complete|","23","`#203336 <https://github.com/llvm/llvm-project/issues/203336>`__","Applied as a Defect Report."
 "`P3395R6 <https://wg21.link/P3395R6>`__","Fix encoding issues and add a formatter for ``std::error_code``","2026-06 (Brno)","","","`#204396 <https://github.com/llvm/llvm-project/issues/204396>`__",""

>From 1068a9a98978204b8b2c2a0b12086e1aff5e413c Mon Sep 17 00:00:00 2001
From: Alex Kremer <akremer at ripple.com>
Date: Fri, 28 Aug 2026 12:46:46 +0100
Subject: [PATCH 4/5] Regenerate version checks

---
 libcxx/docs/FeatureTestMacroTable.rst                        | 2 ++
 libcxx/include/version                                       | 5 ++++-
 .../support.limits.general/expected.version.compile.pass.cpp | 4 ++--
 .../support.limits.general/version.version.compile.pass.cpp  | 4 ++--
 4 files changed, 10 insertions(+), 5 deletions(-)

diff --git a/libcxx/docs/FeatureTestMacroTable.rst b/libcxx/docs/FeatureTestMacroTable.rst
index cabd30847b2af..bde7e9cf75f57 100644
--- a/libcxx/docs/FeatureTestMacroTable.rst
+++ b/libcxx/docs/FeatureTestMacroTable.rst
@@ -558,6 +558,8 @@ Status
     ---------------------------------------------------------- -----------------
     **C++29**
     ----------------------------------------------------------------------------
+    ``__cpp_lib_expected``                                     ``202606L``
+    ---------------------------------------------------------- -----------------
     ``__cpp_lib_map_lookup``                                   *unimplemented*
     ---------------------------------------------------------- -----------------
     ``__cpp_lib_mdspan_copy``                                  *unimplemented*
diff --git a/libcxx/include/version b/libcxx/include/version
index 589ce789ffa7c..ec74903885830 100644
--- a/libcxx/include/version
+++ b/libcxx/include/version
@@ -112,7 +112,8 @@ __cpp_lib_erase_if                                      202002L <deque> <forward
 __cpp_lib_exchange_function                             201304L <utility>
 __cpp_lib_execution                                     201902L <execution>
                                                         201603L // C++17
-__cpp_lib_expected                                      202211L <expected>
+__cpp_lib_expected                                      202606L <expected>
+                                                        202211L // C++23
 __cpp_lib_filesystem                                    201703L <filesystem>
 __cpp_lib_flat_map                                      202511L <flat_map>
 __cpp_lib_flat_set                                      202511L <flat_set>
@@ -661,6 +662,8 @@ __cpp_lib_void_t                                        201411L <type_traits>
 #endif
 
 #if _LIBCPP_STD_VER >= 29
+# undef  __cpp_lib_expected
+# define __cpp_lib_expected                             202606L
 // # define __cpp_lib_map_lookup                           202606L
 // # define __cpp_lib_mdspan_copy                          202606L
 // # define __cpp_lib_pointer_tag_pair                     202606L
diff --git a/libcxx/test/std/language.support/support.limits/support.limits.general/expected.version.compile.pass.cpp b/libcxx/test/std/language.support/support.limits/support.limits.general/expected.version.compile.pass.cpp
index 7c25c805a940e..38b36a8eb8653 100644
--- a/libcxx/test/std/language.support/support.limits/support.limits.general/expected.version.compile.pass.cpp
+++ b/libcxx/test/std/language.support/support.limits/support.limits.general/expected.version.compile.pass.cpp
@@ -132,8 +132,8 @@
 #  ifndef __cpp_lib_expected
 #    error "__cpp_lib_expected should be defined in c++29"
 #  endif
-#  if __cpp_lib_expected != 202211L
-#    error "__cpp_lib_expected should have the value 202211L in c++29"
+#  if __cpp_lib_expected != 202606L
+#    error "__cpp_lib_expected should have the value 202606L in c++29"
 #  endif
 
 #  if !defined(_LIBCPP_VERSION)
diff --git a/libcxx/test/std/language.support/support.limits/support.limits.general/version.version.compile.pass.cpp b/libcxx/test/std/language.support/support.limits/support.limits.general/version.version.compile.pass.cpp
index 769b79c7924d2..cab073405f01e 100644
--- a/libcxx/test/std/language.support/support.limits/support.limits.general/version.version.compile.pass.cpp
+++ b/libcxx/test/std/language.support/support.limits/support.limits.general/version.version.compile.pass.cpp
@@ -9241,8 +9241,8 @@
 #  ifndef __cpp_lib_expected
 #    error "__cpp_lib_expected should be defined in c++29"
 #  endif
-#  if __cpp_lib_expected != 202211L
-#    error "__cpp_lib_expected should have the value 202211L in c++29"
+#  if __cpp_lib_expected != 202606L
+#    error "__cpp_lib_expected should have the value 202606L in c++29"
 #  endif
 
 #  if !defined(_LIBCPP_VERSION) || _LIBCPP_HAS_FILESYSTEM

>From 3eecf48de22a04fe5b5be3c281f4778794ede8f9 Mon Sep 17 00:00:00 2001
From: Alex Kremer <akremer at ripple.com>
Date: Fri, 28 Aug 2026 15:00:47 +0100
Subject: [PATCH 5/5] Fix darker

---
 libcxx/utils/generate_feature_test_macro_components.py | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/libcxx/utils/generate_feature_test_macro_components.py b/libcxx/utils/generate_feature_test_macro_components.py
index a83cc86bb4021..b7e5353c7fe5e 100755
--- a/libcxx/utils/generate_feature_test_macro_components.py
+++ b/libcxx/utils/generate_feature_test_macro_components.py
@@ -567,7 +567,7 @@ def add_version_header(tc):
             "name": "__cpp_lib_expected",
             "values": {
                 "c++23": 202211,
-                "c++29": 202606 # P3798R1 The unexpected in std::expected
+                "c++29": 202606,  # P3798R1 The unexpected in std::expected
             },
             "headers": ["expected"],
         },



More information about the libcxx-commits mailing list