D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 5899 - auto-return function cannot match 'null' with reference type.
Summary: auto-return function cannot match 'null' with reference type.
Status: RESOLVED FIXED
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D2
Hardware: All All
: P2 minor
Assignee: No Owner
URL:
Keywords: patch, rejects-valid
Depends on:
Blocks:
 
Reported: 2011-04-27 08:46 UTC by kennytm
Modified: 2011-11-25 00:29 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 kennytm 2011-04-27 08:46:39 UTC
Test code 1:
---------------------------
class K {}
auto f(bool b) {
    if (b)
        return new K;
    else
        return null;
}
---------------------------
x.d(6): Error: mismatched function return type inference of void* and x.K
---------------------------



Test code 2:
---------------------------
auto f(bool b) {
    if (b)
        return new int;
    else
        return null;
}
---------------------------
x.d(5): Error: mismatched function return type inference of void* and int*
---------------------------



DMD should know that 'null' is a valid value for any reference types.

A workaround is to give an exact type, e.g.
---------------------------
auto f(bool b) {
    if (b)
        return new int; // or new K;
    else {
        typeof(return) n = null;
        return n;
    }
}
---------------------------
but this should not be necessary.


(Perhaps the D spec should give a specific type e.g. 'nullptr_t' for 'null'?)