@@ -2373,23 +2373,19 @@ impl<'tcx> ConstantKind<'tcx> {
2373
2373
param_env : ty:: ParamEnv < ' tcx > ,
2374
2374
span : Option < Span > ,
2375
2375
) -> Result < interpret:: ConstValue < ' tcx > , ErrorHandled > {
2376
- let ( uneval , param_env ) = match self {
2376
+ match self {
2377
2377
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) ) )
2387
2382
}
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
+ }
2393
2389
}
2394
2390
2395
2391
/// Normalizes the constant to a value or an error if possible.
@@ -2605,10 +2601,10 @@ impl<'tcx> ConstantKind<'tcx> {
2605
2601
pub fn from_ty_const ( c : ty:: Const < ' tcx > , tcx : TyCtxt < ' tcx > ) -> Self {
2606
2602
match c. kind ( ) {
2607
2603
ty:: ConstKind :: Value ( valtree) => {
2604
+ // Make sure that if `c` is normalized, then the return value is normalized.
2608
2605
let const_val = tcx. valtree_to_const_val ( ( c. ty ( ) , valtree) ) ;
2609
2606
Self :: Val ( const_val, c. ty ( ) )
2610
2607
}
2611
- ty:: ConstKind :: Unevaluated ( uv) => Self :: Unevaluated ( uv. expand ( ) , c. ty ( ) ) ,
2612
2608
_ => Self :: Ty ( c) ,
2613
2609
}
2614
2610
}
0 commit comments