https://gcc.gnu.org/bugzilla/show_bug.cgi?id=124310

--- Comment #6 from Liam Powell <liam at liampwll dot com> ---
I now have a better test case for this that shows this is 100% a bug.
Specifically this shows erroneous behaviour without any use of containers or
Unchecked_*. Even without digging in to the accessibility rules it is clear
that this is incorrect as 3.10.2 says "The 'accessibility' rules prevent
dangling references (in the absence of uses of certain unchecked features — see
Clause 13)."

Part of this bug is a regression introduced by
g:ddd88925273e86018b6cf57c9f6acc798a38e112 as it changed
Innermost_Master_Scope_Depth to skip dynamic scopes, but changing
Innermost_Master_Scope_Depth to check if Ent is a dynamic scope is not a
complete fix as the below code will still be incorrectly accepted when using
No_Dynamic_Accessibility_Checks.

The below program must be compiled with -O2 for the erroneous behaviour to be
observable:

pragma Ada_2022;

with Ada.Text_IO; use Ada.Text_IO;

procedure Example is
   type Rec (Element : access Integer) is null record;

   function Make_Rec (X : access Integer) return Rec
   is (Element => X);

   type Acc is access all Integer;
   A : Acc;
begin
   for I in 1 .. 10 loop
      declare
         X : aliased Integer;
         R : Rec := Make_Rec (X'Access);
      begin
         if I = 1 then
            X := 0;
         end if;
         Put_Line (X'Address'Image);
         A := R.Element.all'Access;
         Put_Line (A.all'Image);
         X := I;
         Put_Line (X'Image); --  Remove to make output erroneous
      end;
   end loop;

   Put_Line (A.all'Image);
end Example;

Reply via email to