https://gcc.gnu.org/g:34217930444e0a7098cf7374bad0d5c41819c130

commit r16-7234-g34217930444e0a7098cf7374bad0d5c41819c130
Author: Eric Botcazou <[email protected]>
Date:   Sun Feb 1 20:18:41 2026 +0100

    Ada: Fix iterator filter ignored on enumeration type with clause
    
    The problem comes from an oversight in Expand_N_Loop_Statement.
    
    gcc/ada/
            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.
    
    gcc/testsuite/
            * gnat.dg/iter7.adb: New test.
    
    Co-authored-by: Liam Powell <[email protected]>

Diff:
---
 gcc/ada/exp_ch5.adb             | 13 ++++++++-----
 gcc/testsuite/gnat.dg/iter7.adb | 15 +++++++++++++++
 2 files changed, 23 insertions(+), 5 deletions(-)

diff --git a/gcc/ada/exp_ch5.adb b/gcc/ada/exp_ch5.adb
index b88856285fb4..d08c8c4a59d4 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)));
 
diff --git a/gcc/testsuite/gnat.dg/iter7.adb b/gcc/testsuite/gnat.dg/iter7.adb
new file mode 100644
index 000000000000..a983603fb8a8
--- /dev/null
+++ b/gcc/testsuite/gnat.dg/iter7.adb
@@ -0,0 +1,15 @@
+--  { 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