comp.lang.ada
 help / color / mirror / Atom feed
From: mheaney@ni.net (Matthew Heaney)
Subject: Re: Beware: Rep spec on an enumeration type clause
Date: 1997/12/12
Date: 1997-12-12T00:00:00+00:00	[thread overview]
Message-ID: <mheaney-ya023680001212972218120001@news.ni.net> (raw)
In-Reply-To: 34912418.13716044@news.geccs.gecm.com


In article <34912418.13716044@news.geccs.gecm.com>,
brian.orpin@gecm.dot.com wrote:


>>with Ada.Text_IO; use Ada.Text_IO;
>>procedure Main is
>>   type T is (AA, BB, CC);
>>   for T use (AA => -1, BB => 10, CC => 20);
>>   I:T;                -- not initialised

[snip]

>In 83 I would have simply checked to see if it was valid by 
>
>Valid := I in T'First .. T'Last;
>
>Certainly on the Tartan Compiler for C40 this detects any invalid (not
>represented) values of T.

If this works for your implementation, then it's pure surrendipity.  My
expectation is that a compiler will optimize that Boolean expression to
True, since "of course" I has be in the range of T, because it is of type
T.  This particular optimization is unwanted here, which is precisely why
the Valid attribute was added to the language.

This kind of behavior can be especially pernicious if you're using the
object to index an array.  In Ada 83, you can very easily have problems if
the value is outside the range of the array index subtype.  It's exactly
analogous to dereferencing an uninitialized access object.

In Ada 95, things are better.  The language now requires that, even if the
subtype of the index object is in the array index subtype, you're not
allowed to optimize away the range check unless you can prove the index
object has a "sensible" value.  If you try to dereference an array with a
"bad" index, then you might get Constraint_Error, or you might update the
wrong element of the array, but you can't accidently touch "components"
outside the array object.

--------------------------------------------------------------------
Matthew Heaney
Software Development Consultant
<mailto:matthew_heaney@acm.org>
(818) 985-1271




  parent reply	other threads:[~1997-12-12  0:00 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1997-12-12  0:00 Beware: Rep spec on an enumeration type clause Franco Mazzanti
1997-12-12  0:00 ` Ken Garlington
     [not found] ` <34912418.13716044@news.geccs.gecm.com>
1997-12-12  0:00   ` Ken Garlington
1997-12-12  0:00   ` Matthew Heaney [this message]
     [not found]     ` <349e19ec.16536708@news.geccs.gecm.com>
1997-12-18  0:00       ` Matthew Heaney
     [not found]         ` <349a4134.5569381@news.geccs.gecm.com>
1997-12-19  0:00           ` Matthew Heaney
replies disabled

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