Skip to content
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

<deque>: Fix iterator arithmetic #4049

Merged
merged 6 commits into from
Oct 4, 2023

Conversation

frederick-vs-ja
Copy link
Contributor

@frederick-vs-ja frederick-vs-ja commented Sep 24, 2023

Currently we sometimes directly add a size_type value to a fancy pointer, which is not always supported. Also, the difference type of pointer and _Mapptr may be different (which is contrived though). I think we should static_cast to the correct difference type in iterator arithmetic.

The return value of _Getblock is always used in the subscipt, so I guess it's OK to change the return type to the difference type.

Driven-by: avoid seemingly redundant indirection in operator[] and at. (Perhaps we should also do this for front and back).

@frederick-vs-ja frederick-vs-ja requested a review from a team as a code owner September 24, 2023 23:36
@StephanTLavavej StephanTLavavej added the bug Something isn't working label Sep 25, 2023
@StephanTLavavej StephanTLavavej self-assigned this Sep 26, 2023
stl/inc/deque Outdated Show resolved Hide resolved
@achabense
Copy link
Contributor

Hmm, I think it will be horrible to have two difference_types (difference_type and _Map_difference_type, from different sources) here and there :|

// NB: _Mapsize and _Block_size are guaranteed to be powers of 2
return (_Off / _Block_size) & (_Mapsize - 1);
return static_cast<_Map_difference_type>((_Off / _Block_size) & (_Mapsize - 1));
Copy link
Contributor

@achabense achabense Sep 27, 2023

Choose a reason for hiding this comment

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

I think we should put off every cast-to-_Map_difference_type to only when having to add to a pointer.

stl/inc/deque Outdated

reference _At(size_type _Idx) noexcept {
const auto _Block = _Getblock(_Idx);
const auto _Off = static_cast<difference_type>(_Idx % _Block_size);
Copy link
Contributor

@achabense achabense Sep 27, 2023

Choose a reason for hiding this comment

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

If I'm correct, this difference_type comes from _Alty_traits::difference_type, which has a different source from _Map_difference_type (_Al[p]ty_traits).
My suggestions are:

  1. not changing return type of _Getblock.
  2. this function should be
const size_type _Block = _Getblock(_Idx);
const auto _Off        = static_cast<_Map_difference_type>(_Idx(or "_Off") % _Block_size);
return _Map[_Block][_Off];

Copy link
Contributor Author

Choose a reason for hiding this comment

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

If I'm correct, this difference_type comes from _Alty_traits::difference_type, which has a different source from _Map_difference_type (_Al[p]ty_traits).

This is right.

My suggestions are:

  1. not changing return type of _Getblock.
  2. this function should be
const size_type _Block = _Getblock(_Idx);
const auto _Off        = static_cast<_Map_difference_type>(_Idx(or "_Off") % _Block_size);
return _Map[_Block][_Off];

These suggestions don't seem correct. The new test case will be rejected with these applied.

Copy link
Contributor

Choose a reason for hiding this comment

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

The new test case will be rejected with these applied.

Yes, I mistook the types here :(

@@ -74,7 +72,7 @@ public:
}

_Deque_unchecked_const_iterator& operator+=(const difference_type _Off) noexcept {
Copy link
Contributor

Choose a reason for hiding this comment

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

As a side note, these different_type comes from _Alty_traits.

stl/inc/deque Outdated Show resolved Hide resolved
@CaseyCarter

This comment was marked as resolved.

@azure-pipelines

This comment was marked as resolved.

@StephanTLavavej StephanTLavavej removed their assignment Oct 3, 2023
@StephanTLavavej
Copy link
Member

Thanks! I think this needs only one maintainer approval.

@StephanTLavavej StephanTLavavej self-assigned this Oct 3, 2023
@StephanTLavavej
Copy link
Member

I'm mirroring this to the MSVC-internal repo - please notify me if any further changes are pushed.

@StephanTLavavej StephanTLavavej merged commit 623813c into microsoft:main Oct 4, 2023
@StephanTLavavej
Copy link
Member

Thanks for improving our fancy pointer support! 🧐 🎩 🎉

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants