Bug 10560 – Enum typed as int with value equal to 0 or 1 prefer bool over int overload

Status
RESOLVED
Resolution
INVALID
Severity
critical
Priority
P2
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2013-07-06T14:24:31Z
Last change time
2020-06-26T08:38:10Z
Keywords
industry, pull
Assigned to
No Owner
Creator
yazan.dabain
See also
https://issues.dlang.org/show_bug.cgi?id=9999, https://issues.dlang.org/show_bug.cgi?id=19399

Comments

Comment #0 by yazan.dabain — 2013-07-06T14:24:31Z
import std.stdio; void foo(bool b) { writeln("bool"); } void foo(int i) { writeln("int"); } enum Boo : int { a = 1, b = 2, } void main() { foo(Boo.a); //prints 'bool', should print int foo(Boo.b); //prints 'int' correctly } This issue is related to http://d.puremagic.com/issues/show_bug.cgi?id=9999
Comment #1 by maxim — 2013-07-07T00:21:26Z
This is a separate issue. Boo should be Boo in the first place and int in the second. In referenced issue bool and long are overloaded, here are bool and int. If you pass 1 in such case int version should be called. Problem here is that int was converted to bool and passed to bool overload instead of int which is a bug.
Comment #2 by yazan.dabain — 2013-07-07T13:44:11Z
I certainly agree. I just wanted to note the explanation on why an int 'literal' of 1 would match a bool overload in the first place.
Comment #3 by slavo5150 — 2018-09-16T02:58:33Z
A DIP has been submitted to address this issue: https://github.com/dlang/DIPs/blob/master/DIPs/DIP1015.md A PR implementing the DIP can be found at https://github.com/dlang/dmd/pull/7310
Comment #4 by bugzilla — 2018-11-12T02:49:38Z
Comment #5 by razvan.nitu1305 — 2019-06-25T15:37:51Z
Upon discussion with Andrei, an enumerated value is not a literal, hence it is subject to regular type checking and cannot be polysemous. See also https://issues.dlang.org/show_bug.cgi?id=19399 which is causing trouble in industrial application.
Comment #6 by dlang-bot — 2019-06-27T13:12:25Z
@RazvanN7 created dlang/dmd pull request #10099 "Fix Issues 19399 and 10560 - Different Conversion Rules for Same Value and Type Enum" fixing this issue: - Fix Issues 19399 and 10560 - Different Conversion Rules for Same Value and Type Enum https://github.com/dlang/dmd/pull/10099
Comment #7 by bugzilla — 2020-06-26T08:38:10Z
See detailed explanation in https://issues.dlang.org/show_bug.cgi?id=19399 This is not a bug, it is a misunderstanding of how match levels and partial ordering works.