comp.lang.ada
 help / color / mirror / Atom feed
* Anonymous Access and Accessibility Levels
@ 2019-04-20 15:29 Jere
  2019-04-20 15:58 ` J-P. Rosen
  2019-04-22 22:11 ` Randy Brukardt
  0 siblings, 2 replies; 18+ messages in thread
From: Jere @ 2019-04-20 15:29 UTC (permalink / raw)


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.


^ permalink raw reply	[flat|nested] 18+ messages in thread

end of thread, other threads:[~2019-05-14  0:03 UTC | newest]

Thread overview: 18+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-04-20 15:29 Anonymous Access and Accessibility Levels Jere
2019-04-20 15:58 ` J-P. Rosen
2019-04-22 22:03   ` Randy Brukardt
2019-04-24 10:42   ` Jere
2019-04-24 23:27     ` Randy Brukardt
2019-04-26  2:47       ` Optikos
2019-05-11 11:58         ` Jere
2019-04-26 17:12     ` G.B.
2019-05-11 12:06       ` Jere
2019-05-14  0:03         ` Randy Brukardt
2019-04-22 22:11 ` Randy Brukardt
2019-04-22 22:23   ` Shark8
2019-04-23 23:42     ` Randy Brukardt
2019-04-23  7:44   ` Dmitry A. Kazakov
2019-04-23 23:47     ` Randy Brukardt
2019-04-24 10:34   ` Jere
2019-04-24 10:44     ` Jere
2019-04-24 23:21       ` Randy Brukardt

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox