Bug 7585 – functions in templates inferred as delegate

Status
RESOLVED
Resolution
FIXED
Severity
normal
Priority
P2
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2012-02-25T09:22:00Z
Last change time
2013-02-05T17:19:33Z
Keywords
pull, rejects-valid, spec
Assigned to
nobody
Creator
simendsjo

Comments

Comment #0 by simendsjo — 2012-02-25T09:22:34Z
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/[email protected]
Comment #1 by k.hara.pg — 2012-02-25T20:21:04Z
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.
Comment #2 by k.hara.pg — 2012-02-26T07:43:34Z
Comment #3 by acehreli — 2012-02-27T16:46:03Z
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
Comment #4 by issues.dlang — 2012-02-27T16:56:33Z
TDPL says that it's inferred.
Comment #5 by github-bugzilla — 2012-02-29T12:19:43Z
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
Comment #6 by acehreli — 2012-02-29T13:15:41Z
Just a reminder: the spec needs to be updated.
Comment #7 by github-bugzilla — 2012-02-29T15:22:33Z
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.
Comment #8 by k.hara.pg — 2012-06-05T07:41:29Z
Comment #9 by github-bugzilla — 2012-06-12T10:06:25Z
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
Comment #10 by andrej.mitrovich — 2013-02-05T13:08:39Z
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
Comment #11 by timon.gehr — 2013-02-05T13:27:23Z
(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.
Comment #12 by andrej.mitrovich — 2013-02-05T13:28:09Z
(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.
Comment #13 by timon.gehr — 2013-02-05T13:32:58Z
(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.
Comment #14 by k.hara.pg — 2013-02-05T17:19:33Z
(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.