Skip to content

Commit 875f1e3

Browse files
author
Eric Botcazou
committed
Ada: Fix bogus error on aggregate in call with qualified type in instance
This happens with a container aggregate in the testcase, although this can very likely happen with a record aggregate as well. The trick used in the Save_Global_References procedure for aggregates loses the qualification of the type of the formal for which the aggregate is the actual. gcc/ada/ PR ada/123302 * sem_ch12.adb (Save_Global_Reference.Save_References_In_Aggregate): Recurse on the scope of the type to find one that is visible, in the case of an actual in a subprogram call with a local type. gcc/testsuite/ * gnat.dg/aggr34.adb: New test. * gnat.dg/aggr34_pkg1.ads, gnat.dg/aggr34_pkg1.adb: New helper. * gnat.dg/aggr34_pkg2.ads, gnat.dg/aggr34_pkg2.adb: Likewise. * gnat.dg/aggr34_pkg3.ads: Likewise.
1 parent 7b0a85a commit 875f1e3

File tree

7 files changed

+90
-21
lines changed

7 files changed

+90
-21
lines changed

gcc/ada/sem_ch12.adb

Lines changed: 36 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -18064,7 +18064,6 @@ package body Sem_Ch12 is
1806418064
----------------------------------
1806518065

1806618066
procedure Save_References_In_Aggregate (N : Node_Id) is
18067-
Nam : Node_Id;
1806818067
Qual : Node_Id := Empty;
1806918068
Typ : Entity_Id := Empty;
1807018069

@@ -18120,16 +18119,16 @@ package body Sem_Ch12 is
1812018119
end;
1812118120
end if;
1812218121

18123-
-- If the aggregate is an actual in a call, it has been
18124-
-- resolved in the current context, to some local type. The
18122+
-- If the aggregate is an actual in a subprogram call, it has
18123+
-- been resolved in the current context to some local type. The
1812518124
-- enclosing call may have been disambiguated by the aggregate,
1812618125
-- and this disambiguation might fail at instantiation time
1812718126
-- because the type to which the aggregate did resolve is not
1812818127
-- preserved. In order to preserve some of this information,
1812918128
-- wrap the aggregate in a qualified expression, using the id
1813018129
-- of its type. For further disambiguation we qualify the type
18131-
-- name with its scope (if visible and not hidden by a local
18132-
-- homograph) because both id's will have corresponding
18130+
-- name with its scope recursively (if visible and not hidden
18131+
-- by a local homograph) because both will have corresponding
1813318132
-- entities in an instance. This resolves most of the problems
1813418133
-- with missing type information on aggregates in instances.
1813518134

@@ -18139,24 +18138,40 @@ package body Sem_Ch12 is
1813918138
and then Present (Typ)
1814018139
and then Comes_From_Source (Typ)
1814118140
then
18142-
Nam := Make_Identifier (Loc, Chars (Typ));
18141+
declare
18142+
function Qualify_Name (S, E : Entity_Id) return Node_Id is
18143+
(if E = S
18144+
then Make_Identifier (Loc, Chars (E))
18145+
else Make_Selected_Component (Loc,
18146+
Prefix => Qualify_Name (S, Scope (E)),
18147+
Selector_Name =>
18148+
Make_Identifier (Loc, Chars (E))));
18149+
-- Return the qualified name of E up to scope S
18150+
18151+
Nam : Node_Id;
18152+
S : Entity_Id;
1814318153

18144-
if Is_Immediately_Visible (Scope (Typ))
18145-
and then
18146-
(not In_Open_Scopes (Scope (Typ))
18147-
or else Current_Entity (Scope (Typ)) = Scope (Typ))
18148-
then
18149-
Nam :=
18150-
Make_Selected_Component (Loc,
18151-
Prefix =>
18152-
Make_Identifier (Loc, Chars (Scope (Typ))),
18153-
Selector_Name => Nam);
18154-
end if;
18154+
begin
18155+
S := Scope (Typ);
18156+
while not Is_Immediately_Visible (S) loop
18157+
S := Scope (S);
18158+
exit when Is_Generic_Unit (S);
18159+
end loop;
1815518160

18156-
Qual :=
18157-
Make_Qualified_Expression (Loc,
18158-
Subtype_Mark => Nam,
18159-
Expression => Relocate_Node (N));
18161+
if not Is_Generic_Unit (S)
18162+
and then (not In_Open_Scopes (S)
18163+
or else Current_Entity (S) = S)
18164+
then
18165+
Nam := Qualify_Name (S, Typ);
18166+
else
18167+
Nam := Make_Identifier (Loc, Chars (Typ));
18168+
end if;
18169+
18170+
Qual :=
18171+
Make_Qualified_Expression (Loc,
18172+
Subtype_Mark => Nam,
18173+
Expression => Relocate_Node (N));
18174+
end;
1816018175
end if;
1816118176

1816218177
-- For a full aggregate, if the type is global and a derived

gcc/testsuite/gnat.dg/aggr34.adb

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
-- PR ada/123302
2+
-- { dg-do link }
3+
-- { dg-options "-gnat2022" }
4+
5+
with Aggr34_Pkg3;
6+
with Aggr34_Pkg1;
7+
8+
procedure Aggr34 is
9+
10+
package My_Pkg3 is new Aggr34_Pkg3;
11+
package My_Pkg1 is new Aggr34_Pkg1 (My_Pkg3);
12+
13+
begin
14+
My_Pkg1.Proc;
15+
end;
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
-- { dg-do compile }
2+
-- { dg-options "-gnat2022" }
3+
4+
package body Aggr34_Pkg1 is
5+
procedure Proc is null;
6+
end Aggr34_Pkg1;
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
with Aggr34_Pkg3;
2+
with Aggr34_Pkg2;
3+
4+
generic
5+
with package My_Config is new Aggr34_Pkg3;
6+
package Aggr34_Pkg1 is
7+
package My_Module_Basic_Config is new Aggr34_Pkg2 (My_Config);
8+
procedure Proc;
9+
end Aggr34_Pkg1;
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
-- { dg-do compile }
2+
-- { dg-options "-gnat2022" }
3+
4+
package body Aggr34_Pkg2 is
5+
procedure Disable_Prunt is
6+
begin
7+
My_Config.Set (["a", "b"]);
8+
end Disable_Prunt;
9+
end Aggr34_Pkg2;
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
with Aggr34_Pkg3;
2+
3+
generic
4+
with package My_Config is new Aggr34_Pkg3;
5+
package Aggr34_Pkg2 is
6+
procedure Disable_Prunt;
7+
end Aggr34_Pkg2;
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
with Ada.Containers.Indefinite_Vectors;
2+
3+
generic
4+
package Aggr34_Pkg3 is
5+
package Config_Data_Paths is new
6+
Ada.Containers.Indefinite_Vectors (Positive, String);
7+
procedure Set (Path : Config_Data_Paths.Vector) is null;
8+
end Aggr34_Pkg3;

0 commit comments

Comments
 (0)