B2027: an Unchecked_Conversion between types stored in different sizes
Catalog status: active. Default severity: error. Stable name: unchecked-conversion-sizes. Family: Types and constraints.
An instance of Ada.Unchecked_Conversion reads the bits of a value of its source type as a value of its target type. Verada models that exactly when both are scalar types stored in the same number of bits: an Integer_32 and an Unsigned_32, a Float and an Unsigned_32, a Long_Float and an Unsigned_64. Between types stored in different sizes, some bits would be dropped or invented, and which ones depends on the machine, so Verada rejects the instance rather than give it a meaning that proofs could rely on.
Example (Vada)
function Narrow is new Ada.Unchecked_Conversion (Integer_32, Unsigned_16);What to do (Vada)
Convert between types of the same size, then take the part wanted with arithmetic, or convert the value rather than its bits:
function To_Bits is new Ada.Unchecked_Conversion (Integer_32, Unsigned_32);
Low : constant Unsigned_16 := Unsigned_16 (To_Bits (X) and 16#FFFF#);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.