Patchwork [Ada] Warnings about declarations that hide outer homographs

login
register
mail settings
Submitter Arnaud Charlet
Date Oct. 21, 2010, 10:20 a.m.
Message ID <20101021102013.GA28000@adacore.com>
Download mbox | patch
Permalink /patch/68565/
State New
Headers show

Comments

Arnaud Charlet - Oct. 21, 2010, 10:20 a.m.
This patch refines the warning messages about hiding declarations of operators.
If a predefined operator applies to a type declared in the current scope, there
is no hiding of predefined operators (such as "=") and the warning is just
misleading. If the operator is declared on a local subtype the warning is useful
because it exposes the danger that different parts of a program may apply
different operators to objects of the same subtype.

Compiling hide.adb with -gnatwh must yield:

  hide.adb:13:13: warning: declaration of "=" hides predefined operator

procedure hide is
   type Q is range 123 .. 456;
   type T is new Integer range 1 .. 4;
   subtype S is Integer range 9 .. 13;

  type R is record
     a : T;
     b : S;
  end record;

   function "=" (L, R : T) return Boolean is
      begin return False; end;
   function "=" (L, R : S) return Boolean is
      begin return False; end;
   function "=" (L, R : R) return Boolean is
      begin return False; end;
   function "=" (L, R : Q) return Boolean is
      begin return False; end;

 begin
    null;
 end hide;

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

2010-10-21  Ed Schonberg  <schonberg@adacore.com>

	* sem_ch6.adb (Enter_Overloaded_Entity): Refine warning message about
	hiding, to remove noise warnings about hiding predefined operators.

Patch

Index: sem_ch6.adb
===================================================================
--- sem_ch6.adb	(revision 165755)
+++ sem_ch6.adb	(working copy)
@@ -5914,15 +5914,30 @@  package body Sem_Ch6 is
             E := Homonym (E);
             exit when No (E);
 
-            --  Warn unless genuine overloading
+            --  Warn unless genuine overloading. Do not emit warning on
+            --  hiding predefined operators in Standard (these are either an
+            --  (artifact of our implicit declarations, or simple noise) but
+            --  keep warning on a operator defined on a local subtype, because
+            --  of the real danger that different operators may be applied in
+            --  various parts of the program.
 
             if (not Is_Overloadable (E) or else Subtype_Conformant (E, S))
                   and then (Is_Immediately_Visible (E)
                               or else
                             Is_Potentially_Use_Visible (S))
             then
-               Error_Msg_Sloc := Sloc (E);
-               Error_Msg_N ("declaration of & hides one#?", S);
+               if Scope (E) /= Standard_Standard then
+                  Error_Msg_Sloc := Sloc (E);
+                  Error_Msg_N ("declaration of & hides one#?", S);
+
+               elsif Nkind (S) = N_Defining_Operator_Symbol
+                 and then
+                   Scope (
+                     Base_Type (Etype (First_Formal (S)))) /= Scope (S)
+               then
+                  Error_Msg_N
+                    ("declaration of & hides predefined operator?", S);
+               end if;
             end if;
          end loop;
       end if;