DEVELOPMENT EDITION · 769442725406
Diagnostics Reference
On this page

B5103: loop invariant might fail on entry

Catalog status: active. Default severity: error. Stable name: loop-invariant-init. Family: Contracts and termination.

A loop invariant must hold the first time execution reaches it. The prover could not show that it holds then, from what is known before the loop.

Example (Vada)

   Sum : Natural := 1;   --  should start at 0
begin
   for I in 1 .. 10 loop
      pragma Loop_Invariant (Sum = (I - 1) * I / 2);
      Sum := Sum + I;
   end loop;

Example (Varc)

    var sum: Natural = 1;   // should start at 0
    for i in 1..=10 {
        invariant sum == (i - 1) * i / 2;
        sum += i;
    }

What to do

Fix the initialisation before the loop, or correct the invariant so that it describes the first iteration too.

Regression source and further reading

Complete regression example: source, command arguments and expected results. The fixture deliberately reproduces this diagnostic.

See contracts and termination, the code index, and diagnostic interpretation for modes, replay, fix-its and JSON.