@@ -12,7 +12,7 @@ use rustc::mir::interpret::{
12
12
EvalResult , EvalErrorKind ,
13
13
} ;
14
14
use super :: {
15
- EvalContext , Machine , AllocMap , Allocation , AllocationExtra ,
15
+ EvalContext , Machine ,
16
16
MemPlace , MPlaceTy , PlaceTy , Place , MemoryKind ,
17
17
} ;
18
18
pub use rustc:: mir:: interpret:: ScalarMaybeUndef ;
@@ -545,14 +545,7 @@ impl<'a, 'mir, 'tcx, M: Machine<'a, 'mir, 'tcx>> EvalContext<'a, 'mir, 'tcx, M>
545
545
Move ( ref place) =>
546
546
self . eval_place_to_op ( place, layout) ?,
547
547
548
- Constant ( ref constant) => {
549
- let layout = from_known_layout ( layout, || {
550
- let ty = self . monomorphize ( mir_op. ty ( self . mir ( ) , * self . tcx ) ) ?;
551
- self . layout_of ( ty)
552
- } ) ?;
553
- let op = self . const_value_to_op ( * constant. literal ) ?;
554
- OpTy { op, layout }
555
- }
548
+ Constant ( ref constant) => self . lazy_const_to_op ( * constant. literal , layout) ?,
556
549
} ;
557
550
trace ! ( "{:?}: {:?}" , mir_op, * op) ;
558
551
Ok ( op)
@@ -568,38 +561,55 @@ impl<'a, 'mir, 'tcx, M: Machine<'a, 'mir, 'tcx>> EvalContext<'a, 'mir, 'tcx, M>
568
561
. collect ( )
569
562
}
570
563
571
- // Used when Miri runs into a constant, and (indirectly through lazy_const_to_op) by CTFE.
572
- fn const_value_to_op (
564
+ // Used when Miri runs into a constant, and by CTFE.
565
+ pub fn lazy_const_to_op (
573
566
& self ,
574
567
val : ty:: LazyConst < ' tcx > ,
575
- ) -> EvalResult < ' tcx , Operand < M :: PointerTag > > {
576
- trace ! ( "const_value_to_op: {:?}" , val) ;
577
- let val = match val {
568
+ layout : Option < TyLayout < ' tcx > > ,
569
+ ) -> EvalResult < ' tcx , OpTy < ' tcx , M :: PointerTag > > {
570
+ trace ! ( "const_to_op: {:?}" , val) ;
571
+ match val {
578
572
ty:: LazyConst :: Unevaluated ( def_id, substs) => {
579
573
let instance = self . resolve ( def_id, substs) ?;
580
- return Ok ( * OpTy :: from ( self . const_eval_raw ( GlobalId {
574
+ return Ok ( OpTy :: from ( self . const_eval_raw ( GlobalId {
581
575
instance,
582
576
promoted : None ,
583
577
} ) ?) ) ;
584
578
} ,
585
- ty:: LazyConst :: Evaluated ( c) => c,
586
- } ;
587
- match val. val {
579
+ ty:: LazyConst :: Evaluated ( c) => self . const_to_op ( c, layout) ,
580
+ }
581
+ }
582
+
583
+ // Used when Miri runs into a constant, and (indirectly through lazy_const_to_op) by CTFE.
584
+ pub fn const_to_op (
585
+ & self ,
586
+ val : ty:: Const < ' tcx > ,
587
+ layout : Option < TyLayout < ' tcx > > ,
588
+ ) -> EvalResult < ' tcx , OpTy < ' tcx , M :: PointerTag > > {
589
+ let layout = from_known_layout ( layout, || {
590
+ let ty = self . monomorphize ( val. ty ) ?;
591
+ self . layout_of ( ty)
592
+ } ) ?;
593
+ let op = match val. val {
588
594
ConstValue :: ByRef ( id, alloc, offset) => {
589
595
// We rely on mutability being set correctly in that allocation to prevent writes
590
596
// where none should happen -- and for `static mut`, we copy on demand anyway.
591
- Ok ( Operand :: Indirect (
597
+ Operand :: Indirect (
592
598
MemPlace :: from_ptr ( Pointer :: new ( id, offset) , alloc. align )
593
- ) . with_default_tag ( ) )
599
+ ) . with_default_tag ( )
594
600
} ,
595
601
ConstValue :: Slice ( a, b) =>
596
- Ok ( Operand :: Immediate ( Immediate :: ScalarPair (
602
+ Operand :: Immediate ( Immediate :: ScalarPair (
597
603
a. into ( ) ,
598
604
Scalar :: from_uint ( b, self . tcx . data_layout . pointer_size ) . into ( ) ,
599
- ) ) . with_default_tag ( ) ) ,
605
+ ) ) . with_default_tag ( ) ,
600
606
ConstValue :: Scalar ( x) =>
601
- Ok ( Operand :: Immediate ( Immediate :: Scalar ( x. into ( ) ) ) . with_default_tag ( ) ) ,
602
- }
607
+ Operand :: Immediate ( Immediate :: Scalar ( x. into ( ) ) ) . with_default_tag ( ) ,
608
+ } ;
609
+ Ok ( OpTy {
610
+ op,
611
+ layout,
612
+ } )
603
613
}
604
614
605
615
/// Read discriminant, return the runtime value as well as the variant index.
@@ -699,23 +709,4 @@ impl<'a, 'mir, 'tcx, M: Machine<'a, 'mir, 'tcx>> EvalContext<'a, 'mir, 'tcx, M>
699
709
}
700
710
} )
701
711
}
702
-
703
- }
704
-
705
- impl < ' a , ' mir , ' tcx , M > EvalContext < ' a , ' mir , ' tcx , M >
706
- where
707
- M : Machine < ' a , ' mir , ' tcx , PointerTag =( ) > ,
708
- // FIXME: Working around https://github.com/rust-lang/rust/issues/24159
709
- M :: MemoryMap : AllocMap < AllocId , ( MemoryKind < M :: MemoryKinds > , Allocation < ( ) , M :: AllocExtra > ) > ,
710
- M :: AllocExtra : AllocationExtra < ( ) , M :: MemoryExtra > ,
711
- {
712
- // FIXME: CTFE should use allocations, then we can remove this.
713
- pub ( crate ) fn lazy_const_to_op (
714
- & self ,
715
- cnst : ty:: LazyConst < ' tcx > ,
716
- ty : ty:: Ty < ' tcx > ,
717
- ) -> EvalResult < ' tcx , OpTy < ' tcx > > {
718
- let op = self . const_value_to_op ( cnst) ?;
719
- Ok ( OpTy { op, layout : self . layout_of ( ty) ? } )
720
- }
721
712
}
0 commit comments