@@ -213,10 +213,10 @@ 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 leaves directly represent the scalar const value.
218
+ Some ( valtree . unwrap_leaf ( ) . into ( ) )
219
+ }
220
220
_ => None ,
221
221
} ,
222
222
Const :: Val ( val, _) => val. try_to_scalar ( ) ,
@@ -279,7 +279,16 @@ impl<'tcx> Const<'tcx> {
279
279
tcx : TyCtxt < ' tcx > ,
280
280
param_env : ty:: ParamEnv < ' tcx > ,
281
281
) -> Option < Scalar > {
282
- self . eval ( tcx, param_env, None ) . ok ( ) ?. try_to_scalar ( )
282
+ match self {
283
+ Const :: Ty ( c) if c. ty ( ) . is_primitive ( ) => {
284
+ // Avoid the `valtree_to_const_val` query. Can only be done on primitive types that
285
+ // are valtree leaves, and *not* on references. (References should return the
286
+ // pointer here, which valtrees don't represent.)
287
+ let val = c. eval ( tcx, param_env, None ) . ok ( ) ?;
288
+ Some ( val. unwrap_leaf ( ) . into ( ) )
289
+ }
290
+ _ => self . eval ( tcx, param_env, None ) . ok ( ) ?. try_to_scalar ( ) ,
291
+ }
283
292
}
284
293
285
294
#[ inline]
@@ -288,16 +297,7 @@ impl<'tcx> Const<'tcx> {
288
297
tcx : TyCtxt < ' tcx > ,
289
298
param_env : ty:: ParamEnv < ' tcx > ,
290
299
) -> 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
- }
300
+ self . try_eval_scalar ( tcx, param_env) ?. try_to_int ( ) . ok ( )
301
301
}
302
302
303
303
#[ inline]
0 commit comments