The procedure Expand_Array_Equality has an optimization whereby the
equality operation for an array of 2 elements is expanded into a
simple conjunction of two component comparisons instead of into
the generic loop.

But this special circuitry reuses the same expression list for the
indexed component built for both elements, which can cause problems
later when the index type is an enumeration type with a non-standard
representation, because of successive rewriting.

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

2020-06-18  Eric Botcazou  <ebotca...@adacore.com>

gcc/ada/

        * exp_ch4.adb (Expand_Array_Equality): For the optimization of
        the 2-element case, build new expression lists for the indices.
--- gcc/ada/exp_ch4.adb
+++ gcc/ada/exp_ch4.adb
@@ -2009,34 +2009,33 @@ package body Exp_Ch4 is
             Ctyp         : constant Entity_Id := Component_Type (Ltyp);
             L, R         : Node_Id;
             TestL, TestH : Node_Id;
-            Index_List   : List_Id;
 
          begin
-            Index_List := New_List (New_Copy_Tree (Low_Bound (First_Idx)));
-
             L :=
               Make_Indexed_Component (Loc,
                 Prefix      => New_Copy_Tree (New_Lhs),
-                Expressions => Index_List);
+                Expressions =>
+                  New_List (New_Copy_Tree (Low_Bound (First_Idx))));
 
             R :=
               Make_Indexed_Component (Loc,
                 Prefix      => New_Copy_Tree (New_Rhs),
-                Expressions => Index_List);
+                Expressions =>
+                  New_List (New_Copy_Tree (Low_Bound (First_Idx))));
 
             TestL := Expand_Composite_Equality (Nod, Ctyp, L, R, Bodies);
 
-            Index_List := New_List (New_Copy_Tree (High_Bound (First_Idx)));
-
             L :=
               Make_Indexed_Component (Loc,
                 Prefix      => New_Lhs,
-                Expressions => Index_List);
+                Expressions =>
+                  New_List (New_Copy_Tree (High_Bound (First_Idx))));
 
             R :=
               Make_Indexed_Component (Loc,
                 Prefix      => New_Rhs,
-                Expressions => Index_List);
+                Expressions =>
+                  New_List (New_Copy_Tree (High_Bound (First_Idx))));
 
             TestH := Expand_Composite_Equality (Nod, Ctyp, L, R, Bodies);
 

Reply via email to