[Ada] Double evaluation in allocator for unconstrained packed array

Submitted by Arnaud Charlet on Aug. 29, 2011, 12:54 p.m.


Message ID 20110829125404.GA28438@adacore.com
State New
Headers show

Commit Message

Arnaud Charlet Aug. 29, 2011, 12:54 p.m.
This patch fixes a typo in the expansion of an allocator for an unconstrained
packed array, when the expression is a function call.

The execution of tester below must yield:

in next_frame
in next_frame
with Media;
with ada.text_IO; 
procedure Tester is 
   Case_1 :        Media.Image :=     Media.Image' (Media.next_Frame); 
   Case_2 : access Media.Image := new Media.Image' (Media.next_Frame); 
   ada.text_io.put_line (Case_1 (5).Val'Img); 
   ada.text_io.put_line (Case_2 (5).Val'Img); 
with Ada.Text_IO; use Ada.Text_IO; 
package body Media 
   Counter: Integer := 0; 
   function next_Frame return Image is 
      put_line("in next_frame"); 
      counter := counter + 1; 
      return (1..10 => (val => counter)); 
   end next_Frame; 
end Media; 
package Media is 
   type Color is 
         Val : Integer; 
      end record; 

   type Image is array (Integer range <>) of Color; 
   pragma pack (Image);                                -- matters! 

   function next_Frame return Image; 
end Media; 

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

2011-08-29  Ed Schonberg  <schonberg@adacore.com>

	* exp_ch4.adb (Expand_Allocator_Expression): Use consistent name for
	inner expression, to prevent double evaluation.

Patch hide | download patch | download mbox

Index: exp_ch4.adb
--- exp_ch4.adb	(revision 178205)
+++ exp_ch4.adb	(working copy)
@@ -1165,7 +1165,8 @@ 
                Insert_Action (Exp,
                  Make_Subtype_Declaration (Loc,
                    Defining_Identifier => ConstrT,
-                   Subtype_Indication  => Make_Subtype_From_Expr (Exp, T)));
+                   Subtype_Indication  =>
+                     Make_Subtype_From_Expr (Internal_Exp, T)));
                Freeze_Itype (ConstrT, Exp);
                Rewrite (Exp, OK_Convert_To (ConstrT, Internal_Exp));