]> gcc.gnu.org Git - gcc.git/commitdiff
ada: Fix for Default_Component_Value with declare expressions
authorPiotr Trojanek <trojanek@adacore.com>
Thu, 16 May 2024 08:59:31 +0000 (10:59 +0200)
committerMarc Poulhiès <poulhies@adacore.com>
Fri, 21 Jun 2024 08:34:18 +0000 (10:34 +0200)
When the expression of aspect Default_Component_Value includes a declare
expression with current type instance, we attempted to recursively froze
that type, which itself caused an infinite recursion, because we didn't
properly manage the scope of declare expression.

This patch fixes both the detection of the current type instance and
analysis of the expression that caused recursive freezing.

gcc/ada/

* sem_attr.adb (In_Aspect_Specification): Use the standard
condition that works correctly with declare expressions.
* sem_ch13.adb (Analyze_Aspects_At_Freeze_Point): Replace
ordinary analysis with preanalysis of spec expressions.

gcc/ada/sem_attr.adb
gcc/ada/sem_ch13.adb

index 72f5ab49175946597fdb594779c4ab80bfac71df..d56c25a79cc86ff38132bf931c11ba781267fe95 100644 (file)
@@ -1843,7 +1843,9 @@ package body Sem_Attr is
                if Nkind (P) = N_Aspect_Specification then
                   return P_Type = Entity (P);
 
-               elsif Nkind (P) in N_Declaration then
+               --  Prevent the search from going too far
+
+               elsif Is_Body_Or_Package_Declaration (P) then
                   return False;
                end if;
 
index 4012932a6f21fb294351a621cb82ccb31155e095..a86f774018a356c382bd9fd5b2d22e83346b4cad 100644 (file)
@@ -1037,11 +1037,19 @@ package body Sem_Ch13 is
 
          Parent_Type : Entity_Id;
 
+         Save_In_Spec_Expression : constant Boolean := In_Spec_Expression;
+
       begin
          --  Ensure Expr is analyzed so that e.g. all types are properly
-         --  resolved for Find_Type_Reference.
+         --  resolved for Find_Type_Reference. We preanalyze this expression
+         --  as a spec expression (to avoid recursive freezing), while skipping
+         --  resolution (to not fold type self-references, e.g. T'Last).
 
-         Analyze (Expr);
+         In_Spec_Expression := True;
+
+         Preanalyze (Expr);
+
+         In_Spec_Expression := Save_In_Spec_Expression;
 
          --  A self-referential aspect is illegal if it forces freezing the
          --  entity before the corresponding aspect has been analyzed.
This page took 0.083878 seconds and 5 git commands to generate.