public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
* [Ada] Fix if expression returning slice
@ 2022-07-13 10:02 Pierre-Marie de Rodat
  0 siblings, 0 replies; only message in thread
From: Pierre-Marie de Rodat @ 2022-07-13 10:02 UTC (permalink / raw)
  To: gcc-patches; +Cc: Marc Poulhiès

[-- Attachment #1: Type: text/plain, Size: 335 bytes --]

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.

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

gcc/ada/

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

[-- Attachment #2: patch.diff --]
[-- Type: text/x-diff, Size: 1788 bytes --]

diff --git a/gcc/ada/exp_ch4.adb b/gcc/ada/exp_ch4.adb
--- a/gcc/ada/exp_ch4.adb
+++ b/gcc/ada/exp_ch4.adb
@@ -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       =>



^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2022-07-13 10:02 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-07-13 10:02 [Ada] Fix if expression returning slice Pierre-Marie de Rodat

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).