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,FREEMAIL_FROM autolearn=unavailable autolearn_force=no version=3.4.4 X-Received: by 2002:a24:6955:: with SMTP id e82mr7659518itc.102.1555774179562; Sat, 20 Apr 2019 08:29:39 -0700 (PDT) X-Received: by 2002:aca:ba82:: with SMTP id k124mr5562513oif.110.1555774179145; Sat, 20 Apr 2019 08:29:39 -0700 (PDT) Path: eternal-september.org!reader01.eternal-september.org!feeder.eternal-september.org!weretis.net!feeder6.news.weretis.net!feeder.usenetexpress.com!feeder-in1.iad1.usenetexpress.com!border1.nntp.dca1.giganews.com!nntp.giganews.com!b2no306630itd.0!news-out.google.com!w17ni366itb.0!nntp.google.com!136no307555itk.0!postnews.google.com!glegroupsg2000goo.googlegroups.com!not-for-mail Newsgroups: comp.lang.ada Date: Sat, 20 Apr 2019 08:29:38 -0700 (PDT) Complaints-To: groups-abuse@google.com Injection-Info: glegroupsg2000goo.googlegroups.com; posting-host=70.109.61.2; posting-account=QF6XPQoAAABce2NyPxxDAaKdAkN6RgAf NNTP-Posting-Host: 70.109.61.2 User-Agent: G2/1.0 MIME-Version: 1.0 Message-ID: Subject: Anonymous Access and Accessibility Levels From: Jere Injection-Date: Sat, 20 Apr 2019 15:29:39 +0000 Content-Type: text/plain; charset="UTF-8" Xref: reader01.eternal-september.org comp.lang.ada:56171 Date: 2019-04-20T08:29:38-07:00 List-Id: I was trying to get a bit better at understanding how accessibility levels work with respect to anonymous access types. I have GNAT to test out things, but I think I am running into various bugs, so I am not seeing the exceptions or compilation errors I would expect. It could also be that I misunderstand the rules (They are difficult somewhat). Take for example a library level package like so: Library_Level.ads *********************************** package Library_Level is type T is record Value : Integer := 0; end record; type T_Access is access all T; type R is record -- This has accessibility level the same as T_Access I think? The_T : access T := null; end record; -- The discriminant has accessibility level of whereever the D type -- is created I think? type D(The_T : access T) is null record; -- For testing later The_T : aliased T := (others => <>); The_R : R := (others => <>); The_T_Access : T_Access := null; end Library_Level; *********************************** I think that R.The_T has a library level accessibility level? I think that D.The_T has an accessibility level relative to where an object of type D is declared? I think that D.The_T cannot be copied once initialized (at least I think I remember someone mentioning that on comp.lang.ada)? However, when I run some basic tests, I don't get all the errors or exceptions that I expect. main.adb ************************************ with Ada.Text_IO; with Library_Level; procedure Main is The_T : aliased Library_Level.T; -- This fails to compile...Good! -- The_R : Library_Level.R := (The_T => The_T'Access); procedure Test_AA_1(The_T : aliased in out Library_Level.T) is The_R : Library_Level.R; begin -- This fails to compile...Good! -- The_R.The_T := The_T'Access; null; end Test_AA_1; function Test_AA_2 (The_T : aliased in out Library_Level.T) return Library_Level.R is begin -- This compiles??? Shouldn't this fail the same way the -- commented line in Test_AA_1 fails? return (The_T => The_T'Access); end Test_AA_2; procedure Test_AA_3(The_T : aliased in out Library_Level.T) is -- This compiles! I think ok? Anonymous Access use accessibility -- level of this location? The_D : Library_Level.D := (The_T => The_T'Access); begin -- This compiles?? Should this fails since it copies a local -- access to a global access variable? Are we even allowed to -- copy an access discriminant in the first place? Library_Level.The_T_Access := The_D.The_T; end Test_AA_3; The_R : Library_Level.R; begin Ada.Text_IO.Put_Line("Testing Anonymous Access"); Test_AA_1(The_T); The_R := Test_AA_2(The_T); Test_AA_3(The_T); -- Second test of Test_AA_2 to see if a runtime exception occurs. This -- would be copying a local variable access into a library level access -- variable. Library_Level.The_R := Test_AA_2(The_T); Ada.Text_IO.Put_Line("No runtime exceptions...hmmm"); end Main; ************************************ Here Test_AA_1 does everything I expect. However, it seems like Test_AA_2 is able to circumvent the restriction by returning an aggregate instead of assigning directly. I don't know if an aggregate is treated differently or not though. Test_AA_3 not only allows the copy of the local object access, but also allows the discriminant to be copied. I realize it might be much to expect all of these to get caught at compile time, but they don't trigger any runtime exceptions either. Can someone go through and help me understand this better? In particular: 1. Are my assumptions about the accessibility levels of Library_Level.R.The_T and Library_Level.D.The_T correct? 2. Is there a GNAT bug(s) associated with Test_AA_2 and Test_AA_3 or do I misunderstand how those things work? 3. Which of the checks should be compile time and which runtime? Thanks! This was tested with GNAT Community 2018 if that helps.