Skip to content

Invariant inference: an error in the loop body is reported with candidate invariants that were never proved #553

Open

Description

@CatarinaGamboa

Part of the loop invariant inference plan (#542). Found after #551.

Problem. When the check of a loop body stops at an error before it reaches the end of the body, the candidate invariants are never checked at the back edge, so they are never dropped. The error is then reported with facts that were never proved, and Found and the counterexample state things that are false:

static int read() { return 1; }
static void nonNeg(@Refinement("_ >= 0") int x) {}

static void nothing() {
    int total = 0;
    while (total < 100) {
        total = total + read();
        nonNeg(total);
    }
}
Refinement Error: found `total@18#1 >= 0 && total@18#1 <= 0 && total@18#1 <= 100 && total == total@18#1 + #read_result` but expected `total >= 0`
 --> Counterexample: `total == -1 && total@18#1 == 0 && #read_result == -1`

total@18#1 <= 0 (total is 0 at every loop start) is not an invariant: read() can return a positive number. Neither is total@18#1 >= 0. Found should only contain facts that survive a complete check (here only total@18#1 <= 100 from the loop condition).

Not a soundness problem as far as checked: the method already has an error, and code after the loop is still checked (below9(50) after such a loop, and after a loop whose body has an error inside a lambda, are both reported).

Fix idea. Treat a body check that stops before a back edge as "not proved" for the remaining candidates: drop the ones not proved on the paths that did reach a back edge (or all of them if none did), and recheck, so the reported error is built only from proved facts. Test: the example above must not show total@18#1 <= 0 in Found.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingerror messagesRelated to error messages

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions