@@ -213,10 +213,11 @@ impl<'tcx> Const<'tcx> {
213
213
pub fn try_to_scalar ( self ) -> Option < Scalar > {
214
214
match self {
215
215
Const :: Ty ( c) => match c. kind ( ) {
216
- ty:: ConstKind :: Value ( valtree) => match valtree {
217
- ty:: ValTree :: Leaf ( scalar_int) => Some ( Scalar :: Int ( scalar_int) ) ,
218
- ty:: ValTree :: Branch ( _) => None ,
219
- } ,
216
+ ty:: ConstKind :: Value ( valtree) if c. ty ( ) . is_primitive ( ) => {
217
+ // A valtree of a type where valtree leaves directly represent the scalar const
218
+ // value.
219
+ valtree. try_to_scalar ( )
220
+ }
220
221
_ => None ,
221
222
} ,
222
223
Const :: Val ( val, _) => val. try_to_scalar ( ) ,
@@ -279,7 +280,16 @@ impl<'tcx> Const<'tcx> {
279
280
tcx : TyCtxt < ' tcx > ,
280
281
param_env : ty:: ParamEnv < ' tcx > ,
281
282
) -> Option < Scalar > {
282
- self . eval ( tcx, param_env, None ) . ok ( ) ?. try_to_scalar ( )
283
+ match self {
284
+ Const :: Ty ( c) if c. ty ( ) . is_primitive ( ) => {
285
+ // Avoid the `valtree_to_const_val` query. Can only be done on primitive types that
286
+ // are valtree leaves, and *not* on references. (References should return the
287
+ // pointer here, which valtrees don't represent.)
288
+ let val = c. eval ( tcx, param_env, None ) . ok ( ) ?;
289
+ val. try_to_scalar ( )
290
+ }
291
+ _ => self . eval ( tcx, param_env, None ) . ok ( ) ?. try_to_scalar ( ) ,
292
+ }
283
293
}
284
294
285
295
#[ inline]
@@ -288,16 +298,7 @@ impl<'tcx> Const<'tcx> {
288
298
tcx : TyCtxt < ' tcx > ,
289
299
param_env : ty:: ParamEnv < ' tcx > ,
290
300
) -> Option < ScalarInt > {
291
- match self {
292
- // If the constant is already evaluated, we shortcut here.
293
- Const :: Ty ( c) if let ty:: ConstKind :: Value ( valtree) = c. kind ( ) => {
294
- valtree. try_to_scalar_int ( )
295
- } ,
296
- // This is a more general form of the previous case.
297
- _ => {
298
- self . try_eval_scalar ( tcx, param_env) ?. try_to_int ( ) . ok ( )
299
- } ,
300
- }
301
+ self . try_eval_scalar ( tcx, param_env) ?. try_to_int ( ) . ok ( )
301
302
}
302
303
303
304
#[ inline]
0 commit comments