This is an archive of the discontinued LLVM Phabricator instance.

[libc++] Add concepts that ensure a given iterator meets the syntactic requirements
ClosedPublic

Authored by philnik on May 12 2023, 3:32 PM.

Details

Summary

These concepts are used to ensure valid iterators are passed to PSTL algorithms, but can also be used for other interfaces.

Diff Detail

Event Timeline

philnik created this revision.May 12 2023, 3:32 PM
Herald added a project: Restricted Project. · View Herald TranscriptMay 12 2023, 3:32 PM
philnik requested review of this revision.May 12 2023, 3:32 PM
Herald added a project: Restricted Project. · View Herald TranscriptMay 12 2023, 3:32 PM
Herald added a reviewer: Restricted Project. · View Herald Transcript
ldionne added inline comments.May 15 2023, 9:03 AM
libcxx/include/__iterator/check_iterators.h
51 ↗(On Diff #521822)

I am a bit uneasy about adding yet another way to check for iterator-ness, since we already have __is_cpp17_forward_iterator and the forward_iterator concept in C++20. Let's discuss this some more later when we have more time.

ldionne requested changes to this revision.May 16 2023, 11:59 AM
ldionne added inline comments.
libcxx/include/__iterator/check_iterators.h
51 ↗(On Diff #521822)

Ok, suggestion per our discussion:

  1. Refactor __is_cpp17_bidirectional_iterator & friends to properly convey that they are just checking the iterator category and they're not acting like a proper concept.
  2. Introduce __cpp17_bidirectional_iterator as a inline constexpr bool that check these concepts (suggestion: move those to libcxx/include/__iterator/concepts.h or similar)
  3. Then do static_assert(__cpp17_bidirectional_iterator<_It>) in the PSTL algorithms

Does that sound like a plan? I think what I mostly disliked from this as-is is that we were introducing what looked like yet another way to tell whether a type satisfied an iterator concept for C++17, but after (1) this shouldn't be an issue anymore.

We could make (2) be a concept instead of constexpr bool in C++20 so that we get nicer diagnostics for users on C++20 when the static assert fails. I'd also be OK with implementing (2) as concepts directly and enabling those QOI checks only in C++20 mode. After all, those are concept checks and concept checks shouldn't really be expected before concepts exist.

This revision now requires changes to proceed.May 16 2023, 11:59 AM
EricWF added a subscriber: EricWF.May 17 2023, 8:51 AM

I'm going to try rolling this change out at Google to see the scope of the breakage, how many bugs it finds, and what class of bugs those are.
I'll report back in a couple of days.

philnik updated this revision to Diff 523193.May 17 2023, 3:34 PM

Address comments

philnik updated this revision to Diff 523374.May 18 2023, 7:34 AM

Fix formatting

ldionne added inline comments.May 18 2023, 9:56 AM
libcxx/include/CMakeLists.txt
427

Commit message: Add functions -- those are not functions anymore.

libcxx/include/__iterator/cpp17_iterator_concepts.h
159

And throughout?

libcxx/test/libcxx/algorithms/cpp17_iterator_concepts.verify.cpp
10

This comment needs to be updated.

12

You're not testing bidirectional and random access.

philnik retitled this revision from [libc++] Add functions that ensure a given iterator meets the syntactic requirements to [libc++] Add concepts that ensure a given iterator meets the syntactic requirements.May 19 2023, 4:25 PM
philnik edited the summary of this revision. (Show Details)
philnik updated this revision to Diff 524113.May 21 2023, 10:46 AM
philnik marked 3 inline comments as done.

Try to fix CI

philnik updated this revision to Diff 524458.May 22 2023, 1:31 PM

Try to fix CI

philnik updated this revision to Diff 524893.May 23 2023, 2:45 PM

Generate files

I'm going to try rolling this change out at Google to see the scope of the breakage, how many bugs it finds, and what class of bugs those are.
I'll report back in a couple of days.

Did you have a chance to conduct this experiment?

philnik updated this revision to Diff 529035.Jun 6 2023, 1:57 PM
philnik marked an inline comment as done.

Try to fix CI

philnik updated this revision to Diff 529107.Jun 6 2023, 5:29 PM

Try to fix CI

ldionne accepted this revision.Jun 7 2023, 8:17 AM

LGTM w/ comments

libcxx/include/__iterator/cpp17_iterator_concepts.h
49

Per http://eel.is/c++draft/utility.arg.requirements#1.6, a = b must work if b is const. Needs a test too.

56–57

I'd suggest we use _Tp instead of _Iter here, this isn't limited to iterators.

70
This revision is now accepted and ready to land.Jun 7 2023, 8:17 AM
philnik updated this revision to Diff 529423.Jun 7 2023, 1:42 PM

Try to fix CI

philnik updated this revision to Diff 529689.Jun 8 2023, 11:47 AM
philnik marked 3 inline comments as done.

Try to fix CI

philnik added inline comments.Jun 8 2023, 1:17 PM
libcxx/include/__iterator/cpp17_iterator_concepts.h
70

clang-format is doing this for some reason, and I don't really feel like adding a // clang-format off for this tiny nit.

philnik updated this revision to Diff 529779.Jun 8 2023, 4:43 PM

Try to fix CI