Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

LDC emits wrong code #2960

Closed
CyberShadow opened this issue Jan 4, 2019 · 7 comments
Closed

LDC emits wrong code #2960

CyberShadow opened this issue Jan 4, 2019 · 7 comments

Comments

@CyberShadow
Copy link
Contributor

CyberShadow commented Jan 4, 2019

Hello,

This program segfaults crashes with "Memory allocation failed" (attempts to append to array at wrong memory address) when built with LDC, but runs fine when built with DMD:

@safe:

template listDir(alias handler)
{
	struct Context
	{
		void callHandler() { handler(); }
	}

	void listDir()
	{
		string s;
		lazyFun(s);

		(new Context).callHandler();
	}
}

void lazyFun(lazy string s) { }

void main()
{
	string[] result;
	listDir!(() { result ~= ""; })();
}

I'm using LDC as packaged in Arch Linux:

LDC - the LLVM D compiler (1.13.0):
  based on DMD v2.083.1 and LLVM 7.0.1
  built with LDC - the LLVM D compiler (1.12.0)
  Default target: x86_64-pc-linux-gnu
@thewilsonator
Copy link
Contributor

thewilsonator commented Jan 4, 2019

Hmm, interestingly it loops forever for me, which would explain the crashes with "Memory allocation failed".

Ignore that, my command line was completely wrong.

@CyberShadow
Copy link
Contributor Author

With this amendment, it should behave more consistently, I think:

void main()
{
	int magic = 0xDEADBEEF;
	listDir!(() { assert(magic == 0xDEADBEEF); })();
}

@thewilsonator
Copy link
Contributor

Now I get

core.exception.AssertError@./test.d(24): Assertion failure
----------------
??:? _d_assert [0x10cffc907]
??:? pure nothrow @nogc @safe void test.main().__lambda1() [0x10ceb5f83]
??:? pure nothrow @nogc @safe void test.listDir!(test.main().__lambda1()).Context.callHandler() [0x10ceb5f4f]
??:? void test.listDir!(test.main().__lambda1()).listDir() [0x10ceb5f2c]
??:? _Dmain [0x10ceb5edb]

Will take a look.

@kinke
Copy link
Member

kinke commented Jan 5, 2019

Somewhat reduced, showing that there must be some confusion wrt. local frame and context for the Context struct in listDir():

template listDir(alias handler)
{
	struct Context
	{
		void callHandler() { handler(); }
	}

	void listDir()
	{
		int a = 123;
		void blub() { assert(a == 123); } // working if commented out
		Context().callHandler();
	}
}

void main()
{
	int s = 0xDEADBEEF;
	listDir!(() { assert(s == 0xDEADBEEF); })();
}

@kinke
Copy link
Member

kinke commented Jan 19, 2019

listDir() sets up a wrong context for the Context struct, the one it would pass if it called nested blub() (nest.listDir* instead of parent nest.main*). Interestingly, a call to a sibling function works, e.g.,

template listDir(alias handler)
{
	void test() { handler(); }

	void listDir()
	{
		int a = 123;
		void blub() { assert(a == 123); }
		blub();
		test();
	}
}

void main()
{
	int s = 0xDEADBEEF;
	listDir!(() { assert(s == 0xDEADBEEF); })();
}

kinke added a commit to kinke/ldc that referenced this issue Jan 19, 2019
The context for instances of aggregates *not* nested in the current
function, but some parent. Fixes ldc-developers#2960.
kinke added a commit to kinke/ldc that referenced this issue Jan 19, 2019
The context for instances of aggregates *not* nested in the current
function, but some parent. Fixes ldc-developers#2960.
@kinke
Copy link
Member

kinke commented Jan 19, 2019

Thx for the nice reduced testcase; fixed by #2969.

kinke added a commit that referenced this issue Jan 20, 2019
The context for instances of aggregates *not* nested in the current
function, but some parent. Fixes #2960.
@CyberShadow
Copy link
Contributor Author

Very belated thank you :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants