Bug 22665 – ImportC: qualified enum values should be of enum type on the D side, not int
Status
RESOLVED
Resolution
FIXED
Severity
major
Priority
P1
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2022-01-11T08:59:15Z
Last change time
2022-01-14T06:53:05Z
Keywords
ImportC, pull
Assigned to
No Owner
Creator
dave287091
Comments
Comment #0 by dave287091 — 2022-01-11T08:59:15Z
Consider the following:
// an_enum.c
enum AnEnum {
A = 0,
B = 1,
};
// use_an_enum.d
import an_enum;
int foo(AnEnum e){
AnEnum ae = AnEnum.A; // Error: cannot implicitly convert expression `0` of type `int` to `AnEnum`
switch(e){
case AnEnum.A: // Error: cannot implicitly convert expression `0` of type `int` to `AnEnum`
return 1;
case AnEnum.B: // Error: cannot implicitly convert expression `1` of type `int` to `AnEnum`
return 2;
}
}
As one of the major benefits of ImportC is getting access to enums defined in C, this is a major problem.
Comment #1 by bugzilla — 2022-01-13T07:19:25Z
The trouble here is that in C11, 6.7.2.2-3 says "The identifiers in an enumerator list are declared as constants that have type int". I'll see what I can do.
Comment #2 by dave287091 — 2022-01-13T07:25:18Z
(In reply to Walter Bright from comment #1)
> The trouble here is that in C11, 6.7.2.2-3 says "The identifiers in an
> enumerator list are declared as constants that have type int". I'll see what
> I can do.
6.7.2.4 specifies that "Each enumerated type shall be compatible with char, a signed integer type, or an unsigned integer type. The choice of type is implementation-defined) but shall be capable of representing the values of all the members of the enumeration.”, so the value will always fit.
Comment #3 by dave287091 — 2022-01-13T07:27:04Z
(In reply to dave287091 from comment #2)
> (In reply to Walter Bright from comment #1)
> ...
> 6.7.2.4
> ...
Sorry, 6.7.2.2-4
Comment #4 by bugzilla — 2022-01-13T07:54:04Z
"shall be capable of representing the values of" is not the same as being the same type.
Comment #5 by dlang-bot — 2022-01-13T07:55:49Z
@WalterBright created dlang/dmd pull request #13520 "fix Issue 22665 - ImportC: qualified enum values should be of enum ty…" fixing this issue:
- fix Issue 22665 - ImportC: qualified enum values should be of enum type on the D side, not int
https://github.com/dlang/dmd/pull/13520
Comment #6 by dave287091 — 2022-01-13T19:47:48Z
(In reply to Walter Bright from comment #4)
> "shall be capable of representing the values of" is not the same as being
> the same type.
Yes, but it means that the implicit conversion is safe, in the same way that converting a short to an int is safe, or that (int & 0x7f) is allowed to implicitly convert to byte.
Comment #7 by dlang-bot — 2022-01-14T06:53:05Z
dlang/dmd pull request #13520 "fix Issue 22665 - ImportC: qualified enum values should be of enum ty…" was merged into master:
- 0cff8651a892fa3fbfb8564e83c75ac214f7c585 by Walter Bright:
fix Issue 22665 - ImportC: qualified enum values should be of enum type on the D side, not int
https://github.com/dlang/dmd/pull/13520