On this page
B5108: function might not terminate
Catalog status: active. Default severity: error. Stable name: termination. Family: Contracts and termination.
A function must terminate on every call: functions are used in contracts, where a call that never returns would make proofs meaningless. Verada requires evidence of termination for each loop and each recursive call in a function:
- a
forloop terminates by itself; - any other loop needs a
pragma Loop_Variant; - a function that calls itself needs a
Subprogram_Variantaspect.
The variants themselves are then proved (B5107, B5109).
Example (Vada)
function Steps (Start : Positive) return Natural is
N : Positive := Start;
Count : Natural := 0;
begin
while N /= 1 and Count < Natural'Last loop -- no variant
N := (if N mod 2 = 0 then N / 2 else N / 2 + 1);
Count := Count + 1;
end loop;
return Count;
end Steps;Example (Varc)
fn steps(start: Positive) -> Natural {
var n: Positive = start;
var count: Natural = 0;
while n != 1 && count < Natural::Last { // no variant
n = if n %% 2 == 0 { n / 2 } else { n / 2 + 1 };
count += 1;
}
return count;
}
What to do (Vada)
Add pragma Loop_Variant (Decreases => E) (here Natural'Last - Count decreases) next to the loop invariants, or Subprogram_Variant => (Decreases => E) on a recursive function, or rewrite the loop as a for loop.
What to do (Varc)
Add decreases e; to the loop (here decreases Natural::Last - count;) next to the loop invariants, or decreases e to the contract of a recursive function, or rewrite the loop as a for loop.
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.