Array with too many elements only compiler warning?

Viewed 111

I created the following example:

procedure Example is
   type Index_Type is range 1 .. 3;

   type Array_Type is array (Index_Type) of Integer;
   
   Array_Fail_One : Array_Type := (11, 22, 33, 44);
   Array_Fail_Two : Array_Type := (1 => 11, 2 => 22, 3 => 33, 4 => 44);
begin
   null;
end Example;

Why is thind kind of problem only treated as a warning and not as an error?

example.adb:6:35: warning: upper bound of aggregate out of range
example.adb:6:35: warning: Constraint_Error will be raised at run time
example.adb:7:35: warning: upper bound of aggregate out of range
example.adb:7:35: warning: Constraint_Error will be raised at run time
1 Answers

It is a warning because the compiler detected a run time error which will result in the exception Constraint_Error to be raised.

The Ada compiler does not guarantee absence of all run time errors, but it does flag those errors it detects.

Related