October 07, 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@puremagic.com
          Reporter: snarwin+bugzilla@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.

--