]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
ada: Fix crash on legality check for initialization of implicit constructor
authorDenis Mazzucato <mazzucato@adacore.com>
Thu, 18 Dec 2025 12:34:18 +0000 (13:34 +0100)
committerMarc Poulhiès <dkm@gcc.gnu.org>
Fri, 9 Jan 2026 10:57:22 +0000 (11:57 +0100)
This patch fixes a crash occurring during the legality check of the Initialize
aspect when the constructor is implicitly created by the compiler, e.g., the
default copy constructor. In such case, Corresponding_Spec is not available, the
Specification field must be used instead.

gcc/ada/ChangeLog:

* sem_ch13.adb (Check_Constructor_Initialization_Expression): The first
parameter of an implicit constructor comes from Specification, not
Corresponding_Spec.

gcc/ada/sem_ch13.adb

index 06a98e4305aa604b8a18ce32f42a54d15328ae06..38732cf58be299a57afdc42be3687e623c0b9c5c 100644 (file)
@@ -3305,8 +3305,7 @@ package body Sem_Ch13 is
             procedure Check_Constructor_Initialization_Expression
               (Expr : Node_Id; Aspect_Name : String)
             is
-               First_Parameter : constant Entity_Id :=
-                 First_Entity (Corresponding_Spec (N));
+               First_Parameter : Entity_Id;
 
                --  Flag error if N refers to the forbidden entity
                function Check_Node_For_Bad_Reference
@@ -3324,7 +3323,7 @@ package body Sem_Ch13 is
                   then
                      Error_Msg_N
                        ("constructed object referenced in " &
-                       Aspect_Name & " aspect_specification", N);
+                        Aspect_Name & " aspect_specification", N);
                   end if;
 
                   return OK;
@@ -3333,6 +3332,16 @@ package body Sem_Ch13 is
                procedure Check_Tree_For_Bad_Reference is
                  new Traverse_Proc (Check_Node_For_Bad_Reference);
             begin
+               --  If coming from an implicit constructor, the Self parameter
+               --  is retrieved via the specification's defining unit name.
+
+               if Acts_As_Spec (N) then
+                  First_Parameter :=
+                    First_Entity (Defining_Unit_Name (Specification (N)));
+               else
+                  First_Parameter := First_Entity (Corresponding_Spec (N));
+               end if;
+
                Check_Tree_For_Bad_Reference (Expr);
             end Check_Constructor_Initialization_Expression;