theMayer
theMayer

Reputation: 16177

Why is Ada not raising a constraint error?

I have the following declaration of an anonymous subtype:

testConstraint : Integer Range -5 .. 5;

Then later, when assigning it:

testConstraint := -6;

Why am I not getting a Constraint_Error?

Additional Details:

Upvotes: 1

Views: 552

Answers (1)

Jacob Sparre Andersen
Jacob Sparre Andersen

Reputation: 6611

As @Timur and @theMayer notes:

-gnatp suppresses all checks.

From the documentation:

-gnatp

This switch causes the unit to be compiled as though pragma Suppress (All_checks) had been present in the source. Validity checks are also eliminated (in other words -gnatp also implies -gnatVn). Use this switch to improve the performance of the code at the expense of safety in the presence of invalid data or program bugs.

Suppressing all checks is a really bad idea. You can do it for specific units, if you have proven that the checks aren't required (for example by using SPARK), and you have measured that suppressing all checks gives you a performance improvement which you need.

The solution is to use add the compiler flag -gnat-p (and then - if the requirements are met - suppressing checks for individual files).

Upvotes: 6

Related Questions