D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 10560 - Enum typed as int with value equal to 0 or 1 prefer bool over int overload
Summary: Enum typed as int with value equal to 0 or 1 prefer bool over int overload
Status: RESOLVED INVALID
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D2
Hardware: All All
: P2 critical
Assignee: No Owner
URL:
Keywords: industry, pull
Depends on:
Blocks:
 
Reported: 2013-07-06 14:24 UTC by yazan.dabain
Modified: 2020-06-26 08:38 UTC (History)
4 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description yazan.dabain 2013-07-06 14:24:31 UTC
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 Maxim Fomin 2013-07-07 00:21:26 UTC
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 yazan.dabain 2013-07-07 13:44:11 UTC
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 Mike Franklin 2018-09-16 02:58:33 UTC
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 Walter Bright 2018-11-12 02:49:38 UTC
Rejected for same reason as https://issues.dlang.org/show_bug.cgi?id=9999
Comment 5 RazvanN 2019-06-25 15:37:51 UTC
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 Dlang Bot 2019-06-27 13:12:25 UTC
@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 Walter Bright 2020-06-26 08:38:10 UTC
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.