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"]

src/libproc_macro/quote.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ pub fn quote(stream: TokenStream) -> TokenStream {
128128
lit.set_span((@ quote_span(tt.span())));
129129
lit
130130
} else {
131-
unreachable!()
131+
unsafe { ::std::hint::unreachable_unchecked() }
132132
}
133133
}))
134134
})),))

src/libproc_macro/rustc.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -179,8 +179,10 @@ impl TokenTree {
179179
}),
180180

181181
DotEq => op!('.', '='),
182-
OpenDelim(..) | CloseDelim(..) => unreachable!(),
183-
Whitespace | Comment | Shebang(..) | Eof => unreachable!(),
182+
OpenDelim(..) | CloseDelim(..) => unsafe { ::std::hint::unreachable_unchecked() },
183+
Whitespace | Comment | Shebang(..) | Eof => unsafe {
184+
::std::hint::unreachable_unchecked()
185+
},
184186
}
185187
}
186188

@@ -260,7 +262,7 @@ impl TokenTree {
260262
'$' => Dollar,
261263
'?' => Question,
262264
'\'' => SingleQuote,
263-
_ => unreachable!(),
265+
_ => unsafe { ::std::hint::unreachable_unchecked() },
264266
};
265267

266268
let tree = TokenTree::Token(span.0, token);

src/librustc/hir/def_id.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ impl DefIndex {
104104
match self.0 & 1 {
105105
0 => DefIndexAddressSpace::Low,
106106
1 => DefIndexAddressSpace::High,
107-
_ => unreachable!()
107+
_ => unsafe { ::core::hint::unreachable_unchecked() }
108108
}
109109
}
110110

src/librustc/hir/lowering.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3786,7 +3786,9 @@ impl<'a> LoweringContext<'a> {
37863786
(&None, &Some(..), HalfOpen) => "RangeTo",
37873787
(&Some(..), &Some(..), HalfOpen) => "Range",
37883788
(&None, &Some(..), Closed) => "RangeToInclusive",
3789-
(&Some(..), &Some(..), Closed) => unreachable!(),
3789+
(&Some(..), &Some(..), Closed) => unsafe {
3790+
::core::hint::unreachable_unchecked()
3791+
},
37903792
(_, &None, Closed) => self.diagnostic()
37913793
.span_fatal(e.span, "inclusive range with no end")
37923794
.raise(),

src/librustc/hir/print.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2057,7 +2057,7 @@ impl<'a> State<'a> {
20572057
self.print_type(&ty)?;
20582058
self.maybe_print_comment(ty.span.lo())
20592059
}
2060-
hir::DefaultReturn(..) => unreachable!(),
2060+
hir::DefaultReturn(..) => unsafe { ::core::hint::unreachable_unchecked() },
20612061
}
20622062
}
20632063

@@ -2224,7 +2224,7 @@ impl<'a> State<'a> {
22242224
self.ibox(indent_unit)?;
22252225
self.word_space("->")?;
22262226
match decl.output {
2227-
hir::DefaultReturn(..) => unreachable!(),
2227+
hir::DefaultReturn(..) => unsafe { ::core::hint::unreachable_unchecked() },
22282228
hir::Return(ref ty) => self.print_type(&ty)?,
22292229
}
22302230
self.end()?;

src/librustc/infer/error_reporting/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
198198
Some(hir_map::NodeItem(it)) => Self::item_scope_tag(&it),
199199
Some(hir_map::NodeTraitItem(it)) => Self::trait_item_scope_tag(&it),
200200
Some(hir_map::NodeImplItem(it)) => Self::impl_item_scope_tag(&it),
201-
_ => unreachable!()
201+
_ => unsafe { ::core::hint::unreachable_unchecked() }
202202
};
203203
let (prefix, span) = match *region {
204204
ty::ReEarlyBound(ref br) => {

src/librustc/lint/context.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -302,7 +302,7 @@ impl LintStore {
302302
CheckLintNameResult::NoLint => {
303303
Some(struct_err!(sess, E0602, "unknown lint: `{}`", lint_name))
304304
}
305-
CheckLintNameResult::Tool(_) => unreachable!(),
305+
CheckLintNameResult::Tool(_) => unsafe { ::core::hint::unreachable_unchecked() },
306306
};
307307

308308
if let Some(mut db) = db {

src/librustc/middle/reachable.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -374,7 +374,7 @@ impl<'a, 'tcx: 'a> ItemLikeVisitor<'tcx> for CollectPrivateImplItemsVisitor<'a,
374374

375375
let trait_def_id = match trait_ref.path.def {
376376
Def::Trait(def_id) => def_id,
377-
_ => unreachable!()
377+
_ => unsafe { ::core::hint::unreachable_unchecked() }
378378
};
379379

380380
if !trait_def_id.is_local() {

src/librustc/mir/tcx.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -308,7 +308,7 @@ impl BinOp {
308308
BinOp::Gt => hir::BinOpKind::Gt,
309309
BinOp::Le => hir::BinOpKind::Le,
310310
BinOp::Ge => hir::BinOpKind::Ge,
311-
BinOp::Offset => unreachable!()
311+
BinOp::Offset => unsafe { ::core::hint::unreachable_unchecked() }
312312
}
313313
}
314314
}

src/librustc/traits/project.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ impl<'tcx> ProjectionTyCandidateSet<'tcx> {
171171
match (current, candidate) {
172172
(ParamEnv(..), ParamEnv(..)) => convert_to_ambiguous = (),
173173
(ParamEnv(..), _) => return false,
174-
(_, ParamEnv(..)) => { unreachable!(); }
174+
(_, ParamEnv(..)) => { unsafe { ::core::hint::unreachable_unchecked() }; }
175175
(_, _) => convert_to_ambiguous = (),
176176
}
177177
}

src/librustc/ty/query/on_disk_cache.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -658,7 +658,7 @@ impl<'a, 'tcx, 'x> SpecializedDecoder<Span> for CacheDecoder<'a, 'tcx, 'x> {
658658
}
659659
}
660660
_ => {
661-
unreachable!()
661+
unsafe { ::core::hint::unreachable_unchecked() }
662662
}
663663
};
664664

@@ -751,7 +751,7 @@ for CacheDecoder<'a, 'tcx, 'x> {
751751
Ok(mir::ClearCrossCrate::Set(val))
752752
}
753753
_ => {
754-
unreachable!()
754+
unsafe { ::core::hint::unreachable_unchecked() }
755755
}
756756
}
757757
}

src/librustc/ty/query/plumbing.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -650,7 +650,7 @@ macro_rules! handle_cycle_error {
650650
}};
651651
([fatal_cycle$(, $modifiers:ident)*][$this:expr]) => {{
652652
$this.sess.abort_if_errors();
653-
unreachable!();
653+
unsafe { ::core::hint::unreachable_unchecked() };
654654
}};
655655
([$other:ident$(, $modifiers:ident)*][$($args:tt)*]) => {
656656
handle_cycle_error!([$($modifiers),*][$($args)*])

src/librustc/util/bug.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,5 +47,5 @@ fn opt_span_bug_fmt<S: Into<MultiSpan>>(
4747
(None, _) => panic!(msg),
4848
}
4949
});
50-
unreachable!();
50+
unsafe { ::core::hint::unreachable_unchecked() };
5151
}

src/librustc_apfloat/ieee.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1756,7 +1756,7 @@ impl<S: Semantics> IeeeFloat<S> {
17561756
1..=7 => Loss::LessThanHalf,
17571757
8 => Loss::ExactlyHalf,
17581758
9..=15 => Loss::MoreThanHalf,
1759-
_ => unreachable!(),
1759+
_ => unsafe { ::std::hint::unreachable_unchecked() },
17601760
});
17611761
}
17621762
}

src/librustc_apfloat/lib.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@
4343
#![doc(html_logo_url = "https://www.rust-lang.org/logos/rust-logo-128x128-blk-v2.png",
4444
html_favicon_url = "https://doc.rust-lang.org/favicon.ico",
4545
html_root_url = "https://doc.rust-lang.org/nightly/")]
46-
#![forbid(unsafe_code)]
4746

4847
#![cfg_attr(not(stage0), feature(nll))]
4948
#![feature(try_from)]

src/librustc_borrowck/borrowck/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -728,7 +728,7 @@ impl<'a, 'tcx> BorrowckCtxt<'a, 'tcx> {
728728
});
729729
let (move_span, move_note) = match the_move.kind {
730730
move_data::Declared => {
731-
unreachable!();
731+
unsafe { ::std::hint::unreachable_unchecked() };
732732
}
733733

734734
move_data::MoveExpr |

src/librustc_codegen_llvm/back/lto.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ pub(crate) fn run(cgcx: &CodegenContext,
201201
}
202202
thin_lto(&diag_handler, modules, upstream_modules, &arr, timeline)
203203
}
204-
Lto::No => unreachable!(),
204+
Lto::No => unsafe { ::std::hint::unreachable_unchecked() },
205205
}
206206
}
207207

0 commit comments

Comments
 (0)