[Issue 24178] New: Infer base-class return type from derived-class returns

d-bugmail at puremagic.com d-bugmail at puremagic.com
Sat Oct 7 17:00:06 UTC 2023


https://issues.dlang.org/show_bug.cgi?id=24178

          Issue ID: 24178
           Summary: Infer base-class return type from derived-class
                    returns
           Product: D
           Version: D2
          Hardware: All
                OS: All
            Status: NEW
          Severity: enhancement
          Priority: P1
         Component: dmd
          Assignee: nobody at puremagic.com
          Reporter: snarwin+bugzilla at gmail.com

As of DMD 2.104.0, the following program fails to compile:

---
class X {}
class A : X {}
class B : X {}

auto fun(bool b)
{
    if (b) return new A;
    else return new B;
}
---

The error message is:

---
example.d(8): Error: expected return type of `example.A`, not `example.B`:
example.d(7):        Return type of `example.A` inferred here.
---

Since A and B have an unambiguous common type, X, the compiler should infer the
return type of fun to be X.

--


More information about the Digitalmars-d-bugs mailing list