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,b3f788f59498d3af X-Google-Attributes: gid103376,public,usenet X-Google-Language: ENGLISH,ASCII-7-bit Path: g2news1.google.com!postnews.google.com!a26g2000pre.googlegroups.com!not-for-mail From: Adam Beneschan Newsgroups: comp.lang.ada Subject: Re: Exceptions and out procedure arguments (using GNAT GPL) Date: Mon, 18 Jun 2007 08:44:57 -0700 Organization: http://groups.google.com Message-ID: <1182181497.595409.300500@a26g2000pre.googlegroups.com> References: <79c673pq5htg508nkoi935n3udqg5ps7r8@4ax.com> NNTP-Posting-Host: 66.126.103.122 Mime-Version: 1.0 Content-Type: text/plain; charset="us-ascii" X-Trace: posting.google.com 1182181498 24655 127.0.0.1 (18 Jun 2007 15:44:58 GMT) X-Complaints-To: groups-abuse@google.com NNTP-Posting-Date: Mon, 18 Jun 2007 15:44:58 +0000 (UTC) In-Reply-To: <79c673pq5htg508nkoi935n3udqg5ps7r8@4ax.com> User-Agent: G2/1.0 X-HTTP-UserAgent: Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.7.12) Gecko/20050922 Fedora/1.7.12-1.3.1,gzip(gfe),gzip(gfe) Complaints-To: groups-abuse@google.com Injection-Info: a26g2000pre.googlegroups.com; posting-host=66.126.103.122; posting-account=cw1zeQwAAABOY2vF_g6V_9cdsyY_wV9w Xref: g2news1.google.com comp.lang.ada:16220 Date: 2007-06-18T08:44:57-07:00 List-Id: On Jun 15, 6:05 pm, Fionn Mac Cumhaill wrote: > Consider a procedure that starts like this: > > procedure My_Procedure ( > O: out integer > ) > is > begin > > -- various statements follow > > O := 999; > > -- more statements follow > > raise My_Exception; > > I'm using GNAT GPL. > > My question is: > > Is the routine which calls My_Procedure guaranteed to get a value if > it does something like this? > > X := 0; > My_Procedure(X); > > and has an exception handler > > exception > when My_Exception => > null; > > Will X get a value of 999? No. When My_Procedure completes abnormally, due to an exception raise, X should have the value that it had before My_Procedure was called. An Ada compiler that causes X to be 999 here (if it wasn't 999 before) is incorrect. This isn't a matter of "is the compiler allowed to optimize" or "all bets are off" or "you can't rely on the value"; rather, the semantics *require* that X be unchanged. This is because O is a by-copy parameter (6.2(3)), which means that inside the subprogram, O denotes a *separate* object from X (6.2(2)), and O is copied back to X only on *normal* completion of the subprogram (6.4.1(17)), but an exception raise causes My_Procedure to be completed abnormally (7.6.1(2)). By-reference parameters work differently. If, for example, your OUT parameter were a tagged record, and you had assigned a component of it to 999, it should still be 999 even after the exception in My_Procedure is raised. As I interpret the rules in 11.6, this might not be the case if the exception raise is due to a language-defined check; if, after you assign the component to 999, you do an array access on a nonexistent element, so that Constraint_Error is raised, this is a language-defined check, and now I think the compiler may be allowed to optimize in a way so that the assignment of the component to 999 might not take place. But in your example, you have an explicit raise of a user-defined exception, and 11.6 doesn't apply to those, as I read it. The same would apply in an access parameter case; if My_Procedure is abandoned due to a raise of a user-defined exception, you can count on any assignments that you've already done through the access value, but you can't count on assignments done before My_Procedure is abandoned due to a language-defined check. Hope this helps, -- Adam