11use crate :: {
2- ctx:: { BodyId , CloneMap , TranslationCtx } ,
3- translation:: LocalIdent ,
2+ ctx:: CloneMap ,
3+ translation:: fmir :: { self , LocalDecls } ,
44} ;
55use rustc_middle:: {
6- mir:: { tcx:: PlaceTy , HasLocalDecls , Local , Place , PlaceRef } ,
6+ mir:: { tcx:: PlaceTy , Local , Place , PlaceRef } ,
77 ty:: TyKind ,
88} ;
99use why3:: {
@@ -21,36 +21,18 @@ use why3::{
2121use super :: Why3Generator ;
2222
2323impl < ' tcx > Why3Generator < ' tcx > {
24- pub ( crate ) fn place_ty ( & mut self , body_id : BodyId , pl : PlaceRef < ' tcx > ) -> PlaceTy < ' tcx > {
25- let local_ty = self . body ( body_id) . local_decls ( ) [ pl. local ] . ty ;
24+ pub ( crate ) fn place_ty (
25+ & mut self ,
26+ locals : & fmir:: LocalDecls < ' tcx > ,
27+ pl : PlaceRef < ' tcx > ,
28+ ) -> PlaceTy < ' tcx > {
29+ let local_ty = locals[ & pl. local ] . 2 ;
2630 pl. projection . iter ( ) . fold ( PlaceTy :: from_ty ( local_ty) , |place_ty, & elem| {
2731 place_ty. projection_ty ( self . tcx , elem)
2832 } )
2933 }
3034}
3135
32- impl < ' tcx > TranslationCtx < ' tcx > {
33- pub ( crate ) fn translate_local ( & mut self , body_id : BodyId , loc : Local ) -> LocalIdent {
34- let body = self . body ( body_id) ;
35-
36- use rustc_middle:: mir:: VarDebugInfoContents :: Place ;
37- let debug_info: Vec < _ > = body
38- . var_debug_info
39- . iter ( )
40- . filter ( |var_info| match var_info. value {
41- Place ( p) => p. as_local ( ) . map ( |l| l == loc) . unwrap_or ( false ) ,
42- _ => false ,
43- } )
44- . collect ( ) ;
45-
46- assert ! ( debug_info. len( ) <= 1 , "expected at most one debug entry for local {:?}" , loc) ;
47- match debug_info. get ( 0 ) {
48- Some ( info) => LocalIdent :: dbg ( loc, * info) ,
49- None => LocalIdent :: anon ( loc) ,
50- }
51- }
52- }
53-
5436/// Correctly translate an assignment from one place to another. The challenge here is correctly
5537/// construction the expression that assigns deep inside a structure.
5638/// (_1 as Some) = P ---> let _1 = P ??
@@ -65,7 +47,7 @@ impl<'tcx> TranslationCtx<'tcx> {
6547pub ( crate ) fn create_assign_inner < ' tcx > (
6648 ctx : & mut Why3Generator < ' tcx > ,
6749 names : & mut CloneMap < ' tcx > ,
68- body_id : BodyId ,
50+ locals : & LocalDecls < ' tcx > ,
6951 lhs : & Place < ' tcx > ,
7052 rhs : Exp ,
7153) -> mlcfg:: Statement {
@@ -84,7 +66,7 @@ pub(crate) fn create_assign_inner<'tcx>(
8466 for ( proj, elem) in lhs. iter_projections ( ) . rev ( ) {
8567 // twisted stuff
8668 stump = & stump[ 0 ..stump. len ( ) - 1 ] ;
87- let place_ty = ctx. place_ty ( body_id , proj) ;
69+ let place_ty = ctx. place_ty ( locals , proj) ;
8870
8971 match elem {
9072 Deref => {
@@ -94,7 +76,7 @@ pub(crate) fn create_assign_inner<'tcx>(
9476 if mutability == Mut {
9577 inner = RecUp {
9678 record : Box :: new ( translate_rplace_inner (
97- ctx, names, body_id , lhs. local , stump,
79+ ctx, names, locals , lhs. local , stump,
9880 ) ) ,
9981 label : "current" . into ( ) ,
10082 val : Box :: new ( inner) ,
@@ -119,9 +101,7 @@ pub(crate) fn create_assign_inner<'tcx>(
119101 let ctor = names. constructor ( variant. def_id , subst) ;
120102 inner = Let {
121103 pattern : ConsP ( ctor. clone ( ) , field_pats) ,
122- arg : Box :: new ( translate_rplace_inner (
123- ctx, names, body_id, lhs. local , stump,
124- ) ) ,
104+ arg : Box :: new ( translate_rplace_inner ( ctx, names, locals, lhs. local , stump) ) ,
125105 body : Box :: new ( Constructor { ctor, args : varexps } ) ,
126106 }
127107 }
@@ -139,9 +119,7 @@ pub(crate) fn create_assign_inner<'tcx>(
139119
140120 inner = Let {
141121 pattern : TupleP ( field_pats) ,
142- arg : Box :: new ( translate_rplace_inner (
143- ctx, names, body_id, lhs. local , stump,
144- ) ) ,
122+ arg : Box :: new ( translate_rplace_inner ( ctx, names, locals, lhs. local , stump) ) ,
145123 body : Box :: new ( Tuple ( varexps) ) ,
146124 }
147125 }
@@ -160,9 +138,7 @@ pub(crate) fn create_assign_inner<'tcx>(
160138
161139 inner = Let {
162140 pattern : ConsP ( cons. clone ( ) , field_pats) ,
163- arg : Box :: new ( translate_rplace_inner (
164- ctx, names, body_id, lhs. local , stump,
165- ) ) ,
141+ arg : Box :: new ( translate_rplace_inner ( ctx, names, locals, lhs. local , stump) ) ,
166142 body : Box :: new ( Exp :: Constructor { ctor : cons, args : varexps } ) ,
167143 }
168144 }
@@ -171,12 +147,12 @@ pub(crate) fn create_assign_inner<'tcx>(
171147 Downcast ( _, _) => { }
172148 Index ( ix) => {
173149 let set = Exp :: impure_qvar ( QName :: from_string ( "Slice.set" ) . unwrap ( ) ) ;
174- let ix_exp = Exp :: impure_var ( ctx . translate_local ( body_id , ix ) . ident ( ) ) ;
150+ let ix_exp = Exp :: impure_var ( locals [ & ix ] . 0 . ident ( ) ) ;
175151
176152 inner = Call (
177153 Box :: new ( set) ,
178154 vec ! [
179- translate_rplace_inner( ctx, names, body_id , lhs. local, stump) ,
155+ translate_rplace_inner( ctx, names, locals , lhs. local, stump) ,
180156 ix_exp,
181157 inner,
182158 ] ,
@@ -188,7 +164,10 @@ pub(crate) fn create_assign_inner<'tcx>(
188164 }
189165 }
190166
191- let ident = ctx. translate_local ( body_id, lhs. local ) ;
167+ if locals. get ( & lhs. local ) . is_none ( ) {
168+ eprintln ! ( "{lhs:?} {locals:?}" ) ;
169+ }
170+ let ident = locals[ & lhs. local ] . 0 . clone ( ) ;
192171 Assign { lhs : ident. ident ( ) , rhs : inner }
193172}
194173
@@ -198,17 +177,20 @@ pub(crate) fn create_assign_inner<'tcx>(
198177pub ( crate ) fn translate_rplace_inner < ' tcx > (
199178 ctx : & mut Why3Generator < ' tcx > ,
200179 names : & mut CloneMap < ' tcx > ,
201- body_id : BodyId ,
180+ locals : & LocalDecls < ' tcx > ,
202181 loc : Local ,
203182 proj : & [ rustc_middle:: mir:: PlaceElem < ' tcx > ] ,
204183) -> Exp {
205- let mut inner = Exp :: impure_var ( ctx. translate_local ( body_id, loc) . ident ( ) ) ;
184+ if locals. get ( & loc) . is_none ( ) {
185+ eprintln ! ( "{locals:?} {loc:?}" )
186+ } ;
187+ let mut inner = Exp :: impure_var ( locals[ & loc] . 0 . ident ( ) ) ;
206188 if proj. is_empty ( ) {
207189 return inner;
208190 }
209191
210192 use rustc_middle:: mir:: ProjectionElem :: * ;
211- let mut place_ty = ctx. place_ty ( body_id , Place :: from ( loc) . as_ref ( ) ) ;
193+ let mut place_ty = ctx. place_ty ( locals , Place :: from ( loc) . as_ref ( ) ) ;
212194
213195 for elem in proj {
214196 match elem {
@@ -250,7 +232,7 @@ pub(crate) fn translate_rplace_inner<'tcx>(
250232 Downcast ( _, _) => { }
251233 Index ( ix) => {
252234 // TODO: Use [_] syntax
253- let ix_exp = Exp :: impure_var ( ctx . translate_local ( body_id , * ix ) . ident ( ) ) ;
235+ let ix_exp = Exp :: impure_var ( locals [ ix ] . 0 . ident ( ) ) ;
254236 inner = Call (
255237 Box :: new ( Exp :: impure_qvar ( QName :: from_string ( "Slice.get" ) . unwrap ( ) ) ) ,
256238 vec ! [ inner, ix_exp] ,
0 commit comments