D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 6022 - auto return type inference could be improved
Summary: auto return type inference could be improved
Status: RESOLVED WONTFIX
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D2
Hardware: Other All
: P2 normal
Assignee: No Owner
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2011-05-16 21:34 UTC by Andrej Mitrovic
Modified: 2016-08-27 21:51 UTC (History)
1 user (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description Andrej Mitrovic 2011-05-16 21:34:12 UTC
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 hsteoh 2014-08-09 14:54:29 UTC
Still present in git HEAD.
Comment 2 Andrej Mitrovic 2016-08-27 21:51:05 UTC
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.