@@ -273,59 +273,63 @@ pub(crate) fn eval_to_valtree<'tcx>(
273273/// Converts a `ValTree` to a `ConstValue`, which is needed after mir
274274/// construction has finished.
275275// FIXME(valtrees): Merge `valtree_to_const_value` and `valtree_into_mplace` into one function
276- // FIXME(valtrees): Accept `ty::Value` instead of `Ty` and `ty::ValTree` separately.
277276#[ instrument( skip( tcx) , level = "debug" , ret) ]
278277pub fn valtree_to_const_value < ' tcx > (
279278 tcx : TyCtxt < ' tcx > ,
280279 typing_env : ty:: TypingEnv < ' tcx > ,
281- ty : Ty < ' tcx > ,
282- valtree : ty:: ValTree < ' tcx > ,
280+ cv : ty:: Value < ' tcx > ,
283281) -> mir:: ConstValue < ' tcx > {
284282 // Basic idea: We directly construct `Scalar` values from trivial `ValTree`s
285283 // (those for constants with type bool, int, uint, float or char).
286284 // For all other types we create an `MPlace` and fill that by walking
287285 // the `ValTree` and using `place_projection` and `place_field` to
288286 // create inner `MPlace`s which are filled recursively.
289- // FIXME Does this need an example?
290- match * ty. kind ( ) {
287+ // FIXME: Does this need an example?
288+ match * cv . ty . kind ( ) {
291289 ty:: FnDef ( ..) => {
292- assert ! ( valtree. unwrap_branch( ) . is_empty( ) ) ;
290+ assert ! ( cv . valtree. unwrap_branch( ) . is_empty( ) ) ;
293291 mir:: ConstValue :: ZeroSized
294292 }
295293 ty:: Bool | ty:: Int ( _) | ty:: Uint ( _) | ty:: Float ( _) | ty:: Char | ty:: RawPtr ( _, _) => {
296- match valtree {
294+ match cv . valtree {
297295 ty:: ValTree :: Leaf ( scalar_int) => mir:: ConstValue :: Scalar ( Scalar :: Int ( scalar_int) ) ,
298296 ty:: ValTree :: Branch ( _) => bug ! (
299297 "ValTrees for Bool, Int, Uint, Float, Char or RawPtr should have the form ValTree::Leaf"
300298 ) ,
301299 }
302300 }
303- ty:: Pat ( ty, _) => valtree_to_const_value ( tcx, typing_env, ty, valtree) ,
301+ ty:: Pat ( ty, _) => {
302+ let cv = ty:: Value { valtree : cv. valtree , ty } ;
303+ valtree_to_const_value ( tcx, typing_env, cv)
304+ }
304305 ty:: Ref ( _, inner_ty, _) => {
305306 let mut ecx =
306307 mk_eval_cx_to_read_const_val ( tcx, DUMMY_SP , typing_env, CanAccessMutGlobal :: No ) ;
307- let imm = valtree_to_ref ( & mut ecx, valtree, inner_ty) ;
308- let imm =
309- ImmTy :: from_immediate ( imm, tcx. layout_of ( typing_env. as_query_input ( ty) ) . unwrap ( ) ) ;
308+ let imm = valtree_to_ref ( & mut ecx, cv. valtree , inner_ty) ;
309+ let imm = ImmTy :: from_immediate (
310+ imm,
311+ tcx. layout_of ( typing_env. as_query_input ( cv. ty ) ) . unwrap ( ) ,
312+ ) ;
310313 op_to_const ( & ecx, & imm. into ( ) , /* for diagnostics */ false )
311314 }
312315 ty:: Tuple ( _) | ty:: Array ( _, _) | ty:: Adt ( ..) => {
313- let layout = tcx. layout_of ( typing_env. as_query_input ( ty) ) . unwrap ( ) ;
316+ let layout = tcx. layout_of ( typing_env. as_query_input ( cv . ty ) ) . unwrap ( ) ;
314317 if layout. is_zst ( ) {
315318 // Fast path to avoid some allocations.
316319 return mir:: ConstValue :: ZeroSized ;
317320 }
318321 if layout. backend_repr . is_scalar ( )
319- && ( matches ! ( ty. kind( ) , ty:: Tuple ( _) )
320- || matches ! ( ty. kind( ) , ty:: Adt ( def, _) if def. is_struct( ) ) )
322+ && ( matches ! ( cv . ty. kind( ) , ty:: Tuple ( _) )
323+ || matches ! ( cv . ty. kind( ) , ty:: Adt ( def, _) if def. is_struct( ) ) )
321324 {
322325 // A Scalar tuple/struct; we can avoid creating an allocation.
323- let branches = valtree. unwrap_branch ( ) ;
326+ let branches = cv . valtree . unwrap_branch ( ) ;
324327 // Find the non-ZST field. (There can be aligned ZST!)
325328 for ( i, & inner_valtree) in branches. iter ( ) . enumerate ( ) {
326329 let field = layout. field ( & LayoutCx :: new ( tcx, typing_env) , i) ;
327330 if !field. is_zst ( ) {
328- return valtree_to_const_value ( tcx, typing_env, field. ty , inner_valtree) ;
331+ let cv = ty:: Value { valtree : inner_valtree, ty : field. ty } ;
332+ return valtree_to_const_value ( tcx, typing_env, cv) ;
329333 }
330334 }
331335 bug ! ( "could not find non-ZST field during in {layout:#?}" ) ;
@@ -335,9 +339,9 @@ pub fn valtree_to_const_value<'tcx>(
335339 mk_eval_cx_to_read_const_val ( tcx, DUMMY_SP , typing_env, CanAccessMutGlobal :: No ) ;
336340
337341 // Need to create a place for this valtree.
338- let place = create_valtree_place ( & mut ecx, layout, valtree) ;
342+ let place = create_valtree_place ( & mut ecx, layout, cv . valtree ) ;
339343
340- valtree_into_mplace ( & mut ecx, & place, valtree) ;
344+ valtree_into_mplace ( & mut ecx, & place, cv . valtree ) ;
341345 dump_place ( & ecx, & place) ;
342346 intern_const_alloc_recursive ( & mut ecx, InternKind :: Constant , & place) . unwrap ( ) ;
343347
@@ -362,7 +366,7 @@ pub fn valtree_to_const_value<'tcx>(
362366 | ty:: Slice ( _)
363367 | ty:: Dynamic ( ..)
364368 | ty:: UnsafeBinder ( _) => {
365- bug ! ( "no ValTree should have been created for type {:?}" , ty. kind( ) )
369+ bug ! ( "no ValTree should have been created for type {:?}" , cv . ty. kind( ) )
366370 }
367371 }
368372}
0 commit comments