@@ -213,10 +213,10 @@ 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 leaves directly represent the scalar const value. 
218+                     Some ( valtree . unwrap_leaf ( ) . into ( ) ) 
219+                 } 
220220                _ => None , 
221221            } , 
222222            Const :: Val ( val,  _)  => val. try_to_scalar ( ) , 
@@ -279,7 +279,16 @@ impl<'tcx> Const<'tcx> {
279279        tcx :  TyCtxt < ' tcx > , 
280280        param_env :  ty:: ParamEnv < ' tcx > , 
281281    )  -> 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+         } 
283292    } 
284293
285294    #[ inline]  
@@ -288,16 +297,7 @@ impl<'tcx> Const<'tcx> {
288297        tcx :  TyCtxt < ' tcx > , 
289298        param_env :  ty:: ParamEnv < ' tcx > , 
290299    )  -> 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 ( ) 
301301    } 
302302
303303    #[ inline]  
0 commit comments