]> git.ipfire.org Git - thirdparty/gcc.git/commit
[Ada] Inconsistent compile time Constraint_Error warning
authorJustin Squirek <squirek@adacore.com>
Mon, 12 Aug 2019 08:59:33 +0000 (08:59 +0000)
committerPierre-Marie de Rodat <pmderodat@gcc.gnu.org>
Mon, 12 Aug 2019 08:59:33 +0000 (08:59 +0000)
commit33defa7c6c36c0671b81b4785fbb250430a4a953
tree51388445d5dfda223c47e86395fbd2b514cb8572
parent4e896dad492f7484cc239f105454713a3c4596eb
[Ada] Inconsistent compile time Constraint_Error warning

This patch corrects several bugs within the compiler which led to
inconsistent handling of compile time Constraint_Errors. Notibly,
subtype out of range checks which are only out of range of the subtype
must be warnings while out of range checks where the value is out of
range of the base type must be an error. Also, type conversions and
qualified expressions on literals constitute errors on any out of range
value. The compiler needed many of these cases clarified.

------------
-- Source --
------------

--  main.ads

with System;
package Main is

   type T_Enum is (Enum_1, Enum_2, Unknown)
     with Default_Value => Unknown;

   subtype T_Valid_Enum is T_Enum range Enum_1 .. Enum_2;

   Value : T_Valid_Enum; --  WARNING

   generic
      type T_Element is (<>);
      Init : T_Element;
   package Generic_Test is
      Value : T_Element := Init;
   end;

   package titi is new Generic_Test (T_Valid_Enum, Unknown); --  WARNING

   type My_Float is digits System.Max_Base_Digits;

   My_Float_Last : constant := My_Float'Last;
   Out_Of_Range  : constant := My_Float_Last + 1.0;

   Flt1 : My_Float := Out_Of_Range; --  ERROR

   A : Positive := Positive (16#9999_9999_9999#); --  ERROR
   B : Positive := 16#9999_9999_9999#;            --  ERROR
   C : Positive := 0;                             --  WARNING
   D : Positive := Positive (0);                  --  ERROR
   E : Positive := Positive'(16#9999_9999_9999#); --  ERROR
   F : Positive := Positive'(0);                  --  ERROR
end;

-----------------
-- Compilation --
-----------------

$ gnatmake -q -gnatw_a main.adb
main.ads:9:12: warning: value not in range of type "T_Valid_Enum" defined at
line 7
main.ads:9:12: warning: "Constraint_Error" will be raised at run time
main.ads:18:52: warning: value not in range of type "T_Element" defined at
line 12, instance at line 18
main.ads:18:52: warning: "Constraint_Error" will be raised at run time
main.ads:25:23: value not in range of type "My_Float" defined at line 20
main.ads:25:23: static expression fails Constraint_Check
main.ads:27:19: value not in range of type "Standard.Positive"
main.ads:27:19: static expression fails Constraint_Check
main.ads:28:19: value not in range of type "Standard.Positive"
main.ads:28:19: static expression fails Constraint_Check
main.ads:29:19: warning: value not in range of type "Standard.Positive"
main.ads:29:19: warning: "Constraint_Error" will be raised at run time
main.ads:30:19: value not in range of type "Standard.Positive"
main.ads:30:19: static expression fails Constraint_Check
main.ads:31:27: value not in range of type "Standard.Positive"
main.ads:31:27: static expression fails Constraint_Check
main.ads:32:27: value not in range of type "Standard.Positive"
main.ads:32:27: static expression fails Constraint_Check
gnatmake: "main.ads" compilation error

2019-08-12  Justin Squirek  <squirek@adacore.com>

gcc/ada/

* sem_eval.adb (Check_Non_Static_Context): Add a condition to
determine if a range violation constitues a warning or an error.
(Out_Of_Range): Add a condition to determine if a range
violation constitues a warning or an error.

From-SVN: r274288
gcc/ada/ChangeLog
gcc/ada/sem_eval.adb