Open
Description
Dafny restricts the use of field in the first phase of an object constructor. It currently allows a field to be used if the field is a const
with a given RHS. This is sometimes unsound, as the following program demonstrates.
class C {
const a := b + b
const b: int
constructor (x: int) {
var k := a;
print a, "\n";
b := x;
assert k == a;
print a, "\n";
if k != a {
var y := 5 / 0; // this can crash
}
}
}
method Main() {
var c := new C(5);
}
The allowance of reading, in the first phase of a constructor, a const
with a RHS should apply only if the RHS can be determined not to depend on any const
field without a RHS.