This patch fixes a visibility bug in the expansion of a loop over an object whose type carries the GNAT-specific Iterable aspect. Prior to this patch, the loop variable remained visible after exit from the loop.
Compiling iterator_test.adb must yield: iterator_test.adb:26:61: "number" is not visible iterator_test.adb:26:61: non-visible declaration at line 21 iterator_test.adb:26:61: non-visible declaration at line 16 --- with Ada.Text_IO; with iterators; use iterators; procedure iterator_test is it_test : constant my_iterator := create_it(from => 3, to => 10); begin -- reference loop for number in 1 .. 10 loop Ada.Text_IO.Put_Line (Integer'Image (number)); end loop; -- gas day iterations Ada.Text_IO.Put_Line ("Test foreward iteration"); for number in it_test loop Ada.Text_IO.Put_Line (Integer'Image (number)); end loop; Ada.Text_IO.Put_Line ("Test backward iteration"); for number in create_it(from => 10, to => 3) loop Ada.Text_IO.Put_Line (Integer'Image (number)); end loop; Ada.Text_IO.Put_Line ("Out of scope?! " & Integer'Image (number)); end iterator_test; --- package iterators is type my_iterator is private with Iterable => (first => iterator_first, next => iterator_next, has_element => iterator_has_element); function create_it (from : Integer; to : Integer) return my_iterator; function iterator_first (it : my_iterator) return Integer; function iterator_next (it : my_iterator; prev_item : Integer) return Integer; function iterator_has_element (it : my_iterator; prev_item : Integer) return Boolean; private type my_iterator is record from : Integer; to : Integer; end record; end iterators; Tested on x86_64-pc-linux-gnu, committed on trunk 2017-12-05 Ed Schonberg <schonb...@adacore.com> * exp_ch5.adb (Expand_Formal_Container_Loop): Ensure that the loop parameter becomes invisible after analyzing the loop, which has been rewritten as a while-loop.
Index: exp_ch5.adb =================================================================== --- exp_ch5.adb (revision 255408) +++ exp_ch5.adb (working copy) @@ -3135,6 +3135,7 @@ Advance : Node_Id; Init_Decl : Node_Id; + Init_Name : Entity_Id; New_Loop : Node_Id; begin @@ -3169,7 +3170,8 @@ -- the loop. Analyze (Init_Decl); - Set_Ekind (Defining_Identifier (Init_Decl), E_Loop_Parameter); + Init_Name := Defining_Identifier (Init_Decl); + Set_Ekind (Init_Name, E_Loop_Parameter); -- The cursor was marked as a loop parameter to prevent user assignments -- to it, however this renders the advancement step illegal as it is not @@ -3182,9 +3184,11 @@ -- Because we have to analyze the initial declaration of the loop -- parameter multiple times its scope is incorrectly set at this point -- to the one surrounding the block statement - so set the scope - -- manually to be the actual block statement. + -- manually to be the actual block statement, and indicate that it is + -- not visible after the block has been analyzed. - Set_Scope (Defining_Identifier (Init_Decl), Entity (Identifier (N))); + Set_Scope (Init_Name, Entity (Identifier (N))); + Set_Is_Immediately_Visible (Init_Name, False); end Expand_Formal_Container_Loop; ------------------------------------------