From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00 autolearn=unavailable autolearn_force=no version=3.4.4 Path: eternal-september.org!reader01.eternal-september.org!reader02.eternal-september.org!news.eternal-september.org!news.eternal-september.org!news.eternal-september.org!feeder.eternal-september.org!gegeweb.org!news.ecp.fr!news.jacob-sparre.dk!loke.jacob-sparre.dk!pnx.dk!.POSTED!not-for-mail From: "Randy Brukardt" Newsgroups: comp.lang.ada Subject: Re: 'for cursor in container.first loop' bug? Date: Thu, 28 Aug 2014 14:56:18 -0500 Organization: Jacob Sparre Andersen Research & Innovation Message-ID: References: <85y4u8xztj.fsf@stephe-leake.org> NNTP-Posting-Host: static-69-95-181-76.mad.choiceone.net X-Trace: loke.gir.dk 1409255780 12906 69.95.181.76 (28 Aug 2014 19:56:20 GMT) X-Complaints-To: news@jacob-sparre.dk NNTP-Posting-Date: Thu, 28 Aug 2014 19:56:20 +0000 (UTC) X-Priority: 3 X-MSMail-Priority: Normal X-Newsreader: Microsoft Outlook Express 6.00.2900.5931 X-RFC2646: Format=Flowed; Original X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.6157 Xref: news.eternal-september.org comp.lang.ada:21962 Date: 2014-08-28T14:56:18-05:00 List-Id: "Stephen Leake" wrote in message news:85y4u8xztj.fsf@stephe-leake.org... > I'm trying to use the 'for cursor in container.first loop' syntax in Ada > 2012 with my own container, and having lots of problems. > > So first I tried a simple example using Ada.Containers.Vector, with GNAT > 2014: > > with Ada.Text_Io; use Ada.Text_Io; > with Ada.Containers.Vectors; > procedure Try_Containers > is > package Integer_Vectors is new Ada.Containers.Vectors (Natural, > Integer); > use Integer_Vectors; > > A : Vector := To_Vector (1, 10); > begin > Loop_1 : > for Element of A loop > Put_Line ("A (i) => " & Integer'Image (Element)); > -- can't do Element := 2; > end loop Loop_1; > > Loop_2 : > for Cursor in First (A) loop > Put_Line ("A (I) => " & Integer'Image (Element (Cursor))); > Replace_Element (A, Cursor, 2); > Reference (A, Cursor) := 2; > end loop Loop_2; > > end Try_Containers; > > This gives compilation errors: > > gnatmake -k ../try_containers.adb > gcc -c -I../ -I- ../try_containers.adb > try_containers.adb:17:28: invalid prefix in call > try_containers.adb:17:28: invalid prefix in selected component "I572b" > gnatmake: "../try_containers.adb" compilation error > > Same error (with a different generated name) with GnatPro 7.2. > > Commenting out loop_2 gives the expected results. > > Am I missing something simple, or is this a compiler bug? At the very > least, it's not a good error message. You're missing something simple: "First" is not an iterator (it returns a cursor). You need to use one of the functions named "Iterate" that return a value that has an iterator type: Loop_2 : for Cursor in Iterate (A) loop I'd still call it a bug, though, because the error message is inpenatrable for something simple (a violation of 5.5.2(3/3)). Indeed, this shouldn't even resolve. I checked the two more recent GNAT compilers that I have (Gnatpro 7.2.2 and a wavefront), but they both have this message. I'd report it as a bug, because you probably wasted at lot of time (and even some of mine) on this rather simple mistake. I was a bit surprised by this, because I thought the new ACATS tests covered this. (After some checking...) Interestingly, there is a test that checks for this sort of problem, and it produces sensible error messages for cases like: 95. for Item in Sparse_Cursor loop -- ERROR: | >>> cannot iterate over "Cursor" >>> to iterate directly over the elements of a container, write "of Sparse_Cursor" 116. for Item of Sparse_Cursor loop -- ERROR: | >>> cannot iterate over "Cursor" But your test (which looks the same to me) produces gibberish. This is obviously more subtle than I understand. Time to get some use out of your AdaCore support contract. :-) Randy.