===================================================================
@@ -5791,8 +5791,19 @@
end if;
if Is_New_Candidate then
+
+ -- If entity is a child unit, either it is a visible child of
+ -- the prefix, or we are in the body of a generic prefix, as
+ -- will happen when a child unit is instantiated in the body
+ -- of a generic parent. This is because the instance body does
+ -- not restore the full compilation context, given that all
+ -- non-local references have been captured.
+
if Is_Child_Unit (Id) or else P_Name = Standard_Standard then
- exit when Is_Visible_Lib_Unit (Id);
+ exit when Is_Visible_Lib_Unit (Id)
+ or else (Is_Child_Unit (Id)
+ and then In_Open_Scopes (Scope (Id))
+ and then In_Instance_Body);
else
exit when not Is_Hidden (Id);
end if;