The problem comes from an oversight in Expand_N_Loop_Statement.

Tested on x86-64/Linux, applied on the mainline and 15 branch.


2026-02-01  Eric Botcazou  <[email protected]>
            Liam Powell  <[email protected]>

        PR ada/121316
        * exp_ch5.adb (Expand_Iterator_Loop_Over_Array): Minor tweak.
        (Expand_Iterator_Loop_Over_Container): Likewise.
        (Expand_N_Loop_Statement): Use Statements (N) throughout.


2026-02-01  Eric Botcazou  <[email protected]>
            Liam Powell  <[email protected]>

        * gnat.dg/iter7.adb: New test.

-- 
Eric Botcazou
diff --git a/gcc/ada/exp_ch5.adb b/gcc/ada/exp_ch5.adb
index b88856285fb..d08c8c4a59d 100644
--- a/gcc/ada/exp_ch5.adb
+++ b/gcc/ada/exp_ch5.adb
@@ -5061,7 +5061,10 @@ package body Exp_Ch5 is
       Array_Dim  : constant Pos        := Number_Dimensions (Array_Typ);
       Id         : constant Entity_Id  := Defining_Identifier (I_Spec);
       Loc        : constant Source_Ptr := Sloc (Isc);
-      Stats      : List_Id    := Statements (N);
+
+      Stats : List_Id := Statements (N);
+      --  Maybe wrapped in a conditional if a filter is present
+
       Core_Loop  : Node_Id;
       Dim1       : Int;
       Ind_Comp   : Node_Id;
@@ -5351,7 +5354,7 @@ package body Exp_Ch5 is
       Id_Kind  : constant Entity_Kind := Ekind (Id);
       Loc      : constant Source_Ptr  := Sloc (N);
 
-      Stats    : List_Id     := Statements (N);
+      Stats : List_Id := Statements (N);
       --  Maybe wrapped in a conditional if a filter is present
 
       Cursor         : Entity_Id;
@@ -5871,7 +5874,7 @@ package body Exp_Ch5 is
             Loop_Id : constant Entity_Id := Defining_Identifier (LPS);
             Ltype   : constant Entity_Id := Etype (Loop_Id);
             Btype   : constant Entity_Id := Base_Type (Ltype);
-            Stats   : constant List_Id   := Statements (N);
+
             Expr    : Node_Id;
             Decls   : List_Id;
             New_Id  : Entity_Id;
@@ -5892,7 +5895,7 @@ package body Exp_Ch5 is
                Set_Statements (N,
                   New_List (Make_If_Statement (Loc,
                     Condition => Iterator_Filter (LPS),
-                    Then_Statements => Stats)));
+                    Then_Statements => Statements (N))));
                Analyze_List (Statements (N));
             end if;
 
@@ -6012,7 +6015,7 @@ package body Exp_Ch5 is
                        Declarations => Decls,
                        Handled_Statement_Sequence =>
                          Make_Handled_Sequence_Of_Statements (Loc,
-                           Statements => Stats))),
+                           Statements => Statements (N)))),
 
                    End_Label => End_Label (N)));
 
--  { dg-do run }
--  { dg-options "-gnat2022" }

procedure Iter7 is

   type Enum is (A, B, C);
   for Enum use (A => 1, B => 2, C => 3);

   Enum_Filter : array (Enum) of Boolean := (others => False);

begin
   for F in Enum when Enum_Filter (F) loop
      raise Program_Error;
   end loop;
end;

Reply via email to