slice: Remove some uses of unsafe in first/last chunk methods#139145
Merged
bors merged 1 commit intorust-lang:masterfrom Apr 3, 2025
Merged
slice: Remove some uses of unsafe in first/last chunk methods#139145bors merged 1 commit intorust-lang:masterfrom
bors merged 1 commit intorust-lang:masterfrom
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`
Collaborator
Amanieu
reviewed
Apr 2, 2025
| } 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 }; |
Member
There was a problem hiding this comment.
The code for all of these can be made more concise by using ?:
Suggested change
| let Some((first, tail)) = self.split_at_checked(N) else { return None }; | |
| let (first, tail) = self.split_at_checked(N)?; |
Contributor
Author
There was a problem hiding this comment.
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
Member
There was a problem hiding this comment.
Good point! I'd forgotten about that.
Member
|
@bors r+ |
Collaborator
bors
added a commit
to rust-lang-ci/rust
that referenced
this pull request
Apr 3, 2025
…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
bors
added a commit
to rust-lang-ci/rust
that referenced
this pull request
Apr 3, 2025
…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
rust-timer
added a commit
to rust-lang-ci/rust
that referenced
this pull request
Apr 3, 2025
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.
github-actions bot
pushed a commit
to model-checking/verify-rust-std
that referenced
this pull request
Apr 8, 2025
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.
github-actions bot
pushed a commit
to model-checking/verify-rust-std
that referenced
this pull request
Apr 8, 2025
…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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Remove unsafe
split_at_uncheckedandsplit_at_mut_uncheckedin some slicesplit_first_chunk/split_last_chunkmethods.Replace those calls with the safe
split_atandsplit_at_checkedwhere applicable.Add codegen tests to check for no panics when calculating the last chunk index using
checked_subandsplit_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 <= lentolen < mid.Unsafe is still needed for the pointer array casts. Their safety comments are unmodified.