Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement LWG-3843 std::expected<T,E>::value() & assumes E is copy constructible #3737

Merged
merged 8 commits into from
Jun 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 17 additions & 3 deletions stl/inc/expected
Original file line number Diff line number Diff line change
Expand Up @@ -636,13 +636,25 @@ public:
_Throw_bad_expected_access_lv();
}
_NODISCARD constexpr const _Ty&& value() const&& {
// TRANSITION, DevCom-1638273 and LLVM-53224
static_assert(
is_copy_constructible_v<_Err>, "is_copy_constructible_v<E> must be true. N4950 [expected.object.obs]/11");
static_assert(is_constructible_v<_Err, const _Err&&>,
"is_constructible_v<E, const E&&> must be true. N4950 [expected.object.obs]/11");

if (_Has_value) {
return _STD move(_Value);
}

_Throw_bad_expected_access_rv();
}
_NODISCARD constexpr _Ty&& value() && {
// TRANSITION, DevCom-1638273 and LLVM-53224
static_assert(
is_copy_constructible_v<_Err>, "is_copy_constructible_v<E> must be true. N4950 [expected.object.obs]/11");
static_assert(
is_move_constructible_v<_Err>, "is_constructible_v<E, E&&> must be true. N4950 [expected.object.obs]/11");

if (_Has_value) {
return _STD move(_Value);
}
Expand Down Expand Up @@ -1133,9 +1145,6 @@ private:
[[noreturn]] void _Throw_bad_expected_access_lv() const {
_THROW(bad_expected_access{_Unexpected});
}
[[noreturn]] void _Throw_bad_expected_access_lv() {
_THROW(bad_expected_access{_Unexpected});
}
[[noreturn]] void _Throw_bad_expected_access_rv() const {
_THROW(bad_expected_access{_STD move(_Unexpected)});
}
Expand Down Expand Up @@ -1414,6 +1423,11 @@ public:
}
}
constexpr void value() && {
// per LWG-3940
// TRANSITION, DevCom-1638273 and LLVM-53224
static_assert(is_copy_constructible_v<_Err>, "is_copy_constructible_v<E> must be true");
static_assert(is_move_constructible_v<_Err>, "is_move_constructible_v<E> must be true");

if (!_Has_value) {
_Throw_bad_expected_access_rv();
}
Expand Down
6 changes: 6 additions & 0 deletions tests/libcxx/expected_results.txt
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,9 @@ std/ranges/range.factories/range.iota.view/iterator/member_typedefs.compile.pass
# libc++ doesn't implement LWG-3836
std/utilities/expected/expected.expected/ctor/ctor.u.pass.cpp FAIL

# libc++ doesn't implement LWG-3843
std/utilities/expected/expected.expected/observers/value.pass.cpp FAIL

# libc++ doesn't implement LWG-3857
std/strings/string.view/string.view.cons/from_range.pass.cpp FAIL
std/strings/string.view/string.view.cons/from_string1.compile.fail.cpp FAIL
Expand All @@ -152,6 +155,9 @@ std/utilities/memory/specialized.algorithms/uninitialized.fill.n/ranges_uninitia
std/utilities/memory/specialized.algorithms/uninitialized.move/ranges_uninitialized_move.pass.cpp FAIL
std/utilities/memory/specialized.algorithms/uninitialized.move/ranges_uninitialized_move_n.pass.cpp FAIL

# libc++ doesn't speculatively implement LWG-3940
std/utilities/expected/expected.void/observers/value.pass.cpp FAIL

# libc++ doesn't implement P1957R2 "Converting from `T*` to `bool` should be considered narrowing"
std/utilities/variant/variant.variant/variant.assign/conv.pass.cpp FAIL
std/utilities/variant/variant.variant/variant.assign/T.pass.cpp FAIL
Expand Down
76 changes: 76 additions & 0 deletions tests/std/tests/P0323R12_expected/test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2060,6 +2060,81 @@ struct Data {
};
static_assert(((void) expected<void, Data>{unexpect, {1, 2, 3}}, true));

void test_lwg_3843() {
struct Indicator {
Indicator() = default;

Indicator(Indicator& other) noexcept : count(other.count + 256) {}
Indicator(const Indicator& other) noexcept : count(other.count + 1024) {}

Indicator(Indicator&& other) noexcept : count(other.count + 1) {}
Indicator(const Indicator&& other) noexcept : count(other.count + 16) {}

Indicator& operator=(const Indicator&) = default;
Indicator& operator=(Indicator&&) = default;

int count = 0;
};

{
expected<int, Indicator> exv{unexpect};
assert(exv.error().count == 0);

try {
(void) exv.value();
} catch (const bad_expected_access<Indicator>& e) {
assert(e.error().count == 1025);
}

try {
(void) as_const(exv).value();
} catch (const bad_expected_access<Indicator>& e) {
assert(e.error().count == 1025);
}

try {
(void) move(exv).value();
} catch (const bad_expected_access<Indicator>& e) {
assert(e.error().count == 2);
}

try {
(void) move(as_const(exv)).value();
} catch (const bad_expected_access<Indicator>& e) {
assert(e.error().count == 17);
}
}

{
expected<void, Indicator> exv{unexpect};
assert(exv.error().count == 0);

try {
(void) exv.value();
} catch (const bad_expected_access<Indicator>& e) {
assert(e.error().count == 1025);
}

try {
(void) as_const(exv).value();
} catch (const bad_expected_access<Indicator>& e) {
assert(e.error().count == 1025);
}

try {
(void) move(exv).value();
} catch (const bad_expected_access<Indicator>& e) {
assert(e.error().count == 2);
}

try {
(void) move(as_const(exv)).value();
} catch (const bad_expected_access<Indicator>& e) {
assert(e.error().count == 1025);
}
}
}

int main() {
test_unexpected::test_all();
static_assert(test_unexpected::test_all());
Expand All @@ -2075,4 +2150,5 @@ int main() {
static_assert(is_convertible_v<bad_expected_access<int>*, exception*>);

test_reinit_regression();
test_lwg_3843();
}