-
Notifications
You must be signed in to change notification settings - Fork 13.8k
Safer sort partition #147378
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?
Safer sort partition #147378
Conversation
cc @orlp @Voultapher you may find this interesting (but you don't have to) |
I'm not sure that's true. Looking at for example this condition I'm all for reducing unsafe and a smaller code-size, with this PR as is, it might lead to confusing error messages and incorrect understanding of the code in the future. Maybe we can achieve a similar goal using the |
I mean only The additional checks piggyback on returning I still think it's an improvement over the current version of |
The arguments you present don't make sense to me.
|
d04d64f
to
ac2b421
Compare
This PR was rebased onto a different master commit. Here's a range-diff highlighting what actually changed. Rebasing is a normal part of keeping PRs up to date, so no action is needed—this note is just to help reviewers. |
ok, if reporting |
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.
} | ||
let (pivot, v_without_pivot) = v.split_at_mut(1); | ||
v.swap(0, pivot); | ||
let (pivot, v_without_pivot) = v.split_first_mut().unwrap_or_else(|| intrinsics::abort()); |
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.
Maybe .unwrap_or_else(intrinsics::abort);
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'm getting:
.unwrap_or_else(intrinsics::abort);
^^^^^^^^^^^^^^^^^ expected(&mut T, &mut [T])
, found!
This reduces amount of
unsafe
code inpartition()
, while generating essentially the same code.partition()
and functions calling it can only run into out-of-bounds issues if theis_less
function is buggy, so I've used coldpanic_on_ord_violation()
to replace various other panics/aborts. This slightly reduces code size, and gives a more relevant error message.Related to #144327