This is an archive of the discontinued LLVM Phabricator instance.

[libc++] Test nodiscard attributes of node-handle::empty().
AbandonedPublic

Authored by ldionne on Apr 12 2021, 8:40 AM.

Details

Reviewers
Mordante
curdeius
Group Reviewers
Restricted Project

Diff Detail

Event Timeline

curdeius requested review of this revision.Apr 12 2021, 8:40 AM
curdeius created this revision.
Herald added a project: Restricted Project. · View Herald TranscriptApr 12 2021, 8:40 AM
Herald added a reviewer: Restricted Project. · View Herald Transcript
Quuxplusone added inline comments.
libcxx/include/__node_handle
16

Nit: suggest removing this comment line, as it'll just bit-rot.

46

// nodiscard since C++20
Also, shouldn't this whole comment block use /* */ comments? Seeing nested // // comments is a bit weird. Unless we use // comments on some other synopsis comments already, perhaps?

libcxx/test/std/containers/container.node/node_handle.empty.nodiscard.verify.cpp
17

extract and empty were added in C++17, so we should have a C++17 test for this. It looks like we mark empty as nodiscard only "AFTER_CXX17"; we should also test that it's marked, as an extension, in C++17 mode. We have existing tests for nodiscard extensions in
test/libcxx/diagnostics/nodiscard_extensions.pass.cpp
test/libcxx/diagnostics/nodiscard_extensions.verify.cpp
and I'd like to see these tests added in there for consistency, not scattered here, if humanly possible.

26

I suggest populating this container, to avoid the appearance of trying to extract from an empty container. (Even if this test is compile-only, which I think it is, despite the main routine.)
I suggest:

template<class Container>
void test_one(Container c) {
    auto n = c.extract(c.begin());
    n.empty();  // expected-warning 8{{ignoring return value of function}}
}

void test() {
    test_one(std::set<int>{1});
    test_one(std::multiset<int>{1});
    test_one(std::map<int, int>{{1, 1}});
    test_one(std::multimap<int, int>{{1, 1}});
    test_one(std::unordered_set<int>{1});
    test_one(std::unordered_multiset<int>{1});
    test_one(std::unordered_map<int, int>{{1, 1}});
    test_one(std::unordered_multimap<int, int>{{1, 1}});
}

(and no main routine).

However, see my other comment.

D99895 is related, btw (and I just landed it, so rebase before attempting to mess with libcxx/test/libcxx/diagnostics if you're going that route).

curdeius updated this revision to Diff 336937.Apr 12 2021, 12:31 PM
curdeius marked 3 inline comments as done.
  • Apply review comments.

FWIW, LGTM at this point except that I still think I'd prefer to see this tested in test/libcxx/diagnostics/nodiscard_extensions.verify.cpp.

FWIW, LGTM at this point except that I still think I'd prefer to see this tested in test/libcxx/diagnostics/nodiscard_extensions.verify.cpp.

Even if it's not an extension?

libcxx/include/__node_handle
46

Done and used /* */.

libcxx/test/std/containers/container.node/node_handle.empty.nodiscard.verify.cpp
17

Not sure if I understand you correctly, you want to have all the tests there or only those for the C++17 extension?

Also, adding [[nodiscard]] as an extension would mean having this ugliness (or adding yet another macro):

#if _LIBCPP_STD_VER <= 17
_LIBCPP_NODISCARD_EXT
#endif
_LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY
bool empty() const { return __ptr_ == nullptr; }
libcxx/test/std/containers/container.node/node_handle.empty.nodiscard.verify.cpp
17

IIUC, if the user defines -D_LIBCPP_ENABLE_NODISCARD=1, then _LIBCPP_NODISCARD_AFTER_CXX17 expands to [[nodiscard]] in all supported modes (yes even C++11/14/17).

Basically, _LIBCPP_NODISCARD_AFTER_CXX17 already implies "WG21 has decided that making this [[nodiscard]] is a good idea," which implies "making this [[nodiscard]] is always a good idea." So it already implies the extension. We just need tests for the extension.

I had intended "all the tests there," but I'm not confident in that — I now think it's likely that your tests here should stay here, but the C++17 extension tests should go over there. You'll have to take a look at those files and see how they're currently organized.

curdeius added inline comments.Apr 13 2021, 2:39 AM
libcxx/test/std/containers/container.node/node_handle.empty.nodiscard.verify.cpp
17

Actually, I'm inclined to add this as an extension in another patch. First, I'd like to add missing tests for P0600.

Mordante accepted this revision as: Mordante.Apr 18 2021, 5:01 AM
Mordante added a subscriber: Mordante.

Since this is mandated I think it makes sense to add the test here. IMO it doesn't belong to the extension tests.

libcxx/test/std/containers/container.node/node_handle.empty.nodiscard.verify.cpp
26

This is indeed a compile-time test. It tests whether the expected compiler diagnostics are issues. Basically this is our typical clang unit test.

ldionne commandeered this revision.Sep 7 2021, 12:27 PM
ldionne added a reviewer: curdeius.
ldionne added a subscriber: ldionne.

I wasn't aware of this change and I did the same thing in D109027. Apologies for the duplicated effort.

ldionne abandoned this revision.Sep 7 2021, 12:28 PM

Abandoning since this was done in a different change, to clear up the review queue and help avoid these issues!