-
Notifications
You must be signed in to change notification settings - Fork 13.6k
Optimize indexing slices and strs with inclusive ranges #145024
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
base: master
Are you sure you want to change the base?
Conversation
The check for `self.end() == usize::MAX` can be combined with the `self.end() + 1 > slice.len()` check into `self.en() >= slice.len()`, since `self.end() < slice.len()` implies both `self.end() <= slice.len()` and `self.end() < usize::MAX`. The tradeoff is slightly worse error reporting: previously there would be a special panic message in the `range.end() == usize::MAX` case.
Same reasoning as previous commit.
Consolidate all the panicking functions in `slice/index.rs` to use a single `slice_index_fail` function, similar to how it is done in `str/traits.rs`.
r? @ibraheemdev rustbot has assigned @ibraheemdev. Use |
The job Click to see the possible cause of the failure (guessed by this bot)
|
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.
As you saw from the PR build failure, if you're touching things like slice_index_order_fail
you'll need to look through every mention of those in the repo -- there are a bunch of codegen tests that look for their absence, and you need to make sure that you don't make those tests useless by renaming what they're looking for.
(You probably also need to re-bless some MIR tests that include indexing.)
Instead of separately checking for
end == usize::MAX
andend + 1 > slice.len()
, we can check forend >= slice.len()
. Also consolidate all the slice indexing related panic functions into a single function which reports the correct error depending on the arguments, as the str indexing code already does.The downside of all this is that the panic message is slightly less specific when trying to index with
[..=usize::MAX]
: instead of saying "attempted to index slice up to maximum usize" it just says "range end index {end} out of range for slice of length {len}". But this is a rare enough case that I think it is acceptable