349 – Function matching with enums is erratic

D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 349 - Function matching with enums is erratic
Summary: Function matching with enums is erratic
Status: RESOLVED FIXED
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D1 (retired)
Hardware: x86 Windows
: P2 normal
Assignee: Walter Bright
URL:
Keywords: accepts-invalid, rejects-valid
Depends on:
Blocks:
Reported: 2006年09月14日 20:36 UTC by Stewart Gordon
Modified: 2014年02月15日 13:20 UTC (History)
0 users

See Also:


Attachments
Add an attachment (proposed patch, testcase, etc.)

Note You need to log in before you can comment on or make changes to this issue.
Description Stewart Gordon 2006年09月14日 20:36:48 UTC
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 Thomas Kühne 2006年09月20日 14:20:50 UTC
-----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-----
Comment 2 Walter Bright 2006年11月25日 03:28:25 UTC
Fixed DMD 0.175


AltStyle によって変換されたページ (->オリジナル) /