From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 94333 invoked by alias); 5 Dec 2017 11:46:46 -0000 Mailing-List: contact gcc-patches-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Archive: List-Post: List-Help: Sender: gcc-patches-owner@gcc.gnu.org Received: (qmail 94324 invoked by uid 89); 5 Dec 2017 11:46:46 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-11.1 required=5.0 tests=BAYES_00,GIT_PATCH_2,GIT_PATCH_3,KAM_ASCII_DIVIDERS,RCVD_IN_DNSWL_NONE,SPF_PASS autolearn=ham version=3.3.2 spammy=remained X-HELO: rock.gnat.com Received: from rock.gnat.com (HELO rock.gnat.com) (205.232.38.15) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Tue, 05 Dec 2017 11:46:45 +0000 Received: from localhost (localhost.localdomain [127.0.0.1]) by filtered-rock.gnat.com (Postfix) with ESMTP id BC9E3116ABF; Tue, 5 Dec 2017 06:46:43 -0500 (EST) Received: from rock.gnat.com ([127.0.0.1]) by localhost (rock.gnat.com [127.0.0.1]) (amavisd-new, port 10024) with LMTP id Y4LjhNjF5zNa; Tue, 5 Dec 2017 06:46:43 -0500 (EST) Received: from tron.gnat.com (tron.gnat.com [IPv6:2620:20:4000:0:46a8:42ff:fe0e:e294]) by rock.gnat.com (Postfix) with ESMTP id A971D116AB2; Tue, 5 Dec 2017 06:46:43 -0500 (EST) Received: by tron.gnat.com (Postfix, from userid 4862) id A5CD8379; Tue, 5 Dec 2017 06:46:43 -0500 (EST) Date: Tue, 05 Dec 2017 11:46:00 -0000 From: Pierre-Marie de Rodat To: gcc-patches@gcc.gnu.org Cc: Ed Schonberg Subject: [Ada] Improper visibility of loop parameter with Iterable aspect Message-ID: <20171205114643.GA106058@adacore.com> MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="Dxnq1zWXvFF0Q93v" Content-Disposition: inline User-Agent: Mutt/1.5.23 (2014-03-12) X-IsSubscribed: yes X-SW-Source: 2017-12/txt/msg00202.txt.bz2 --Dxnq1zWXvFF0Q93v Content-Type: text/plain; charset=us-ascii Content-Disposition: inline Content-length: 2056 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 * 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. --Dxnq1zWXvFF0Q93v Content-Type: text/plain; charset=us-ascii Content-Disposition: attachment; filename=difs Content-length: 1424 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; ------------------------------------------ --Dxnq1zWXvFF0Q93v--