When we make a copy of a tree containing a block, we need to make new
entities for variables declared in the block. If not, the entity
points to the wrong declaration, which is an invalid tree and can
cause issues when we need static links and that variable is an uplevel
reference. There may also be latent issues if the variable is
actually in the tree twice.
Tested on x86_64-pc-linux-gnu, committed on trunk
gcc/ada/
* sem_util.adb (Visit_Node): Add handling for N_Block_Statement
with declarations.
diff --git a/gcc/ada/sem_util.adb b/gcc/ada/sem_util.adb
--- a/gcc/ada/sem_util.adb
+++ b/gcc/ada/sem_util.adb
@@ -24344,6 +24344,26 @@ package body Sem_Util is
EWA_Inner_Scope_Level := EWA_Inner_Scope_Level + 1;
end if;
+ -- If the node is a block, we need to process all declarations
+ -- in the block and make new entities for each.
+
+ if Nkind (N) = N_Block_Statement and then Present (Declarations (N))
+ then
+ declare
+ Decl : Node_Id := First (Declarations (N));
+
+ begin
+ while Present (Decl) loop
+ if Nkind (Decl) = N_Object_Declaration then
+ Add_New_Entity (Defining_Identifier (Decl),
+ New_Copy (Defining_Identifier (Decl)));
+ end if;
+
+ Next (Decl);
+ end loop;
+ end;
+ end if;
+
declare
procedure Action (U : Union_Id);
procedure Action (U : Union_Id) is