@@ -273,59 +273,63 @@ pub(crate) fn eval_to_valtree<'tcx>(
273
273
/// Converts a `ValTree` to a `ConstValue`, which is needed after mir
274
274
/// construction has finished.
275
275
// 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.
277
276
#[ instrument( skip( tcx) , level = "debug" , ret) ]
278
277
pub fn valtree_to_const_value < ' tcx > (
279
278
tcx : TyCtxt < ' tcx > ,
280
279
typing_env : ty:: TypingEnv < ' tcx > ,
281
- ty : Ty < ' tcx > ,
282
- valtree : ty:: ValTree < ' tcx > ,
280
+ cv : ty:: Value < ' tcx > ,
283
281
) -> mir:: ConstValue < ' tcx > {
284
282
// Basic idea: We directly construct `Scalar` values from trivial `ValTree`s
285
283
// (those for constants with type bool, int, uint, float or char).
286
284
// For all other types we create an `MPlace` and fill that by walking
287
285
// the `ValTree` and using `place_projection` and `place_field` to
288
286
// 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 ( ) {
291
289
ty:: FnDef ( ..) => {
292
- assert ! ( valtree. unwrap_branch( ) . is_empty( ) ) ;
290
+ assert ! ( cv . valtree. unwrap_branch( ) . is_empty( ) ) ;
293
291
mir:: ConstValue :: ZeroSized
294
292
}
295
293
ty:: Bool | ty:: Int ( _) | ty:: Uint ( _) | ty:: Float ( _) | ty:: Char | ty:: RawPtr ( _, _) => {
296
- match valtree {
294
+ match cv . valtree {
297
295
ty:: ValTree :: Leaf ( scalar_int) => mir:: ConstValue :: Scalar ( Scalar :: Int ( scalar_int) ) ,
298
296
ty:: ValTree :: Branch ( _) => bug ! (
299
297
"ValTrees for Bool, Int, Uint, Float, Char or RawPtr should have the form ValTree::Leaf"
300
298
) ,
301
299
}
302
300
}
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
+ }
304
305
ty:: Ref ( _, inner_ty, _) => {
305
306
let mut ecx =
306
307
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
+ ) ;
310
313
op_to_const ( & ecx, & imm. into ( ) , /* for diagnostics */ false )
311
314
}
312
315
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 ( ) ;
314
317
if layout. is_zst ( ) {
315
318
// Fast path to avoid some allocations.
316
319
return mir:: ConstValue :: ZeroSized ;
317
320
}
318
321
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( ) ) )
321
324
{
322
325
// A Scalar tuple/struct; we can avoid creating an allocation.
323
- let branches = valtree. unwrap_branch ( ) ;
326
+ let branches = cv . valtree . unwrap_branch ( ) ;
324
327
// Find the non-ZST field. (There can be aligned ZST!)
325
328
for ( i, & inner_valtree) in branches. iter ( ) . enumerate ( ) {
326
329
let field = layout. field ( & LayoutCx :: new ( tcx, typing_env) , i) ;
327
330
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) ;
329
333
}
330
334
}
331
335
bug ! ( "could not find non-ZST field during in {layout:#?}" ) ;
@@ -335,9 +339,9 @@ pub fn valtree_to_const_value<'tcx>(
335
339
mk_eval_cx_to_read_const_val ( tcx, DUMMY_SP , typing_env, CanAccessMutGlobal :: No ) ;
336
340
337
341
// 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 ) ;
339
343
340
- valtree_into_mplace ( & mut ecx, & place, valtree) ;
344
+ valtree_into_mplace ( & mut ecx, & place, cv . valtree ) ;
341
345
dump_place ( & ecx, & place) ;
342
346
intern_const_alloc_recursive ( & mut ecx, InternKind :: Constant , & place) . unwrap ( ) ;
343
347
@@ -362,7 +366,7 @@ pub fn valtree_to_const_value<'tcx>(
362
366
| ty:: Slice ( _)
363
367
| ty:: Dynamic ( ..)
364
368
| 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( ) )
366
370
}
367
371
}
368
372
}
0 commit comments