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=unavailable autolearn_force=no version=3.4.4 Path: eternal-september.org!reader01.eternal-september.org!feeder.eternal-september.org!news.swapon.de!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail From: Niklas Holsti Newsgroups: comp.lang.ada Subject: Re: How can one record component be local and another not? Date: Wed, 6 May 2020 20:30:55 +0300 Organization: Tidorum Ltd Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 8bit X-Trace: individual.net p1GviX00WxEO6OxeQLlBrge/7OnpGWrvNbqOwk0HAt/tUenpFT Cancel-Lock: sha1:nnWP7Fe9jzQj7slWXI8FwnZ9PF0= User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.14; rv:68.0) Gecko/20100101 Thunderbird/68.5.0 In-Reply-To: Content-Language: en-US Xref: reader01.eternal-september.org comp.lang.ada:58614 Date: 2020-05-06T20:30:55+03:00 List-Id: On 2020-05-05 20:32, hreba wrote: > Ok, with all your hints I came to the following solution: > > -- > package Aux is >    type Integer_P is access all Integer; >    type Rec is limited record >       a: aliased Integer; >       p: Integer_P; >    end record; > >    procedure Init (r: access Rec); > end Aux; > -- > package body Aux is >    procedure Init (r: access Rec) is >    begin >       r.p:= r.a'Access; >    end Init; > end Aux; > -- > with Aux; > procedure Test is >    r:    aliased Aux.Rec; > begin >    Aux.Init (r'Access); > end Test; > -- > > It compiles nicely, and then: > > frank@pc-frank:~/Temp/Test0$ ./test > raised PROGRAM_ERROR : aux.adb:4 accessibility check failed > > #@!!0ßx*~@!!! Just to close out this issue, I wrote some test programs and explored various changes. I believe the reason for this exception is that the accessibility level of the object "Test.r" is local, nested in the procedure "Test" (so "r" exists only for the relevant call of "Test") while the access type "Integer_P" is library-level and so out-lasts the call of "Test". Note that, although "Test" here is no doubt the program's main subprogram and so its call (assuming it is not recursive) lasts almost as long as the execution of the program, the compiler does not make use of this fact to determine the life-times of types and objects in "Test". If "r" is moved to a package, to be a statically allocated, library-level object, "Aux.Init (r'Access)" works without raising this exception. Somewhat surprisingly to me, any call of the form "Aux.Init (r'Unchecked_Access)" also works, whether "r" is a library-level object or a local (limited-life) object, even if "Aux.Init" itself uses "'Access" and not "'Unchecked_Access". Apparently the accessibility level provided in the value of Unchecked_Access is such as to make the check succeed, as for a library-level object. I also found that a call like "Aux.Init (new Aux.Rec)", which applies "Aux.Init" to a heap-allocated "Rec" object, fails the accessibility check, so this check is really strict. Of course, the heap object can be deallocated at any time, so it may have a short life-time, but that can be done (mainly) by Unchecked_Deallocation, so it involves "unchecked" programming. Hm. -- Niklas Holsti Tidorum Ltd niklas holsti tidorum fi . @ .