Bug 4312 – std.traits.ReturnType no longer accepts function literals

Status
RESOLVED
Resolution
WORKSFORME
Severity
regression
Priority
P2
Component
phobos
Product
D
Version
D2
Platform
Other
OS
All
Creation time
2010-06-14T08:43:00Z
Last change time
2011-05-27T07:33:12Z
Keywords
rejects-valid
Assigned to
rsinfu
Creator
sandford
Depends on
4217, 4333

Comments

Comment #0 by sandford — 2010-06-14T08:43:52Z
std.traits.ReturnType no longer accepts function literals. Here is a simplified test case: void main(string[] args) { writeln( (ReturnType!( function(int a){return a;} )).stringof ); return; } Results in the error: Error: function std.traits.__funcliteral1 cannot access frame of function __funcliteral1
Comment #1 by rsinfu — 2010-06-16T14:12:20Z
The cause of this regression is bug 4333. Bug 4217 is also related.
Comment #2 by andrej.mitrovich — 2011-05-26T14:12:31Z
I can't reproduce this on 2.053, it compiles and runs. Can you confirm?
Comment #3 by sandford — 2011-05-26T19:27:18Z
I can confirm that non-nested function literals now compile. (i.e. the original bug report) But nested function literals don't compile (DMD 2.053): void main(string[] args) { int b; writeln( (ReturnType!( function(int a){return a+b;} )).stringof ); } Error: function hello.main.__funcliteral1 cannot access frame of function D main Not too sure if this is a separate issue or not.
Comment #4 by kennytm — 2011-05-26T23:41:38Z
(In reply to comment #3) > I can confirm that non-nested function literals now compile. (i.e. the original > bug report) But nested function literals don't compile (DMD 2.053): > > void main(string[] args) { > int b; > writeln( (ReturnType!( function(int a){return a+b;} )).stringof ); > } > > Error: function hello.main.__funcliteral1 cannot access frame of function D > main > > Not too sure if this is a separate issue or not. This is expected. A function literal cannot form a closure. You need a delegate. -------------------------- void main() { int b; auto c = function(int a){return a+b;}; } -------------------------- x.d(6): Error: function x.main.__funcliteral1 cannot access frame of function D main --------------------------
Comment #5 by sandford — 2011-05-27T06:46:29Z
I'm not defining a closure. I'm defining a nested function literal. And given that the following compiles: void main() { int b; int funcliteral(int a){return a+b;} writeln( (ReturnType!funcliteral).stringof ); } I would also expect void main(string[] args) { int b; writeln( (ReturnType!( function(int a){return a+b;} )).stringof ); } to work. But, as you pointed out, this isn't a problem with ReturnType anymore.
Comment #6 by kennytm — 2011-05-27T07:16:28Z
(In reply to comment #5) > I'm not defining a closure. I'm defining a nested function literal. And given > that the following compiles: > > void main() > { > int b; > int funcliteral(int a){return a+b;} > writeln( (ReturnType!funcliteral).stringof ); > } > That's because the '&' of that 'funcliteral' is a delegate. Try it: --------------------------------- void main() { void f() {}; static void g() {}; pragma(msg, typeof(&f)); // void delegate() pragma(msg, typeof(&g)); // void function() } --------------------------------- And the following also does not compile: --------------------------------- void main() { int b; static int func(int a) { return a+b; } } --------------------------------- x.d(6): Error: function x.main.func cannot access frame of function D main --------------------------------- See http://d-programming-language.org/expression.html#FunctionLiteral. > I would also expect > > void main(string[] args) { > int b; > writeln( (ReturnType!( function(int a){return a+b;} )).stringof ); > } > > to work. But, as you pointed out, this isn't a problem with ReturnType anymore.
Comment #7 by sandford — 2011-05-27T07:33:12Z
Ah. Thank you. I think then that we can close this bug.