| |
| Posted by d-bugmail | PermalinkReply |
|
d-bugmail
| 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.
--
|