1- use prusti_interface:: PrustiError ;
21use prusti_rustc_interface:: {
2+ const_eval:: interpret:: AllocRange ,
33 middle:: {
44 mir:: {
55 self , ConstValue ,
@@ -14,6 +14,7 @@ use vir::CastType;
1414
1515use crate :: encoders:: {
1616 MirPureEnc , MirPureEncTask , PureKind ,
17+ addr:: AddrUseEnc ,
1718 ty:: {
1819 RustTyDecomposition ,
1920 generics:: { GParams , GenericParamsEnc } ,
@@ -70,7 +71,7 @@ impl ConstEnc {
7071 val : ConstValue ,
7172 ty : ty:: Ty < ' vir > ,
7273 context : GParams < ' vir > ,
73- span : Option < Span > ,
74+ _span : Option < Span > ,
7475 ) -> Result < vir:: ExprCSnap < ' vir > , EncodeFullError < ' vir , Self > > {
7576 vir:: with_vcx ( |vcx| {
7677 let ty_task = RustTyDecomposition :: from_ty ( ty, vcx. tcx ( ) , context) ;
@@ -87,22 +88,41 @@ impl ConstEnc {
8788 GlobalAlloc :: Function { .. } => todo ! ( ) ,
8889 GlobalAlloc :: VTable ( _, _) => todo ! ( ) ,
8990 GlobalAlloc :: Static ( _) => todo ! ( ) ,
90- GlobalAlloc :: Memory ( _mem) => {
91- // If the `unwrap` ever panics we need a different way to get the inner type
92- // let inner_ty = ty.builtin_deref(true).map(|t| t.ty).unwrap_or(ty);
93- let _inner_ty = ty. builtin_deref ( true ) . unwrap ( ) ;
94- vcx. with_span ( span. unwrap ( ) , |vcx| {
95- vcx. handle_error (
96- "application.precondition:assertion.false" ,
97- move |_| {
98- Some ( vec ! [ PrustiError :: verification(
99- format!( "unsupported const {val:?} might be reached" ) ,
100- span. unwrap( ) . into( ) ,
101- ) ] )
91+ GlobalAlloc :: Memory ( mem) => {
92+ let inner_ty = ty. builtin_deref ( true ) . unwrap ( ) ;
93+ let inner_ty_task =
94+ RustTyDecomposition :: from_ty ( inner_ty, vcx. tcx ( ) , context) ;
95+ let inner_kind = deps
96+ . require_dep :: < TyUsePureEnc > ( inner_ty_task) ?
97+ . expect_primitive ( ) ;
98+ let bytes = mem
99+ . 0
100+ . 0
101+ . read_scalar (
102+ & vcx. tcx ( ) ,
103+ AllocRange {
104+ start : ptr. prov_and_relative_offset ( ) . 1 ,
105+ size : mem. 0 . 0 . size ( ) ,
102106 } ,
103- ) ;
104- kind. unreachable_to_snap ( ) . downcast_ty ( )
105- } )
107+ false ,
108+ )
109+ . unwrap ( ) ;
110+ let addr_to_ref = deps. require_dep :: < AddrUseEnc > ( ( ) ) ?. ref_from_addr ;
111+ let ( prov, offset) = ptr. prov_and_relative_offset ( ) ;
112+ let alloc_id = prov. alloc_id ( ) . 0 ;
113+ let rel_addr =
114+ ( ( alloc_id. get ( ) as u128 ) << 64 ) | offset. bytes ( ) as u128 ;
115+
116+ kind. expect_immref ( ) . prim_to_snap (
117+ ( addr_to_ref) (
118+ vcx. mk_const_expr ( vir:: ConstData :: Int ( rel_addr) )
119+ . downcast_ty ( ) ,
120+ ) ,
121+ ( ( inner_kind. prim_to_snap ) ( vcx. mk_const_expr (
122+ vir:: ConstData :: Int ( bytes. to_bits ( mem. 0 . 0 . size ( ) ) . unwrap ( ) ) ,
123+ ) ) )
124+ . upcast_ty ( ) ,
125+ )
106126 }
107127 GlobalAlloc :: TypeId { .. } => todo ! ( ) ,
108128 }
0 commit comments