From: "Dmitry A. Kazakov" <mailbox@dmitry-kazakov.de>
Subject: Re: Interesting effects in array renaming
Date: Mon, 23 Jun 2003 10:12:30 +0200
Date: 2003-06-23T10:12:30+02:00 [thread overview]
Message-ID: <bd6ckc$p8kv1$1@ID-77047.news.dfncis.de> (raw)
In-Reply-To: 3EF5E6B8.3030203@spam.com
Jeffrey Carter wrote:
> Dmitry A. Kazakov wrote:
>> ---------- test.adb
>> with Ada.Text_IO; use Ada.Text_IO;
>>
>> procedure Test is
>> type Some_Array is array (Integer range <>) of Integer;
>>
>> procedure Foo (X : Some_Array) is
>> subtype Constrained is Some_Array (1..X'Length);
>> XX : Constrained renames X;
>> begin
>> Put_Line
>> ( "XX'Range:"
>> & Integer'Image (XX'First)
>> & ".."
>> & Integer'Image (XX'Last)
>> & ", Constrained'Range:"
>> & Integer'Image (Constrained'First)
>> & ".."
>> & Integer'Image (Constrained'Last)
>> );
>> end Foo;
>>
>> Object : Some_Array (-3..2) := (-3, -2, -1, 0, 1, 2);
>> begin
>> Foo (Object);
>> end Test;
>> ---------- test.adb
>>
>> Surprisingly the above produces different ranges for XX and its declared
>> subtype. Isn't something wrong here?
>
> Note that you can also do
>
> subtype Constrained is Some_Array (1..X'Length-1);
>
> or
>
> subtype Constrained is Some_Array (1..3);
>
> with similar results. You can also do
>
> XX : Some_Array renames X;
>
> which perhaps demonstrates more clearly where the object's subtype comes
> from.
I see no link with subtypes. ARM clearly states: 8.5.1(6)
"... any constraint implied by the subtype_mark of the
object_renaming_declaration is ingored)."
I.e. whatever subtype you, a programmer, might specify I, the compiler, will
shamelessly ignore it!
How safe! Do you really think it is OK? Consider this:
X1 : Constrained := Constrained (X);
X2 : Constrained renames X;
What is OK here, that X1 and X2 have different constraints or that X2 is
potentially invalid?
I see only two alternatives:
Either to add a dynamic semantics ensuring that the result of renaming is a
valid object of the declared subtype. [X2 raises Constraint_Error if
Constrained'Range /= X'Range]
Or [painful] to require X1 and X2 be same in all cases, and thus:
XX : Some_Array renames X (X'First + 1 .. X'Last);
would have length different from X, so a new array dope has to be generated
etc.
--
Regards,
Dmitry A. Kazakov
www.dmitry-kazakov.de
next prev parent reply other threads:[~2003-06-23 8:12 UTC|newest]
Thread overview: 20+ messages / expand[flat|nested] mbox.gz Atom feed top
2003-06-22 14:52 Interesting effects in array renaming Dmitry A. Kazakov
2003-06-22 17:24 ` Jeffrey Carter
2003-06-23 8:12 ` Dmitry A. Kazakov [this message]
2003-06-23 10:29 ` Georg Bauhaus
2003-06-23 11:37 ` Dmitry A. Kazakov
2003-06-23 13:28 ` Georg Bauhaus
2003-06-24 7:35 ` Dmitry A. Kazakov
2003-06-24 14:38 ` Georg Bauhaus
2003-06-25 10:28 ` Dmitry A. Kazakov
2003-06-25 14:23 ` Georg Bauhaus
2003-06-25 19:00 ` Dmitry A. Kazakov
2003-06-24 2:35 ` Robert I. Eachus
2003-06-24 7:35 ` Dmitry A. Kazakov
2003-06-24 10:08 ` Lutz Donnerhacke
2003-06-24 11:53 ` Georg Bauhaus
2003-06-24 12:48 ` Dmitry A. Kazakov
2003-06-26 2:54 ` Randy Brukardt
2003-06-26 6:27 ` Vinzent Hoefler
2003-06-26 12:44 ` Georg Bauhaus
2003-06-26 13:01 ` Vinzent Hoefler
replies disabled
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox