From c57fbb120dabb4fee85cc12ae99abe5060213d93 Mon Sep 17 00:00:00 2001
From: Viljar Indus <indus@adacore.com>
Date: Thu, 6 Jul 2023 16:02:19 +0300
Subject: [PATCH] ada: Avoid iterator conflicts in container aggregates

Create temporary scope for the iterators defined in a
container aggregate so that it would not be put to the
same scope where the expression was used. This would
otherwise lead to multiple aggregates with iterators that have
the same name leading to a name conflict.

gcc/ada/

	* sem_aggr.adb (Resolve_Iterated_Association): Add temporary scope
	when analyzing the Iterator Specification. Use preanalysis instead
	of Analysis to avoid polluting the tree.
---
 gcc/ada/sem_aggr.adb | 15 ++++++++++++++-
 1 file changed, 14 insertions(+), 1 deletion(-)

diff --git a/gcc/ada/sem_aggr.adb b/gcc/ada/sem_aggr.adb
index ecd508a66f82..364217d03db4 100644
--- a/gcc/ada/sem_aggr.adb
+++ b/gcc/ada/sem_aggr.adb
@@ -3231,11 +3231,24 @@ package body Sem_Aggr is
             Typ := Key_Type;
 
          elsif Present (Iterator_Specification (Comp)) then
+            --  Create a temporary scope to avoid some modifications from
+            --  escaping the Analyze call below. The original Tree will be
+            --  reanalyzed later.
+
+            Ent := New_Internal_Entity
+                     (E_Loop, Current_Scope, Sloc (Comp), 'L');
+            Set_Etype  (Ent, Standard_Void_Type);
+            Set_Parent (Ent, Parent (Comp));
+            Push_Scope (Ent);
+
             Copy    := Copy_Separate_Tree (Iterator_Specification (Comp));
             Id_Name :=
               Chars (Defining_Identifier (Iterator_Specification (Comp)));
 
-            Analyze (Copy);
+            Preanalyze (Copy);
+
+            End_Scope;
+
             Typ := Etype (Defining_Identifier (Copy));
 
          else
-- 
GitLab