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.3 required=5.0 tests=BAYES_00,INVALID_MSGID autolearn=no autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: f891f,eac70c5fad02d925 X-Google-Attributes: gidf891f,public X-Google-Thread: 103376,eac70c5fad02d925 X-Google-Attributes: gid103376,public From: adam@irvine.com (Adam Beneschan) Subject: Re: Concerning subscript bounds checks Date: 1996/06/24 Message-ID: <4qmp9u$sjm@krusty.irvine.com>#1/1 X-Deja-AN: 161893132 references: <4qdj3e$btf@goanna.cs.rmit.EDU.AU> organization: /z/news/newsctl/organization keywords: subscripts newsgroups: comp.lang.ada,comp.lang.misc Date: 1996-06-24T00:00:00+00:00 List-Id: In article dewar@cs.nyu.edu (Robert Dewar) writes: >Richard said > >"The remaining 4 occur in this context: > subtype Simplex_Range is Natural range 0 .. Point'Length; > P: "array (Simplex_Range) of ..." > Y: "array (Simplex_Range) of ..." > X: Point; > J: Simplex_Range; > ... > J := 0; -- at the start, J = Simplex_Range'First > for I in X'Range loop > ... > P(J) := ... > Y(J) := ... > J := J + 1; > end loop; -- at the end, J = Simplex_Range'Last > P(J) := ... > Y(J) := ... >end; > >A reasonably smart compiler should be able to tell that these four >subscripts are also safe. >" > >I suspect this judgment is based on informal reasoning ("well it is pretty >obvious to me that it can be figured out"). As always compiler optimizations, >particularly range analysis are always more complicated than they appear >from simple examples. Yes, a compiler could figure this out, but "reasonably >smart" is probably an underestimate. I would be surprised if many existing >compilers can figure even this particular one out. > >P.S. GNAT has not even started to think about optimizing checks yet, >you get junk checks even in simple loops. It's something we plan >to start work on soon! Maybe I'm missing something, but it seems that the compiler should be able to figure this out easily just from the fact that J is declared as having subtype Simplex_Range. If the program isn't erroneous, J can never have a value outside Simplex_Range, and therefore no array bounds checks should be necessary. But is this still the case with Ada95, given the new definitions of "bounded error" and the like? In any case, if the declaration of J is changed to J : Natural; then yes, the compiler's job would be harder. -- Adam