@@ -2373,23 +2373,19 @@ impl<'tcx> ConstantKind<'tcx> {
23732373 param_env : ty:: ParamEnv < ' tcx > ,
23742374 span : Option < Span > ,
23752375 ) -> Result < interpret:: ConstValue < ' tcx > , ErrorHandled > {
2376- let ( uneval , param_env ) = match self {
2376+ match self {
23772377 ConstantKind :: Ty ( c) => {
2378- if let ty:: ConstKind :: Unevaluated ( uneval) = c. kind ( ) {
2379- // Avoid the round-trip via valtree, evaluate directly to ConstValue.
2380- let ( param_env, uneval) = uneval. prepare_for_eval ( tcx, param_env) ;
2381- ( uneval. expand ( ) , param_env)
2382- } else {
2383- // It's already a valtree, or an error.
2384- let val = c. eval ( tcx, param_env, span) ?;
2385- return Ok ( tcx. valtree_to_const_val ( ( self . ty ( ) , val) ) ) ;
2386- }
2378+ // We want to consistently have a "clean" value for type system constants (i.e., no
2379+ // data hidden in the padding), so we always go through a valtree here.
2380+ let val = c. eval ( tcx, param_env, span) ?;
2381+ Ok ( tcx. valtree_to_const_val ( ( self . ty ( ) , val) ) )
23872382 }
2388- ConstantKind :: Unevaluated ( uneval, _) => ( uneval, param_env) ,
2389- ConstantKind :: Val ( val, _) => return Ok ( val) ,
2390- } ;
2391- // FIXME: We might want to have a `try_eval`-like function on `Unevaluated`
2392- tcx. const_eval_resolve ( param_env, uneval, span)
2383+ ConstantKind :: Unevaluated ( uneval, _) => {
2384+ // FIXME: We might want to have a `try_eval`-like function on `Unevaluated`
2385+ tcx. const_eval_resolve ( param_env, uneval, span)
2386+ }
2387+ ConstantKind :: Val ( val, _) => Ok ( val) ,
2388+ }
23932389 }
23942390
23952391 /// Normalizes the constant to a value or an error if possible.
@@ -2605,10 +2601,10 @@ impl<'tcx> ConstantKind<'tcx> {
26052601 pub fn from_ty_const ( c : ty:: Const < ' tcx > , tcx : TyCtxt < ' tcx > ) -> Self {
26062602 match c. kind ( ) {
26072603 ty:: ConstKind :: Value ( valtree) => {
2604+ // Make sure that if `c` is normalized, then the return value is normalized.
26082605 let const_val = tcx. valtree_to_const_val ( ( c. ty ( ) , valtree) ) ;
26092606 Self :: Val ( const_val, c. ty ( ) )
26102607 }
2611- ty:: ConstKind :: Unevaluated ( uv) => Self :: Unevaluated ( uv. expand ( ) , c. ty ( ) ) ,
26122608 _ => Self :: Ty ( c) ,
26132609 }
26142610 }
0 commit comments