This is an archive of the discontinued LLVM Phabricator instance.

[libc++] Implement LWG3843 (std::expected<T,E>::value() & assumes E is copy constructible)
ClosedPublic

Authored by yronglin on Jun 29 2023, 9:50 AM.

Details

Summary

Implement LWG3843 (std::expected<T,E>::value() & assumes E is copy constructible)
https://wg21.link/LWG3843

Diff Detail

Event Timeline

yronglin created this revision.Jun 29 2023, 9:50 AM
Herald added a project: Restricted Project. · View Herald TranscriptJun 29 2023, 9:50 AM
yronglin requested review of this revision.Jun 29 2023, 9:50 AM
Herald added a project: Restricted Project. · View Herald TranscriptJun 29 2023, 9:50 AM
Herald added a reviewer: Restricted Project. · View Herald Transcript
yronglin edited the summary of this revision. (Show Details)Jun 29 2023, 9:52 AM
yronglin added reviewers: ldionne, huixie90, philnik.
crtrott added a subscriber: crtrott.Jul 4 2023, 8:20 AM

LGTM, but I haven't been here long enough to feel like I can accept this on behalf of libc++ team :-)

LGTM, but I haven't been here long enough to feel like I can accept this on behalf of libc++ team :-)

Many thanks for your review! @crtrott let's wait for the sign off from libc++ team.

Thanks for the patch! I really appreciate the added .verify.pass.cpp tests. I have a few questions but this basically LGTM.

libcxx/include/__expected/expected.h
565

I don't think it's possible to test this added as_const, since IIUC this is a no-op given that this member function has a const& qualifier. Right?

573

I don't think we're testing the added as_const here. I think we could test this by using an error type with two constructors:

Error(Error const&);
Error(Error&);

We'd make sure that the first one got called, not the second one. Does that make sense?

libcxx/test/libcxx/utilities/expected/expected.expected/value.observers.verify.cpp
124–129

Right?

yronglin updated this revision to Diff 537426.Jul 5 2023, 10:42 AM
yronglin marked 2 inline comments as done.

Address comments, and undo the change of ReleastNotes.rst

Thanks for your review @ldionne !

libcxx/include/__expected/expected.h
565

Yes, as you said, this member function has a const& qualifier.

573

Agree. I have add a test case to check this.

libcxx/test/libcxx/utilities/expected/expected.expected/value.observers.verify.cpp
124–129

Yes!

ldionne added inline comments.Jul 5 2023, 2:23 PM
libcxx/test/std/utilities/expected/expected.expected/observers/value.pass.cpp
23–39

I don't think this tests what you intend to test. If you remove as_const in the & overload, does this test suddenly fail? I don't think it would, since you're still providing the declaration of the member function.

Instead, I think what you want is this:

bool ref_called = true;
bool const_ref_called = true;

struct Foo {
  Foo(Foo const&) { const_ref_called = true; }
  Foo(Foo&) { ref_called = true; }
};

int main() {
  std::expected<int, Foo> e;
  try {
    e.value();
  catch (std::bad_expected_access const&) {
    assert(const_ref_called);
  }
}

Obviously this is a poor way to test this, but I think you see the point I'm trying to make?

yronglin updated this revision to Diff 537690.Jul 6 2023, 6:26 AM

Address comments.

yronglin marked an inline comment as done.Jul 6 2023, 6:27 AM

Thanks for your review @ldionne !

libcxx/test/std/utilities/expected/expected.expected/observers/value.pass.cpp
23–39

Good catch! It's my mistake, I have update the test case.

ldionne accepted this revision.Jul 7 2023, 2:27 PM

Thanks!

This revision is now accepted and ready to land.Jul 7 2023, 2:27 PM
yronglin marked an inline comment as done.Jul 7 2023, 8:55 PM

Thanks!

Thanks for your review!