Skip to content

Commit bc3ee37

Browse files
committed
Optim was useful! Let's share it with everyone
1 parent a5637e5 commit bc3ee37

File tree

1 file changed

+14
-1
lines changed

1 file changed

+14
-1
lines changed

Diff for: compiler/rustc_middle/src/mir/consts.rs

+14-1
Original file line numberDiff line numberDiff line change
@@ -293,7 +293,20 @@ impl<'tcx> Const<'tcx> {
293293

294294
#[inline]
295295
pub fn try_eval_bits(&self, tcx: TyCtxt<'tcx>, param_env: ty::ParamEnv<'tcx>) -> Option<u128> {
296-
let int = self.try_eval_scalar_int(tcx, param_env)?;
296+
debug_assert!(matches!(
297+
self.ty().kind(),
298+
ty::Bool | ty::Int(_) | ty::Uint(_) | ty::Float(_) | ty::Char
299+
));
300+
let int = match self {
301+
// If the constant is already evaluated, we shortcut here.
302+
Const::Ty(c) if let ty::ConstKind::Value(valtree) = c.kind() => {
303+
valtree.unwrap_leaf()
304+
},
305+
// This is a more general form of the previous case.
306+
_ => {
307+
self.try_eval_scalar_int(tcx, param_env)?
308+
},
309+
};
297310
let size =
298311
tcx.layout_of(param_env.with_reveal_all_normalized(tcx).and(self.ty())).ok()?.size;
299312
int.to_bits(size).ok()

0 commit comments

Comments
 (0)