From: Pierre-Marie de Rodat <derodat@adacore.com>
To: gcc-patches@gcc.gnu.org
Cc: Ed Schonberg <schonberg@adacore.com>
Subject: [Ada] Improper visibility of loop parameter with Iterable aspect
Date: Tue, 05 Dec 2017 11:46:00 -0000 [thread overview]
Message-ID: <20171205114643.GA106058@adacore.com> (raw)
[-- Attachment #1: Type: text/plain, Size: 2056 bytes --]
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 <schonberg@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.
[-- Attachment #2: difs --]
[-- Type: text/plain, Size: 1424 bytes --]
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;
------------------------------------------
reply other threads:[~2017-12-05 11:46 UTC|newest]
Thread overview: [no followups] expand[flat|nested] mbox.gz Atom feed
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20171205114643.GA106058@adacore.com \
--to=derodat@adacore.com \
--cc=gcc-patches@gcc.gnu.org \
--cc=schonberg@adacore.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).