]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
Ada: Fix iterator filter ignored on enumeration type with clause
authorEric Botcazou <ebotcazou@adacore.com>
Sun, 1 Feb 2026 19:18:41 +0000 (20:18 +0100)
committerEric Botcazou <ebotcazou@adacore.com>
Sun, 1 Feb 2026 19:33:26 +0000 (20:33 +0100)
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 <liam@liampwll.com>
gcc/ada/exp_ch5.adb
gcc/testsuite/gnat.dg/iter7.adb [new file with mode: 0644]

index b88856285fb4aa6820d6bcb9680c111df86d4af6..d08c8c4a59d41a28fc6fda4b7014364ea7733b37 100644 (file)
@@ -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 (file)
index 0000000..a983603
--- /dev/null
@@ -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;