A loop parameter specification over a container is rewrtten as an
iterator specification, so that expansion can use the Iterator
opwrations declared for the container type. If an Ada2020 interator
filter appears on the loop_parameter_specification it must be transfered
to the iterator, and preanalyzed for semantic correctness. Expansion of
the filter must be deferred to expansion of the enclosing loop, to
prevent premature references to the loop parameter outside of its scope.

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

gcc/ada/

        * sem_ch5.adb (Analyze_Iterator_Specification): If iterator
        filter is present, preanalyze filter without expansion.
        (Analyze_Loop_Parameter_Specification): When
        loop_Parameter_Specification is rewritten as
        Iterator_Specification, transfer Iterator_Filter if present.
diff --git a/gcc/ada/sem_ch5.adb b/gcc/ada/sem_ch5.adb
--- a/gcc/ada/sem_ch5.adb
+++ b/gcc/ada/sem_ch5.adb
@@ -2620,7 +2620,10 @@ package body Sem_Ch5 is
       end if;
 
       if Present (Iterator_Filter (N)) then
-         Analyze_And_Resolve (Iterator_Filter (N), Standard_Boolean);
+         --  Preanalyze the filter. Expansion will take place when enclosing
+         --  loop is expanded.
+
+         Preanalyze_And_Resolve (Iterator_Filter (N), Standard_Boolean);
       end if;
    end Analyze_Iterator_Specification;
 
@@ -3017,6 +3020,9 @@ package body Sem_Ch5 is
             begin
                Set_Iterator_Specification (Scheme, I_Spec);
                Set_Loop_Parameter_Specification (Scheme, Empty);
+               Set_Iterator_Filter (I_Spec,
+                 Relocate_Node (Iterator_Filter (N)));
+
                Analyze_Iterator_Specification (I_Spec);
 
                --  In a generic context, analyze the original domain of


Reply via email to