[Ada] Spurious possible contraint error warning with No_Exception_Propagation
authorJustin Squirek <squirek@adacore.com>
Mon, 16 Jul 2018 14:11:04 +0000 (14:11 +0000)
committerPierre-Marie de Rodat <pmderodat@gcc.gnu.org>
Mon, 16 Jul 2018 14:11:04 +0000 (14:11 +0000)
This patch corrects an issue whereby spurious unhandled exception warnings on
integer literals within static if and case expressions would be emitted when
the restriction No_Exception_Propagation is enabled.

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

--  gnat.adc

pragma Restrictions (No_Exception_Propagation);
pragma SPARK_Mode (On);

--  pack.ads

package Pack is
   procedure Filter (Ret : out Integer);
end Pack;

--  pack.adb

package body Pack is

   subtype Nat is Integer range 0 .. 10;

   Default       : constant Nat := 1;
   User_Override : constant Integer := -1;

   procedure Filter (Ret : out Integer) is
      Val : constant Nat :=
              (if User_Override in Nat then
                  User_Override
               else
                  Default);
   begin
      Ret := Val;
   end Filter;
end Pack;

----------------------------
-- Compilation and output --
----------------------------

& gcc -c -gnatp -gnatwa pack.adb

2018-07-16  Justin Squirek  <squirek@adacore.com>

gcc/ada/

* sem_eval.adb (Eval_Integer_Literal): Add exception for avoiding
checks on expanded literals within if and case expressions.

From-SVN: r262714

gcc/ada/ChangeLog
gcc/ada/sem_eval.adb

index a782582..6d8572d 100644 (file)
@@ -1,3 +1,8 @@
+2018-07-16  Justin Squirek  <squirek@adacore.com>
+
+       * sem_eval.adb (Eval_Integer_Literal): Add exception for avoiding
+       checks on expanded literals within if and case expressions.
+
 2018-07-16  Hristian Kirtchev  <kirtchev@adacore.com>
 
        * libgnat/s-wchwts.adb (Wide_String_To_String): Use the appropriate
index a567143..ab85879 100644 (file)
@@ -2720,16 +2720,23 @@ package body Sem_Eval is
    --  Start of processing for Eval_Integer_Literal
 
    begin
-
       --  If the literal appears in a non-expression context, then it is
       --  certainly appearing in a non-static context, so check it. This is
       --  actually a redundant check, since Check_Non_Static_Context would
       --  check it, but it seems worthwhile to optimize out the call.
 
-      --  An exception is made for a literal in an if or case expression
+      --  Additionally, when the literal appears within an if or case
+      --  expression it must be checked as well. However, due to the literal
+      --  appearing within a conditional statement, expansion greatly changes
+      --  the nature of its context and performing some of the checks within
+      --  Check_Non_Static_Context on an expanded literal may lead to spurious
+      --  and misleading warnings.
 
       if (Nkind_In (Parent (N), N_If_Expression, N_Case_Expression_Alternative)
            or else Nkind (Parent (N)) not in N_Subexpr)
+        and then (not Nkind_In (Parent (N), N_If_Expression,
+                                 N_Case_Expression_Alternative)
+                   or else Comes_From_Source (N))
         and then not In_Any_Integer_Context
       then
          Check_Non_Static_Context (N);