]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
[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)
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

index d30e8e9b72081095a01a0ddd094676ce428216d5..07166c6b3ea157f2b5237043185e806188c72335 100644 (file)
@@ -1,3 +1,10 @@
+2019-08-12  Justin Squirek  <squirek@adacore.com>
+
+       * 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.
+
 2019-08-12  Eric Botcazou  <ebotcazou@adacore.com>
 
        * exp_ch4.adb (Real_Range_Check): Do not rewrite the conversion
index 734c961fe620561700dc7eaa2e8eb798d632a509..e417a0719d1dac8a0f72cecbdc654d4051aa9f26 100644 (file)
@@ -562,23 +562,31 @@ package body Sem_Eval is
       elsif Is_Out_Of_Range (N, Base_Type (T), Assume_Valid => True) then
          Out_Of_Range (N);
 
-      --  Give warning if outside subtype (where one or both of the bounds of
-      --  the subtype is static). This warning is omitted if the expression
-      --  appears in a range that could be null (warnings are handled elsewhere
-      --  for this case).
+      --  Give a warning or error on the value outside the subtype. A
+      --  warning is omitted if the expression appears in a range that could
+      --  be null (warnings are handled elsewhere for this case).
 
       elsif T /= Base_Type (T) and then Nkind (Parent (N)) /= N_Range then
          if Is_In_Range (N, T, Assume_Valid => True) then
             null;
 
          elsif Is_Out_Of_Range (N, T, Assume_Valid => True) then
-
             --  Ignore out of range values for System.Priority in CodePeer
             --  mode since the actual target compiler may provide a wider
             --  range.
 
             if CodePeer_Mode and then T = RTE (RE_Priority) then
                Set_Do_Range_Check (N, False);
+
+            --  Determine if the out of range violation constitutes a warning
+            --  or an error based on context according to RM 4.9 (34/3).
+
+            elsif Nkind_In (Original_Node (N), N_Type_Conversion,
+                                               N_Qualified_Expression)
+              and then Comes_From_Source (Original_Node (N))
+            then
+               Apply_Compile_Time_Constraint_Error
+                 (N, "value not in range of}", CE_Range_Check_Failed);
             else
                Apply_Compile_Time_Constraint_Error
                  (N, "value not in range of}<<", CE_Range_Check_Failed);
@@ -5515,8 +5523,18 @@ package body Sem_Eval is
          --  CodePeer mode where the target runtime may have more priorities.
 
          elsif not CodePeer_Mode or else Etype (N) /= RTE (RE_Priority) then
-            Apply_Compile_Time_Constraint_Error
-              (N, "value not in range of}", CE_Range_Check_Failed);
+            --  Determine if the out of range violation constitutes a warning
+            --  or an error based on context according to RM 4.9 (34/3).
+
+            if Nkind (Original_Node (N)) = N_Type_Conversion
+              and then not Comes_From_Source (Original_Node (N))
+            then
+               Apply_Compile_Time_Constraint_Error
+                 (N, "value not in range of}??", CE_Range_Check_Failed);
+            else
+               Apply_Compile_Time_Constraint_Error
+                 (N, "value not in range of}", CE_Range_Check_Failed);
+            end if;
          end if;
 
       --  Here we generate a warning for the Ada 83 case, or when we are in an