diff --git a/compiler/rustc_middle/src/mir/consts.rs b/compiler/rustc_middle/src/mir/consts.rs index 8f63ed757bad7..cde45943d7051 100644 --- a/compiler/rustc_middle/src/mir/consts.rs +++ b/compiler/rustc_middle/src/mir/consts.rs @@ -279,7 +279,14 @@ impl<'tcx> Const<'tcx> { tcx: TyCtxt<'tcx>, param_env: ty::ParamEnv<'tcx>, ) -> Option { - self.eval(tcx, param_env, None).ok()?.try_to_scalar() + match self { + Const::Ty(c) => { + // Avoid the `valtree_to_const_val` query. + let val = c.eval(tcx, param_env, None).ok()?; + val.try_to_scalar() + } + _ => self.eval(tcx, param_env, None).ok()?.try_to_scalar(), + } } #[inline] @@ -288,16 +295,7 @@ impl<'tcx> Const<'tcx> { tcx: TyCtxt<'tcx>, param_env: ty::ParamEnv<'tcx>, ) -> Option { - match self { - // If the constant is already evaluated, we shortcut here. - Const::Ty(c) if let ty::ConstKind::Value(valtree) = c.kind() => { - valtree.try_to_scalar_int() - }, - // This is a more general form of the previous case. - _ => { - self.try_eval_scalar(tcx, param_env)?.try_to_int().ok() - }, - } + self.try_eval_scalar(tcx, param_env)?.try_to_int().ok() } #[inline]