DEVELOPMENT EDITION · 769442725406
Diagnostics Reference
On this page

B4107: discriminant check might fail

Catalog status: active. Default severity: error. Stable name: discriminant-check. Family: Executable checks.

A record with a variant part has some components only for some values of its discriminants. Reading or writing a component of another variant is an error that Ada checks at run time; the prover found a case where the component might not exist. The same code is used when a value assigned to a constrained record object (or given for a discriminant constraint) might have other discriminants than the object.

Example (Vada)

   type Shape (K : Kind := Circle) is record
      case K is
         when Circle => Radius : Natural;
         when Square => Side : Natural;
      end case;
   end record;

   function Side_Of (S : Shape) return Natural is (S.Side);   --  S may be a circle

Example (Varc)

    pub struct Shape(k: Kind = Circle) {
        match k {
            Circle => {
                pub radius: Natural,
            }
            Square => {
                pub side: Natural,
            }
        }
    }

    pub fn side_of(s: Shape) -> Natural = s.side;   // s may be a circle

What to do (Vada)

Test the discriminant first (if S.K = Square then ... S.Side ..., or a case on it), or require it: with Pre => S.K = Square.

What to do (Varc)

Test the discriminant first (if s.k == Square { ... s.side ... }, or a match on it), or require it: requires s.k == Square.

Regression source and further reading

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

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