|
| 1 | +//===----------------------------------------------------------------------===// |
| 2 | +// |
| 3 | +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | +// See https://llvm.org/LICENSE.txt for license information. |
| 5 | +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | +// |
| 7 | +//===----------------------------------------------------------------------===// |
| 8 | + |
| 9 | +// UNSUPPORTED: c++03, c++11, c++14, c++17, c++20 |
| 10 | + |
| 11 | +// Test the mandates |
| 12 | +// constexpr T& value() & ; |
| 13 | +// constexpr const T& value() const &; |
| 14 | +// Mandates: is_copy_constructible_v<E> is true. |
| 15 | + |
| 16 | +// constexpr T&& value() &&; |
| 17 | +// constexpr const T&& value() const &&; |
| 18 | +// Mandates: is_copy_constructible_v<E> is true and is_constructible_v<E, decltype(std::move(error()))> is true. |
| 19 | + |
| 20 | +#include <expected> |
| 21 | +#include <utility> |
| 22 | + |
| 23 | +#include "MoveOnly.h" |
| 24 | + |
| 25 | +struct CopyConstructible { |
| 26 | + constexpr CopyConstructible() = default; |
| 27 | + constexpr CopyConstructible(const CopyConstructible&) = default; |
| 28 | +}; |
| 29 | + |
| 30 | +struct CopyConstructibleButNotMoveConstructible { |
| 31 | + constexpr CopyConstructibleButNotMoveConstructible() = default; |
| 32 | + constexpr CopyConstructibleButNotMoveConstructible(const CopyConstructibleButNotMoveConstructible&) = default; |
| 33 | + constexpr CopyConstructibleButNotMoveConstructible(CopyConstructibleButNotMoveConstructible&&) = delete; |
| 34 | + constexpr CopyConstructibleButNotMoveConstructible(const CopyConstructibleButNotMoveConstructible&&) = delete; |
| 35 | +}; |
| 36 | + |
| 37 | +struct CopyConstructibleAndMoveConstructible { |
| 38 | + constexpr CopyConstructibleAndMoveConstructible() = default; |
| 39 | + constexpr CopyConstructibleAndMoveConstructible(const CopyConstructibleAndMoveConstructible&) = default; |
| 40 | + constexpr CopyConstructibleAndMoveConstructible(CopyConstructibleAndMoveConstructible&&) = default; |
| 41 | +}; |
| 42 | + |
| 43 | +// clang-format off |
| 44 | +void test() { |
| 45 | + |
| 46 | + // Test & overload |
| 47 | + { |
| 48 | + // is_copy_constructible_v<E> is true. |
| 49 | + { |
| 50 | + std::expected<int, CopyConstructible> e; |
| 51 | + [[maybe_unused]] auto val = e.value(); |
| 52 | + } |
| 53 | + |
| 54 | + // is_copy_constructible_v<E> is false. |
| 55 | + { |
| 56 | + std::expected<int, MoveOnly> e; |
| 57 | + [[maybe_unused]] auto val = e.value(); |
| 58 | + // expected-error-re@*:* {{{{(static_assert|static assertion)}} failed {{.*}}error_type has to be copy constructible}} |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + // Test const& overload |
| 63 | + { |
| 64 | + // is_copy_constructible_v<E> is true. |
| 65 | + { |
| 66 | + const std::expected<int, CopyConstructible> e; |
| 67 | + [[maybe_unused]] auto val = e.value(); |
| 68 | + } |
| 69 | + |
| 70 | + // is_copy_constructible_v<E> is false. |
| 71 | + { |
| 72 | + const std::expected<int, MoveOnly> e; |
| 73 | + [[maybe_unused]] auto val = e.value(); |
| 74 | + // expected-error-re@*:* {{{{(static_assert|static assertion)}} failed {{.*}}error_type has to be copy constructible}} |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + // Test && overload |
| 79 | + { |
| 80 | + // is_copy_constructible_v<E> is false. |
| 81 | + { |
| 82 | + std::expected<int, MoveOnly> e; |
| 83 | + [[maybe_unused]] auto val = std::move(e).value(); |
| 84 | + // expected-error-re@*:* {{{{(static_assert|static assertion)}} failed {{.*}}error_type has to be both copy constructible and constructible from decltype(std::move(error()))}} |
| 85 | + } |
| 86 | + |
| 87 | + // is_copy_constructible_v<E> is true and is_constructible_v<E, decltype(std::move(error()))> is true. |
| 88 | + { |
| 89 | + std::expected<int, CopyConstructibleAndMoveConstructible> e; |
| 90 | + [[maybe_unused]] auto val = std::move(e).value(); |
| 91 | + } |
| 92 | + |
| 93 | + // is_copy_constructible_v<E> is true and is_constructible_v<E, decltype(std::move(error()))> is false. |
| 94 | + { |
| 95 | + std::expected<int, CopyConstructibleButNotMoveConstructible> e; |
| 96 | + [[maybe_unused]] auto val = std::move(e).value(); |
| 97 | + // expected-error-re@*:* {{{{(static_assert|static assertion)}} failed {{.*}}error_type has to be both copy constructible and constructible from decltype(std::move(error()))}} |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + // Test const&& overload |
| 102 | + { |
| 103 | + // is_copy_constructible_v<E> is false. |
| 104 | + { |
| 105 | + const std::expected<int, MoveOnly> e; |
| 106 | + [[maybe_unused]] auto val = std::move(e).value(); |
| 107 | + // expected-error-re@*:* {{{{(static_assert|static assertion)}} failed {{.*}}error_type has to be both copy constructible and constructible from decltype(std::move(error()))}} |
| 108 | + } |
| 109 | + |
| 110 | + // is_copy_constructible_v<E> is true and is_constructible_v<E, decltype(std::move(error()))> is true. |
| 111 | + { |
| 112 | + const std::expected<int, CopyConstructibleAndMoveConstructible> e; |
| 113 | + [[maybe_unused]] auto val = std::move(e).value(); |
| 114 | + } |
| 115 | + |
| 116 | + // is_copy_constructible_v<E> is true and is_constructible_v<E, decltype(std::move(error()))> is false. |
| 117 | + { |
| 118 | + const std::expected<int, CopyConstructibleButNotMoveConstructible> e; |
| 119 | + [[maybe_unused]] auto val = std::move(e).value(); |
| 120 | + // expected-error-re@*:* {{{{(static_assert|static assertion)}} failed {{.*}}error_type has to be both copy constructible and constructible from decltype(std::move(error()))}} |
| 121 | + } |
| 122 | + } |
| 123 | +// These diagnostics happen when we try to construct bad_expected_access from the non copy-constructible error type. |
| 124 | +#ifndef _LIBCPP_HAS_NO_EXCEPTIONS |
| 125 | + // expected-error-re@*:* {{call to deleted constructor of{{.*}}}} |
| 126 | + // expected-error-re@*:* {{call to deleted constructor of{{.*}}}} |
| 127 | + // expected-error-re@*:* {{call to deleted constructor of{{.*}}}} |
| 128 | + // expected-error-re@*:* {{call to deleted constructor of{{.*}}}} |
| 129 | +#endif |
| 130 | +} |
| 131 | +// clang-format on |
0 commit comments