From: Steve Baird <ba...@adacore.com>

In some cases where an object is declared with an initial value that is
an aggregate and also with a specified Address (either via an
aspect_specification or via an attribute_definition_clause), the
check that the initial value satisfies the constraints of the object's
subtype was incorrectly omitted.

gcc/ada/

        * exp_util.adb (Remove_Side_Effects): Make_Reference assumes that
        the referenced object satisfies the constraints of the designated
        subtype of the access type. Ensure that this assumption holds by
        introducing a qualified expression if needed (and then ensuring
        that checking associated with evaluation of the qualified
        expression is not suppressed).

Tested on x86_64-pc-linux-gnu, committed on master.

---
 gcc/ada/exp_util.adb | 29 +++++++++++++++++++++++++++++
 1 file changed, 29 insertions(+)

diff --git a/gcc/ada/exp_util.adb b/gcc/ada/exp_util.adb
index b71f7739481..654ea7d9124 100644
--- a/gcc/ada/exp_util.adb
+++ b/gcc/ada/exp_util.adb
@@ -12772,6 +12772,35 @@ package body Exp_Util is
             --  since we know it cannot be null and we don't want a check.
 
             else
+               --  Make_Reference assumes that the referenced
+               --  object satisfies the constraints of the designated
+               --  subtype of the access type. Ensure that this assumption
+               --  holds by introducing a qualified expression if needed.
+
+               if not Analyzed (Exp)
+                 and then Nkind (Exp) = N_Aggregate
+                 and then (Is_Array_Type (Exp_Type)
+                           or else Has_Discriminants (Exp_Type))
+                 and then Is_Constrained (Exp_Type)
+               then
+                  --  Do not suppress checks associated with the qualified
+                  --  expression we are about to introduce (unless those
+                  --  checks were already suppressed when Remove_Side_Effects
+                  --  was called).
+
+                  if Is_Array_Type (Exp_Type) then
+                     Scope_Suppress.Suppress (Length_Check)
+                       := Svg_Suppress.Suppress (Length_Check);
+                  else
+                     Scope_Suppress.Suppress (Discriminant_Check)
+                       := Svg_Suppress.Suppress (Discriminant_Check);
+                  end if;
+
+                  E := Make_Qualified_Expression (Loc,
+                         Subtype_Mark => New_Occurrence_Of (Exp_Type, Loc),
+                         Expression => E);
+               end if;
+
                New_Exp := Make_Reference (Loc, E);
                Set_Is_Known_Non_Null (Def_Id);
             end if;
-- 
2.43.2

Reply via email to