B5104: loop invariant might not be preserved
Catalog status: active. Default severity: error. Stable name: loop-invariant-preservation. Family: Contracts and termination.
The prover assumes the invariant at the start of an arbitrary iteration, runs the iteration, and must show the invariant again at the next one. It could not. The prover knows nothing about the variables the loop changes except what the invariant (and the variables' subtypes) say, so an invariant often fails here because it is too weak, not because it is false.
Example (Vada)
for I in 1 .. 10 loop
pragma Loop_Invariant (Sum <= I); -- Sum grows faster than I
Sum := Sum + I;
end loop;Example (Varc)
for i in 1..=10 {
invariant sum <= i; // sum grows faster than i
sum += i;
}
What to do
Strengthen the invariant with the facts the next iteration needs (bounds of every variable the loop changes are a good start), or fix it if it is false.
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.