1

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:

  • There are no pragma suppress statements in the code file in question (though there are some in files added via with;
  • The build is done via gprbuild using GPS 5.0.2 and GNAT Pro 6.4.2
  • There are the following flags used: -gnatf -gnatp -gnat2012 -d
  • It's also got flags coming in from "external" in the .gpr file - but I don't know where these are or what this means.
theMayer
  • 15,456
  • 7
  • 58
  • 90
  • 1
    what compiler and compile flags are you using? See https://stackoverflow.com/questions/14526945/why-does-an-ada-compiler-let-range-violations-pass-why-is-my-type-declaration-a/14527545#14527545 –  Dec 01 '17 at 16:58
  • That's interesting- would the same be applicable for gprbuild? – theMayer Dec 01 '17 at 17:22
  • I did try adding the arguments but it did not have any effect. – theMayer Dec 01 '17 at 17:31
  • I got compiler warning after removing -gnatp switch, but I had to run clean & build, it was not enough to run just build after removing -gnatp switch. I used GNAT GPS 2017 – Timur Samkharadze Dec 01 '17 at 18:09
  • OK, so looks like -gnatp disables runtime checks, including range checks, but -gnat-p removes this switch if it's been added (via an import). I am debating whether to delete this question, since it was asked out of utter ignorance - but can leave it here if people think it would be useful to future askers? – theMayer Dec 01 '17 at 18:44
  • 1
    @theMayer The question is real enough (even if you could read the answer in the manual), so I've added an answer for completeness. – Jacob Sparre Andersen Dec 01 '17 at 19:05

1 Answers1

6

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).

theMayer
  • 15,456
  • 7
  • 58
  • 90
Jacob Sparre Andersen
  • 6,733
  • 17
  • 22