[Ada] Fix copy operation with private discriminated record type
diff mbox series

Message ID 20190919132819.GA41891@adacore.com
State New
Headers show
Series
  • [Ada] Fix copy operation with private discriminated record type
Related show

Commit Message

Pierre-Marie de Rodat Sept. 19, 2019, 1:28 p.m. UTC
This prevents the object code from reading too many bytes from the
source for a copy operation involving a private discriminated record
type with default discriminants and generated for the assignment of an
aggregate to a variable or the initialization of a constant.

The front-end already knows that it needs to convert the operation
involving the aggregate into individual assignments if the type of the
aggregate has mutable components, but it would not do so if this type is
private, which does not change anything for code generation.

Running these commands:

  gnatmake -q p -g -fsanitize=address
  p

On the following sources:

with Q; use Q;

procedure P is

   type Rec is record
      A : T;
   end record;

   C : constant Rec := Rec'(A => Default_T);

begin
   null;
end;

package Q is

   type T is private;

   Default_T : constant T;

private

   A : constant := 170;
   B : constant := 8192;

   type A_Index is range 1 .. A;
   type B_Index is range 1 .. B;

   type A_Array is array (A_Index) of Boolean;
   type B_Array is array (B_Index) of Boolean;

   type Data_Type is (A_Type, B_Type);

   type T (Discriminant : Data_Type := A_Type) is record
      case Discriminant is
         when A_Type =>
            Field_A : A_Array;
         when B_Type =>
            Field_B : B_Array;
      end case;
   end record;

   Default_T : constant T :=
     T'(Discriminant => A_Type, Field_A => (others => True));

end Q;

Should execute silently.

Tested on x86_64-pc-linux-gnu, committed on trunk

2019-09-19  Eric Botcazou  <ebotcazou@adacore.com>

gcc/ada/

	* exp_aggr.adb (Has_Mutable_Components): Look at the underlying
	type of components to find out whether they are mutable.

Patch
diff mbox series

--- gcc/ada/exp_aggr.adb
+++ gcc/ada/exp_aggr.adb
@@ -8162,13 +8162,15 @@  package body Exp_Aggr is
 
    function Has_Mutable_Components (Typ : Entity_Id) return Boolean is
       Comp : Entity_Id;
+      Ctyp : Entity_Id;
 
    begin
       Comp := First_Component (Typ);
       while Present (Comp) loop
-         if Is_Record_Type (Etype (Comp))
-           and then Has_Discriminants (Etype (Comp))
-           and then not Is_Constrained (Etype (Comp))
+         Ctyp := Underlying_Type (Etype (Comp));
+         if Is_Record_Type (Ctyp)
+           and then Has_Discriminants (Ctyp)
+           and then not Is_Constrained (Ctyp)
          then
             return True;
          end if;