Skip to content

Commit 7398b39

Browse files
committed
Make panic's more specific
1 parent 64d1433 commit 7398b39

File tree

1 file changed

+13
-10
lines changed

1 file changed

+13
-10
lines changed

Diff for: compiler/rustc_index/src/slice.rs

+13-10
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use std::fmt;
22
use std::marker::PhantomData;
33
use std::ops::{Index, IndexMut};
4+
use std::slice::GetDisjointMutError::*;
45
use std::slice::{self, SliceIndex};
56

67
use crate::{Idx, IndexVec, IntoSliceIdx};
@@ -115,33 +116,35 @@ impl<I: Idx, T> IndexSlice<I, T> {
115116

116117
/// Returns mutable references to two distinct elements, `a` and `b`.
117118
///
118-
/// Panics if `a == b`.
119+
/// Panics if `a == b` or if some of them are out of bounds.
119120
#[inline]
120121
pub fn pick2_mut(&mut self, a: I, b: I) -> (&mut T, &mut T) {
121122
let (ai, bi) = (a.index(), b.index());
122-
assert!(ai != bi);
123-
let len = self.raw.len();
124-
assert!(ai < len && bi < len);
125123

126124
match self.raw.get_disjoint_mut([ai, bi]) {
127125
Ok([a, b]) => (a, b),
128-
Err(_) => panic!("Can't get mutable references"),
126+
Err(OverlappingIndices) => panic!("Indices {ai:?} and {bi:?} are not disjoint!"),
127+
Err(IndexOutOfBounds) => {
128+
panic!("Some indices among ({ai:?}, {bi:?}) are out of bounds")
129+
}
129130
}
130131
}
131132

132133
/// Returns mutable references to three distinct elements.
133134
///
134-
/// Panics if the elements are not distinct.
135+
/// Panics if the elements are not distinct or if some of them are out of bounds.
135136
#[inline]
136137
pub fn pick3_mut(&mut self, a: I, b: I, c: I) -> (&mut T, &mut T, &mut T) {
137138
let (ai, bi, ci) = (a.index(), b.index(), c.index());
138-
assert!(ai != bi && bi != ci && ci != ai);
139-
let len = self.raw.len();
140-
assert!(ai < len && bi < len && ci < len);
141139

142140
match self.raw.get_disjoint_mut([ai, bi, ci]) {
143141
Ok([a, b, c]) => (a, b, c),
144-
Err(_) => panic!("Can't get mutable references"),
142+
Err(OverlappingIndices) => {
143+
panic!("Indices {ai:?}, {bi:?} and {ci:?} are not disjoint!")
144+
}
145+
Err(IndexOutOfBounds) => {
146+
panic!("Some indices among ({ai:?}, {bi:?}, {ci:?}) are out of bounds")
147+
}
145148
}
146149
}
147150

0 commit comments

Comments
 (0)