[cfe-dev] Clang generates invalid code when lambdas call lambdas.

Ian Stewart Ian.Stewart at autodesk.com
Wed Mar 13 15:37:39 PDT 2013


Hello, 

I have created a simple program that crashes when running the resulting code generated by Clang.

	int main( int argc, char *argv[])
	{
		auto first = [&]() { return argc; };
		auto second = [&]() { return first(); };
		return second();
	}

I have investigated a little, and what I have found is that Clang mistakenly thinks that 'first' and 'second' should have the same implementation, only generates code for one of them, yet calls it for both.
If I change the resulting type of 'first' to differ from 'second', it works:

	int main( int argc, char *argv[])
	{
		auto first = [&]() -> unsigned int { return argc; };
		auto second = [&]() -> int { return first(); };
		return second();
	}

LLVM 3.3 @ SVN 176884
clang++.exe -Xclang -cxx-abi -Xclang microsoft -S -emit-llvm lambdafail.cpp -o lambdafail.cpp.bc
lli.exe lambdafail.cpp.bc x y

expected result: 3

Thank you,
Ian




More information about the cfe-dev mailing list