-
Notifications
You must be signed in to change notification settings - Fork 13.3k
slice: Remove some uses of unsafe in first/last chunk methods #139145
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
Conversation
Remove unsafe `split_at_unchecked` and `split_at_mut_unchecked` in some slice `split_first_chunk`/`split_last_chunk` methods. Replace those calls with the safe `split_at` and `split_at_checked` where applicable. Add codegen tests to check for no panics when calculating the last chunk index using `checked_sub` and `split_at`
} else { | ||
// SAFETY: We manually verified the bounds of the split. | ||
let (first, tail) = unsafe { self.split_at_unchecked(N) }; | ||
let Some((first, tail)) = self.split_at_checked(N) else { return None }; |
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 code for all of these can be made more concise by using ?
:
let Some((first, tail)) = self.split_at_checked(N) else { return None }; | |
let (first, tail) = self.split_at_checked(N)?; |
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.
These functions are const fn
so I don't believe ?
is available to use.
That results in the following error:
error[E0015]: `?` is not allowed on `Option<(&[T], &[T])>` in constant functions
--> library\core\src\slice\mod.rs:385:29
|
385 | let (first, tail) = self.split_at_checked(N)?;
| ^^^^^^^^^^^^^^^^^^^^^^^^^
|
= note: calls in constant functions are limited to constant functions, tuple structs and tuple variants
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.
Good point! I'd forgotten about that.
@bors r+ |
…iaskrgr Rollup of 7 pull requests Successful merges: - rust-lang#139080 (Experimental feature gate for `super let`) - rust-lang#139145 (slice: Remove some uses of unsafe in first/last chunk methods) - rust-lang#139149 (unstable book: document import_trait_associated_functions) - rust-lang#139273 (Apply requested API changes to `cell_update`) - rust-lang#139282 (rustdoc: make settings checkboxes always square) - rust-lang#139283 (Rustc dev guide subtree update) - rust-lang#139294 (Fix the `f16`/`f128` feature gates on integer literals) r? `@ghost` `@rustbot` modify labels: rollup
…iaskrgr Rollup of 7 pull requests Successful merges: - rust-lang#139080 (Experimental feature gate for `super let`) - rust-lang#139145 (slice: Remove some uses of unsafe in first/last chunk methods) - rust-lang#139149 (unstable book: document import_trait_associated_functions) - rust-lang#139273 (Apply requested API changes to `cell_update`) - rust-lang#139282 (rustdoc: make settings checkboxes always square) - rust-lang#139283 (Rustc dev guide subtree update) - rust-lang#139294 (Fix the `f16`/`f128` feature gates on integer literals) r? `@ghost` `@rustbot` modify labels: rollup
Rollup merge of rust-lang#139145 - okaneco:safe_splits, r=Amanieu slice: Remove some uses of unsafe in first/last chunk methods Remove unsafe `split_at_unchecked` and `split_at_mut_unchecked` in some slice `split_first_chunk`/`split_last_chunk` methods. Replace those calls with the safe `split_at` and `split_at_checked` where applicable. Add codegen tests to check for no panics when calculating the last chunk index using `checked_sub` and `split_at`. Better viewed with whitespace disabled in diff view --- The unchecked calls are mostly manual implementations of the safe methods, but with the safety condition negated from `mid <= len` to `len < mid`. ```rust if self.len() < N { None } else { // SAFETY: We manually verified the bounds of the split. let (first, tail) = unsafe { self.split_at_unchecked(N) }; // Or for the last_chunk methods let (init, last) = unsafe { self.split_at_unchecked(self.len() - N) }; ``` Unsafe is still needed for the pointer array casts. Their safety comments are unmodified.
slice: Remove some uses of unsafe in first/last chunk methods Remove unsafe `split_at_unchecked` and `split_at_mut_unchecked` in some slice `split_first_chunk`/`split_last_chunk` methods. Replace those calls with the safe `split_at` and `split_at_checked` where applicable. Add codegen tests to check for no panics when calculating the last chunk index using `checked_sub` and `split_at`. Better viewed with whitespace disabled in diff view --- The unchecked calls are mostly manual implementations of the safe methods, but with the safety condition negated from `mid <= len` to `len < mid`. ```rust if self.len() < N { None } else { // SAFETY: We manually verified the bounds of the split. let (first, tail) = unsafe { self.split_at_unchecked(N) }; // Or for the last_chunk methods let (init, last) = unsafe { self.split_at_unchecked(self.len() - N) }; ``` Unsafe is still needed for the pointer array casts. Their safety comments are unmodified.
…iaskrgr Rollup of 7 pull requests Successful merges: - rust-lang#139080 (Experimental feature gate for `super let`) - rust-lang#139145 (slice: Remove some uses of unsafe in first/last chunk methods) - rust-lang#139149 (unstable book: document import_trait_associated_functions) - rust-lang#139273 (Apply requested API changes to `cell_update`) - rust-lang#139282 (rustdoc: make settings checkboxes always square) - rust-lang#139283 (Rustc dev guide subtree update) - rust-lang#139294 (Fix the `f16`/`f128` feature gates on integer literals) r? `@ghost` `@rustbot` modify labels: rollup
Remove unsafe
split_at_unchecked
andsplit_at_mut_unchecked
in some slicesplit_first_chunk
/split_last_chunk
methods.Replace those calls with the safe
split_at
andsplit_at_checked
where applicable.Add codegen tests to check for no panics when calculating the last chunk index using
checked_sub
andsplit_at
.Better viewed with whitespace disabled in diff view
The unchecked calls are mostly manual implementations of the safe methods, but with the safety condition negated from
mid <= len
tolen < mid
.Unsafe is still needed for the pointer array casts. Their safety comments are unmodified.