Commit d2cb198f authored by Marek Kurdej's avatar Marek Kurdej Committed by Louis Dionne
Browse files

[libc++] Make future_error constructor standard-compliant

This patch removes the non compliant constructor of std::future_error
and adds the standards compliant constructor in C++17 instead.

Note that we can't support the constructor as an extension in all
standard modes because it uses delegating constructors, which require
C++11. We could in theory support the constructor as an extension in
C++11 and C++14 only, however I believe it is acceptable not to do that
since I expect the breakage from this patch will be minimal.

If it turns out that more code than we expect is broken by this, we can
reconsider that decision.

This was found during D99515.

Differential Revision: https://reviews.llvm.org/D99567


Co-authored-by: default avatarLouis Dionne <ldionne.2@gmail.com>
parent 2c49311d
Loading
Loading
Loading
Loading
+3 −0
Original line number Diff line number Diff line
@@ -87,6 +87,9 @@ Deprecations and Removals
  warning). ``_LIBCPP_ENABLE_ASSERTIONS`` will be removed entirely in the next release and setting it will become an
  error. See :ref:`the hardening documentation <using-hardening-modes>` for more details.

- The non-conforming constructor ``std::future_error(std::error_code)`` has been removed. Please use the
  ``std::future_error(std::future_errc)`` constructor provided in C++17 instead.

Upcoming Deprecations and Removals
----------------------------------

+23 −16
Original line number Diff line number Diff line
@@ -44,14 +44,15 @@ error_condition make_error_condition(future_errc e) noexcept;

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); // since C++17

    const error_code& code() const noexcept;
    const char*       what() const noexcept;

private:
    error_code ec_;             // exposition only
};

template <class R>
@@ -516,12 +517,25 @@ make_error_condition(future_errc __e) _NOEXCEPT
    return error_condition(static_cast<int>(__e), future_category());
}

_LIBCPP_NORETURN inline _LIBCPP_HIDE_FROM_ABI
#ifndef _LIBCPP_HAS_NO_EXCEPTIONS
_LIBCPP_AVAILABILITY_FUTURE_ERROR
#endif
void __throw_future_error(future_errc __ev);

class _LIBCPP_EXPORTED_FROM_ABI _LIBCPP_AVAILABILITY_FUTURE_ERROR future_error
    : public logic_error
{
    error_code __ec_;

    future_error(error_code);
    friend void __throw_future_error(future_errc);
    template <class> friend class promise;

public:
    future_error(error_code __ec);
#if _LIBCPP_STD_VER >= 17
    _LIBCPP_HIDE_FROM_ABI explicit future_error(future_errc __ec) : future_error(std::make_error_code(__ec)) {}
#endif

    _LIBCPP_INLINE_VISIBILITY
    const error_code& code() const _NOEXCEPT {return __ec_;}
@@ -530,10 +544,7 @@ public:
    ~future_error() _NOEXCEPT override;
};

_LIBCPP_NORETURN inline _LIBCPP_INLINE_VISIBILITY
#ifndef _LIBCPP_HAS_NO_EXCEPTIONS
_LIBCPP_AVAILABILITY_FUTURE_ERROR
#endif
// Declared above std::future_error
void __throw_future_error(future_errc __ev)
{
#ifndef _LIBCPP_HAS_NO_EXCEPTIONS
@@ -1359,9 +1370,7 @@ promise<_Rp>::~promise()
    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))));
        __state_->__release_shared();
    }
}
@@ -1502,9 +1511,7 @@ promise<_Rp&>::~promise()
    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))));
        __state_->__release_shared();
    }
}
+1 −3
Original line number Diff line number Diff line
@@ -204,9 +204,7 @@ promise<void>::~promise()
    {
#ifndef _LIBCPP_HAS_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(future_errc::broken_promise)));
#endif // _LIBCPP_HAS_NO_EXCEPTIONS
        __state_->__release_shared();
    }
+31 −37
Original line number Diff line number Diff line
@@ -9,48 +9,42 @@
// UNSUPPORTED: no-threads

// <future>

//
// class future_error
//     future_error(error_code __ec);  // exposition only
//     explicit future_error(future_errc _Ev) : __ec_(make_error_code(_Ev)) {} // C++17

// const error_code& code() const throw();
//
// const error_code& code() const noexcept;

#include <future>
#include <cassert>
#include <future>
#include <utility>

#include "test_macros.h"

int main(int, char**)
{
    {
        std::error_code ec = std::make_error_code(std::future_errc::broken_promise);
        std::future_error f(ec);
        assert(f.code() == ec);
    }
    {
        std::error_code ec = std::make_error_code(std::future_errc::future_already_retrieved);
        std::future_error f(ec);
        assert(f.code() == ec);
    }
int main(int, char**) {
  ASSERT_NOEXCEPT(std::declval<std::future_error const&>().code());
  ASSERT_SAME_TYPE(decltype(std::declval<std::future_error const&>().code()), std::error_code const&);

  // Before C++17, we can't construct std::future_error directly in a standards-conforming way
#if TEST_STD_VER >= 17
  {
        std::error_code ec = std::make_error_code(std::future_errc::promise_already_satisfied);
        std::future_error f(ec);
        assert(f.code() == ec);
    std::future_error const f(std::future_errc::broken_promise);
    std::error_code const& code = f.code();
    assert(code == std::make_error_code(std::future_errc::broken_promise));
  }
  {
        std::error_code ec = std::make_error_code(std::future_errc::no_state);
        std::future_error f(ec);
        assert(f.code() == ec);
    std::future_error const f(std::future_errc::future_already_retrieved);
    std::error_code const& code = f.code();
    assert(code == std::make_error_code(std::future_errc::future_already_retrieved));
  }
#if TEST_STD_VER > 14
  {
        std::future_error f(std::future_errc::broken_promise);
        assert(f.code() == std::make_error_code(std::future_errc::broken_promise));
    std::future_error const f(std::future_errc::promise_already_satisfied);
    std::error_code const& code = f.code();
    assert(code == std::make_error_code(std::future_errc::promise_already_satisfied));
  }
  {
        std::future_error f(std::future_errc::no_state);
        assert(f.code() == std::make_error_code(std::future_errc::no_state));
    std::future_error const f(std::future_errc::no_state);
    std::error_code const& code = f.code();
    assert(code == std::make_error_code(std::future_errc::no_state));
  }
#endif

+44 −0
Original line number Diff line number Diff line
//===----------------------------------------------------------------------===//
//
// 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: no-threads
// UNSUPPORTED: c++03, c++11, c++14

// <future>

// class future_error
//
// explicit future_error(future_errc e); // since C++17

#include <cassert>
#include <future>
#include <type_traits>

int main(int, char**) {
  {
    std::future_error f(std::future_errc::broken_promise);
    assert(f.code() == std::make_error_code(std::future_errc::broken_promise));
  }
  {
    std::future_error f(std::future_errc::future_already_retrieved);
    assert(f.code() == std::make_error_code(std::future_errc::future_already_retrieved));
  }
  {
    std::future_error f(std::future_errc::promise_already_satisfied);
    assert(f.code() == std::make_error_code(std::future_errc::promise_already_satisfied));
  }
  {
    std::future_error f(std::future_errc::no_state);
    assert(f.code() == std::make_error_code(std::future_errc::no_state));
  }

  // Make sure the constructor is explicit
  static_assert(!std::is_convertible_v<std::future_errc, std::future_error>);

  return 0;
}
Loading