Bug 6022 – auto return type inference could be improved

Status
RESOLVED
Resolution
WONTFIX
Severity
normal
Priority
P2
Component
dmd
Product
D
Version
D2
Platform
Other
OS
All
Creation time
2011-05-16T21:34:00Z
Last change time
2016-08-27T21:51:05Z
Assigned to
nobody
Creator
andrej.mitrovich

Comments

Comment #0 by andrej.mitrovich — 2011-05-16T21:34:12Z
auto foo() { if (1) { return [0, 0]; } else { size_t one; size_t two; return [one, two]; } } void main(){ } Error: mismatched function return type inference of uint[] and int[] A sufficiently smart compiler could figure out a common type for the two arrays. :)
Comment #1 by hsteoh — 2014-08-09T14:54:29Z
Still present in git HEAD.
Comment #2 by andrej.mitrovich — 2016-08-27T21:51:05Z
Hmm actually it's better if this is explicit. Otherwise the compiler may end up changing the return type if it finds a better type which all the return expressions implicitly convert to. For example: ----- auto foo() { if (0) { return [long.max]; } else { return [int.max + 1]; } } ----- Currently it's an error. In theory the compiler could decide to make the return type `long[]`, that would make the second array contain a positive long integer. *But*, if you remove the first expression the compiler will infer the return type as `int[]` and the second array will contain a negative number (due to signed int wraparound). I feel it would be a little dangerous to have such implicit type deduction matching in place. I'm marking my enhancement request as wontfix.