Skip to content

Commit 1451ae6

Browse files
committed
Auto merge of #116281 - Nadrieril:eager-const-eval, r=<try>
Cleanup number handling in match exhaustiveness Doing a little bit of cleanup; handling number constants was somewhat messy. In particular, this: - evals float consts once instead of repetitively - reduces `Constructor` from 88 bytes to 56 (`mir::Const` is big!) The `fast_try_eval_bits` function was mostly constructed from inlining existing code but I don't fully understand it; I don't follow how consts work and are evaluated very well.
2 parents ca62d2c + bc3ee37 commit 1451ae6

File tree

4 files changed

+240
-139
lines changed

4 files changed

+240
-139
lines changed

compiler/rustc_middle/src/mir/consts.rs

Lines changed: 14 additions & 1 deletion
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)