diff --git a/libcxx/include/future b/libcxx/include/future --- a/libcxx/include/future +++ b/libcxx/include/future @@ -44,14 +44,17 @@ const error_category& future_category() noexcept; -class future_error - : public logic_error +class future_error : public logic_error { public: future_error(error_code ec); // exposition only - explicit future_error(future_errc); // C++17 + explicit future_error(future_errc e); // C++17 + const error_code& code() const noexcept; const char* what() const noexcept; + +private: + error_code ec_; // exposition only }; template @@ -500,10 +503,20 @@ { error_code __ec_; public: - future_error(error_code __ec); -#if _LIBCPP_STD_VERS > 14 - explicit future_error(future_errc _Ev) : logic_error(), __ec_(make_error_code(_Ev)) {} + struct __tag { + explicit __tag() = default; + }; + + explicit future_error(error_code __ec, __tag) + : logic_error(__ec.message()), __ec_(__ec) {} + +#if _LIBCPP_STD_VER > 14 + explicit future_error(future_errc _Ev) + : future_error(make_error_code(_Ev), __tag()) {} +#else + future_error(error_code __ec) : logic_error(__ec.message()), __ec_(__ec) {} #endif + _LIBCPP_INLINE_VISIBILITY const error_code& code() const _NOEXCEPT {return __ec_;} @@ -518,7 +531,7 @@ void __throw_future_error(future_errc _Ev) { #ifndef _LIBCPP_NO_EXCEPTIONS - throw future_error(make_error_code(_Ev)); + throw future_error(make_error_code(_Ev), future_error::__tag()); #else ((void)_Ev); _VSTD::abort(); @@ -1338,9 +1351,9 @@ if (__state_) { if (!__state_->__has_value() && __state_->use_count() > 1) - __state_->set_exception(make_exception_ptr( - future_error(make_error_code(future_errc::broken_promise)) - )); + __state_->set_exception(make_exception_ptr( + future_error(make_error_code(future_errc::broken_promise), + future_error::__tag()))); __state_->__release_shared(); } } @@ -1481,9 +1494,9 @@ if (__state_) { if (!__state_->__has_value() && __state_->use_count() > 1) - __state_->set_exception(make_exception_ptr( - future_error(make_error_code(future_errc::broken_promise)) - )); + __state_->set_exception(make_exception_ptr( + future_error(make_error_code(future_errc::broken_promise), + future_error::__tag()))); __state_->__release_shared(); } } diff --git a/libcxx/src/future.cpp b/libcxx/src/future.cpp --- a/libcxx/src/future.cpp +++ b/libcxx/src/future.cpp @@ -71,12 +71,6 @@ return __f; } -future_error::future_error(error_code __ec) - : logic_error(__ec.message()), - __ec_(__ec) -{ -} - future_error::~future_error() noexcept { } @@ -207,9 +201,9 @@ { #ifndef _LIBCPP_NO_EXCEPTIONS if (!__state_->__has_value() && __state_->use_count() > 1) - __state_->set_exception(make_exception_ptr( - future_error(make_error_code(future_errc::broken_promise)) - )); + __state_->set_exception(make_exception_ptr( + future_error(make_error_code(future_errc::broken_promise), + future_error::__tag()))); #endif // _LIBCPP_NO_EXCEPTIONS __state_->__release_shared(); } diff --git a/libcxx/test/std/thread/futures/futures.future_error/code.pass.cpp b/libcxx/test/std/thread/futures/futures.future_error/code.pass.cpp --- a/libcxx/test/std/thread/futures/futures.future_error/code.pass.cpp +++ b/libcxx/test/std/thread/futures/futures.future_error/code.pass.cpp @@ -11,10 +11,10 @@ // // class future_error -// future_error(error_code __ec); // exposition only -// explicit future_error(future_errc _Ev) : __ec_(make_error_code(_Ev)) {} // C++17 +// future_error(error_code ec); // exposition only +// explicit future_error(future_errc e); // C++17 -// const error_code& code() const throw(); +// const error_code& code() const noexcept; #include #include @@ -23,6 +23,7 @@ int main(int, char**) { +#if TEST_STD_VER <= 14 { std::error_code ec = std::make_error_code(std::future_errc::broken_promise); std::future_error f(ec); @@ -43,16 +44,16 @@ std::future_error f(ec); assert(f.code() == ec); } -#if TEST_STD_VER > 14 +#endif { std::future_error f(std::future_errc::broken_promise); + ASSERT_NOEXCEPT(f.code()); assert(f.code() == std::make_error_code(std::future_errc::broken_promise)); } { std::future_error f(std::future_errc::no_state); assert(f.code() == std::make_error_code(std::future_errc::no_state)); } -#endif return 0; } diff --git a/libcxx/test/std/thread/futures/futures.future_error/ctor.fail.cpp b/libcxx/test/std/thread/futures/futures.future_error/ctor.fail.cpp new file mode 100644 --- /dev/null +++ b/libcxx/test/std/thread/futures/futures.future_error/ctor.fail.cpp @@ -0,0 +1,29 @@ +//===----------------------------------------------------------------------===// +// +// 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 +// +//===----------------------------------------------------------------------===// +// +// UNSUPPORTED: c++03, c++11, c++14 +// UNSUPPORTED: libcpp-has-no-threads + +// + +// class future_error +// explicit future_error(future_errc e); // C++17 + +#include + +int main(int, char**) { + auto test_explicit = [](std::future_errc e) -> std::future_error { + return {e}; + // expected-error@-1 {{chosen constructor is explicit in copy-initialization}} + }; + + std::error_code ec = std::make_error_code(std::future_errc::no_state); + std::future_error f(ec); // expected-error {{no matching constructor for initialization of 'std::future_error'}} + + return 0; +} diff --git a/libcxx/test/std/thread/futures/futures.future_error/what.pass.cpp b/libcxx/test/std/thread/futures/futures.future_error/what.pass.cpp --- a/libcxx/test/std/thread/futures/futures.future_error/what.pass.cpp +++ b/libcxx/test/std/thread/futures/futures.future_error/what.pass.cpp @@ -19,7 +19,7 @@ // class future_error -// const char* what() const throw(); +// const char* what() const noexcept; #include #include @@ -30,21 +30,22 @@ int main(int, char**) { { - std::future_error f(std::make_error_code(std::future_errc::broken_promise)); + std::future_error f(std::future_errc::broken_promise); + ASSERT_NOEXCEPT(f.what()); LIBCPP_ASSERT(std::strcmp(f.what(), "The associated promise has been destructed prior " "to the associated state becoming ready.") == 0); } { - std::future_error f(std::make_error_code(std::future_errc::future_already_retrieved)); + std::future_error f(std::future_errc::future_already_retrieved); LIBCPP_ASSERT(std::strcmp(f.what(), "The future has already been retrieved from " "the promise or packaged_task.") == 0); } { - std::future_error f(std::make_error_code(std::future_errc::promise_already_satisfied)); + std::future_error f(std::future_errc::promise_already_satisfied); LIBCPP_ASSERT(std::strcmp(f.what(), "The state of the promise has already been set.") == 0); } { - std::future_error f(std::make_error_code(std::future_errc::no_state)); + std::future_error f(std::future_errc::no_state); LIBCPP_ASSERT(std::strcmp(f.what(), "Operation not permitted on an object without " "an associated state.") == 0); }