From bc3ee3798c65fba08fa49defbe67cb992e15fb92 Mon Sep 17 00:00:00 2001 From: Nadrieril Date: Sun, 1 Oct 2023 03:11:03 +0200 Subject: [PATCH] Optim was useful! Let's share it with everyone --- compiler/rustc_middle/src/mir/consts.rs | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/compiler/rustc_middle/src/mir/consts.rs b/compiler/rustc_middle/src/mir/consts.rs index 7c8a57b840b74..5356d1f7c42ca 100644 --- a/compiler/rustc_middle/src/mir/consts.rs +++ b/compiler/rustc_middle/src/mir/consts.rs @@ -293,7 +293,20 @@ impl<'tcx> Const<'tcx> { #[inline] pub fn try_eval_bits(&self, tcx: TyCtxt<'tcx>, param_env: ty::ParamEnv<'tcx>) -> Option { - let int = self.try_eval_scalar_int(tcx, param_env)?; + debug_assert!(matches!( + self.ty().kind(), + ty::Bool | ty::Int(_) | ty::Uint(_) | ty::Float(_) | ty::Char + )); + let int = match self { + // If the constant is already evaluated, we shortcut here. + Const::Ty(c) if let ty::ConstKind::Value(valtree) = c.kind() => { + valtree.unwrap_leaf() + }, + // This is a more general form of the previous case. + _ => { + self.try_eval_scalar_int(tcx, param_env)? + }, + }; let size = tcx.layout_of(param_env.with_reveal_all_normalized(tcx).and(self.ty())).ok()?.size; int.to_bits(size).ok()