From 292d5bba86d2d04813be5b887cd86a98876e02ea Mon Sep 17 00:00:00 2001 From: Ralf Jung Date: Thu, 14 Sep 2023 07:44:49 +0200 Subject: [PATCH] always evaluate ConstantKind::Ty through valtrees --- compiler/rustc_middle/src/mir/mod.rs | 28 ++++++++++++---------------- 1 file changed, 12 insertions(+), 16 deletions(-) diff --git a/compiler/rustc_middle/src/mir/mod.rs b/compiler/rustc_middle/src/mir/mod.rs index 3b22ecfbe50c..1ce7140f361d 100644 --- a/compiler/rustc_middle/src/mir/mod.rs +++ b/compiler/rustc_middle/src/mir/mod.rs @@ -2373,23 +2373,19 @@ impl<'tcx> ConstantKind<'tcx> { param_env: ty::ParamEnv<'tcx>, span: Option, ) -> Result, ErrorHandled> { - let (uneval, param_env) = match self { + match self { ConstantKind::Ty(c) => { - if let ty::ConstKind::Unevaluated(uneval) = c.kind() { - // Avoid the round-trip via valtree, evaluate directly to ConstValue. - let (param_env, uneval) = uneval.prepare_for_eval(tcx, param_env); - (uneval.expand(), param_env) - } else { - // It's already a valtree, or an error. - let val = c.eval(tcx, param_env, span)?; - return Ok(tcx.valtree_to_const_val((self.ty(), val))); - } + // We want to consistently have a "clean" value for type system constants (i.e., no + // data hidden in the padding), so we always go through a valtree here. + let val = c.eval(tcx, param_env, span)?; + Ok(tcx.valtree_to_const_val((self.ty(), val))) } - ConstantKind::Unevaluated(uneval, _) => (uneval, param_env), - ConstantKind::Val(val, _) => return Ok(val), - }; - // FIXME: We might want to have a `try_eval`-like function on `Unevaluated` - tcx.const_eval_resolve(param_env, uneval, span) + ConstantKind::Unevaluated(uneval, _) => { + // FIXME: We might want to have a `try_eval`-like function on `Unevaluated` + tcx.const_eval_resolve(param_env, uneval, span) + } + ConstantKind::Val(val, _) => Ok(val), + } } /// Normalizes the constant to a value or an error if possible. @@ -2605,10 +2601,10 @@ impl<'tcx> ConstantKind<'tcx> { pub fn from_ty_const(c: ty::Const<'tcx>, tcx: TyCtxt<'tcx>) -> Self { match c.kind() { ty::ConstKind::Value(valtree) => { + // Make sure that if `c` is normalized, then the return value is normalized. let const_val = tcx.valtree_to_const_val((c.ty(), valtree)); Self::Val(const_val, c.ty()) } - ty::ConstKind::Unevaluated(uv) => Self::Unevaluated(uv.expand(), c.ty()), _ => Self::Ty(c), } }