Skip to content

Commit 3b7fe5b

Browse files
committed
[experiment] use unreachable_unchecked() everywhere
1 parent 3e05f80 commit 3b7fe5b

File tree

102 files changed

+240
-210
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

102 files changed

+240
-210
lines changed

src/liballoc/borrow.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ impl<'a, B: ?Sized> Cow<'a, B>
208208
Borrowed(borrowed) => {
209209
*self = Owned(borrowed.to_owned());
210210
match *self {
211-
Borrowed(..) => unreachable!(),
211+
Borrowed(..) => unsafe { ::core::hint::unreachable_unchecked() },
212212
Owned(ref mut owned) => owned,
213213
}
214214
}

src/liballoc/collections/btree/map.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ impl<K: Clone, V: Clone> Clone for BTreeMap<K, V> {
164164
{
165165
let mut out_node = match out_tree.root.as_mut().force() {
166166
Leaf(leaf) => leaf,
167-
Internal(_) => unreachable!(),
167+
Internal(_) => unsafe { ::core::hint::unreachable_unchecked() },
168168
};
169169

170170
let mut in_edge = leaf.first_edge();
@@ -979,7 +979,7 @@ impl<K: Ord, V> BTreeMap<K, V> {
979979
// We need to steal.
980980
let mut last_kv = match last_edge.left_kv() {
981981
Ok(left) => left,
982-
Err(_) => unreachable!(),
982+
Err(_) => unsafe { ::core::hint::unreachable_unchecked() },
983983
};
984984
last_kv.bulk_steal_left(node::MIN_LEN - right_child_len);
985985
last_edge = last_kv.right_edge();
@@ -1057,7 +1057,7 @@ impl<K: Ord, V> BTreeMap<K, V> {
10571057
break;
10581058
}
10591059
_ => {
1060-
unreachable!();
1060+
unsafe { ::core::hint::unreachable_unchecked() };
10611061
}
10621062
}
10631063
}
@@ -2508,7 +2508,7 @@ impl<'a, K: Ord, V> OccupiedEntry<'a, K, V> {
25082508
while cur_node.len() < node::CAPACITY / 2 {
25092509
match handle_underfull_node(cur_node) {
25102510
AtRoot => break,
2511-
EmptyParent(_) => unreachable!(),
2511+
EmptyParent(_) => unsafe { ::core::hint::unreachable_unchecked() },
25122512
Merged(parent) => {
25132513
if parent.len() == 0 {
25142514
// We must be at the root

src/liballoc/collections/btree/node.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1425,7 +1425,7 @@ impl<'a, K, V> Handle<NodeRef<marker::Mut<'a>, K, V, marker::Internal>, marker::
14251425
move_edges(left, new_left_len + 1, right, 0, count);
14261426
},
14271427
(ForceResult::Leaf(_), ForceResult::Leaf(_)) => { }
1428-
_ => { unreachable!(); }
1428+
_ => { ::core::hint::unreachable_unchecked(); }
14291429
}
14301430
}
14311431
}
@@ -1486,7 +1486,7 @@ impl<'a, K, V> Handle<NodeRef<marker::Mut<'a>, K, V, marker::Internal>, marker::
14861486
right.correct_childrens_parent_links(0, new_right_len + 1);
14871487
},
14881488
(ForceResult::Leaf(_), ForceResult::Leaf(_)) => { }
1489-
_ => { unreachable!(); }
1489+
_ => { ::core::hint::unreachable_unchecked(); }
14901490
}
14911491
}
14921492
}
@@ -1571,7 +1571,7 @@ impl<'a, K, V> Handle<NodeRef<marker::Mut<'a>, K, V, marker::LeafOrInternal>, ma
15711571
move_edges(left, left_new_len + 1, right, 1, right_new_len);
15721572
},
15731573
(ForceResult::Leaf(_), ForceResult::Leaf(_)) => { }
1574-
_ => { unreachable!(); }
1574+
_ => { ::core::hint::unreachable_unchecked(); }
15751575
}
15761576
}
15771577
}

src/liballoc/raw_vec.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -421,7 +421,7 @@ impl<T, A: Alloc> RawVec<T, A> {
421421
pub fn reserve_exact(&mut self, used_cap: usize, needed_extra_cap: usize) {
422422
match self.reserve_internal(used_cap, needed_extra_cap, Infallible, Exact) {
423423
Err(CapacityOverflow) => capacity_overflow(),
424-
Err(AllocErr) => unreachable!(),
424+
Err(AllocErr) => unsafe { ::core::hint::unreachable_unchecked() },
425425
Ok(()) => { /* yay */ }
426426
}
427427
}
@@ -501,7 +501,7 @@ impl<T, A: Alloc> RawVec<T, A> {
501501
pub fn reserve(&mut self, used_cap: usize, needed_extra_cap: usize) {
502502
match self.reserve_internal(used_cap, needed_extra_cap, Infallible, Amortized) {
503503
Err(CapacityOverflow) => capacity_overflow(),
504-
Err(AllocErr) => unreachable!(),
504+
Err(AllocErr) => unsafe { ::core::hint::unreachable_unchecked() },
505505
Ok(()) => { /* yay */ }
506506
}
507507
}

src/liballoc/tests/btree/map.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -368,7 +368,7 @@ fn test_entry() {
368368

369369
// Existing key (insert)
370370
match map.entry(1) {
371-
Vacant(_) => unreachable!(),
371+
Vacant(_) => unsafe { ::core::hint::unreachable_unchecked() },
372372
Occupied(mut view) => {
373373
assert_eq!(view.get(), &10);
374374
assert_eq!(view.insert(100), 10);
@@ -380,7 +380,7 @@ fn test_entry() {
380380

381381
// Existing key (update)
382382
match map.entry(2) {
383-
Vacant(_) => unreachable!(),
383+
Vacant(_) => unsafe { ::core::hint::unreachable_unchecked() },
384384
Occupied(mut view) => {
385385
let v = view.get_mut();
386386
*v *= 10;
@@ -391,7 +391,7 @@ fn test_entry() {
391391

392392
// Existing key (take)
393393
match map.entry(3) {
394-
Vacant(_) => unreachable!(),
394+
Vacant(_) => unsafe { ::core::hint::unreachable_unchecked() },
395395
Occupied(view) => {
396396
assert_eq!(view.remove(), 30);
397397
}
@@ -402,7 +402,7 @@ fn test_entry() {
402402

403403
// Inexistent key (insert)
404404
match map.entry(10) {
405-
Occupied(_) => unreachable!(),
405+
Occupied(_) => unsafe { ::core::hint::unreachable_unchecked() },
406406
Vacant(view) => {
407407
assert_eq!(*view.insert(1000), 1000);
408408
}

src/libcore/iter/mod.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -323,6 +323,7 @@ use ops::{self, Try};
323323
use usize;
324324
use intrinsics;
325325
use mem;
326+
use super::hint;
326327

327328
#[stable(feature = "rust1", since = "1.0.0")]
328329
pub use self::iterator::Iterator;
@@ -1177,7 +1178,7 @@ impl<A, B> ZipImpl<A, B> for Zip<A, B>
11771178
match (self.a.next_back(), self.b.next_back()) {
11781179
(Some(x), Some(y)) => Some((x, y)),
11791180
(None, None) => None,
1180-
_ => unreachable!(),
1181+
_ => unsafe { hint::unreachable_unchecked() },
11811182
}
11821183
}
11831184

@@ -2035,7 +2036,7 @@ impl<I: Iterator> Peekable<I> {
20352036
match self.peeked {
20362037
Some(Some(ref value)) => Some(value),
20372038
Some(None) => None,
2038-
_ => unreachable!(),
2039+
_ => unsafe { hint::unreachable_unchecked() },
20392040
}
20402041
}
20412042
}

src/libcore/macros.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -459,7 +459,7 @@ macro_rules! writeln {
459459
/// if 3*i < i { panic!("u32 overflow"); }
460460
/// if x < 3*i { return i-1; }
461461
/// }
462-
/// unreachable!();
462+
/// unreachable!()
463463
/// }
464464
/// ```
465465
#[macro_export]

src/libcore/num/dec2flt/rawfp.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ use num::FpCategory::{Infinite, Zero, Subnormal, Normal, Nan};
3636
use num::FpCategory;
3737
use num::dec2flt::num::{self, Big};
3838
use num::dec2flt::table;
39+
use super::super::super::hint;
3940

4041
#[derive(Copy, Clone, Debug)]
4142
pub struct Unpacked {
@@ -298,14 +299,16 @@ pub fn encode_normal<T: RawFloat>(x: Unpacked) -> T {
298299
"encode_normal: exponent out of range");
299300
// Leave sign bit at 0 ("+"), our numbers are all positive
300301
let bits = (k_enc as u64) << T::EXPLICIT_SIG_BITS | sig_enc;
301-
T::from_bits(bits.try_into().unwrap_or_else(|_| unreachable!()))
302+
T::from_bits(bits.try_into().unwrap_or_else(|_|
303+
unsafe { hint::unreachable_unchecked() }))
302304
}
303305

304306
/// Construct a subnormal. A mantissa of 0 is allowed and constructs zero.
305307
pub fn encode_subnormal<T: RawFloat>(significand: u64) -> T {
306308
assert!(significand < T::MIN_SIG, "encode_subnormal: not actually subnormal");
307309
// Encoded exponent is 0, the sign bit is 0, so we just have to reinterpret the bits.
308-
T::from_bits(significand.try_into().unwrap_or_else(|_| unreachable!()))
310+
T::from_bits(significand.try_into().unwrap_or_else(|_|
311+
unsafe { hint::unreachable_unchecked() }))
309312
}
310313

311314
/// Approximate a bignum with an Fp. Rounds within 0.5 ULP with half-to-even.

src/libcore/tests/slice.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -399,7 +399,7 @@ fn test_iter_ref_consistency() {
399399
let v : &[T] = &[x, x, x];
400400
let v_ptrs : [*const T; 3] = match v {
401401
[ref v1, ref v2, ref v3] => [v1 as *const _, v2 as *const _, v3 as *const _],
402-
_ => unreachable!()
402+
_ => unsafe { ::core::hint::unreachable_unchecked() }
403403
};
404404
let len = v.len();
405405

@@ -455,7 +455,7 @@ fn test_iter_ref_consistency() {
455455
let v_ptrs : [*mut T; 3] = match v {
456456
[ref v1, ref v2, ref v3] =>
457457
[v1 as *const _ as *mut _, v2 as *const _ as *mut _, v3 as *const _ as *mut _],
458-
_ => unreachable!()
458+
_ => unsafe { ::core::hint::unreachable_unchecked() }
459459
};
460460
let len = v.len();
461461

src/libpanic_unwind/emcc.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ pub unsafe fn panic(data: Box<dyn Any + Send>) -> u32 {
4646
ptr::write(exception, data);
4747
__cxa_throw(exception as *mut _, ptr::null_mut(), ptr::null_mut());
4848

49-
unreachable!()
49+
unsafe { ::core::hint::unreachable_unchecked() }
5050
}
5151

5252
#[lang = "eh_personality"]

0 commit comments

Comments
 (0)