@@ -106,6 +106,54 @@ pub const unsafe fn unreachable_unchecked() -> ! {
106
106
}
107
107
}
108
108
109
+ /// Makes a *soundness* promise to the compiler that `cond` holds.
110
+ ///
111
+ /// This may allow the optimizer to simplify things,
112
+ /// but it might also make the generated code slower.
113
+ /// Either way, calling it will most likely make compilation take longer.
114
+ ///
115
+ /// This is a situational tool for micro-optimization, and is allowed to do nothing.
116
+ /// Any use should come with a repeatable benchmark to show the value
117
+ /// and allow removing it later should the optimizer get smarter and no longer need it.
118
+ ///
119
+ /// The more complicated the condition the less likely this is to be fruitful.
120
+ /// For example, `assert_unchecked(foo.is_sorted())` is a complex enough value
121
+ /// that the compiler is unlikely to be able to take advantage of it.
122
+ ///
123
+ /// There's also no need to `assert_unchecked` basic properties of things. For
124
+ /// example, the compiler already knows the range of `count_ones`, so there's no
125
+ /// benefit to `let n = u32::count_ones(x); assert_unchecked(n <= u32::BITS);`.
126
+ ///
127
+ /// If ever you're tempted to write `assert_unchecked(false)`, then you're
128
+ /// actually looking for [`unreachable_unchecked()`].
129
+ ///
130
+ /// You may know this from other places
131
+ /// as [`llvm.assume`](https://llvm.org/docs/LangRef.html#llvm-assume-intrinsic)
132
+ /// or [`__builtin_assume`](https://clang.llvm.org/docs/LanguageExtensions.html#builtin-assume).
133
+ ///
134
+ /// This promotes a correctness requirement to a soundness requirement.
135
+ /// Don't do that without very good reason.
136
+ ///
137
+ /// # Safety
138
+ ///
139
+ /// `cond` must be `true`. It's immediate UB to call this with `false`.
140
+ ///
141
+ #[ inline( always) ]
142
+ #[ doc( alias = "assume" ) ]
143
+ #[ track_caller]
144
+ #[ unstable( feature = "hint_assert_unchecked" , issue = "119131" ) ]
145
+ #[ rustc_const_unstable( feature = "const_hint_assert_unchecked" , issue = "119131" ) ]
146
+ pub const unsafe fn assert_unchecked ( cond : bool ) {
147
+ // SAFETY: The caller promised `cond` is true.
148
+ unsafe {
149
+ intrinsics:: assert_unsafe_precondition!(
150
+ "hint::assert_unchecked must never be called when the condition is false" ,
151
+ ( cond: bool ) => cond,
152
+ ) ;
153
+ crate :: intrinsics:: assume ( cond) ;
154
+ }
155
+ }
156
+
109
157
/// Emits a machine instruction to signal the processor that it is running in
110
158
/// a busy-wait spin-loop ("spin lock").
111
159
///
0 commit comments