Bug 12542 – No function attribute inference for recursive functions

Status
RESOLVED
Resolution
FIXED
Severity
critical
Priority
P2
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2014-04-07T23:06:00Z
Last change time
2014-11-18T22:29:10Z
Assigned to
nobody
Creator
monarchdodra
See also
https://issues.dlang.org/show_bug.cgi?id=13745

Comments

Comment #0 by monarchdodra — 2014-04-07T23:06:23Z
I've seen this being mentioned before in other bugs, but haven't been able to find an issue proper for it. Basically, if a function is recursive, then none of it's attributes are inferred: It's always impure, throwing and unsafe. //---- int logOf(int n) { if (n) return 1 + logOf(n/2); return 0; } void main() @safe nothrow pure { int log = logOf(9); } //---- Error: pure function 'D main' cannot call impure function 'main.logOf' Error: safe function 'D main' cannot call system function 'main.logOf' Error: 'main.logOf' is not nothrow Error: function 'D main' is nothrow yet may throw //---- The compiler should be able to tell that logOf is nothrow and pure. I think it's safe too: Potential risk of stack overflow aren't considered memory unsafe, are they? In any case it's blocking the fixing of certain function attributes, such as those of sort, or sum.
Comment #1 by k.hara.pg — 2014-04-08T01:13:13Z
(In reply to comment #0) > //---- > int logOf(int n) > { > if (n) > return 1 + logOf(n/2); > return 0; > } > > void main() @safe nothrow pure > { > int log = logOf(9); > } For attribute inference, logOf should be template function. https://github.com/D-Programming-Language/dmd/pull/3436
Comment #2 by monarchdodra — 2014-04-08T02:29:31Z
(In reply to comment #1) > For attribute inference, logOf should be template function. Oops. Sorry! > https://github.com/D-Programming-Language/dmd/pull/3436 Oh, wow. I didn't expect a fix so quickly. Cool.
Comment #3 by github-bugzilla — 2014-04-09T09:48:46Z
Commits pushed to master at https://github.com/D-Programming-Language/dmd https://github.com/D-Programming-Language/dmd/commit/5943e5a6da8c12648dad323c15fe8bcd3de80414 fix Issue 12542 - No function attribute inference for recursive functions https://github.com/D-Programming-Language/dmd/commit/3d69d6b7158ef574fc9441efbd9eff78a5b8d629 Merge pull request #3436 from 9rnsr/fix12542 Issue 12542 - No function attribute inference for recursive functions