@@ -6,12 +6,13 @@ use std::convert::TryFrom;
6
6
use std:: hash:: Hash ;
7
7
8
8
use rustc:: mir;
9
- use rustc:: mir:: interpret:: truncate;
9
+ use rustc:: mir:: interpret:: { truncate, ConstValue } ;
10
10
use rustc:: ty:: layout:: {
11
11
self , Align , HasDataLayout , LayoutOf , PrimitiveExt , Size , TyLayout , VariantIdx ,
12
12
} ;
13
13
use rustc:: ty:: TypeFoldable ;
14
14
use rustc:: ty:: { self , Ty } ;
15
+ use rustc:: ty:: { self , Ty , TyCtxt } ;
15
16
use rustc_macros:: HashStable ;
16
17
17
18
use super :: {
@@ -195,15 +196,34 @@ impl<'tcx, Tag> MPlaceTy<'tcx, Tag> {
195
196
_ => bug ! ( "vtable not supported on type {:?}" , self . layout. ty) ,
196
197
}
197
198
}
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
+ }
198
213
}
199
214
200
215
// These are defined here because they produce a place.
201
216
impl < ' tcx , Tag : :: std:: fmt:: Debug + Copy > OpTy < ' tcx , Tag > {
202
217
#[ 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 > > {
204
222
match * self {
205
223
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
+ }
207
227
Operand :: Immediate ( imm) => Err ( ImmTy { imm, layout : self . layout } ) ,
208
228
}
209
229
}
0 commit comments