]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
ada: Fix wrong initialization of library-level object by conditional expression
authorEric Botcazou <ebotcazou@adacore.com>
Tue, 25 Mar 2025 17:23:50 +0000 (18:23 +0100)
committerMarc Poulhiès <dkm@gcc.gnu.org>
Tue, 10 Jun 2025 07:32:07 +0000 (09:32 +0200)
The previous fix was not robust enough in the presence of transient scopes.

gcc/ada/ChangeLog:

* exp_ch4.adb (Insert_Conditional_Object_Declaration): Deal with a
transient scope being created around the declaration.
* freeze.adb (Freeze_Entity): Do not call Freeze_Static_Object for
a renaming declaration.

gcc/ada/exp_ch4.adb
gcc/ada/freeze.adb

index 1c2a876371117c89676ff82b77e25442241d94f7..0cf5643ebe47693bca6c1d10e18ed9b5f90c70c6 100644 (file)
@@ -13321,9 +13321,19 @@ package body Exp_Ch4 is
       Insert_Action (Expr, Obj_Decl);
 
       --  The object can never be local to an elaboration routine at library
-      --  level since we will take 'Unrestricted_Access of it.
-
-      Set_Is_Statically_Allocated (Obj_Id, Is_Library_Level_Entity (Obj_Id));
+      --  level since we will take 'Unrestricted_Access of it. Beware that
+      --  Is_Library_Level_Entity always returns False when called from within
+      --  a transient scope, but the associated block will not be materialized
+      --  when the transient scope is finally closed in the case of an object
+      --  declaration (see Exp.Ch7.Wrap_Transient_Declaration).
+
+      if Scope (Obj_Id) = Current_Scope and then Scope_Is_Transient then
+         Set_Is_Statically_Allocated
+           (Obj_Id, Is_Library_Level_Entity (Scope (Obj_Id)));
+      else
+         Set_Is_Statically_Allocated
+           (Obj_Id, Is_Library_Level_Entity (Obj_Id));
+      end if;
 
       --  If the object needs finalization, we need to insert its Master_Node
       --  manually because 1) the machinery in Exp_Ch7 will not pick it since
index ce9a9742274602bbc8cc5bcf15473312ad47886f..076d4eead6b969c793d7737089d58565ed3155f4 100644 (file)
@@ -6870,9 +6870,10 @@ package body Freeze is
                end if;
             end if;
 
-            --  Static objects require special handling
+            --  Statically allocated objects require special handling
 
             if (Ekind (E) = E_Constant or else Ekind (E) = E_Variable)
+              and then No (Renamed_Object (E))
               and then Is_Statically_Allocated (E)
             then
                Freeze_Static_Object (E);