@@ -213,10 +213,11 @@ impl<'tcx> Const<'tcx> {
213213 pub fn try_to_scalar ( self ) -> Option < Scalar > {
214214 match self {
215215 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+ }
220221 _ => None ,
221222 } ,
222223 Const :: Val ( val, _) => val. try_to_scalar ( ) ,
@@ -279,7 +280,16 @@ impl<'tcx> Const<'tcx> {
279280 tcx : TyCtxt < ' tcx > ,
280281 param_env : ty:: ParamEnv < ' tcx > ,
281282 ) -> 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+ }
283293 }
284294
285295 #[ inline]
@@ -288,16 +298,7 @@ impl<'tcx> Const<'tcx> {
288298 tcx : TyCtxt < ' tcx > ,
289299 param_env : ty:: ParamEnv < ' tcx > ,
290300 ) -> 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 ( )
301302 }
302303
303304 #[ inline]
0 commit comments