Skip to content

Commit 1dbb5ef

Browse files
authored
Rollup merge of #107442 - lukas-code:slice-panics, r=cuviper
improve panic message for slice windows and chunks before: ```text thread 'main' panicked at 'size is zero', /rustc/1e225413a21fa69570bd3fefea9eb05e33f8b917/library/core/src/slice/mod.rs:809:44 ``` ```text thread 'main' panicked at 'assertion failed: `(left != right)` left: `0`, right: `0`: chunks cannot have a size of zero', /rustc/1e225413a21fa69570bd3fefea9eb05e33f8b917/library/core/src/slice/mod.rs:843:9 ``` after: ```text thread 'main' panicked at 'chunk size must be non-zero', src/main.rs:4:22 ``` fixes #107437
2 parents 0d2ab67 + 2fbe927 commit 1dbb5ef

File tree

1 file changed

+32
-16
lines changed

1 file changed

+32
-16
lines changed

library/core/src/slice/mod.rs

+32-16
Original file line numberDiff line numberDiff line change
@@ -805,8 +805,9 @@ impl<T> [T] {
805805
/// ```
806806
#[stable(feature = "rust1", since = "1.0.0")]
807807
#[inline]
808+
#[track_caller]
808809
pub fn windows(&self, size: usize) -> Windows<'_, T> {
809-
let size = NonZeroUsize::new(size).expect("size is zero");
810+
let size = NonZeroUsize::new(size).expect("window size must be non-zero");
810811
Windows::new(self, size)
811812
}
812813

@@ -839,8 +840,9 @@ impl<T> [T] {
839840
/// [`rchunks`]: slice::rchunks
840841
#[stable(feature = "rust1", since = "1.0.0")]
841842
#[inline]
843+
#[track_caller]
842844
pub fn chunks(&self, chunk_size: usize) -> Chunks<'_, T> {
843-
assert_ne!(chunk_size, 0, "chunks cannot have a size of zero");
845+
assert!(chunk_size != 0, "chunk size must be non-zero");
844846
Chunks::new(self, chunk_size)
845847
}
846848

@@ -877,8 +879,9 @@ impl<T> [T] {
877879
/// [`rchunks_mut`]: slice::rchunks_mut
878880
#[stable(feature = "rust1", since = "1.0.0")]
879881
#[inline]
882+
#[track_caller]
880883
pub fn chunks_mut(&mut self, chunk_size: usize) -> ChunksMut<'_, T> {
881-
assert_ne!(chunk_size, 0, "chunks cannot have a size of zero");
884+
assert!(chunk_size != 0, "chunk size must be non-zero");
882885
ChunksMut::new(self, chunk_size)
883886
}
884887

@@ -914,8 +917,9 @@ impl<T> [T] {
914917
/// [`rchunks_exact`]: slice::rchunks_exact
915918
#[stable(feature = "chunks_exact", since = "1.31.0")]
916919
#[inline]
920+
#[track_caller]
917921
pub fn chunks_exact(&self, chunk_size: usize) -> ChunksExact<'_, T> {
918-
assert_ne!(chunk_size, 0, "chunks cannot have a size of zero");
922+
assert!(chunk_size != 0, "chunk size must be non-zero");
919923
ChunksExact::new(self, chunk_size)
920924
}
921925

@@ -956,8 +960,9 @@ impl<T> [T] {
956960
/// [`rchunks_exact_mut`]: slice::rchunks_exact_mut
957961
#[stable(feature = "chunks_exact", since = "1.31.0")]
958962
#[inline]
963+
#[track_caller]
959964
pub fn chunks_exact_mut(&mut self, chunk_size: usize) -> ChunksExactMut<'_, T> {
960-
assert_ne!(chunk_size, 0, "chunks cannot have a size of zero");
965+
assert!(chunk_size != 0, "chunk size must be non-zero");
961966
ChunksExactMut::new(self, chunk_size)
962967
}
963968

@@ -1037,9 +1042,10 @@ impl<T> [T] {
10371042
/// ```
10381043
#[unstable(feature = "slice_as_chunks", issue = "74985")]
10391044
#[inline]
1045+
#[track_caller]
10401046
#[must_use]
10411047
pub fn as_chunks<const N: usize>(&self) -> (&[[T; N]], &[T]) {
1042-
assert_ne!(N, 0, "chunks cannot have a size of zero");
1048+
assert!(N != 0, "chunk size must be non-zero");
10431049
let len = self.len() / N;
10441050
let (multiple_of_n, remainder) = self.split_at(len * N);
10451051
// SAFETY: We already panicked for zero, and ensured by construction
@@ -1068,9 +1074,10 @@ impl<T> [T] {
10681074
/// ```
10691075
#[unstable(feature = "slice_as_chunks", issue = "74985")]
10701076
#[inline]
1077+
#[track_caller]
10711078
#[must_use]
10721079
pub fn as_rchunks<const N: usize>(&self) -> (&[T], &[[T; N]]) {
1073-
assert_ne!(N, 0, "chunks cannot have a size of zero");
1080+
assert!(N != 0, "chunk size must be non-zero");
10741081
let len = self.len() / N;
10751082
let (remainder, multiple_of_n) = self.split_at(self.len() - len * N);
10761083
// SAFETY: We already panicked for zero, and ensured by construction
@@ -1108,8 +1115,9 @@ impl<T> [T] {
11081115
/// [`chunks_exact`]: slice::chunks_exact
11091116
#[unstable(feature = "array_chunks", issue = "74985")]
11101117
#[inline]
1118+
#[track_caller]
11111119
pub fn array_chunks<const N: usize>(&self) -> ArrayChunks<'_, T, N> {
1112-
assert_ne!(N, 0, "chunks cannot have a size of zero");
1120+
assert!(N != 0, "chunk size must be non-zero");
11131121
ArrayChunks::new(self)
11141122
}
11151123

@@ -1186,9 +1194,10 @@ impl<T> [T] {
11861194
/// ```
11871195
#[unstable(feature = "slice_as_chunks", issue = "74985")]
11881196
#[inline]
1197+
#[track_caller]
11891198
#[must_use]
11901199
pub fn as_chunks_mut<const N: usize>(&mut self) -> (&mut [[T; N]], &mut [T]) {
1191-
assert_ne!(N, 0, "chunks cannot have a size of zero");
1200+
assert!(N != 0, "chunk size must be non-zero");
11921201
let len = self.len() / N;
11931202
let (multiple_of_n, remainder) = self.split_at_mut(len * N);
11941203
// SAFETY: We already panicked for zero, and ensured by construction
@@ -1223,9 +1232,10 @@ impl<T> [T] {
12231232
/// ```
12241233
#[unstable(feature = "slice_as_chunks", issue = "74985")]
12251234
#[inline]
1235+
#[track_caller]
12261236
#[must_use]
12271237
pub fn as_rchunks_mut<const N: usize>(&mut self) -> (&mut [T], &mut [[T; N]]) {
1228-
assert_ne!(N, 0, "chunks cannot have a size of zero");
1238+
assert!(N != 0, "chunk size must be non-zero");
12291239
let len = self.len() / N;
12301240
let (remainder, multiple_of_n) = self.split_at_mut(self.len() - len * N);
12311241
// SAFETY: We already panicked for zero, and ensured by construction
@@ -1265,8 +1275,9 @@ impl<T> [T] {
12651275
/// [`chunks_exact_mut`]: slice::chunks_exact_mut
12661276
#[unstable(feature = "array_chunks", issue = "74985")]
12671277
#[inline]
1278+
#[track_caller]
12681279
pub fn array_chunks_mut<const N: usize>(&mut self) -> ArrayChunksMut<'_, T, N> {
1269-
assert_ne!(N, 0, "chunks cannot have a size of zero");
1280+
assert!(N != 0, "chunk size must be non-zero");
12701281
ArrayChunksMut::new(self)
12711282
}
12721283

@@ -1297,8 +1308,9 @@ impl<T> [T] {
12971308
/// [`windows`]: slice::windows
12981309
#[unstable(feature = "array_windows", issue = "75027")]
12991310
#[inline]
1311+
#[track_caller]
13001312
pub fn array_windows<const N: usize>(&self) -> ArrayWindows<'_, T, N> {
1301-
assert_ne!(N, 0, "windows cannot have a size of zero");
1313+
assert!(N != 0, "window size must be non-zero");
13021314
ArrayWindows::new(self)
13031315
}
13041316

@@ -1331,8 +1343,9 @@ impl<T> [T] {
13311343
/// [`chunks`]: slice::chunks
13321344
#[stable(feature = "rchunks", since = "1.31.0")]
13331345
#[inline]
1346+
#[track_caller]
13341347
pub fn rchunks(&self, chunk_size: usize) -> RChunks<'_, T> {
1335-
assert!(chunk_size != 0);
1348+
assert!(chunk_size != 0, "chunk size must be non-zero");
13361349
RChunks::new(self, chunk_size)
13371350
}
13381351

@@ -1369,8 +1382,9 @@ impl<T> [T] {
13691382
/// [`chunks_mut`]: slice::chunks_mut
13701383
#[stable(feature = "rchunks", since = "1.31.0")]
13711384
#[inline]
1385+
#[track_caller]
13721386
pub fn rchunks_mut(&mut self, chunk_size: usize) -> RChunksMut<'_, T> {
1373-
assert!(chunk_size != 0);
1387+
assert!(chunk_size != 0, "chunk size must be non-zero");
13741388
RChunksMut::new(self, chunk_size)
13751389
}
13761390

@@ -1408,8 +1422,9 @@ impl<T> [T] {
14081422
/// [`chunks_exact`]: slice::chunks_exact
14091423
#[stable(feature = "rchunks", since = "1.31.0")]
14101424
#[inline]
1425+
#[track_caller]
14111426
pub fn rchunks_exact(&self, chunk_size: usize) -> RChunksExact<'_, T> {
1412-
assert!(chunk_size != 0);
1427+
assert!(chunk_size != 0, "chunk size must be non-zero");
14131428
RChunksExact::new(self, chunk_size)
14141429
}
14151430

@@ -1451,8 +1466,9 @@ impl<T> [T] {
14511466
/// [`chunks_exact_mut`]: slice::chunks_exact_mut
14521467
#[stable(feature = "rchunks", since = "1.31.0")]
14531468
#[inline]
1469+
#[track_caller]
14541470
pub fn rchunks_exact_mut(&mut self, chunk_size: usize) -> RChunksExactMut<'_, T> {
1455-
assert!(chunk_size != 0);
1471+
assert!(chunk_size != 0, "chunk size must be non-zero");
14561472
RChunksExactMut::new(self, chunk_size)
14571473
}
14581474

0 commit comments

Comments
 (0)