comp.lang.ada
 help / color / mirror / Atom feed
From: Adam Beneschan <adam@irvine.com>
Subject: Re: Missing Index Value in Array Aggregate
Date: Wed, 26 Sep 2007 08:54:31 -0700
Date: 2007-09-26T08:54:31-07:00	[thread overview]
Message-ID: <1190822071.488118.316450@y42g2000hsy.googlegroups.com> (raw)
In-Reply-To: <1i50iqb.14r2zb11w8eyc2N%csampson@inetworld.net>

On Sep 26, 6:21 am, csamp...@inetworld.net (Charles H. Sampson) wrote:
>      The compiler used in my work allowed an aggregate for
> an array with an elaboration type index to omit one of the
> index values.  (There was no others choice.)  As a result,
> Constraint_Error was raised at library elaboration time,
> which is a nasty bug to run down in this environment.
>
>      I was getting ready to file a bug report, certain
> that this was a compile-time error rather than run-time,
> when I decided to take a close look at that old LRM.  I
> was surprised to find that the legality rules of 4.4.3

Should be 4.3.3

> appear to be designed to allow just that, particularly the
> second clause of paragraph 18.
>
>      Does anyone know what the purpose of this is?  I can
> see that there's an occasional need for it, as in the case
> of an aggregate for a formal parameter of an unconstrained
> array type.  It certainly seems to me that in the case of
> an aggregate for a constrained array object, catching this
> error at compile time in is line with Ada's concept of
> doing as much as possible at compile time.  Or am I wrong?
> Are there other paragraphs that do require this to be
> caught during compilation?

There can be nothing illegal about an aggregate such as

   (2 => 1010, 3 => 2020, 4 => 3030)

when taken by itself.  Any problems have to happen only when the
compiler knows what type is expected for the aggregate.  When the
expected type is determined, the aggregate can resolve to any one-
dimensional array type whose index and element types are some integer
type; if the expected type is something else, then the aggregate is
illegal.

Note, though, that array *types* don't have bounds on their indexes.
An array subtype is an array type plus (possibly) a constraint, where
the constraint puts bounds on the indexes.  Once the type of the above
aggregate is determined, the aggregate will have an (unnamed) subtype
of that type with a constraint of (2..4).  Then, depending on what you
do with that aggregate, the subtype will be *converted* to some other
subtype.  If you declare an object:

   type int4 is array(1..4) of integer;
   A : int4 := (2 => 1010, 3 => 2020, 4 => 3030);

the semantics involve a type conversion of the unnamed aggregate
subtype to "int4".  The rules for type conversions are spelled out in
4.6.  The result is that a Constraint_Error will be raised.

In general, type conversions raise Constraint_Error when the
constraint isn't satisfied; they don't make a program illegal.  This
is consistent, because of course a compiler can't always catch every
case where a type conversion can cause a Constraint_Error:

  type intx is array (I..J) of integer;
  A : intx := (2 => 1010, 3 => 2020, 4 => 3030);

where I and J are, say, procedure parameters.

Ada 83 was consistent about this.  In Ada 95, I believe that some
constraint violations can make a program illegal, when static
expressions are involved, but only if they occur in the *middle* of
evaluating a static expression (4.9(34)), not when a static expression
can be evaluated but is known to be outside the bounds of the subtype
it will be converted to.

In any event, though, when it comes to catching subtype constraint
violations, I don't think "doing as much as possible at compile time"
is part of the Ada philosophy, since it leads to inconsistencies, and
having clear and consistent rules is very important also.  That's just
my guess at an explanation for why this isn't illegal.

Our compiler, by the way, would probably have caught the aggregate you
tried to use (depending on the exact construct).  It would have
displayed a warning that an exception will be raised at runtime.  I
just tried GNAT, and it also displays a warning at least in the case I
tried.  Our compiler also has an option that will treat a program as
illegal if it encounters any such cases where an exception is known to
be raised, but then of course this isn't strictly Ada.

                        -- Adam




  reply	other threads:[~2007-09-26 15:54 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2007-09-26 13:21 Missing Index Value in Array Aggregate Charles H. Sampson
2007-09-26 15:54 ` Adam Beneschan [this message]
2007-09-26 23:33   ` Randy Brukardt
2007-09-28  3:21     ` Charles H. Sampson
2007-09-28 10:05       ` Stephen Leake
2007-09-28 10:26         ` Ludovic Brenta
2007-09-28 16:21           ` Pascal Obry
2007-09-28 20:26             ` Maciej Sobczak
2007-10-03 17:01               ` roderick.chapman
2007-09-29 15:31         ` Charles H. Sampson
2007-09-30 11:02           ` Stephen Leake
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox