Skip to content

Constant fields depending on non-initialized fields should not be usable in first-phase construction #2727

Open
@RustanLeino

Description

@RustanLeino

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.

Metadata

Metadata

Assignees

Labels

during 3: execution of incorrect programAn bug in the verifier that allows Dafny to run a program that does not correctly implement its specintroduced: pre-2012part: resolverResolution and typecheckingpriority: not yetWill reconsider working on this when we're looking for work

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions