www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 349] New: Function matching with enums is erratic

reply d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=349

           Summary: Function matching with enums is erratic
           Product: D
           Version: 0.166
          Platform: PC
        OS/Version: Windows
            Status: NEW
          Keywords: accepts-invalid, rejects-valid
          Severity: normal
          Priority: P2
         Component: DMD
        AssignedTo: bugzilla digitalmars.com
        ReportedBy: smjg iname.com


Integer types are not implicitly convertible to enum types.  However, under
certain conditions the process of matching functions to calls likes to think
otherwise.

----------
import std.stdio;

enum Qwert { yuiop }

void asdfg(Qwert hjkl) { writefln("Called hjkl(Qwert)"); }
void asdfg(uint zxcvb) { writefln("Called hjkl(uint)"); }

void main() {
    int nm = 2;

    asdfg(nm);                  // works
    asdfg(cast(int) nm);        // works
    asdfg(3);                   // fails
    asdfg(cast(int) 3);         // fails
    asdfg(3L);                  // fails
    asdfg(cast(int) 3L);        // fails
    asdfg(3 + 2);               // fails
    asdfg(cast(int) (3 + 2));   // fails
    asdfg(nm + 2);              // works
    asdfg(cast(int) (nm + 2));  // works
    asdfg(3 + nm);              // works
    asdfg(cast(int) (3 + nm));  // works
}
----------
D:\My Documents\Programming\D\Tests\bugs\implicit_enum_1.d(13): function
implicit_enum_1.asdfg called with argument types:
        (int)
matches both:
        implicit_enum_1.asdfg(Qwert)
and:
        implicit_enum_1.asdfg(uint)
D:\My Documents\Programming\D\Tests\bugs\implicit_enum_1.d(14): function
implicit_enum_1.asdfg called with argument types:
        (int)
matches both:
        implicit_enum_1.asdfg(Qwert)
and:
        implicit_enum_1.asdfg(uint)
D:\My Documents\Programming\D\Tests\bugs\implicit_enum_1.d(15): function
implicit_enum_1.asdfg called with argument types:
        (long)
matches both:
        implicit_enum_1.asdfg(Qwert)
and:
        implicit_enum_1.asdfg(uint)
D:\My Documents\Programming\D\Tests\bugs\implicit_enum_1.d(16): function
implicit_enum_1.asdfg called with argument types:
        (int)
matches both:
        implicit_enum_1.asdfg(Qwert)
and:
        implicit_enum_1.asdfg(uint)
D:\My Documents\Programming\D\Tests\bugs\implicit_enum_1.d(17): function
implicit_enum_1.asdfg called with argument types:
        (int)
matches both:
        implicit_enum_1.asdfg(Qwert)
and:
        implicit_enum_1.asdfg(uint)
D:\My Documents\Programming\D\Tests\bugs\implicit_enum_1.d(18): function
implicit_enum_1.asdfg called with argument types:
        (int)
matches both:
        implicit_enum_1.asdfg(Qwert)
and:
        implicit_enum_1.asdfg(uint)
----------

All of these arguments, except for 3L, are of type int.  You can see for
yourself that int isn't implicitly convertible to Qwert by adding

    Qwert qaz = nm;

So why does the compiler think that it matches the function?  In fact, _all_
calls should match, of the options given, only asdfg(uint).  Indeed, if
asdfg(Qwert) is removed, the code compiles without error.

It isn't necessary to overload the function name to show the bug.  Those that
report the conflict are the only ones that work if asdfg(uint) is removed. 
This compiles without error:

----------
import std.stdio;

enum Qwert { yuiop }

void asdfg(Qwert hjkl) { writefln("Called hjkl(Qwert)"); }

void main() {
    int nm = 2;

    asdfg(3);
    asdfg(cast(int) 3);
    asdfg(3L);
    asdfg(cast(int) 3L);
    asdfg(3 + 2);
    asdfg(cast(int) (3 + 2));
}
----------

even though, in fact, not one of the calls to asdfg is legal.


-- 
Sep 14 2006
next sibling parent Thomas Kuehne <thomas-dloop kuehne.cn> writes:
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

d-bugmail puremagic.com schrieb am 2006-09-15:
 http://d.puremagic.com/issues/show_bug.cgi?id=349

 Integer types are not implicitly convertible to enum types.  However, under
 certain conditions the process of matching functions to calls likes to think
 otherwise.

 ----------
 import std.stdio;

 enum Qwert { yuiop }

 void asdfg(Qwert hjkl) { writefln("Called hjkl(Qwert)"); }
 void asdfg(uint zxcvb) { writefln("Called hjkl(uint)"); }

 void main() {
     int nm = 2;

     asdfg(nm);                  // works
     asdfg(cast(int) nm);        // works
     asdfg(3);                   // fails
     asdfg(cast(int) 3);         // fails
     asdfg(3L);                  // fails
     asdfg(cast(int) 3L);        // fails
     asdfg(3 + 2);               // fails
     asdfg(cast(int) (3 + 2));   // fails
     asdfg(nm + 2);              // works
     asdfg(cast(int) (nm + 2));  // works
     asdfg(3 + nm);              // works
     asdfg(cast(int) (3 + nm));  // works
 }

<snip>
 All of these arguments, except for 3L, are of type int.  You can see for
 yourself that int isn't implicitly convertible to Qwert by adding

     Qwert qaz = nm;

<snip> Added to DStress as http://dstress.kuehne.cn/compile/e/enum_48_A.d http://dstress.kuehne.cn/compile/e/enum_48_B.d http://dstress.kuehne.cn/compile/e/enum_48_C.d http://dstress.kuehne.cn/compile/e/enum_48_D.d http://dstress.kuehne.cn/run/e/enum_49_A.d http://dstress.kuehne.cn/run/e/enum_49_B.d http://dstress.kuehne.cn/run/e/enum_49_C.d http://dstress.kuehne.cn/run/e/enum_49_D.d Thomas -----BEGIN PGP SIGNATURE----- iD8DBQFFEZ/6LK5blCcjpWoRAnRDAKCDBe4uz0URAuP5XeEK0T3nf7cHsQCfdPaQ YU4T6MAdN15zqNbgMon4oZU= =lN1Q -----END PGP SIGNATURE-----
Sep 20 2006
prev sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=349


bugzilla digitalmars.com changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
         Resolution|                            |FIXED




------- Comment #2 from bugzilla digitalmars.com  2006-11-25 03:28 -------
Fixed DMD 0.175


-- 
Nov 25 2006