D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 10962 - Incorrect merging of same enum types with different qualifiers
Summary: Incorrect merging of same enum types with different qualifiers
Status: RESOLVED WORKSFORME
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D2
Hardware: All All
: P2 normal
Assignee: No Owner
URL:
Keywords: rejects-valid
Depends on:
Blocks:
 
Reported: 2013-09-03 19:53 UTC by kekeniro2
Modified: 2020-03-21 03:56 UTC (History)
2 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description kekeniro2 2013-09-03 19:53:37 UTC
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 yebblies 2013-11-12 21:13:11 UTC
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 Denis Shelomovskii 2013-12-22 00:53:52 UTC
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
---