[COMMITTED,21/30] ada: Fix crash in quantified expression expansion

Message ID 20240801151738.400796-21-poulhies@adacore.com
State Committed
Commit f6629e1458aab45c59c58ed856658f70b23f5a66
Headers
Series [COMMITTED,01/30] ada: Remove obsolete workaround |

Commit Message

Marc Poulhiès Aug. 1, 2024, 3:17 p.m. UTC
  From: Ronan Desplanques <desplanques@adacore.com>

Before this patch, the compiler failed to handle the case where the for loop
created by expansion of a quantified expression required cleanup handlers.

gcc/ada/

	* sem_ch5.adb (Analyze_Loop_Parameter_Specification): Fix test for
	expanded quantified expression context.
	(Is_Expanded_Quantified_Expr): New function.

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

---
 gcc/ada/sem_ch5.adb | 28 ++++++++++++++++++++++++----
 1 file changed, 24 insertions(+), 4 deletions(-)
  

Patch

diff --git a/gcc/ada/sem_ch5.adb b/gcc/ada/sem_ch5.adb
index ac8cd0821ff..6a479726e86 100644
--- a/gcc/ada/sem_ch5.adb
+++ b/gcc/ada/sem_ch5.adb
@@ -2823,6 +2823,9 @@  package body Sem_Ch5 is
       --  forms. In this case it is not sufficient to check the static
       --  predicate function only, look for a dynamic predicate aspect as well.
 
+      function Is_Expanded_Quantified_Expr (N : Node_Id) return Boolean;
+      --  Return Whether N comes from the expansion of a quantified expression.
+
       procedure Process_Bounds (R : Node_Id);
       --  If the iteration is given by a range, create temporaries and
       --  assignment statements block to capture the bounds and perform
@@ -2908,6 +2911,16 @@  package body Sem_Ch5 is
          end if;
       end Check_Predicate_Use;
 
+      ---------------------------------
+      -- Is_Expanded_Quantified_Expr --
+      ---------------------------------
+
+      function Is_Expanded_Quantified_Expr (N : Node_Id) return Boolean is
+      begin
+         return Nkind (N) = N_Expression_With_Actions
+           and then Nkind (Original_Node (N)) = N_Quantified_Expression;
+      end Is_Expanded_Quantified_Expr;
+
       --------------------
       -- Process_Bounds --
       --------------------
@@ -3081,6 +3094,16 @@  package body Sem_Ch5 is
 
       DS_Copy : Node_Id;
 
+      Is_Loop_Of_Expanded_Quantified_Expr : constant Boolean :=
+        Present (Loop_Nod)
+          and then (Is_Expanded_Quantified_Expr (Parent (Loop_Nod))
+      --  We also have to consider the case where the loop was wrapped with
+      --  Wrap_Loop_Statement.
+            or else (Present (Parent (Loop_Nod))
+              and then Present (Parent (Parent (Loop_Nod)))
+              and then Is_Expanded_Quantified_Expr
+                (Parent (Parent (Parent (Loop_Nod))))));
+
    --  Start of processing for Analyze_Loop_Parameter_Specification
 
    begin
@@ -3271,10 +3294,7 @@  package body Sem_Ch5 is
         or else
           (Present (Etype (Id))
             and then Is_Itype (Etype (Id))
-            and then Present (Loop_Nod)
-            and then Nkind (Parent (Loop_Nod)) = N_Expression_With_Actions
-            and then Nkind (Original_Node (Parent (Loop_Nod))) =
-                                                   N_Quantified_Expression)
+            and then Is_Loop_Of_Expanded_Quantified_Expr)
       then
          Set_Etype (Id, Etype (DS));
       end if;