-
Notifications
You must be signed in to change notification settings - Fork 15.1k
[libc++] P3168R2 Give std::optional Range Support #146491
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
Conversation
…<optional<_T>> = range_format::disabled
…t_macro_components.py
|
Thank you for submitting a Pull Request (PR) to the LLVM Project! This PR will be automatically labeled and the relevant teams will be notified. If you wish to, you can add reviewers by using the "Reviewers" section on this page. If this is not working for you, it is probably because you do not have write permissions for the repository. In which case you can instead tag reviewers by name in a comment by using If you have received no comments on your PR for a week, you can request a review by "ping"ing the PR by adding a comment “Ping”. The common courtesy "ping" rate is once a week. Please remember that you are asking for valuable time from other developers. If you have further questions, they may be answered by the LLVM GitHub User Guide. You can also ask questions in a comment on this PR, on the LLVM Discord or on the forums. |
libcxx/include/optional
Outdated
| _LIBCPP_HIDE_FROM_ABI iterator begin() { | ||
| if (!this->has_value()) | ||
| return this->end(); | ||
| return iterator(reinterpret_cast<_Tp*>(this->__get())); | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
constexpr is missing, and reinterpret_cast is incompatible with constant evaluation. Ditto below.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Will use static_cast then
| int main() { | ||
| std::optional<int> val = 2; | ||
| for (const auto& elem : val) { | ||
| std::cout << elem << std::endl; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should assert here.
Also, there should be a file testing the properties of the iterator types.
…onal> Co-authored-by: A. Jiang <[email protected]>
Co-authored-by: A. Jiang <[email protected]>
_LIBCPP_HIDE_FROM_ABI constexpr reference operator*() const {
if (__ptr_ == nullptr)
std::__throw_runtime_error("deferencering end iterator");
return *__ptr_;
}
_LIBCPP_HIDE_FROM_ABI constexpr pointer operator->() const {
if (__ptr_ == nullptr)
std::__throw_runtime_error("deferencering end iterator");
return *__ptr_;
}Should these functions return |
|
libcxx/include/optional
Outdated
| _LIBCPP_HIDE_FROM_ABI constexpr pointer operator->() const { return *__ptr_; } | ||
|
|
||
| _LIBCPP_HIDE_FROM_ABI constexpr __optional_iterator& operator++() { | ||
| __ptr_ = nullptr; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This doesn't meet the Cpp17RandomAccessIterator requirements.
libcxx/test/std/utilities/optional/optional.range/iteration.pass.cpp
Outdated
Show resolved
Hide resolved
libcxx/test/std/utilities/optional/optional.range/satisfies_range_concept.pass.cpp
Outdated
Show resolved
Hide resolved
libcxx/include/optional
Outdated
| constexpr auto format_kind<optional<_T>> = range_format::disabled; | ||
|
|
||
| template <typename _Tp> | ||
| class __optional_iterator { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Couldn't we just use __wrap_iter?
| auto end = val.end(); | ||
| TEST_DOES_NOT_THROW(*end); | ||
| return 0; | ||
| } No newline at end of file |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Missing newline.
| // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
| // | ||
| //===----------------------------------------------------------------------===// | ||
|
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please add comments in all tests what they are supported to do.
| #include <cassert> | ||
| #include <optional> | ||
|
|
||
| int main() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
All tests should also be constant evaluated.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please don't use .fail.cpp.
| #include <ranges> | ||
|
|
||
| int main() { | ||
| static_assert(std::ranges::range<std::optional<int>>, "std::ranges::range<std::optional<int>> is false"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You don't have to state the obvious. If there is not much to add, just don't add a message.
libcxx/include/optional
Outdated
| template <class _T> | ||
| constexpr auto format_kind<optional<_T>> = range_format::disabled; | ||
|
|
||
| template <typename _Tp> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please add tests to make sure that the iterator fulfills all the iterator requirements.
| template <class _T> | ||
| constexpr bool ranges::enable_view<optional<_T>> = true; | ||
|
|
||
| template <class _T> | ||
| constexpr auto format_kind<optional<_T>> = range_format::disabled; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These don't have tests AFAICT.
libcxx/include/optional
Outdated
| # if _LIBCPP_STD_VER >= 26 | ||
|
|
||
| template <typename _Tp> | ||
| class __optional_iterator; | ||
|
|
||
| # endif // _LIBCPP_STD_VER >= 26 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why is this needed?
libcxx/include/optional
Outdated
| # include <__concepts/invocable.h> | ||
| # include <__config> | ||
| # include <__exception/exception.h> | ||
| # include <__format/format_context.h> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why is this include needed?
libcxx/include/optional
Outdated
| # if _LIBCPP_STD_VER >= 26 | ||
|
|
||
| using iterator = __optional_iterator<_Tp>; | ||
| using const_iterator = __optional_iterator<_Tp>; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The reference type for const_iterator is const _Tp&.
libcxx/include/optional
Outdated
| _LIBCPP_HIDE_FROM_ABI constexpr pointer operator->() const { return *__it_.operator->(); } | ||
|
|
||
| _LIBCPP_HIDE_FROM_ABI constexpr __optional_iterator& operator++() noexcept { | ||
| __it_ = _Base(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Note that std::contiguous_iterator requires std::to_address(iter2) == std::to_address(iter1) + std::iter_difference_t<Iter>(iter2 - iter1) even if iter1 is dereferenceable and iter2 is NON-dereferenceable.
|
is it ready to merge? |
My fault. It seems that all review comments are addressed currently. |
It's nothing |
libcxx/include/optional
Outdated
|
|
||
| template <typename _Tp> | ||
| class __optional_iterator { | ||
| using _Base = __wrap_iter<_Tp*>; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Missing _LIBCPP_NODEBUG.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I meant that we don't need to invent a new iterator at all. We can just use __wrap_iter instead.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
oh, I misunderstood you. I'll use __wrap-iter then
libcxx/include/optional
Outdated
| # if _LIBCPP_STD_VER >= 26 | ||
|
|
||
| using iterator = __optional_iterator<_Tp>; | ||
| using const_iterator = __optional_iterator<const _Tp&>; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Did you mean __optional_iterator<const _Tp>?
This needs tests as well.
libcxx/include/optional
Outdated
| template <typename _Tp> | ||
| _LIBCPP_HIDE_FROM_ABI constexpr _Tp* to_address(const __optional_iterator<_Tp>& __it) noexcept { | ||
| return std::to_address(__it.__it_); | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you explain why adding this function? P3168 doesn't add this this function.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here
Note that std::contiguous_iterator requires std::to_address(iter2) == std::to_address(iter1) + std::iter_difference_t(iter2 - iter1) even if iter1 is dereferenceable and iter2 is NON-dereferenceable.
I thought I would need to add std::to_address for __optional_iterator
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry
libcxx/include/optional
Outdated
| _LIBCPP_HIDE_FROM_ABI constexpr iterator end() { return iterator(__wrap_iter(static_cast<_Tp*>(nullptr))); } | ||
|
|
||
| _LIBCPP_HIDE_FROM_ABI constexpr const_iterator end() const { | ||
| return const_iterator(__wrap_iter(static_cast<const _Tp*>(nullptr))); | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Seems wrong after updating implementation of operator++.
libcxx/test/std/utilities/optional/optional.range/enable_ranges_view.pass.cpp
Show resolved
Hide resolved
You can test this locally with the following command:git-clang-format --diff HEAD~1 HEAD --extensions ,cpp -- libcxx/test/std/utilities/optional/optional.range/enable_ranges_view.compile.pass.cpp libcxx/test/std/utilities/optional/optional.range/format_kind.pass.cpp libcxx/test/std/utilities/optional/optional.range/iteration.pass.cpp libcxx/test/std/utilities/optional/optional.range/iterator_types.verify.cpp libcxx/test/std/utilities/optional/optional.range/runtime_error.verify.cpp libcxx/test/std/utilities/optional/optional.range/satisfies_range_concept.verify.cpp libcxx/include/optional libcxx/include/version libcxx/test/std/language.support/support.limits/support.limits.general/optional.version.compile.pass.cpp libcxx/test/std/language.support/support.limits/support.limits.general/version.version.compile.pass.cppView the diff from clang-format here.diff --git a/libcxx/include/optional b/libcxx/include/optional
index 58a0d080a..571fe49e2 100644
--- a/libcxx/include/optional
+++ b/libcxx/include/optional
@@ -630,7 +630,7 @@ public:
_LIBCPP_HIDE_FROM_ABI constexpr __optional_iterator& operator=(const __optional_iterator&) = default;
_LIBCPP_HIDE_FROM_ABI constexpr reference operator*() const { return *__it_; }
- _LIBCPP_HIDE_FROM_ABI constexpr pointer operator->() const { return __it_.operator->(); }
+ _LIBCPP_HIDE_FROM_ABI constexpr pointer operator->() const { return __it_.operator->(); }
_LIBCPP_HIDE_FROM_ABI constexpr __optional_iterator& operator++() noexcept {
++__it_;
|
|
I'm going to fix issues later, but not tomorrow |
libcxx/test/std/utilities/optional/optional.range/format_kind.pass.cpp
Outdated
Show resolved
Hide resolved
libcxx/test/std/utilities/optional/optional.range/iterator_types.verify.cpp
Outdated
Show resolved
Hide resolved
libcxx/test/std/utilities/optional/optional.range/satisfies_range_concept.verify.cpp
Outdated
Show resolved
Hide resolved
…common_range, and contiguous_range assertions for std::optional
…e.iteration.pass Co-authored-by: A. Jiang <[email protected]>
Link to P3168R2: https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2024/p3168r2.html