This patch inproves the error report produced on a selected component Nam.Comp
which appears in an object declaration, when Nam has seeveral interpretations 
as a function, and there is non-visible package with the same name.

Compiling pqi.adb must yield:

   pqi.adb:15:07: no legal interpretations as function call,
   pqi.adb:15:07: package "Map" is not visible

---
with Map;
with Gtk_Widget; use Gtk_Widget;
procedure PQI is
  type Main_Window_Record_T is new Gtk_Widget.Gobject_Record with
    record
      Ii : Integer;
    end record;

  procedure Map (Truc : Boolean) is
  begin
    null;
  end;

  X : Map.T;

begin
  null;
end;
---
package body Gtk_Widget is
  procedure Map (Jjj : Integer) is
  begin
    null;
  end;
end Gtk_Widget;
---
package Gtk_Widget is
  procedure Map (Jjj : Integer);

  type GObject_Record is tagged private;

  type GObject is access all GObject_Record'Class;

  procedure Map (Widget : not null access Gobject_Record);

private
  type GObject_Record is tagged record
    I : Integer;
  end record;
end Gtk_Widget;
---
package Map is
  type T is new Integer;
end Map;

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

2017-01-19  Ed Schonberg  <schonb...@adacore.com>

        * sem_ch4.adb (Diagnose_Call): Improve error message when a
        selected component has a prefix that might be interpreted
        as a parameterless function call, but none of the candidate
        interpretations is parameterless, and there is a hidden homonym
        of the prefix that is a package.
        * sem_ch8.adb (Find_Selected_Component): If the prefix might be
        interpreted as a parameterless function call and its analysis
        fails, do not call Analyze_Selected_Component.

Index: sem_ch4.adb
===================================================================
--- sem_ch4.adb (revision 244612)
+++ sem_ch4.adb (working copy)
@@ -5881,6 +5881,38 @@
          end loop;
       end if;
 
+      --  Before listing the possible candidates, check whether this
+      --  a prefix of a selected component that has been rewritten as
+      --  a parameterless function call because there is a callable
+      --  candidate interpretation. If there is a hidden package in
+      --  the list of homonyms of the function name (bad programming
+      --  style in any case) suggest that this is the intended entity.
+
+      if No (Parameter_Associations (N))
+        and then Nkind (Parent (N)) = N_Selected_Component
+        and then Nkind (Parent (Parent (N))) in N_Declaration
+        and then Is_Overloaded (Nam)
+      then
+         declare
+            Ent : Entity_Id;
+
+         begin
+            Ent := Current_Entity (Nam);
+            while Present (Ent) loop
+               if Ekind (Ent) = E_Package then
+                  Error_Msg_N
+                    ("no legal interpretations as function call,!", Nam);
+                  Error_Msg_NE ("\package& is not visible", N, Ent);
+                  Rewrite (Parent (N),
+                    New_Occurrence_Of (Any_Type, Sloc (N)));
+                  return;
+               end if;
+
+               Ent := Homonym (Ent);
+            end loop;
+         end;
+      end if;
+
       --   Analyze each candidate call again, with full error reporting
       --   for each.
 
Index: sem_ch8.adb
===================================================================
--- sem_ch8.adb (revision 244612)
+++ sem_ch8.adb (working copy)
@@ -7048,7 +7048,18 @@
                   --  Now analyze the reformatted node
 
                   Analyze_Call (P);
-                  Analyze_Selected_Component (N);
+
+                  --  If the prefix is illegal after this transformation,
+                  --  there may be visibility errors on the prefix. The
+                  --  safest is to treat the selected component as an error.
+
+                  if Error_Posted (P) then
+                     Set_Etype (N, Any_Type);
+                     return;
+
+                  else
+                     Analyze_Selected_Component (N);
+                  end if;
                end if;
             end if;
 

Reply via email to