https://gcc.gnu.org/g:b4ee289b9cb171e279fd44e9978e83e347cdced2

commit r17-1328-gb4ee289b9cb171e279fd44e9978e83e347cdced2
Author: Eric Botcazou <[email protected]>
Date:   Tue May 19 00:54:00 2026 +0200

    ada: Set Is_Local_Anonymous_Access on the anonymous access type of return 
objects
    
    It is already set on the anonymous access type of function results and will
    prevent return objects from being treated as Ada 2012's stand-alone objects,
    thus make it easier to enforce the RM 3.10.2(13.2) rule.
    
    gcc/ada/ChangeLog:
    
            * einfo.ads (Is_Local_Anonymous_Access): Mention return objects.
            * accessibility.adb (Type_Access_Level): Call Enclosing_Subprogram.
            * sem_ch3.adb (Find_Type_Of_Object): Set Is_Local_Anonymous_Access
            on the anonymous access type of return objects.

Diff:
---
 gcc/ada/accessibility.adb | 24 ++++++------------------
 gcc/ada/einfo.ads         | 11 ++++++-----
 gcc/ada/sem_ch3.adb       |  5 +++--
 3 files changed, 15 insertions(+), 25 deletions(-)

diff --git a/gcc/ada/accessibility.adb b/gcc/ada/accessibility.adb
index 635b162a5d79..a1472fab9198 100644
--- a/gcc/ada/accessibility.adb
+++ b/gcc/ada/accessibility.adb
@@ -2708,25 +2708,13 @@ package body Accessibility is
                 (Is_Itype (Btyp)
                   and then Nkind (Associated_Node_For_Itype (Btyp)) =
                                                          N_Object_Declaration
-                  and then Is_Return_Object
-                             (Defining_Identifier
-                                (Associated_Node_For_Itype (Btyp))))
+                  and then
+                    Is_Return_Object
+                      (Defining_Identifier (Associated_Node_For_Itype (Btyp))))
             then
-               declare
-                  Scop : Entity_Id;
-
-               begin
-                  Scop := Scope (Scope (Btyp));
-                  while Present (Scop) loop
-                     exit when Ekind (Scop) = E_Function;
-                     Scop := Scope (Scop);
-                  end loop;
-
-                  --  Treat the return object's type as having the level of the
-                  --  function's result subtype (as per RM05-6.5(5.3/2)).
-
-                  return Type_Access_Level (Etype (Scop), Allow_Alt_Model);
-               end;
+               return
+                 Type_Access_Level
+                   (Etype (Enclosing_Subprogram (Btyp)), Allow_Alt_Model);
             end if;
          end if;
 
diff --git a/gcc/ada/einfo.ads b/gcc/ada/einfo.ads
index e22762e90a46..abd7398562f3 100644
--- a/gcc/ada/einfo.ads
+++ b/gcc/ada/einfo.ads
@@ -3073,11 +3073,12 @@ package Einfo is
 --    Is_Local_Anonymous_Access
 --       Defined in access types. Set for an anonymous access type to indicate
 --       that the type is created for an array or record component with access
---       definition, an access result, or (pre-Ada 2012) a standalone object.
---       Such anonymous types have an accessibility level equal to that of the
---       declaration in which they appear, unlike the anonymous access types
---       that are created for access parameters, access discriminants, and
---       (as of Ada 2012) stand-alone objects.
+--       definition, an access result, a return object, or (before Ada 2012) a
+--       stand-alone object. Such anonymous types have an accessibility level
+--       equal to that of the declaration in which they appear (or something
+--       equivalent), unlike the anonymous access types that are created for
+--       access parameters, access discriminants, and (in Ada 2012 and later
+--       versions of the language) stand-alone objects.
 
 --    Is_Loop_Parameter
 --       Applies to all entities. Certain loops, in particular "for ... of"
diff --git a/gcc/ada/sem_ch3.adb b/gcc/ada/sem_ch3.adb
index 337c99c03248..ca9826ddf8d8 100644
--- a/gcc/ada/sem_ch3.adb
+++ b/gcc/ada/sem_ch3.adb
@@ -19251,8 +19251,9 @@ package body Sem_Ch3 is
 
          Set_Is_Local_Anonymous_Access
            (T, Ada_Version < Ada_2012
-                   or else Nkind (P) /= N_Object_Declaration
-                   or else Is_Library_Level_Entity (Defining_Identifier (P)));
+                 or else Nkind (P) /= N_Object_Declaration
+                 or else Is_Return_Object (Defining_Identifier (P))
+                 or else Is_Library_Level_Entity (Defining_Identifier (P)));
 
       --  Otherwise, either the object definition is just a subtype_mark or we
       --  are analyzing a component declaration.

Reply via email to