@@ -6,12 +6,13 @@ use std::convert::TryFrom;
66use std:: hash:: Hash ;
77
88use rustc:: mir;
9- use rustc:: mir:: interpret:: truncate;
9+ use rustc:: mir:: interpret:: { truncate, ConstValue } ;
1010use rustc:: ty:: layout:: {
1111 self , Align , HasDataLayout , LayoutOf , PrimitiveExt , Size , TyLayout , VariantIdx ,
1212} ;
1313use rustc:: ty:: TypeFoldable ;
1414use rustc:: ty:: { self , Ty } ;
15+ use rustc:: ty:: { self , Ty , TyCtxt } ;
1516use rustc_macros:: HashStable ;
1617
1718use super :: {
@@ -195,15 +196,34 @@ impl<'tcx, Tag> MPlaceTy<'tcx, Tag> {
195196 _ => bug ! ( "vtable not supported on type {:?}" , self . layout. ty) ,
196197 }
197198 }
199+
200+ #[ inline( always) ]
201+ pub fn to_const_value ( self , tcx : TyCtxt < ' tcx > ) -> ConstValue < ' tcx > {
202+ match self . mplace . ptr {
203+ Scalar :: Ptr ( ptr) => {
204+ let alloc = tcx. alloc_map . lock ( ) . unwrap_memory ( ptr. alloc_id ) ;
205+ ConstValue :: ByRef { alloc, offset : ptr. offset }
206+ }
207+ Scalar :: Raw { data, .. } => {
208+ assert_eq ! ( data, self . layout. align. abi. bytes( ) . into( ) ) ;
209+ ConstValue :: Scalar ( Scalar :: zst ( ) )
210+ }
211+ }
212+ }
198213}
199214
200215// These are defined here because they produce a place.
201216impl < ' tcx , Tag : :: std:: fmt:: Debug + Copy > OpTy < ' tcx , Tag > {
202217 #[ inline( always) ]
203- pub fn try_as_mplace ( self , cx : & impl HasDataLayout ) -> Result < MPlaceTy < ' tcx , Tag > , ImmTy < ' tcx , Tag > > {
218+ pub fn try_as_mplace (
219+ self ,
220+ cx : & impl HasDataLayout ,
221+ ) -> Result < MPlaceTy < ' tcx , Tag > , ImmTy < ' tcx , Tag > > {
204222 match * self {
205223 Operand :: Indirect ( mplace) => Ok ( MPlaceTy { mplace, layout : self . layout } ) ,
206- Operand :: Immediate ( _) if self . layout . is_zst ( ) => Ok ( MPlaceTy :: dangling ( self . layout , cx) ) ,
224+ Operand :: Immediate ( _) if self . layout . is_zst ( ) => {
225+ Ok ( MPlaceTy :: dangling ( self . layout , cx) )
226+ }
207227 Operand :: Immediate ( imm) => Err ( ImmTy { imm, layout : self . layout } ) ,
208228 }
209229 }
0 commit comments