www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Struct with lambda argument causes segfault at runtime

reply "H. S. Teoh via Digitalmars-d" <digitalmars-d puremagic.com> writes:
Here's a lovely Saturday morning exercise for you D aficionados out
there:

	struct S(alias fun)
	{
	    auto g() { return fun(0); }
	}

	auto f(int arr)
	{
	    return S!(a => arr)();
	}

	auto r = f(2);

	void main() {
	    r.g();
	}

This causes a segfault at runtime. Moving the declaration of r inside
main() fixes the problem. Why?

	https://issues.dlang.org/show_bug.cgi?id=11826

My suspicion is that the lambda is trying to access the local variable
'arr', which has gone out of scope.


T

-- 
Leather is waterproof.  Ever see a cow with an umbrella?
Aug 09 2014
parent reply Timon Gehr <timon.gehr gmx.ch> writes:
On 08/09/2014 07:11 PM, H. S. Teoh via Digitalmars-d wrote:
 This causes a segfault at runtime. Moving the declaration of r inside
 main() fixes the problem. Why?
My guess is it is just a missing diagnostic for the missing support for closures in CTFE. You are effectively trying to do something like auto foo(int a){ return (int x)=>x+a; } auto d=foo(2); which fails with Error: closures are not yet supported in CTFE
Aug 09 2014
parent "H. S. Teoh via Digitalmars-d" <digitalmars-d puremagic.com> writes:
On Sat, Aug 09, 2014 at 07:39:51PM +0200, Timon Gehr via Digitalmars-d wrote:
 On 08/09/2014 07:11 PM, H. S. Teoh via Digitalmars-d wrote:
This causes a segfault at runtime. Moving the declaration of r inside
main() fixes the problem. Why?
My guess is it is just a missing diagnostic for the missing support for closures in CTFE. You are effectively trying to do something like auto foo(int a){ return (int x)=>x+a; } auto d=foo(2); which fails with Error: closures are not yet supported in CTFE
Aha! I think you're right. Moving declaration into main() and changing it to enum (to force CTFE) causes the same problem. So, looks like it's a missing check for an unsupported feature in CTFE. T -- Shin: (n.) A device for finding furniture in the dark.
Aug 09 2014