Skip to content

Conversation

@dywoq
Copy link

@dywoq dywoq commented Jul 1, 2025

@dywoq dywoq requested a review from a team as a code owner July 1, 2025 09:04
@github-actions
Copy link

github-actions bot commented Jul 1, 2025

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 @ followed by their GitHub username.

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.

Comment on lines 1076 to 1080
_LIBCPP_HIDE_FROM_ABI iterator begin() {
if (!this->has_value())
return this->end();
return iterator(reinterpret_cast<_Tp*>(this->__get()));
}
Copy link
Contributor

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.

Copy link
Author

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;
Copy link
Contributor

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.

@dywoq
Copy link
Author

dywoq commented Jul 1, 2025

 _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 __ptr_ instead of throwing exceptions if __ptr_ is nullptr? Throwing exceptions is not allowed in constexpr functions

@zwuis
Copy link
Contributor

zwuis commented Jul 1, 2025

 _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 __ptr_ instead of throwing exceptions if __ptr_ is nullptr? Throwing exceptions is not allowed in constexpr functions

*end() is UB so these if statements should be removed.

_LIBCPP_HIDE_FROM_ABI constexpr pointer operator->() const { return *__ptr_; }

_LIBCPP_HIDE_FROM_ABI constexpr __optional_iterator& operator++() {
__ptr_ = nullptr;
Copy link
Contributor

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.

@dywoq dywoq requested a review from frederick-vs-ja July 1, 2025 13:33
constexpr auto format_kind<optional<_T>> = range_format::disabled;

template <typename _Tp>
class __optional_iterator {
Copy link
Contributor

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
Copy link
Contributor

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
//
//===----------------------------------------------------------------------===//

Copy link
Contributor

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() {
Copy link
Contributor

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.

Copy link
Contributor

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");
Copy link
Contributor

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.

template <class _T>
constexpr auto format_kind<optional<_T>> = range_format::disabled;

template <typename _Tp>
Copy link
Contributor

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.

Comment on lines +605 to +609
template <class _T>
constexpr bool ranges::enable_view<optional<_T>> = true;

template <class _T>
constexpr auto format_kind<optional<_T>> = range_format::disabled;
Copy link
Contributor

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.

Comment on lines 584 to 589
# if _LIBCPP_STD_VER >= 26

template <typename _Tp>
class __optional_iterator;

# endif // _LIBCPP_STD_VER >= 26
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this needed?

# include <__concepts/invocable.h>
# include <__config>
# include <__exception/exception.h>
# include <__format/format_context.h>
Copy link
Contributor

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?

# if _LIBCPP_STD_VER >= 26

using iterator = __optional_iterator<_Tp>;
using const_iterator = __optional_iterator<_Tp>;
Copy link
Contributor

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&.

_LIBCPP_HIDE_FROM_ABI constexpr pointer operator->() const { return *__it_.operator->(); }

_LIBCPP_HIDE_FROM_ABI constexpr __optional_iterator& operator++() noexcept {
__it_ = _Base();
Copy link
Contributor

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.

@dywoq
Copy link
Author

dywoq commented Jul 2, 2025

is it ready to merge?

@zwuis
Copy link
Contributor

zwuis commented Jul 2, 2025

is it ready to merge?

No. Some review comments are not addressed. Note that some review comments are marked "Outdated" by GitHub, which should be addressed as well.

My fault. It seems that all review comments are addressed currently.

@dywoq
Copy link
Author

dywoq commented Jul 2, 2025

is it ready to merge?

No. Some review comments are not addressed. Note that some review comments are marked "Outdated" by GitHub, which should be addressed as well.

My fault. It seems that all review comments are addressed currently.

It's nothing


template <typename _Tp>
class __optional_iterator {
using _Base = __wrap_iter<_Tp*>;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing _LIBCPP_NODEBUG.

Copy link
Contributor

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.

Copy link
Author

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

# if _LIBCPP_STD_VER >= 26

using iterator = __optional_iterator<_Tp>;
using const_iterator = __optional_iterator<const _Tp&>;
Copy link
Contributor

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.

Comment on lines 711 to 714
template <typename _Tp>
_LIBCPP_HIDE_FROM_ABI constexpr _Tp* to_address(const __optional_iterator<_Tp>& __it) noexcept {
return std::to_address(__it.__it_);
}
Copy link
Contributor

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.

Copy link
Author

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

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry

Comment on lines 1138 to 1142
_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)));
}
Copy link
Contributor

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++.

@github-actions
Copy link

github-actions bot commented Jul 2, 2025

⚠️ C/C++ code formatter, clang-format found issues in your code. ⚠️

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.cpp
View 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_;

@dywoq
Copy link
Author

dywoq commented Jul 2, 2025

I'm going to fix issues later, but not tomorrow

@dywoq dywoq closed this by deleting the head repository Oct 11, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants