]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
ada: Incorrect optimization for unconstrained limited record component type
authorSteve Baird <baird@adacore.com>
Thu, 8 Jun 2023 21:59:32 +0000 (14:59 -0700)
committerMarc Poulhiès <poulhies@adacore.com>
Tue, 1 Aug 2023 08:06:45 +0000 (10:06 +0200)
If the discriminants of an immutably limited record type have defaults, then
it is safe to assume that a discriminant of an object of this type will never
change once it is initialized. In some cases, this means that the default
discriminant values can be treated like a constraint for purposes of
determining the amount of storage needed for an unconstrained object.
However, it is not safe to perform this optimization when determining
the size needed for an unconstrained component of an enclosing type. This
optimization was sometimes being incorrectly performed in this case. This could
save storage in some cases, but in other cases a constraint check could
incorrectly fail when initializing a component of an aggregate if the
discriminant values of the component differ from the default values.

gcc/ada/

* sem_ch3.adb (Analyze_Component_Declaration): Remove
Build_Default_Subtype_OK call and code that could only executed in
the case where the removed call would have returned True. Other
calls to Build_Default_Subtype_Ok are unaffected by this change.

gcc/ada/sem_ch3.adb

index 85019dfffa5ed90db2f15013d6fc7d124d82ce8d..ed337f5408ecb12222593a57f847c4594ae7003d 100644 (file)
@@ -1868,7 +1868,6 @@ package body Sem_Ch3 is
    -----------------------------------
 
    procedure Analyze_Component_Declaration (N : Node_Id) is
-      Loc : constant Source_Ptr := Sloc (Component_Definition (N));
       Id  : constant Entity_Id  := Defining_Identifier (N);
       E   : constant Node_Id    := Expression (N);
       Typ : constant Node_Id    :=
@@ -2205,23 +2204,6 @@ package body Sem_Ch3 is
          end if;
       end if;
 
-      --  When possible, build the default subtype
-
-      if Build_Default_Subtype_OK (T) then
-         declare
-            Act_T : constant Entity_Id := Build_Default_Subtype (T, N);
-
-         begin
-            Set_Etype (Id, Act_T);
-
-            --  Rewrite component definition to use the constrained subtype
-
-            Rewrite (Component_Definition (N),
-              Make_Component_Definition (Loc,
-                Subtype_Indication => New_Occurrence_Of (Act_T, Loc)));
-         end;
-      end if;
-
       Set_Original_Record_Component (Id, Id);
 
       if Has_Aspects (N) then