cppfront icon indicating copy to clipboard operation
cppfront copied to clipboard

[BUG] `forward` of capture `$` crashes compiler

Open realgdman opened this issue 2 years ago • 4 comments
trafficstars

Describe the bug Enclosing capture in forward crashes compiler

Reproduction Code

main: ()   [[post: (forward c$) == 5]] //error
= {
	c := 5;
	:() = { (forward c$); }; //error	
	:() = { foo(forward c$); } (); //error - possibly valid usecase	
}

foo: (forward x) = std::cout << x;

https://cpp2.godbolt.org/z/E4rqKW4cn

Version latest (38aec57)

Command lines cppfront/cppfront $1.cpp2 -p clang++-15 -Icppfront/include $1.cpp -std=c++20 -o $1

Expected result Diagnostic, probably disallowing forward with capture

Actual result cppfront: source/cppfront.cpp:2712: void cpp2::cppfront::emit(cpp2::postfix_expression_node &, bool): Assertion `!current_args.back().ptoken' failed. Aborted (core dumped)

Additional context Assert line is https://github.com/hsutter/cppfront/blob/main/source/cppfront.cpp#L2712

This example shows forward can be written at call site. https://github.com/hsutter/cppfront/blob/38aec572f34997699454efdb13cf4e4e409c2cc7/regression-tests/mixed-forwarding.cpp2#L21 and it was mentioned somewhere else. Don't know if forward with capture makes sense.

realgdman avatar May 09 '23 21:05 realgdman

Probably related: x := (forward T<int>()); assert on previous line. cppfront: source/cppfront.cpp:2711: void cpp2::cppfront::emit(cpp2::postfix_expression_node &, bool): Assertion `n.expr->get_token()' failed.

realgdman avatar May 10 '23 23:05 realgdman

Resolving #408 might happen to fix this.

JohelEGP avatar Jun 02 '23 17:06 JohelEGP

This fails with the same error (https://cpp2.godbolt.org/z/49jcdPja6):

to: (forward v) = (forward v.to<i32>());
main: () = { }
cppfront: source/cppfront.cpp:2734: void cpp2::cppfront::emit(cpp2::postfix_expression_node&, bool): Assertion `!current_args.back().ptoken' failed.
Program terminated with signal: SIGSEGV
Compiler returned: 139

JohelEGP avatar Aug 06 '23 13:08 JohelEGP

I tried to work around this by adding parentheses. It simply lowers forward without std::. I suppose it's due to commit c39a94cb7451bd55188d87c1c00848bfae2e6cc2 (#77). (forward (r$)); lowers to return forward(_0); (https://cpp2.godbolt.org/z/YTP8vd61K).

Expected result Diagnostic, probably disallowing forward with capture

This could work with std::forward_like<decltype(this)> semantics (given https://github.com/hsutter/cppfront/commit/4bd0c0438f2d3fa65d3e65a55b17c3a296bd8bc3#commitcomment-133309652): :(forward this) (forward c$).

JohelEGP avatar Nov 29 '23 00:11 JohelEGP