@@ -1436,6 +1436,25 @@ package body Exp_Attr is
Insert_Action (Loop_Stmt, Func_Decl);
Pop_Scope;
+ -- The analysis of the condition may have generated itypes
+ -- that are now used within the function: Adjust their
+ -- scopes accordingly so that their use appears in their
+ -- scope of definition.
+
+ declare
+ Ityp : Entity_Id;
+
+ begin
+ Ityp := First_Entity (Loop_Id);
+
+ while Present (Ityp) loop
+ if Is_Itype (Ityp) then
+ Set_Scope (Ityp, Func_Id);
+ end if;
+ Next_Entity (Ityp);
+ end loop;
+ end;
+
-- Transform the original while loop into an infinite loop
-- where the last statement checks the negated condition. This
-- placement ensures that the condition will not be evaluated
new file mode 100644
@@ -0,0 +1,11 @@
+-- { dg-do compile }
+-- { dg-options "-gnata" }
+
+procedure Loop_Entry2 (S : String) is
+ J : Integer := S'First;
+begin
+ while S(J..J+1) = S(J..J+1) loop
+ pragma Loop_Invariant (for all K in J'Loop_Entry .. J => K <= J);
+ J := J + 1;
+ end loop;
+end;