Bug 5288 – auto return: forward ref error when using it with recursive functions

Status
REOPENED
Severity
normal
Priority
P3
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2010-11-29T03:21:53Z
Last change time
2024-12-13T17:54:25Z
Assigned to
No Owner
Creator
nfxjfg
Moved to GitHub: dmd#18320 →

Comments

Comment #0 by nfxjfg — 2010-11-29T03:21:53Z
auto x(int z) { if (z == 1) { return x(z); //line 3 } else { return z; //line 5 } } z.d(3): Error: forward reference to x z.d(5): Error: mismatched function return type inference of int and _error_ It works when you switch line 3 and 5.
Comment #1 by bugzilla — 2011-04-03T22:12:01Z
This one is a little hard to fix, because the semantic analysis for function bodies goes statement by statement in a forward manner.
Comment #2 by bearophile_hugs — 2011-04-04T11:54:27Z
(In reply to comment #1) > This one is a little hard to fix, because the semantic analysis for function > bodies goes statement by statement in a forward manner. This is a nice bug report, and to solve this kind of code the D compiler probably needs a little more powerful type inference. If you are able to do this improvement with a reasonable amount of work and code, then it's OK. Otherwise an option it to mark this with WONTFIX to keep the D compiler simpler. A better type inferencer may require more compilation time and a bigger compiler.
Comment #3 by k.hara.pg — 2011-11-25T11:12:06Z
There is mutually recursive call as a slightly more complex case. From http://d.puremagic.com/issues/show_bug.cgi?id=2810#c8 auto foo() { bar(); return 1; } auto bar() { foo(); return 1; }
Comment #4 by k.hara.pg — 2011-12-03T06:06:08Z
*** Issue 7059 has been marked as a duplicate of this issue. ***
Comment #5 by maximzms — 2013-03-04T08:57:00Z
Now this doesn't work even if lines 3 and 5 go in reverse order: -------------------- auto x(int z) { if (z == 1) { return z; } else { return x(z); } } void main() {} -------------------- test.d(6): Error: forward reference to x -------------------- DMD from Git head: https://github.com/D-Programming-Language/dmd/commit/13b3bdbf3819fec810ebfb077957510612dfa815
Comment #6 by pro.mathias.lang — 2022-03-21T08:25:43Z
*** Issue 13986 has been marked as a duplicate of this issue. ***
Comment #7 by robert.schadek — 2024-12-13T17:54:25Z
THIS ISSUE HAS BEEN MOVED TO GITHUB https://github.com/dlang/dmd/issues/18320 DO NOT COMMENT HERE ANYMORE, NOBODY WILL SEE IT, THIS ISSUE HAS BEEN MOVED TO GITHUB