cppfront
cppfront copied to clipboard
[BUG] Aggressively consuming copied variables
trafficstars
The issue is that the last use detection of variables doesn't take into account loops. If the last use is detected inside a loop body it will apply std::move() and it will be consumed on the first iteration.
wreakit: (copy s: std::string) -> std::string = {
wreak: std::string = "it";
i: int = 0;
while i < 2 next i++ {
wreak = s;
}
return wreak;
}
Generates the code:
[[nodiscard]] auto wreakit(std::string s) -> std::string{
std::string wreak { "it" };
int i { 0 };
for( ; i < 2; ++i ) {
wreak = std::move(s);
}
return wreak;
}
Expected value returned by wreakit("ralph"); would be "ralph" but actual value is "";