DEVELOPMENT EDITION · 769442725406
Diagnostics Reference
On this page

B2015: 'Old in a part of a postcondition that might not be evaluated

Catalog status: active. Default severity: error. Stable name: conditional-old. Family: Types and constraints.

X'Old is evaluated when the subprogram is entered, before anyone knows whether the part of the postcondition containing it will be evaluated at the end. Inside the right operand of and then or or else, a branch of an if or case expression, or the predicate of a quantified expression, Ada therefore allows 'Old only when its prefix names an object (X'Old, R.Field'Old), or when every condition deciding whether that part is evaluated is already known on entry: static, an in parameter, a constant, or itself an 'Old (RM 6.1.1(27)). Otherwise the copy taken on entry could fail (an index out of range, a failed precondition) in a case where the program never asks for it.

Example (Vada)

   procedure Clear_If_Positive (V : in out Vector; I : Index)
     with Post => (if V (1) > 0 then V (I)'Old = 0);
   --  V (1) is read on exit, so V (I)'Old might not be needed

Example (Varc)

    fn clear_if_positive(v: inout Vector, i: Index)
        ensures if v[1] > 0 { old(v[i]) == 0 };
    // v[1] is read on exit, so old(v[i]) might not be needed

What to do (Vada)

Apply 'Old to the whole object and index the old value, (if V (1) > 0 then V'Old (I) = 0), or make the deciding condition known on entry, (if V (1)'Old > 0 then V (I)'Old = 0).

What to do (Varc)

Apply old to the whole object, if v[1] > 0 { old(v)[i] == 0 }, or make the deciding condition known on entry, if old(v[1]) > 0 { old(v[i]) == 0 }.

Regression source and further reading

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

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