]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
[Ada] Fix if expression returning slice
authorMarc Poulhiès <poulhies@adacore.com>
Thu, 30 Jun 2022 11:35:10 +0000 (13:35 +0200)
committerPierre-Marie de Rodat <derodat@adacore.com>
Wed, 13 Jul 2022 10:01:15 +0000 (10:01 +0000)
The compiler incorrectly assumed the prefix for a slice returned in one
branch of an if expression has its bounds known at compile time and would
crash when this is not true.

gcc/ada/

* exp_ch4.adb (Expand_N_If_Expression): Test for compile time
known bounds when handling slices.

gcc/ada/exp_ch4.adb

index 911bdf31b7871bbad01e41677eb74745ce0beffe..18fb88f3f9f42eaaba63049e0b25f85f2f21acf3 100644 (file)
@@ -6174,7 +6174,13 @@ package body Exp_Ch4 is
                Slice_Bnd : Node_Id) return Node_Id is
 
             begin
-               if Nkind (Elsex) = N_Slice then
+               --  We need to use the special processing for slices only if
+               --  they do not have compile-time known bounds; if they do, they
+               --  can be treated like any other expressions.
+
+               if Nkind (Elsex) = N_Slice
+                 and then not Compile_Time_Known_Bounds (Etype (Elsex))
+               then
                   if Compile_Time_Known_Value (Slice_Bnd)
                     and then Expr_Value (Slice_Bnd) = Then_Bnd
                   then
@@ -6230,7 +6236,11 @@ package body Exp_Ch4 is
          begin
             Get_First_Index_Bounds (Etype (Thenx), Then_Lo, Then_Hi);
 
-            if Nkind (Elsex) = N_Slice then
+            --  See the rationale in Build_New_Bound
+
+            if Nkind (Elsex) = N_Slice
+              and then not Compile_Time_Known_Bounds (Etype (Elsex))
+            then
                Slice_Lo := Low_Bound (Discrete_Range (Elsex));
                Slice_Hi := High_Bound (Discrete_Range (Elsex));
                Get_First_Index_Bounds
@@ -6289,7 +6299,11 @@ package body Exp_Ch4 is
 
             Set_Suppress_Assignment_Checks (Last (Then_List));
 
-            if Nkind (Elsex) = N_Slice then
+            --  See the rationale in Build_New_Bound
+
+            if Nkind (Elsex) = N_Slice
+              and then not Compile_Time_Known_Bounds (Etype (Elsex))
+            then
                Else_List := New_List (
                  Make_Assignment_Statement (Loc,
                    Name       =>