[libcxx-commits] [libcxx] 823efd8 - [libc++] Implement P3798R1: The unexpected in `std::expected` (#219236)

via libcxx-commits libcxx-commits at lists.llvm.org
Sat Aug 29 04:32:07 PDT 2026


Author: Alex Kremer
Date: 2026-08-29T19:32:01+08:00
New Revision: 823efd84c5be6eb3b5585caea3b80b9403a1ce1b

URL: https://github.com/llvm/llvm-project/commit/823efd84c5be6eb3b5585caea3b80b9403a1ce1b
DIFF: https://github.com/llvm/llvm-project/commit/823efd84c5be6eb3b5585caea3b80b9403a1ce1b.diff

LOG: [libc++] Implement P3798R1: The unexpected in `std::expected` (#219236)

Closes https://github.com/llvm/llvm-project/issues/204394
Re-application of https://github.com/llvm/llvm-project/pull/204826
(45a65bb48b5925707f43d08e30df2263a5e4e268)

Implements P3798R1 and related tests.

Added: 
    libcxx/test/std/utilities/expected/expected.expected/observers/has_error.pass.cpp
    libcxx/test/std/utilities/expected/expected.void/observers/has_error.pass.cpp

Modified: 
    libcxx/docs/FeatureTestMacroTable.rst
    libcxx/docs/ReleaseNotes/24.rst
    libcxx/docs/Status/Cxx29Papers.csv
    libcxx/include/__expected/expected.h
    libcxx/include/version
    libcxx/test/libcxx/utilities/expected/nodiscard.verify.cpp
    libcxx/test/std/language.support/support.limits/support.limits.general/expected.version.compile.pass.cpp
    libcxx/test/std/language.support/support.limits/support.limits.general/version.version.compile.pass.cpp
    libcxx/utils/generate_feature_test_macro_components.py

Removed: 
    


################################################################################
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/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 3c48fe89be33d..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)","","","`#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>`__",""

diff  --git a/libcxx/include/__expected/expected.h b/libcxx/include/__expected/expected.h
index 5588196939738..13d01ad53c62c 100644
--- a/libcxx/include/__expected/expected.h
+++ b/libcxx/include/__expected/expected.h
@@ -827,6 +827,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()) {
@@ -1601,6 +1605,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/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/libcxx/utilities/expected/nodiscard.verify.cpp b/libcxx/test/libcxx/utilities/expected/nodiscard.verify.cpp
index c9af7a91c67f2..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,6 +49,9 @@ 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}}
@@ -110,6 +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/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

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..b6190c054d387
--- /dev/null
+++ b/libcxx/test/std/utilities/expected/expected.expected/observers/has_error.pass.cpp
@@ -0,0 +1,40 @@
+//===----------------------------------------------------------------------===//
+//
+// 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 "../../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..98bbf118c028e
--- /dev/null
+++ b/libcxx/test/std/utilities/expected/expected.void/observers/has_error.pass.cpp
@@ -0,0 +1,40 @@
+//===----------------------------------------------------------------------===//
+//
+// 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 "../../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 6b4acf671a0c9..f8ccd37690ce4
--- 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,
             },
             "headers": ["expected"],
         },


        


More information about the libcxx-commits mailing list