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