Bug 24178 – Infer base-class return type from derived-class returns

Status
RESOLVED
Resolution
DUPLICATE
Severity
enhancement
Priority
P1
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2023-10-07T17:00:06Z
Last change time
2023-10-09T12:05:55Z
Assigned to
No Owner
Creator
Paul Backus
See also
https://issues.dlang.org/show_bug.cgi?id=12283, https://issues.dlang.org/show_bug.cgi?id=3543

Comments

Comment #0 by snarwin+bugzilla — 2023-10-07T17:00:06Z
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.
Comment #1 by razvan.nitu1305 — 2023-10-09T12:05:55Z
*** This issue has been marked as a duplicate of issue 16194 ***