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-Thread: 103376,ea5071f634c2ea8b X-Google-NewGroupId: yes X-Google-Attributes: gida07f3367d7,domainid0,public,usenet X-Google-Language: ENGLISH,ASCII Received: by 10.68.35.68 with SMTP id f4mr18323652pbj.5.1321895310697; Mon, 21 Nov 2011 09:08:30 -0800 (PST) Path: lh20ni2191pbb.0!nntp.google.com!news2.google.com!postnews.google.com!20g2000prp.googlegroups.com!not-for-mail From: Adam Beneschan Newsgroups: comp.lang.ada Subject: Re: Generic-Package Elaboration Question / Possible GNAT Bug. Date: Mon, 21 Nov 2011 09:00:15 -0800 (PST) Organization: http://groups.google.com Message-ID: <97bea48e-b4e9-418e-8f0c-51a3aef9941f@20g2000prp.googlegroups.com> References: <7bf9bc32-850a-40c6-9ae2-5254fe220533@f29g2000yqa.googlegroups.com> NNTP-Posting-Host: 66.126.103.122 Mime-Version: 1.0 X-Trace: posting.google.com 1321895310 13929 127.0.0.1 (21 Nov 2011 17:08:30 GMT) X-Complaints-To: groups-abuse@google.com NNTP-Posting-Date: Mon, 21 Nov 2011 17:08:30 +0000 (UTC) Complaints-To: groups-abuse@google.com Injection-Info: 20g2000prp.googlegroups.com; posting-host=66.126.103.122; posting-account=duW0ogkAAABjRdnxgLGXDfna0Gc6XqmQ User-Agent: G2/1.0 X-Google-Web-Client: true X-Google-Header-Order: ARLUEHNKC X-HTTP-UserAgent: Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.0; WOW64; Trident/4.0; SLCC1; .NET CLR 2.0.50727; Media Center PC 5.0; .NET CLR 3.5.21022; .NET CLR 3.5.30729; .NET CLR 3.0.30618; .NET4.0C),gzip(gfe) Xref: news2.google.com comp.lang.ada:14480 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable Date: 2011-11-21T09:00:15-08:00 List-Id: On Nov 19, 1:14=A0pm, Shark8 wrote: > I was working on some simple little array manipulation functions when > I encountered an interesting behavior in the compiler; it said that > trying to instantiate a function of the form: > =A0 =A0Generic > =A0 =A0 =A0 Type Element is Private; > =A0 =A0 =A0 Type Index_Type is (<>); > =A0 =A0 =A0 Type Array_Type is Array (Index_Type Range <>) of Element; > =A0 =A0Function Generic_Delete( Index : In Index_Type; Data : In > Array_Type ) Return Array_Type; > > When given the following body, the compiler complained about the > Index_Range'First/Last not being static: > > =A0 =A0Function Generic_Delete( Index : In Index_Type; Data : In > Array_Type ) Return Array_Type is > =A0 =A0begin > =A0 =A0 =A0 if Index not in Data'Range then > =A0 =A0 =A0 =A0 =A0Raise Constraint_Error; > =A0 =A0 =A0 else > =A0 =A0 =A0 =A0 =A0 =A0case Index is > =A0 =A0 =A0 =A0 =A0 =A0when Index_Type'First =3D> Return > Data( Index_Type'Succ(Index)..Data'Last ); > =A0 =A0 =A0 =A0 =A0 =A0when Index_Type'Last =3D> Return > Data( Data'First..Index_Type'Pred(Index) ); > =A0 =A0 =A0 =A0 =A0 =A0when others =3D> > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 Return Data( Data'First..Index_Type'P= red(Index) ) > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0& Data( Index_Type'Succ(Index)..Data'L= ast ); > =A0 =A0 =A0 =A0 =A0 =A0end case; > =A0 =A0 =A0 end if; > =A0 =A0end Generic_Delete; > > Now, I know that the following works as a fix: > > =A0 =A0 =A0Function Generic_Delete( Index : In Index_Type; Data : In > Array_Type ) Return Array_Type is > =A0 =A0 =A0begin > =A0 =A0 =A0 =A0 if Index not in Data'Range then > =A0 =A0 =A0 =A0 =A0 =A0Raise Constraint_Error; > =A0 =A0 =A0 =A0 else > =A0 =A0 =A0 =A0 =A0 =A0if Index =3D Data'First then > =A0 =A0 =A0 =A0 =A0 =A0 =A0 Return Data( Index_Type'Succ(Index)..Data'Las= t ); > =A0 =A0 =A0 =A0 =A0 =A0elsif Index =3D Data'Last then > =A0 =A0 =A0 =A0 =A0 =A0 =A0 Return Data( Data'First..Index_Type'Pred(Inde= x) ); > =A0 =A0 =A0 =A0 =A0 =A0else > =A0 =A0 =A0 =A0 =A0 =A0 =A0 Return Data( Data'First..Index_Type'Pred(Inde= x) ) > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0& Data( Index_Type'Succ(Index)..Data'L= ast ); > =A0 =A0 =A0 =A0 =A0 =A0end if; > =A0 =A0 =A0 =A0 end if; > =A0 =A0 =A0end Generic_Delete; > > But the question that came to my mind after reading the ARM section on > the error (sorry, I forgot to write it down) is, why is it not static? > Is it because though the elaboration-parameters *may* be known [and > static] at compile-time that some instantiation might NOT be > [guaranteed] to be known at compile-time? (i.e. because the compiler > cannot guarantee that you won't throw 1..N where N is something pulled > from the user-input?) I think one thing to keep in mind is that it's legal in Ada to compile the specification of a generic, and *then* compile one or more instances of the generic, before the generic body is ever seen. So say you compile source S1 containing the specification of Generic_Delete, and then compile source S2 which instantiates Generic_Delete with Index_Type =3D> Some_Subtype where Some_Subtype has a non-static bound (like your 1..N). Later, you compile source S3 containing the body of Generic_Delete. If the CASE statement you wrote were legal, it would make source S2 illegal retroactively, which isn't really feasible. I think that's one reason, perhaps the main reason, why the Ada rules are written the way they are---a generic body has to be legal for all possible instantiations for which the specification is legal. -- Adam