Bug 10962 – Incorrect merging of same enum types with different qualifiers
Status
RESOLVED
Resolution
WORKSFORME
Severity
normal
Priority
P2
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2013-09-03T19:53:37Z
Last change time
2020-03-21T03:56:40Z
Keywords
rejects-valid
Assigned to
No Owner
Creator
kekeniro2
Comments
Comment #0 by kekeniro2 — 2013-09-03T19:53:37Z
I have no idea whether this is a very specific case.
class SomeClass {
enum E : short { init }
E e1;
E foo() const {
//E ret = e1 ? E.init : e1; // OK
//E e2;
//return e2 ? E.init : e2; // OK
return e1 ? E.init : e1; // NG
}
}
DMD gives:
Error: cannot implicitly convert expression (this.e1 ? 0 : cast(int)this.e1) of type int to E
Comment #1 by yebblies — 2013-11-12T21:13:11Z
Because E.init has type E and e1 has type const(E), it misses the t1->equals(t2) condition in CondExp::semantic, and goes to typeMerge, which calls toBaseType on both, giving short, which it then promotes to int.
CondExp::semantic and/or typeMerge need to be fixed to prefer const conversion over promotion.
Comment #2 by verylonglogin.reg — 2013-12-22T00:53:52Z
Reduced testcase (the issue has nothing to do with `const` functions):
This code should compile:
---
enum E { a }
void main()
{
const E ce;
auto x = 1 ? ce : E.a;
E e = x; // line 7
static assert(is(typeof(x) == const E)); // line 8
}
---
main.d(7): Error: cannot implicitly convert expression (x) of type int to E
main.d(8): Error: static assert (is(int == const(E))) is false
---