Bug 349 – Function matching with enums is erratic

Status
RESOLVED
Resolution
FIXED
Severity
normal
Priority
P2
Component
dmd
Product
D
Version
D1 (retired)
Platform
x86
OS
Windows
Creation time
2006-09-14T20:36:00Z
Last change time
2014-02-15T13:20:31Z
Keywords
accepts-invalid, rejects-valid
Assigned to
bugzilla
Creator
smjg

Comments

Comment #0 by smjg — 2006-09-14T20:36:48Z
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.
Comment #1 by thomas-dloop — 2006-09-20T14:20:50Z
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 [email protected] 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-----
Comment #2 by bugzilla — 2006-11-25T03:28:25Z
Fixed DMD 0.175