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=ham autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,36d61bbd5bb05d97 X-Google-Attributes: gid103376,public X-Google-ArrivalTime: 2002-10-10 13:09:23 PST Path: archiver1.google.com!news1.google.com!newsfeed.stanford.edu!bloom-beacon.mit.edu!news-out.cwix.com!newsfeed.cwix.com!feed2.news.rcn.net!rcn!newsfeed1.earthlink.net!newsfeed.earthlink.net!stamper.news.pas.earthlink.net!newsread2.prod.itd.earthlink.net.POSTED!not-for-mail Message-ID: <3DA5DE58.4030105@acm.org> From: Jeffrey Carter User-Agent: Mozilla/5.0 (Windows; U; Win98; en-US; rv:1.0.0) Gecko/20020530 X-Accept-Language: en-us, en MIME-Version: 1.0 Newsgroups: comp.lang.ada Subject: Re: Question about subtype of loop index variable References: <3DA5CF6D.C703306F@cadence.com> Content-Type: text/plain; charset=us-ascii; format=flowed Content-Transfer-Encoding: 7bit Date: Thu, 10 Oct 2002 20:09:25 GMT NNTP-Posting-Host: 63.184.0.81 X-Complaints-To: abuse@earthlink.net X-Trace: newsread2.prod.itd.earthlink.net 1034280565 63.184.0.81 (Thu, 10 Oct 2002 13:09:25 PDT) NNTP-Posting-Date: Thu, 10 Oct 2002 13:09:25 PDT Organization: EarthLink Inc. -- http://www.EarthLink.net Xref: archiver1.google.com comp.lang.ada:29690 Date: 2002-10-10T20:09:25+00:00 List-Id: Paul Graham wrote: > with Text_Io; use Text_Io; > procedure Test_Range is > package Int_Io is new Integer_Io(Integer); use Int_Io; > type T1 is range 0 .. 7; > type T2 is array(T1 range <>) of Integer; > X : T2(0 .. 3); > Y : Integer; > begin > Y := 0; > for I in X'First - 1 .. X'Last + 1 loop > Y := Y + 1; > end loop; > Put("Y = "); > Put(Y); > New_Line; > end; > > My question is, what is the type (and subtype) of loop index I? Since > the > expressions X'First - 1 and X'Last + 1 are of type T1, then > I is also of type T1. But the values X'First - 1 and X'Last + 1 lie > outside > the bounds of T1, so I would think that a range constraint error would > occur > as I is assigned these values. Yet gnat 3.13 compiles and executes this > code > without error. Am I misunderstanding something? I think so. The expressions X'First - 1 and X'Last + 1 are evaluated using the base type of the subtype. In this case the subtype is T1, because types are always anonymous and the name in a type statement is the "first named subtype". The base type of T1 is T1'Base. T1'Base has a range of at least -7 .. 7 because signed integer types are symmetric about 0 but may have an additional negative value. The compiler almost certainly used an 8-bit base type here, so the real range is probably -128 .. 127; you can check by printing out T1'Base'First and T1'Base'Last. Of course, it would be easier and quicker to calculate Y as Y := X'Length + 2; -- Jeff Carter "You've got the brain of a four-year-old boy, and I bet he was glad to get rid of it." Horse Feathers