www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 7585] New: functions in templates inferred as delegate

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

           Summary: functions in templates inferred as delegate
           Product: D
           Version: D2
          Platform: All
        OS/Version: All
            Status: NEW
          Severity: normal
          Priority: P2
         Component: DMD
        AssignedTo: nobody puremagic.com
        ReportedBy: simendsjo gmail.com


--- Comment #0 from simendsjo <simendsjo gmail.com> 2012-02-25 09:22:34 PST ---
extern(C) alias void function() Callback;

template Wrap(alias dg)
{
    extern(C) void Wrap()
    {
        dg();
    }
}

void main()
{
    Callback cb = &Wrap!( () {} );
}

Error: cannot implicitly convert expression (&Wrap) of type void delegate()
pure nothrow  safe to extern (C) void function()

According to Timon Gehr: "'() {}' should be inferred as void function()pure
nothrow  safe." http://forum.dlang.org/post/jib1a2$1jl5$1 digitalmars.com

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Feb 25 2012
next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=7585



--- Comment #1 from Kenji Hara <k.hara.pg gmail.com> 2012-02-25 20:21:04 PST ---
The root cause is conservative escape analysis for nested template
instantiation.

extern(C) alias void function() Callback;

template Wrap(alias dg)
{
    extern(C) void Wrap() { dg(); }
}

void f1(){}

void main()
{
    static void f2(){}
    void f3(){}

    Callback cb1 = &Wrap!(f1);      // OK
    Callback cb2 = &Wrap!(f2);      // OK
    Callback cb3 = &Wrap!(f3);      // NG
    Callback cb3 = &Wrap!((){});    // NG
}

Current implementation always treats lambda literal as nested symbol.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Feb 25 2012
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=7585


Kenji Hara <k.hara.pg gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|                            |pull, rejects-valid


--- Comment #2 from Kenji Hara <k.hara.pg gmail.com> 2012-02-26 07:43:34 PST ---
https://github.com/D-Programming-Language/dmd/pull/767

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Feb 26 2012
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=7585


Ali Cehreli <acehreli yahoo.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |acehreli yahoo.com


--- Comment #3 from Ali Cehreli <acehreli yahoo.com> 2012-02-27 16:46:03 PST ---
I would hate to stop the implementation of a useful feature, but isn't this
against the current spec?

  http://dlang.org/expression.html#FunctionLiteral

"If the keywords function or delegate are omitted, it defaults to being a
delegate."

Ali

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Feb 27 2012
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=7585


Jonathan M Davis <jmdavisProg gmx.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |jmdavisProg gmx.com


--- Comment #4 from Jonathan M Davis <jmdavisProg gmx.com> 2012-02-27 16:56:33
PST ---
TDPL says that it's inferred.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Feb 27 2012
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=7585



--- Comment #5 from github-bugzilla puremagic.com 2012-02-29 12:19:43 PST ---
Commit pushed to master at https://github.com/D-Programming-Language/dmd

https://github.com/D-Programming-Language/dmd/commit/65fe59384a05de9e59cda85f0de8296d6fc9c478
Merge pull request #767 from 9rnsr/fix7585

Issue 7585 - functions in templates inferred as delegate

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Feb 29 2012
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=7585



--- Comment #6 from Ali Cehreli <acehreli yahoo.com> 2012-02-29 13:15:41 PST ---
Just a reminder: the spec needs to be updated.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Feb 29 2012
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=7585



--- Comment #7 from github-bugzilla puremagic.com 2012-02-29 15:22:33 PST ---
Commit pushed to master at https://github.com/D-Programming-Language/dmd

https://github.com/D-Programming-Language/dmd/commit/181fb9cd10cf11f20feb22ee13e305f41f9cbe90
Revert "fix Issue 7585 - functions in templates inferred as delegate"

This reverts commit 182edee285f9c7b8c552e5de3c11636aac154991.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Feb 29 2012
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=7585



--- Comment #8 from Kenji Hara <k.hara.pg gmail.com> 2012-06-05 07:41:29 PDT ---
A new pull to retry fixing:
https://github.com/D-Programming-Language/dmd/pull/982

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Jun 05 2012
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=7585



--- Comment #9 from github-bugzilla puremagic.com 2012-06-12 10:06:25 PDT ---
Commits pushed to master at https://github.com/D-Programming-Language/dmd

https://github.com/D-Programming-Language/dmd/commit/24b83f373e93fc553e42213132adabb992c03405
fix Issue 7585 - functions in templates inferred as delegate

https://github.com/D-Programming-Language/dmd/commit/c66af72bd96110c027db69ea560b6dc3f88b3054
Merge pull request #982 from 9rnsr/fix7585

Issue 7585 - functions in templates inferred as delegate

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Jun 12 2012
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=7585


Andrej Mitrovic <andrej.mitrovich gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |andrej.mitrovich gmail.com


--- Comment #10 from Andrej Mitrovic <andrej.mitrovich gmail.com> 2013-02-05
13:08:39 PST ---
What's the state of this? OP sample works, but in Kenji's Comment #1 sample the
line which fails is:

Callback cb3 = &Wrap!(f3);      // NG

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Feb 05 2013
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=7585


timon.gehr gmx.ch changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
         Resolution|                            |FIXED


--- Comment #11 from timon.gehr gmx.ch 2013-02-05 13:27:23 PST ---
(In reply to comment #10)
 What's the state of this? OP sample works, but in Kenji's Comment #1 sample the
 line which fails is:
 
 Callback cb3 = &Wrap!(f3);      // NG

It is to be expected that this fails. If the OP sample works, I assume this is fixed. -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: -------
Feb 05 2013
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=7585



--- Comment #12 from Andrej Mitrovic <andrej.mitrovich gmail.com> 2013-02-05
13:28:09 PST ---
(In reply to comment #11)
 (In reply to comment #10)
 What's the state of this? OP sample works, but in Kenji's Comment #1 sample the
 line which fails is:
 
 Callback cb3 = &Wrap!(f3);      // NG

It is to be expected that this fails. If the OP sample works, I assume this is fixed.

I guess he also expects this to fail then: Callback cb3 = &Wrap!((){}); // NG but it doesn't fail. -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: -------
Feb 05 2013
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=7585



--- Comment #13 from timon.gehr gmx.ch 2013-02-05 13:32:58 PST ---
(In reply to comment #12)
 (In reply to comment #11)
 (In reply to comment #10)
 What's the state of this? OP sample works, but in Kenji's Comment #1 sample the
 line which fails is:
 
 Callback cb3 = &Wrap!(f3);      // NG

It is to be expected that this fails. If the OP sample works, I assume this is fixed.

I guess he also expects this to fail then: Callback cb3 = &Wrap!((){}); // NG but it doesn't fail.

This should work. What he wanted to show is that (){ } currently behaves like f3, while it should behave like f1 or f2. -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: -------
Feb 05 2013
prev sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=7585



--- Comment #14 from Kenji Hara <k.hara.pg gmail.com> 2013-02-05 17:19:33 PST
---
(In reply to comment #10)
 What's the state of this? OP sample works, but in Kenji's Comment #1 sample the
 line which fails is:
 
 Callback cb3 = &Wrap!(f3);      // NG

The sample in comment#1 was shown old broken compiler behavior.
    Callback cb1 = &Wrap!(f1);      // OK
    Callback cb2 = &Wrap!(f2);      // OK

The Wrap function inside Wrap template is declared with extern(C). So it cannot have any hidden context pointer. And, calling f1 or f2 will not need such extra pointer, then Wrap!(f1) and Wrap!(f2) will succeed to compile.
    Callback cb3 = &Wrap!(f3);      // NG

f3 is a nested function. So the Wrap function should get a hidden context to main function, but it is impossible. Then the instantiation Wrap!(f3) will fail.
    Callback cb3 = &Wrap!((){});    // NG

This should be compiled. The lambda function (){} has no outer context access, so it should not be treated as nested. But, it had been accidentally done. As a conclusion, current compiler works as expected. This bug is already fixed. -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: -------
Feb 05 2013