flux icon indicating copy to clipboard operation
flux copied to clipboard

Failing to assume invariants on field when unfolding a struct

Open Ralender opened this issue 1 year ago • 4 comments

Why is the following program considered not safe ?

struct A {
  val: u32,
}

impl A {
  fn f(&mut self) -> () {
    if self.val == 0 {
      return;
    }
    let a = self.val - 1;
  }
}

but the following is considered safe ?

fn f(val: u32) -> () {
  if val == 0 {
    return;
  }
  let a = val - 1;
}

Ralender avatar May 19 '23 04:05 Ralender