[Ada] Further illegalities with operator call in functional notation

Submitted by Arnaud Charlet on June 22, 2010, 8:44 a.m.


Message ID 20100622084415.GA29927@adacore.com
State New
Headers show

Commit Message

Arnaud Charlet June 22, 2010, 8:44 a.m.
With this patch the compiler properly rejects a call of the form P."+" (X, Y)
when X and Y are numeric literals, and the result type is not declared in P.

Compiling the following must be rejected with:

   derived_folded_mistyped.adb:8:27: expect type "T3"
procedure Derived_Folded_Mistyped is
   package Scop is
      type T2 is range 1 .. 4;
   end Scop;

   type T3 is new Scop.T2;
   X : constant T3 := Scop."+" (1, 2); --  ERROR: Scop."+" returns T2
end Derived_Folded_Mistyped;

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

2010-06-22  Ed Schonberg  <schonberg@adacore.com>

	* sem_res.adb (Make_Call_Into_Operator): Diagnose an incorrect scope
	for an operator in a functional notation, when operands are universal.

Patch hide | download patch | download mbox

Index: sem_res.adb
--- sem_res.adb	(revision 161144)
+++ sem_res.adb	(working copy)
@@ -1422,6 +1422,31 @@  package body Sem_Res is
               ("& not declared in&", N, Selector_Name (Name (N)));
             Set_Etype (N, Any_Type);
+         --  Detect a mismatch between the context type and the result type
+         --  in the named package, which is otherwise not detected if the
+         --  operands are universal. Check is only needed if source entity is
+         --  an operator, not a function that renames an operator.
+         elsif Nkind (Parent (N)) /= N_Type_Conversion
+           and then Ekind (Entity (Name (N))) = E_Operator
+           and then Is_Numeric_Type (Typ)
+           and then not Is_Universal_Numeric_Type (Typ)
+           and then Scope (Base_Type (Typ)) /= Pack
+           and then not In_Instance
+         then
+            if Is_Fixed_Point_Type (Typ)
+              and then (Op_Name = Name_Op_Multiply
+                          or else
+                        Op_Name = Name_Op_Divide)
+            then
+               --  Already checked above
+               null;
+            else
+               Error_Msg_NE ("expect type&", N, Typ);
+            end if;
          end if;
       end if;