https://gcc.gnu.org/g:37c312486186ed9dc2561b2e341fd81f4f1627ec

commit r15-9620-g37c312486186ed9dc2561b2e341fd81f4f1627ec
Author: Eric Botcazou <ebotca...@adacore.com>
Date:   Mon May 5 12:58:58 2025 +0200

    Ada: Fix assertion failure on Finalizable aspect for tagged record type
    
    This is a (benign) assertion failure on the mainline for the new Finalizable
    aspect put on a tagged record type when not all the primitives are declared.
    This compiles and runs on the 15 branch because assertions are disabled.
    
    gcc/ada/
            PR ada/120104
            * exp_ch3.adb (Expand_Freeze_Record_Type): For a controlled tagged
            type, freeze only the controlled primitives that are present.
    
    gcc/testsuite/
            * gnat.dg/specs/finalizable1.ads: New test.

Diff:
---
 gcc/ada/exp_ch3.adb                          | 30 ++++++++++++++++++----------
 gcc/testsuite/gnat.dg/specs/finalizable1.ads | 11 ++++++++++
 2 files changed, 30 insertions(+), 11 deletions(-)

diff --git a/gcc/ada/exp_ch3.adb b/gcc/ada/exp_ch3.adb
index 0dfd8102df18..bc46fd37e0c6 100644
--- a/gcc/ada/exp_ch3.adb
+++ b/gcc/ada/exp_ch3.adb
@@ -6321,19 +6321,27 @@ package body Exp_Ch3 is
             --  frozen inside.
 
             if Is_Controlled (Typ) then
-               Append_Freeze_Actions (Typ,
-                 Freeze_Entity
-                   (Find_Controlled_Prim_Op (Typ, Name_Initialize), Typ));
+               declare
+                  Prim : Entity_Id;
 
-               if not Is_Limited_Type (Typ) then
-                  Append_Freeze_Actions (Typ,
-                    Freeze_Entity
-                      (Find_Controlled_Prim_Op (Typ, Name_Adjust), Typ));
-               end if;
+               begin
+                  Prim := Find_Controlled_Prim_Op (Typ, Name_Initialize);
+                  if Present (Prim) then
+                     Append_Freeze_Actions (Typ, Freeze_Entity (Prim, Typ));
+                  end if;
 
-               Append_Freeze_Actions (Typ,
-                 Freeze_Entity
-                   (Find_Controlled_Prim_Op (Typ, Name_Finalize), Typ));
+                  if not Is_Limited_Type (Typ) then
+                     Prim := Find_Controlled_Prim_Op (Typ, Name_Adjust);
+                     if Present (Prim) then
+                        Append_Freeze_Actions (Typ, Freeze_Entity (Prim, Typ));
+                     end if;
+                  end if;
+
+                  Prim := Find_Controlled_Prim_Op (Typ, Name_Finalize);
+                  if Present (Prim) then
+                     Append_Freeze_Actions (Typ, Freeze_Entity (Prim, Typ));
+                  end if;
+               end;
             end if;
 
             --  Freeze rest of primitive operations. There is no need to handle
diff --git a/gcc/testsuite/gnat.dg/specs/finalizable1.ads 
b/gcc/testsuite/gnat.dg/specs/finalizable1.ads
new file mode 100644
index 000000000000..5fa8f5cf3c3f
--- /dev/null
+++ b/gcc/testsuite/gnat.dg/specs/finalizable1.ads
@@ -0,0 +1,11 @@
+-- { dg-do compile }
+-- { dg-options "-gnatX0" }
+
+package Finalizable1 is
+
+  type Root is abstract tagged null record
+    with Finalizable => (Finalize => Finalize);
+
+  procedure Finalize (This : in out Root) is abstract;
+
+end Finalizable1;

Reply via email to