D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 14965 - [REG2.031] Forward reference to inferred return type of function call when using auto return type
Summary: [REG2.031] Forward reference to inferred return type of function call when us...
Status: RESOLVED FIXED
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D2
Hardware: All All
: P1 regression
Assignee: No Owner
URL:
Keywords: pull, rejects-valid
Depends on:
Blocks:
 
Reported: 2015-08-26 19:20 UTC by Tomáš Chaloupka
Modified: 2016-03-07 20:06 UTC (History)
3 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description Tomáš Chaloupka 2015-08-26 19:20:08 UTC
import std.stdio;
import std.range : chain;

auto test(string a) {
    return test(a, "b");
}

auto test(string a, string b) {
    return chain(a, b);
}

void main() {
    writeln(test(a));
}

Ends with: Error: forward reference to inferred return type of function call 'test'

I know this exact sample is solvable by default parameter but there are cases where it is not possible.

Workaround is discussed here: http://forum.dlang.org/post/cyvekwuwskhexribcbbl@forum.dlang.org
Comment 1 timon.gehr 2015-08-26 19:38:20 UTC
Minimal example:

auto foo(){ return foo(0); }
int foo(int x){ return x; }

(Also, the original post has a typo, I suspect main should do writeln(test("a")).)
Comment 2 Vladimir Panteleev 2015-09-01 10:54:29 UTC
(In reply to timon.gehr from comment #1)
> auto foo(){ return foo(0); }
> int foo(int x){ return x; }

This appears to be a regression (introduced in DMD 2.031).
Comment 4 Andrea Fontana 2016-02-02 11:54:05 UTC
Recursive example:

auto guessreturn(uint i)
{
	if (i == 0) return 0;
	else return guessreturn(i-1);
}
Comment 5 github-bugzilla 2016-03-07 20:06:49 UTC
Commits pushed to master at https://github.com/D-Programming-Language/dmd

https://github.com/D-Programming-Language/dmd/commit/4b6779d2ff0b95866205189cb55e5aef00c13c7e
fix Issue 14965 - Forward reference to inferred return type of function call when using auto return type

If a function symbol is used, its overload resolution should be deferred until:

```d
func(...)           // CallExp.semantic
func.mangleof       // DotIdExp.semanticY
typeof(func)        // TypeTypeof.resolve
typeof(&func)       // TypeTypeof.resolve
auto v = &func;     // ExpInitializer.inferType
&func;              // ExpStatement.semantic
return &func;       // ReturnStatement
```

When the semantic analysis reaches to them, the function forward reference can beome actual error.

Other use of 'func' should be treated as a property-like function call (done in `resolveProperties`) and finally handled in `CallExp`.

https://github.com/D-Programming-Language/dmd/commit/624ad66c0bbfdfad8c5c1fc95339f77ac3659694
Merge pull request #5202 from 9rnsr/fix14965

[REG2.031] Issue 14965 - Forward reference to inferred return type of function call when using auto return type