]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
[Ada] Fix handling of slices with subtype names
authorPiotr Trojanek <trojanek@adacore.com>
Fri, 27 Aug 2021 15:48:15 +0000 (17:48 +0200)
committerPierre-Marie de Rodat <derodat@adacore.com>
Mon, 4 Oct 2021 08:45:07 +0000 (08:45 +0000)
gcc/ada/

* sem_res.adb (Resolve_Slice): Fix application of range checks
to slice range given as a subtype name.

gcc/ada/sem_res.adb

index ba733a504ca5d5c8fd7f2bb56cdfdbdaf436e781..ec2d5ec353feabad534586afc5e8e2fbee6b2d1a 100644 (file)
@@ -11347,18 +11347,19 @@ package body Sem_Res is
       then
          null;
 
-      --  The discrete_range is specified by a subtype indication. Create a
-      --  shallow copy and inherit the type, parent and source location from
-      --  the discrete_range. This ensures that the range check is inserted
-      --  relative to the slice and that the runtime exception points to the
-      --  proper construct.
+      --  The discrete_range is specified by a subtype name. Create an
+      --  equivalent range attribute, apply checks to this attribute, but
+      --  insert them into the range expression of the slice itself.
 
       elsif Is_Entity_Name (Drange) then
-         Dexpr := New_Copy (Scalar_Range (Entity (Drange)));
+         Dexpr :=
+           Make_Attribute_Reference
+             (Sloc (Drange),
+              Prefix         =>
+                New_Occurrence_Of (Entity (Drange), Sloc (Drange)),
+              Attribute_Name => Name_Range);
 
-         Set_Etype  (Dexpr, Etype  (Drange));
-         Set_Parent (Dexpr, Parent (Drange));
-         Set_Sloc   (Dexpr, Sloc   (Drange));
+         Analyze_And_Resolve (Dexpr, Etype  (Drange));
 
       elsif Nkind (Drange) = N_Subtype_Indication then
          Dexpr := Range_Expression (Constraint (Drange));
@@ -11379,7 +11380,7 @@ package body Sem_Res is
       end if;
 
       if Present (Dexpr) then
-         Apply_Range_Check (Dexpr, Index_Type);
+         Apply_Range_Check (Dexpr, Index_Type, Insert_Node => Drange);
       end if;
 
       Set_Slice_Subtype (N);