Comment #0 by bearophile_hugs — 2011-02-16T16:20:11Z
A case of forward template forward reference (I am not sure this is actually a bug):
import std.traits;
auto foo() {
alias ReturnType!foo T;
return 0;
}
void main() {}
DMD 2.052 shows (it's not a regression):
...\dmd\src\phobos\std\traits.d(122): Error: template instance forward reference of foo
...\dmd\src\phobos\std\traits.d(105): Error: forward reference to foo
...\dmd\src\phobos\std\traits.d(122): Error: template instance std.traits.staticLength!(foo) error instantiating
Comment #1 by kennytm — 2011-04-24T00:12:34Z
Also happens in D1, as described in issue 2885. (I'm closing 2885 as the report there isn't quite to-the-point.)
Comment #2 by kennytm — 2011-04-24T00:14:44Z
*** Issue 2885 has been marked as a duplicate of this issue. ***
Comment #3 by k.hara.pg — 2013-11-22T01:47:37Z
(In reply to comment #0)
> A case of forward template forward reference (I am not sure this is actually a
> bug):
>
>
> import std.traits;
> auto foo() {
> alias ReturnType!foo T;
> return 0;
> }
> void main() {}
>
>
> DMD 2.052 shows (it's not a regression):
>
> ...\dmd\src\phobos\std\traits.d(122): Error: template instance forward
> reference of foo
> ...\dmd\src\phobos\std\traits.d(105): Error: forward reference to foo
> ...\dmd\src\phobos\std\traits.d(122): Error: template instance
> std.traits.staticLength!(foo) error instantiating
This is correct behavior. Before function body semantic finished, getting type of auto function will cause forward reference error since its return type is not determined.
Comment #4 by bearophile_hugs — 2013-11-22T02:52:35Z
(In reply to comment #3)
> Before function body semantic finished, getting type
> of auto function will cause forward reference error since its return type is
> not determined.
Can't the type inferencer become a little smarter and see that the return type of foo is something defined in terms of itself, but it's also an int, and so decide it's an int?
Comment #5 by k.hara.pg — 2013-11-22T03:31:55Z
(In reply to comment #4)
> (In reply to comment #3)
>
> > Before function body semantic finished, getting type
> > of auto function will cause forward reference error since its return type is
> > not determined.
>
> Can't the type inferencer become a little smarter and see that the return type
> of foo is something defined in terms of itself, but it's also an int, and so
> decide it's an int?
No. In statement scope, all of statements are ordered from top to the bottom. We can regard as the return type of auto function is declared at the end of the function body. So seeing return type inside auto-function is exactly same as forward reference issue.
Consider the following invalid code. If the return type is visible inside function, compiler cannot determine the return type of foo.
auto foo(int n)
{
if (n == 1)
return 1;
// forward reference to return type
static if (is(ReturnType!foo == int))
return "";
assert(0);
}
Therefore it is necessary limitation to avoid indeterminate case.