diff mbox

[Ada] Obscure ambiguity involving user-defined operators returning Boolean

Message ID 20150130152550.GA29818@adacore.com
State New
Headers show

Commit Message

Arnaud Charlet Jan. 30, 2015, 3:25 p.m. UTC
This patch fixes a rather obscure bug in case there is a user-defined "+"
operator returning Boolean, passed to two contexts where the expected type is
Boolean, and the expected type is the result of the PREdefined operator.
This is ambiguous, and therefore illegal.

gnatmake -q -f cutdown1-main.adb

cutdown1-main.adb:4:04: ambiguous expression (cannot resolve "P")
cutdown1-main.adb:4:04: possible interpretation at cutdown1.ads:8
cutdown1-main.adb:4:04: possible interpretation at cutdown1.ads:7
gnatmake: "cutdown1-main.adb" compilation error

package Cutdown1 is

    type T1 is range 1 .. 10;
    -- We have a predefined "+"(T1, T1) --> T1 here that seems to be ignored.

    function "+" (X, Y : T1) return Boolean;
    procedure P (X : Boolean);
    procedure P (X : T1);

end Cutdown1;
with Ada.Text_IO; use Ada.Text_IO;
package body Cutdown1 is

    function "+" (X, Y : T1) return Boolean is
    begin
       Put_Line ("Hello from ""+""");
       return False;
    end "+";

    procedure P (X : T1) is
    begin
       Put_Line ("Hello from P(T1)");
    end P;

    procedure P (X : Boolean) is
    begin
       Put_Line ("Hello from P(Boolean)");
    end P;

end Cutdown1;
with Cutdown1; use Cutdown1;
procedure Cutdown1.Main is
begin
   P (T1'(1) + 1); -- ERROR: ambiguous
end Cutdown1.Main;

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

2015-01-30  Bob Duff  <duff@adacore.com>

	* sem_type.adb: sem_type.adb (Remove_Conversions): Need to
	check both operands of an operator.
diff mbox

Patch

Index: sem_type.adb
===================================================================
--- sem_type.adb	(revision 220273)
+++ sem_type.adb	(working copy)
@@ -6,7 +6,7 @@ 
 --                                                                          --
 --                                 B o d y                                  --
 --                                                                          --
---          Copyright (C) 1992-2014, Free Software Foundation, Inc.         --
+--          Copyright (C) 1992-2015, Free Software Foundation, Inc.         --
 --                                                                          --
 -- GNAT is free software;  you can  redistribute it  and/or modify it under --
 -- terms of the  GNU General Public License as published  by the Free Soft- --
@@ -1539,6 +1539,8 @@ 
 
                if Nkind (Act1) in N_Op
                  and then Is_Overloaded (Act1)
+                 and then Nkind_In (Left_Opnd (Act1), N_Integer_Literal,
+                                                      N_Real_Literal)
                  and then Nkind_In (Right_Opnd (Act1), N_Integer_Literal,
                                                        N_Real_Literal)
                  and then Has_Compatible_Type (Act1, Standard_Boolean)