1 #![stable(feature = "core_hint", since = "1.27.0")]
3 //! Hints to compiler that affects how code should be emitted or optimized.
7 /// Informs the compiler that this point in the code is not reachable, enabling
8 /// further optimizations.
12 /// Reaching this function is completely *undefined behavior* (UB). In
13 /// particular, the compiler assumes that all UB must never happen, and
14 /// therefore will eliminate all branches that reach to a call to
15 /// `unreachable_unchecked()`.
17 /// Like all instances of UB, if this assumption turns out to be wrong, i.e., the
18 /// `unreachable_unchecked()` call is actually reachable among all possible
19 /// control flow, the compiler will apply the wrong optimization strategy, and
20 /// may sometimes even corrupt seemingly unrelated code, causing
21 /// difficult-to-debug problems.
23 /// Use this function only when you can prove that the code will never call it.
25 /// The [`unreachable!()`] macro is the safe counterpart of this function, which
26 /// will panic instead when executed.
28 /// [`unreachable!()`]: ../macro.unreachable.html
33 /// fn div_1(a: u32, b: u32) -> u32 {
34 /// use std::hint::unreachable_unchecked;
36 /// // `b.saturating_add(1)` is always positive (not zero),
37 /// // hence `checked_div` will never return None.
38 /// // Therefore, the else branch is unreachable.
39 /// a.checked_div(b.saturating_add(1))
40 /// .unwrap_or_else(|| unsafe { unreachable_unchecked() })
43 /// assert_eq!(div_1(7, 0), 7);
44 /// assert_eq!(div_1(9, 1), 4);
45 /// assert_eq!(div_1(11, std::u32::MAX), 0);
48 #[stable(feature = "unreachable", since = "1.27.0")]
49 pub unsafe fn unreachable_unchecked() -> ! {
50 intrinsics::unreachable()