@@ -12,8 +12,8 @@ use rustc_data_structures::indexed_vec::Idx;
12
12
use syntax:: ast:: Mutability ;
13
13
use syntax:: codemap:: Span ;
14
14
15
- use rustc:: mir:: interpret:: { EvalResult , EvalError , EvalErrorKind , GlobalId , Value , Pointer , PrimVal , PtrAndAlign } ;
16
- use super :: { Place , PlaceExtra , EvalContext , StackPopCleanup , ValTy , HasMemory } ;
15
+ use rustc:: mir:: interpret:: { EvalResult , EvalError , EvalErrorKind , GlobalId , Value , Pointer , PrimVal } ;
16
+ use super :: { Place , EvalContext , StackPopCleanup , ValTy } ;
17
17
18
18
use rustc_const_math:: ConstInt ;
19
19
@@ -357,11 +357,9 @@ pub fn const_eval_provider<'a, 'tcx>(
357
357
( _, Err ( err) ) => Err ( err) ,
358
358
( Ok ( ( miri_val, miri_ty) ) , Ok ( ctfe) ) => {
359
359
let mut ecx = mk_eval_cx ( tcx, instance, key. param_env ) . unwrap ( ) ;
360
- let miri_ptr = PtrAndAlign {
361
- ptr : miri_val,
362
- align : ecx. layout_of ( miri_ty) . unwrap ( ) . align
363
- } ;
364
- check_ctfe_against_miri ( & mut ecx, miri_ptr, miri_ty, ctfe. val ) ;
360
+ let layout = ecx. layout_of ( miri_ty) . unwrap ( ) ;
361
+ let miri_place = Place :: from_primval_ptr ( miri_val, layout. align ) ;
362
+ check_ctfe_against_miri ( & mut ecx, miri_place, miri_ty, ctfe. val ) ;
365
363
Ok ( ctfe)
366
364
}
367
365
}
@@ -372,60 +370,49 @@ pub fn const_eval_provider<'a, 'tcx>(
372
370
373
371
fn check_ctfe_against_miri < ' a , ' tcx > (
374
372
ecx : & mut EvalContext < ' a , ' tcx , CompileTimeEvaluator > ,
375
- miri_val : PtrAndAlign ,
373
+ miri_place : Place ,
376
374
miri_ty : Ty < ' tcx > ,
377
375
ctfe : ConstVal < ' tcx > ,
378
376
) {
379
377
use rustc:: middle:: const_val:: ConstAggregate :: * ;
380
378
use rustc_const_math:: ConstFloat ;
381
379
use rustc:: ty:: TypeVariants :: * ;
380
+ let miri_val = ValTy {
381
+ value : ecx. read_place ( miri_place) . unwrap ( ) ,
382
+ ty : miri_ty
383
+ } ;
382
384
match miri_ty. sty {
383
385
TyInt ( int_ty) => {
384
- let value = ecx. read_with_align ( miri_val. align , |ectx| {
385
- ectx. try_read_value ( miri_val. ptr , miri_ty)
386
- } ) ;
387
- let prim = get_prim ( ecx, value) ;
386
+ let prim = get_prim ( ecx, miri_val) ;
388
387
let c = ConstInt :: new_signed_truncating ( prim as i128 ,
389
388
int_ty,
390
389
ecx. tcx . sess . target . isize_ty ) ;
391
390
let c = ConstVal :: Integral ( c) ;
392
391
assert_eq ! ( c, ctfe, "miri evaluated to {:?}, but ctfe yielded {:?}" , c, ctfe) ;
393
392
} ,
394
393
TyUint ( uint_ty) => {
395
- let value = ecx. read_with_align ( miri_val. align , |ectx| {
396
- ectx. try_read_value ( miri_val. ptr , miri_ty)
397
- } ) ;
398
- let prim = get_prim ( ecx, value) ;
394
+ let prim = get_prim ( ecx, miri_val) ;
399
395
let c = ConstInt :: new_unsigned_truncating ( prim,
400
396
uint_ty,
401
397
ecx. tcx . sess . target . usize_ty ) ;
402
398
let c = ConstVal :: Integral ( c) ;
403
399
assert_eq ! ( c, ctfe, "miri evaluated to {:?}, but ctfe yielded {:?}" , c, ctfe) ;
404
400
} ,
405
401
TyFloat ( ty) => {
406
- let value = ecx. read_with_align ( miri_val. align , |ectx| {
407
- ectx. try_read_value ( miri_val. ptr , miri_ty)
408
- } ) ;
409
- let prim = get_prim ( ecx, value) ;
402
+ let prim = get_prim ( ecx, miri_val) ;
410
403
let f = ConstVal :: Float ( ConstFloat { bits : prim, ty } ) ;
411
404
assert_eq ! ( f, ctfe, "miri evaluated to {:?}, but ctfe yielded {:?}" , f, ctfe) ;
412
405
} ,
413
406
TyBool => {
414
- let value = ecx. read_with_align ( miri_val. align , |ectx| {
415
- ectx. try_read_value ( miri_val. ptr , miri_ty)
416
- } ) ;
417
- let bits = get_prim ( ecx, value) ;
407
+ let bits = get_prim ( ecx, miri_val) ;
418
408
if bits > 1 {
419
409
bug ! ( "miri evaluated to {}, but expected a bool {:?}" , bits, ctfe) ;
420
410
}
421
411
let b = ConstVal :: Bool ( bits == 1 ) ;
422
412
assert_eq ! ( b, ctfe, "miri evaluated to {:?}, but ctfe yielded {:?}" , b, ctfe) ;
423
413
} ,
424
414
TyChar => {
425
- let value = ecx. read_with_align ( miri_val. align , |ectx| {
426
- ectx. try_read_value ( miri_val. ptr , miri_ty)
427
- } ) ;
428
- let bits = get_prim ( ecx, value) ;
415
+ let bits = get_prim ( ecx, miri_val) ;
429
416
if let Some ( cm) = :: std:: char:: from_u32 ( bits as u32 ) {
430
417
assert_eq ! (
431
418
ConstVal :: Char ( cm) , ctfe,
@@ -436,10 +423,8 @@ fn check_ctfe_against_miri<'a, 'tcx>(
436
423
}
437
424
} ,
438
425
TyStr => {
439
- let value = ecx. read_with_align ( miri_val. align , |ectx| {
440
- ectx. try_read_value ( miri_val. ptr , miri_ty)
441
- } ) ;
442
- if let Ok ( Some ( Value :: ByValPair ( PrimVal :: Ptr ( ptr) , PrimVal :: Bytes ( len) ) ) ) = value {
426
+ let value = ecx. follow_by_ref_value ( miri_val. value , miri_val. ty ) ;
427
+ if let Ok ( Value :: ByValPair ( PrimVal :: Ptr ( ptr) , PrimVal :: Bytes ( len) ) ) = value {
443
428
let bytes = ecx
444
429
. memory
445
430
. read_bytes ( ptr. into ( ) , len as u64 )
@@ -463,7 +448,6 @@ fn check_ctfe_against_miri<'a, 'tcx>(
463
448
} ,
464
449
TyArray ( elem_ty, n) => {
465
450
let n = n. val . to_const_int ( ) . unwrap ( ) . to_u64 ( ) . unwrap ( ) ;
466
- let size = ecx. layout_of ( elem_ty) . unwrap ( ) . size . bytes ( ) ;
467
451
let vec: Vec < ( ConstVal , Ty < ' tcx > ) > = match ctfe {
468
452
ConstVal :: ByteStr ( arr) => arr. data . iter ( ) . map ( |& b| {
469
453
( ConstVal :: Integral ( ConstInt :: U8 ( b) ) , ecx. tcx . types . u8 )
@@ -476,10 +460,12 @@ fn check_ctfe_against_miri<'a, 'tcx>(
476
460
} ,
477
461
_ => bug ! ( "miri produced {:?}, but ctfe yielded {:?}" , miri_ty, ctfe) ,
478
462
} ;
463
+ let layout = ecx. layout_of ( miri_ty) . unwrap ( ) ;
479
464
for ( i, elem) in vec. into_iter ( ) . enumerate ( ) {
480
465
assert ! ( ( i as u64 ) < n) ;
481
- let ptr = miri_val. offset ( size * i as u64 , & ecx) . unwrap ( ) ;
482
- check_ctfe_against_miri ( ecx, ptr, elem_ty, elem. 0 ) ;
466
+ let ( field_place, _) =
467
+ ecx. place_field ( miri_place, Field :: new ( i) , layout) . unwrap ( ) ;
468
+ check_ctfe_against_miri ( ecx, field_place, elem_ty, elem. 0 ) ;
483
469
}
484
470
} ,
485
471
TyTuple ( ..) => {
@@ -489,22 +475,22 @@ fn check_ctfe_against_miri<'a, 'tcx>(
489
475
} ;
490
476
let layout = ecx. layout_of ( miri_ty) . unwrap ( ) ;
491
477
for ( i, elem) in vec. into_iter ( ) . enumerate ( ) {
492
- let offset = layout . fields . offset ( i ) ;
493
- let ptr = miri_val . offset ( offset . bytes ( ) , & ecx ) . unwrap ( ) ;
494
- check_ctfe_against_miri ( ecx, ptr , elem. ty , elem. val ) ;
478
+ let ( field_place , _ ) =
479
+ ecx . place_field ( miri_place , Field :: new ( i ) , layout ) . unwrap ( ) ;
480
+ check_ctfe_against_miri ( ecx, field_place , elem. ty , elem. val ) ;
495
481
}
496
482
} ,
497
483
TyAdt ( def, _) => {
498
- let ( struct_variant, extra) = if def. is_enum ( ) {
499
- let discr = ecx. read_discriminant_value (
500
- Place :: Ptr { ptr : miri_val, extra : PlaceExtra :: None } ,
501
- miri_ty) . unwrap ( ) ;
484
+ let mut miri_place = miri_place;
485
+ let struct_variant = if def. is_enum ( ) {
486
+ let discr = ecx. read_discriminant_value ( miri_place, miri_ty) . unwrap ( ) ;
502
487
let variant = def. discriminants ( ecx. tcx ) . position ( |variant_discr| {
503
488
variant_discr. to_u128_unchecked ( ) == discr
504
489
} ) . expect ( "miri produced invalid enum discriminant" ) ;
505
- ( & def. variants [ variant] , PlaceExtra :: DowncastVariant ( variant) )
490
+ miri_place = ecx. place_downcast ( miri_place, variant) . unwrap ( ) ;
491
+ & def. variants [ variant]
506
492
} else {
507
- ( def. struct_variant ( ) , PlaceExtra :: None )
493
+ def. struct_variant ( )
508
494
} ;
509
495
let vec = match ctfe {
510
496
ConstVal :: Aggregate ( Struct ( v) ) => v,
@@ -518,12 +504,9 @@ fn check_ctfe_against_miri<'a, 'tcx>(
518
504
let layout = ecx. layout_of ( miri_ty) . unwrap ( ) ;
519
505
for & ( name, elem) in vec. into_iter ( ) {
520
506
let field = struct_variant. fields . iter ( ) . position ( |f| f. name == name) . unwrap ( ) ;
521
- let ( place, _) = ecx. place_field (
522
- Place :: Ptr { ptr : miri_val, extra } ,
523
- Field :: new ( field) ,
524
- layout,
525
- ) . unwrap ( ) ;
526
- check_ctfe_against_miri ( ecx, place. to_ptr_align ( ) , elem. ty , elem. val ) ;
507
+ let ( field_place, _) =
508
+ ecx. place_field ( miri_place, Field :: new ( field) , layout) . unwrap ( ) ;
509
+ check_ctfe_against_miri ( ecx, field_place, elem. ty , elem. val ) ;
527
510
}
528
511
} ,
529
512
TySlice ( _) => bug ! ( "miri produced a slice?" ) ,
@@ -543,11 +526,9 @@ fn check_ctfe_against_miri<'a, 'tcx>(
543
526
// should be fine
544
527
TyFnDef ( ..) => { }
545
528
TyFnPtr ( _) => {
546
- let value = ecx. read_with_align ( miri_val. align , |ectx| {
547
- ectx. try_read_value ( miri_val. ptr , miri_ty)
548
- } ) ;
529
+ let value = ecx. value_to_primval ( miri_val) ;
549
530
let ptr = match value {
550
- Ok ( Some ( Value :: ByVal ( PrimVal :: Ptr ( ptr) ) ) ) => ptr,
531
+ Ok ( PrimVal :: Ptr ( ptr) ) => ptr,
551
532
value => bug ! ( "expected fn ptr, got {:?}" , value) ,
552
533
} ;
553
534
let inst = ecx. memory . get_fn ( ptr) . unwrap ( ) ;
@@ -569,13 +550,10 @@ fn check_ctfe_against_miri<'a, 'tcx>(
569
550
570
551
fn get_prim < ' a , ' tcx > (
571
552
ecx : & mut EvalContext < ' a , ' tcx , CompileTimeEvaluator > ,
572
- res : Result < Option < Value > , EvalError < ' tcx > > ,
553
+ val : ValTy < ' tcx > ,
573
554
) -> u128 {
574
- match res {
575
- Ok ( Some ( Value :: ByVal ( prim) ) ) => unwrap_miri ( ecx, prim. to_bytes ( ) ) ,
576
- Err ( err) => unwrap_miri ( ecx, Err ( err) ) ,
577
- val => bug ! ( "got {:?}" , val) ,
578
- }
555
+ let res = ecx. value_to_primval ( val) . and_then ( |prim| prim. to_bytes ( ) ) ;
556
+ unwrap_miri ( ecx, res)
579
557
}
580
558
581
559
fn unwrap_miri < ' a , ' tcx , T > (
0 commit comments