public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
* [Ada] Obscure ambiguity involving user-defined operators returning Boolean
@ 2015-01-30 17:00 Arnaud Charlet
  0 siblings, 0 replies; only message in thread
From: Arnaud Charlet @ 2015-01-30 17:00 UTC (permalink / raw)
  To: gcc-patches; +Cc: Bob Duff

[-- Attachment #1: Type: text/plain, Size: 1499 bytes --]

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.


[-- Attachment #2: difs --]
[-- Type: text/plain, Size: 1287 bytes --]

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)

^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2015-01-30 15:26 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2015-01-30 17:00 [Ada] Obscure ambiguity involving user-defined operators returning Boolean Arnaud Charlet

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).