Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Labeled exit within generalized labeled loop not allowed?

118 views
Skip to first unread message

Marc C

unread,
Jul 13, 2012, 12:19:37 PM7/13/12
to
I find it hard to believe that this would be a GNAT GPL 2012 compiler bug, but I don't see anything in the LRM that indicates this wouldn't be legal.

Code containing a labeled generalized loop (the "for Elem of Some_Container" type), with a conditional exit referencing the loop label, fails to compile with an "Invalid loop name in exit statement" error.

What's the rationale for this? Or a consequence of what? Or is it a compiler bug?

Marc A. Criley

Here's an example:

with Text_IO; use Text_IO;

procedure Loop_Check is

type Int_Array_Type is array (1..10) of Integer;

IA : Int_Array_Type := (6 => 6, others => 3);

begin
Loop_A: For I in IA'range loop
Put_Line(Integer'Image(IA(I)));
exit Loop_A When IA(I) = 6;
end loop Loop_A;

Loop_B: For I of IA loop
Put_Line(Integer'Image(I));
exit when I = 6;
end loop Loop_B;

Loop_C: For I of IA loop
Put_Line(Integer'Image(I));
exit Loop_C when I = 6; -- Compiler error on this line
end loop Loop_C;
end Loop_Check;

Thomas Løcke

unread,
Jul 13, 2012, 1:21:35 PM7/13/12
to
On 07/13/2012 06:19 PM, Marc C wrote:
> I find it hard to believe that this would be a GNAT GPL 2012 compiler bug, but I don't see anything in the LRM that indicates this wouldn't be legal.
>
> Code containing a labeled generalized loop (the "for Elem of Some_Container" type), with a conditional exit referencing the loop label, fails to compile with an "Invalid loop name in exit statement" error.
>
> What's the rationale for this? Or a consequence of what? Or is it a compiler bug?


Yea, I got hit by that one also, and it's annoying as heck.

I need my loop labels, or my sanity will crumple. Yes, I'm that
fragile. :D

--
Thomas Lųcke | tho...@12boo.net | http://12boo.net


Thomas Løcke

unread,
Jul 16, 2012, 5:33:48 AM7/16/12
to
On 07/13/2012 06:19 PM, Marc C wrote:
> I find it hard to believe that this would be a GNAT GPL 2012 compiler bug, but I don't see anything in the LRM that indicates this wouldn't be legal.
>
> Code containing a labeled generalized loop (the "for Elem of Some_Container" type), with a conditional exit referencing the loop label, fails to compile with an "Invalid loop name in exit statement" error.
>
> What's the rationale for this? Or a consequence of what? Or is it a compiler bug?


Hmm, it appears we're the only people who experience this, or perhaps
the only people who care? :D

I would've thought that labeled exits were something that were widely
used.

--
Thomas L�cke | tho...@12boo.net | http://12boo.net


Niklas Holsti

unread,
Jul 16, 2012, 6:33:47 AM7/16/12
to
On 12-07-16 12:33 , Thomas L�cke wrote:
> On 07/13/2012 06:19 PM, Marc C wrote:
>> I find it hard to believe that this would be a GNAT GPL 2012 compiler
>> bug, but I don't see anything in the LRM that indicates this wouldn't
>> be legal.
>>
>> Code containing a labeled generalized loop (the "for Elem of
>> Some_Container" type), with a conditional exit referencing the loop
>> label, fails to compile with an "Invalid loop name in exit statement"
>> error.
>>
>> What's the rationale for this? Or a consequence of what? Or is it a
>> compiler bug?
>
>
> Hmm, it appears we're the only people who experience this, or perhaps
> the only people who care? :D

My guess is a compiler bug, not discovered earlier because the labeled
exit statements are rare, and users of Ada 2012 also (so far).

> I would've thought that labeled exits were something that were widely
> used.

A data point on this:

I scanned an Ada 2005 project of mine. It has 893 loops but only 35
labeled exit statements. These are always on large, complex loops,
sometimes nested loops where the exit leaves the outermost loop from an
innner loop. When these loops are traversing a data structure, the
structure is usually dynamic (e.g. a work-list) that can shrink and grow
during the execution of the loop.

This project has many loop exits from small traversal loops of fixed
data structures. These loops could use the generalized loop syntax, but
the loops are small and usually not nested, so they are not named.

To me, the generalized loop syntax suggests that the loop should be
executed for all the elements in the container, that the amount of data
in the container does not change during the loop, and that the traversal
order is not important. I know that the order is defined; I am talking
about my impression while reading the program. I'm not sure if data can
be added to or removed from a container during a generalized iteration
over the container; would that be "tampering"?

--
Niklas Holsti
Tidorum Ltd
niklas holsti tidorum fi
. @ .


Niklas Holsti

unread,
Jul 16, 2012, 8:03:19 AM7/16/12
to
I forgot to state my conclusion explicitly (at the end, below):
The conclusion is that I (as one example of an Ada programmer) usually
would not code an exit statement (whether labeled or unlabeled) in a
simple loop that uses the generalized loop syntax, because then the loop
would not process all the elements in the container.

Vice versa, I would also be unlikely to use the generalized loop syntax
for a loop that needs a labeled exit statement, because such a loop is
likely to be more complex and dynamic than seems suitable (to me) for
the generalized loop syntax. Moreover, an exit statement usually means
that iteration order is important, which I (subjectively) do not
associate with the generalized syntax.

Thomas Løcke

unread,
Jul 16, 2012, 8:29:43 AM7/16/12
to
On 07/16/2012 02:03 PM, Niklas Holsti wrote:
> I forgot to state my conclusion explicitly (at the end, below):
>> To me, the generalized loop syntax suggests that the loop should be
>> executed for all the elements in the container, that the amount of data
>> in the container does not change during the loop, and that the traversal
>> order is not important. I know that the order is defined; I am talking
>> about my impression while reading the program. I'm not sure if data can
>> be added to or removed from a container during a generalized iteration
>> over the container; would that be "tampering"?
>
> The conclusion is that I (as one example of an Ada programmer) usually
> would not code an exit statement (whether labeled or unlabeled) in a
> simple loop that uses the generalized loop syntax, because then the loop
> would not process all the elements in the container.
>
> Vice versa, I would also be unlikely to use the generalized loop syntax
> for a loop that needs a labeled exit statement, because such a loop is
> likely to be more complex and dynamic than seems suitable (to me) for
> the generalized loop syntax. Moreover, an exit statement usually means
> that iteration order is important, which I (subjectively) do not
> associate with the generalized syntax.



To me, the new generalized loops are mostly syntactic sugar. I don't
associate them with loops where the loop _must_ be executed for all
elements.

I think you're right: It's probably an undiscovered compiler bug due
to low usage of the feature. But low usage does not equal no usage.

I want my named exit statements back! :D

Marc C

unread,
Jul 16, 2012, 11:24:53 AM7/16/12
to
On Monday, July 16, 2012 7:29:43 AM UTC-5, Thomas Løcke wrote:

> I think you're right: It's probably an undiscovered compiler bug due
> to low usage of the feature. But low usage does not equal no usage.

Reported this to AdaCore and received confirmation that it's a bug. Got missed because there's not been a lot of user experience with the Ada 2012 features yet.

> I want my named exit statements back! :D

GNAT GPL 2013 :-)

Thomas Løcke

unread,
Jul 17, 2012, 2:01:15 AM7/17/12
to
On 07/16/2012 05:24 PM, Marc C wrote:
> Reported this to AdaCore and received confirmation that it's a bug. Got missed because there's not been a lot of user experience with the Ada 2012 features yet.


YAY!


> GNAT GPL 2013 :-)


I'm a patient guy. I can handle the wait.

/me stares blankly at monitor

Randy Brukardt

unread,
Jul 19, 2012, 2:41:00 AM7/19/12
to
"Niklas Holsti" <niklas...@tidorum.invalid> wrote in message
news:a6ie8c...@mid.individual.net...
...
> The conclusion is that I (as one example of an Ada programmer) usually
> would not code an exit statement (whether labeled or unlabeled) in a
> simple loop that uses the generalized loop syntax, because then the loop
> would not process all the elements in the container.

The AARM specifically suggests using an exit statement if you need to just
handle part of a container. See A.18.2(230.a-c/3).

That's the justification for why there is an iterator form that has a start
location, but no iterator with a stop location. (Beyond the fact that there
are problems if the stop location is not in the container.)

So it is intended that such exits be used. I'd expect the need to be
relatively rare, but certainly not non-existent.

Randy.


0 new messages