Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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
Implement indexing slices with pairs of core::ops::Bound<usize> #77704
Implement indexing slices with pairs of core::ops::Bound<usize> #77704
Changes from 1 commit
7efba4f
6763a40
File filter
Filter by extension
Conversations
Jump to
There are no files selected for viewing
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.
Somewhere in the user-facing documentation of the unchecked indexing methods, it should say that the user is also responsible for avoiding overflow when using an
Excluded
lower-bound or anIncluded
upper-bound. Where does it say that?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.
It seems like those could possibly go on the
impl
itself. There are other invariants documented on otherimpl SliceIndex
es already.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.
Isn't that basically undiscoverable?
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.
Yeh, it’s not as discoverable as directly on the methods themselves.
But it looks like this is actually also a problem for
RangeInclusive::get_unchecked
, which doesn’t appear to document the requirement thatself.end != usize::MAX
in the public API docs.Maybe we should flesh out the docs for
[T]::get_unchecked{_mut}
a bit to mention the possibility of overflowing bounds if they’re converted between inclusive/exclusive?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.
Yes, I agree this is an existing problem.
Sounds reasonable.
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 documentation right now says:
Since
usize::MAX
would always be out of bounds for anything taking up space, we're only talking about overflows in[T]::get_unchecked
for ZSTs here, right? Or am I missing something?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.
And even for ZSTs, slices can not be longer than usize::MAX. So an
Included(usize::MAX)
range end already counts as out-of-bounds, permitting UB.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.
Hm, I can agree with that interpretation, but I feel it would be worth calling out explicitly that if there is an overflow while computing the bounds, that also counts as out-of-bounds.
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.
At the very least it would be a great inline comment here and in
RangeInclusive
.