diff mbox series

[Ada] Spurious error with unqualified aggregate in instantiation.

Message ID 20170918095612.GA78136@adacore.com
State New
Headers show
Series [Ada] Spurious error with unqualified aggregate in instantiation. | expand

Commit Message

Pierre-Marie de Rodat Sept. 18, 2017, 9:56 a.m. UTC
When an aggregate appears without an explicit qualification in a generic
unit, the compiler builds a qualified expression for it, using the type of
the aggregate and when possible the scope of that type, so that both of
these entities are properly resolved in an instantiation. This patch verifies
that the scope is not hidden by a local declaration, to preent a spurious
visibility error in an instance.

The following must compile quietly:

   gcc -c foo.adb

---
with Langkit_Support.Lexical_Env;

procedure Foo is
   package Envs is new Langkit_Support.Lexical_Env (Natural);
begin
   null;
end Foo;
---
generic
   type T is private;
package Langkit_Support.Lexical_Env is

   type Record_Type is record
      V : T;
   end record;

   type Lexical_Env is null record;

   function Get
     (Self   : Lexical_Env;
      Value   : T;
      Filter : access function (R : Record_Type; Env : Lexical_Env)
                                return Boolean := null)
      return Boolean;

end Langkit_Support.Lexical_Env;
---
package body Langkit_Support.Lexical_Env is

   function Get
     (Self   : Lexical_Env;
      Value  : T;
      Filter : access function (R : Record_Type; Env : Lexical_Env)
                                return Boolean := null)
      return Boolean
   is
      Filtered_Out : constant Boolean :=
         Filter /= null
         and then not Filter ((V => Value), Self);
   begin
      return Filtered_Out;
   end Get;

end Langkit_Support.Lexical_Env;

Tested on x86_64-pc-linux-gnu, committed on trunk

2017-09-18  Ed Schonberg  <schonberg@adacore.com>

	* sem_ch12.adb (Save_References_In_Aggregate): When constructing a
	qualified exxpression for an aggregate in a generic unit, verify that
	the scope of the type is itself visible and not hidden, so that the
	qualified expression is correctly resolved in any instance.
diff mbox series

Patch

Index: sem_ch12.adb
===================================================================
--- sem_ch12.adb	(revision 252907)
+++ sem_ch12.adb	(working copy)
@@ -15118,10 +15118,10 @@ 
                --  preserved. In order to preserve some of this information,
                --  wrap the aggregate in a qualified expression, using the id
                --  of its type. For further disambiguation we qualify the type
-               --  name with its scope (if visible) because both id's will have
-               --  corresponding entities in an instance. This resolves most of
-               --  the problems with missing type information on aggregates in
-               --  instances.
+               --  name with its scope (if visible and not hidden by a local
+               --  homograph) because both id's will have corresponding
+               --  entities in an instance. This resolves most of the problems
+               --  with missing type information on aggregates in instances.
 
                if Present (N2)
                  and then Nkind (N2) = Nkind (N)
@@ -15131,7 +15131,9 @@ 
                then
                   Nam := Make_Identifier (Loc, Chars (Typ));
 
-                  if Is_Immediately_Visible (Scope (Typ)) then
+                  if Is_Immediately_Visible (Scope (Typ))
+                    and then Current_Entity (Scope (Typ)) = Scope (Typ)
+                  then
                      Nam :=
                        Make_Selected_Component (Loc,
                          Prefix        =>