www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 12542] New: No function attribute inference for recursive functions

reply d-bugmail puremagic.com writes:
https://d.puremagic.com/issues/show_bug.cgi?id=12542

           Summary: No function attribute inference for recursive
                    functions
           Product: D
           Version: D2
          Platform: All
        OS/Version: All
            Status: NEW
          Severity: critical
          Priority: P2
         Component: DMD
        AssignedTo: nobody puremagic.com
        ReportedBy: monarchdodra gmail.com


--- Comment #0 from monarchdodra gmail.com 2014-04-07 23:06:23 PDT ---
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.

-- 
Configure issuemail: https://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Apr 07 2014
next sibling parent d-bugmail puremagic.com writes:
https://d.puremagic.com/issues/show_bug.cgi?id=12542



--- Comment #1 from Kenji Hara <k.hara.pg gmail.com> 2014-04-08 01:13:13 PDT ---
(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 -- Configure issuemail: https://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: -------
Apr 08 2014
prev sibling parent d-bugmail puremagic.com writes:
https://d.puremagic.com/issues/show_bug.cgi?id=12542



--- Comment #2 from monarchdodra gmail.com 2014-04-08 02:29:31 PDT ---
(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. -- Configure issuemail: https://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: -------
Apr 08 2014