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,d0310bb11aeb7260 X-Google-NewGroupId: yes X-Google-Attributes: gida07f3367d7,domainid0,public,usenet X-Google-Language: ENGLISH,ASCII Received: by 10.43.48.202 with SMTP id ux10mr33301593icb.6.1321484175147; Wed, 16 Nov 2011 14:56:15 -0800 (PST) Path: h5ni62798pba.0!nntp.google.com!news2.google.com!postnews.google.com!o11g2000prg.googlegroups.com!not-for-mail From: Adam Beneschan Newsgroups: comp.lang.ada Subject: Re: GNAT 4.4.5 order of conditional processing? Date: Wed, 16 Nov 2011 14:40:33 -0800 (PST) Organization: http://groups.google.com Message-ID: References: <9ig1t4F4uaU1@mid.individual.net> <87a589f9-e675-4672-a8c5-77e4a9db289e@g21g2000yqc.googlegroups.com> <22d4b4be-1a2e-435c-9b92-fbda6999a4f3@d17g2000yql.googlegroups.com> <8b60c7a5-8b9e-4573-9470-0c3aca099dbc@r9g2000vbw.googlegroups.com> NNTP-Posting-Host: 66.126.103.122 Mime-Version: 1.0 X-Trace: posting.google.com 1321484174 20204 127.0.0.1 (16 Nov 2011 22:56:14 GMT) X-Complaints-To: groups-abuse@google.com NNTP-Posting-Date: Wed, 16 Nov 2011 22:56:14 +0000 (UTC) Complaints-To: groups-abuse@google.com Injection-Info: o11g2000prg.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:14431 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable Date: 2011-11-16T14:40:33-08:00 List-Id: On Nov 16, 1:31=A0pm, Gautier write-only wrote: > On 15 nov, 22:49, awdorrin wrote: > > > Figured that was is, sloppy programming that just managed to work (for > > 20 years) due to a compiler implementation... ;-) > > Waw, so there was an Ada compiler doing (erroneously of course) a > silent short-circuit ?... Not necessarily... Later on, the poster said that the issue was with uninitialized variables. In an expression like this: if Expression-1 and Expression-2 then... where Expression-1=3DFALSE means that some variables in Expression-2 are uninitialized. The fact is, though, that even if Expression-2 involves variables that contain unpredictable garbage, using them is unlikely to cause any harm in most situations. For instance: subtype Index_Subtype is Integer range 1 .. 10; Arr : array (Index_Subtype) of Float; Curr_Index : Index_Subtype; if Curr_Index_Initialized and Arr(Curr_Index) < -1.0 then ... Suppose Curr_Index_Initialized is FALSE and Curr_Index has never been initialized to anything. The result is that if the right side is evaluated, Curr_Index's uninitialized value may be a value outside the range 1..10, and then the code will read an element of Arr that is outside the bounds of Arr. Big deal. That's not likely to make a program fail. (Unless the bit pattern it reads happens to be a Signaling NaN, maybe!!) But I also want to point out that a "silent short-circuit" is NOT necessarily erroneous. In fact, it's probably not erroneous in most cases. The compiler doesn't have to evaluate both operands of "X and Y" if X is false and evaluating Y cannot have any effect on anything. Also, if the only possible effect of evaluating Y is to fail a language check, then I think Y doesn't have to be evaluated by 11.6. Thus, if you write: if (N in My_Array'Range) and (My_Array(N) =3D 0) then ... If N is out of range, the language says this should raise Constraint_Error when My_Array(N) is evaluated. So that means that the above code shouldn't be written like that. But a compiler that generates code that doesn't raise Constraint_Error is, I think, legal by 11.6. So a compiler that generates the exact same code for the above example and for if (N in My_Array'Range) and then (My_Array(N) =3D 0) then ... is OK. Obviously, programmers shouldn't count on it. (And there may be some differences of opinion as to how 11.6 is to be interpreted. It's caused arguments in the past.) I believe the only time the compiler *must* generate code that always evaluates both operands is if the right-hand side contains a function call that could have a side-effect, or if it refers to an object where reading the object could have some external effect (i.e. a memory- mapped device address or the like). So there's a couple of reasons why (in a correct implementation) using "and" instead of "and then" could be erroneous but still not cause the program to fail. -- Adam