diff mbox

[Ada] Quantified expressions and functions with side effects

Message ID 20120330092928.GA28029@adacore.com
State New
Headers show

Commit Message

Arnaud Charlet March 30, 2012, 9:29 a.m. UTC
This patch corrects the preanalysis of quantified expressions to avoid the
generation of temporaries to capture the bounds of iteration.

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

--  main.adb

with Ada.Text_IO; use Ada.Text_IO;
procedure Main is
   Bound : Integer;
   function F return Integer is
   begin
      Bound := Bound + 1;
      return Bound;
   end F;
   type Int_Array  is array (1 .. 10) of Integer;
   type Bool_Array is array (1 .. 10) of Boolean;
   procedure Put (Obj : Int_Array) is
   begin
      for Index in Obj'Range loop
         Put_Line (Index'Img & " =>" & Obj (Index)'Img & ";");
      end loop;
   end Put;
   procedure Put (Obj : Bool_Array) is
   begin
      for Index in Obj'Range loop
         Put_Line (Index'Img & " => " & Obj (Index)'Img & ";");
      end loop;
   end Put;
   Ints  : Int_Array;
   Bools : Bool_Array;
begin
   Bound := 0;
   Ints := (others => F);
   Put (Ints);
   Bound := 0;
   Bools := (others => (for some Index in 1 .. F => Ints (Index) = 6));
   Put (Bools);
end Main;

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

$ gnatmake -q -gnat12 main.adb
$ ./main
 1 => 1;
 2 => 2;
 3 => 3;
 4 => 4;
 5 => 5;
 6 => 6;
 7 => 7;
 8 => 8;
 9 => 9;
 10 => 10;
 1 => FALSE;
 2 => FALSE;
 3 => FALSE;
 4 => FALSE;
 5 => FALSE;
 6 => TRUE;
 7 => TRUE;
 8 => TRUE;
 9 => TRUE;
 10 => TRUE;

Tested on x86_64-pc-linux-gnu, committed on trunk

2012-03-30  Hristian Kirtchev  <kirtchev@adacore.com>

	* sem_ch5.adb (Analyze_Iteration_Scheme): Preanalyze the subtype
	definition of a loop when the context is a quantified expression.
diff mbox

Patch

Index: sem_ch5.adb
===================================================================
--- sem_ch5.adb	(revision 185995)
+++ sem_ch5.adb	(working copy)
@@ -1972,11 +1972,14 @@ 
                      N);
                end if;
 
-               --  Now analyze the subtype definition. If it is a range, create
-               --  temporaries for bounds.
+               --  Analyze the subtype definition and create temporaries for
+               --  the bounds. Do not evaluate the range when preanalyzing a
+               --  quantified expression because bounds expressed as function
+               --  calls with side effects will be erroneously replicated.
 
                if Nkind (DS) = N_Range
                  and then Expander_Active
+                 and then Nkind (Parent (N)) /= N_Quantified_Expression
                then
                   Process_Bounds (DS);