Skip to content

Commit

Permalink
Fresh copy let var initializer.
Browse files Browse the repository at this point in the history
This is safe becaues it consumes whatever is the initializer.
  • Loading branch information
yannbolliger committed May 20, 2021
1 parent 3a7fc13 commit 170b129
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 11 deletions.
6 changes: 5 additions & 1 deletion stainless_extraction/src/expr/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,8 +124,12 @@ impl<'a, 'l, 'tcx> BodyExtractor<'a, 'l, 'tcx> {
let exprs = acc_exprs.clone();
acc_exprs.clear();
let body_expr = self.extract_block_(stmts, acc_exprs, acc_specs, final_expr);

// wrap that body expression into the Let
let init = self.extract_expr(init);

// This is safe, because we don't support mutable references.
let init = self.extract_aliasable_expr(init);

let last_expr = if vd.is_mutable() {
f.LetVar(vd, init, body_expr).into()
} else {
Expand Down
26 changes: 16 additions & 10 deletions stainless_frontend/tests/pass/mut_local_lets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,22 @@

extern crate stainless;

pub enum IntOption {
None,
Some(i32),
pub fn f<T>(option: Option<T>) {
// Again, local mutation
let mut option2 = None;
let mut option = None;
option2 = option;
option = Some(123);
}

pub struct Thing<T> {
field: T,
}

pub fn change_thing<T>(thing: Thing<T>, t: T) -> Thing<T> {
let mut new_field = thing.field;
new_field = t;
Thing { field: new_field }
}

pub fn main() {
Expand All @@ -13,11 +26,4 @@ pub fn main() {
assert!(x == 1);
x = 3;
assert!(x == 3);

// Again, local mutation
let option = IntOption::Some(2);
let mut option2 = IntOption::None;
let mut option = IntOption::None;
option2 = option;
option = IntOption::Some(123);
}

0 comments on commit 170b129

Please sign in to comment.