Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,9 @@ auto add_42_to_subrange(auto& rng, cpp2::impl::in<int> start, cpp2::impl::in<int
auto count {0};
for (

auto& i : rng ) { do
auto& i : rng ) { bool _for_break = false; do
if ([_0 = start, _1 = count, _2 = end]{ return cpp2::impl::cmp_less_eq(_0,_1) && cpp2::impl::cmp_less_eq(_1,_2); }()) {
i += 42;
} while (false); ++count; }
} while (false); if (_for_break) { break; } ++count; }
}

Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,9 @@ auto print_subrange(auto const& rng, cpp2::impl::in<int> start, cpp2::impl::in<i
auto count {0};
for (

auto const& i : rng ) { do
auto const& i : rng ) { bool _for_break = false; do
if (cpp2::impl::cmp_less_eq(start,count) && cpp2::impl::cmp_less_eq(count,end)) {
std::cout << i << "\n";
} while (false); ++count; }
} while (false); if (_for_break) { break; } ++count; }
}

Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@
{
std::vector<int> v {1, 2, 3, 4, 5};
auto counter {42};
for ( auto const& i : cpp2::move(v) ) { do {
for ( auto const& i : cpp2::move(v) ) { bool _for_break = false; do {
std::cout << i << " " << counter << "\n";
} while (false); counter *= 2; }
} while (false); if (_for_break) { break; } counter *= 2; }
}

4 changes: 2 additions & 2 deletions regression-tests/test-results/pure2-autodiff-higher-order.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -972,7 +972,7 @@ std::vector<cpp2::taylor<double,6>> v_d {};
r = 0.0;
{
auto t_d_iter{CPP2_UFCS(begin)(cpp2::move(v_d))};
for ( auto const& t : cpp2::move(v) ) { do {
for ( auto const& t : cpp2::move(v) ) { bool _for_break = false; do {
{
auto const& t_d{*cpp2::impl::assert_not_null(t_d_iter)};
{
Expand All @@ -981,7 +981,7 @@ auto const& t_d{*cpp2::impl::assert_not_null(t_d_iter)};
}
}
}
while (false); (++t_d_iter); }
while (false); if (_for_break) { break; } (++t_d_iter); }
}
return { std::move(r), std::move(r_d) };
}
Expand Down
4 changes: 2 additions & 2 deletions regression-tests/test-results/pure2-autodiff.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1186,7 +1186,7 @@ std::vector<double> v_d {};
r = 0.0;
{
auto t_d_iter{CPP2_UFCS(begin)(cpp2::move(v_d))};
for ( auto const& t : cpp2::move(v) ) { do {
for ( auto const& t : cpp2::move(v) ) { bool _for_break = false; do {
{
auto const& t_d{*cpp2::impl::assert_not_null(t_d_iter)};
{
Expand All @@ -1195,7 +1195,7 @@ auto const& t_d{*cpp2::impl::assert_not_null(t_d_iter)};
}
}
}
while (false); (++t_d_iter); }
while (false); if (_for_break) { break; } (++t_d_iter); }
}
return { std::move(r), std::move(r_d) };
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ auto main(int const argc_, char** argv_) -> int{

for ( auto const& i : [_0 = args]() mutable -> decltype(auto) { return _0; }() ) static_cast<void>(i);
for ( auto const& j : [](auto&& x) -> decltype(auto) { return CPP2_FORWARD(x); }(args) ) static_cast<void>(j);
for ( auto const& k : [](auto&& x) -> decltype(auto) { return CPP2_FORWARD(x); }(args) ) { do static_cast<void>(k); while (false); static_cast<void>([_0 = args]() mutable -> decltype(auto) { return _0; }()); }
for ( auto const& l : [](auto&& x) -> decltype(auto) { return CPP2_FORWARD(x); }(args) ) { do static_cast<void>(l); while (false); static_cast<void>([](auto&& x) -> decltype(auto) { return x; }(args)); }
for ( auto const& k : [](auto&& x) -> decltype(auto) { return CPP2_FORWARD(x); }(args) ) { bool _for_break = false; do static_cast<void>(k); while (false); if (_for_break) { break; } static_cast<void>([_0 = args]() mutable -> decltype(auto) { return _0; }()); }
for ( auto const& l : [](auto&& x) -> decltype(auto) { return CPP2_FORWARD(x); }(args) ) { bool _for_break = false; do static_cast<void>(l); while (false); if (_for_break) { break; } static_cast<void>([](auto&& x) -> decltype(auto) { return x; }(args)); }
}

4 changes: 2 additions & 2 deletions regression-tests/test-results/pure2-last-use.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1393,8 +1393,8 @@ auto loops() -> void{
auto x {cpp2_new<int>(0)};
for (

[[maybe_unused]] auto const& unnamed_param_1 : { 0 } ) { do
{} while (false); f_inout(x); }
[[maybe_unused]] auto const& unnamed_param_1 : { 0 } ) { bool _for_break = false; do
{} while (false); if (_for_break) { break; } f_inout(x); }
});

static_cast<void>([]() -> void{
Expand Down
2 changes: 1 addition & 1 deletion regression-tests/test-results/pure2-print.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ requires (true) inline CPP2_CONSTEXPR T outer::object_alias{ 42 };

do {} while ( [&]{ b() ; return true; }() && CPP2_UFCS(empty)(s));

for ( [[maybe_unused]] auto const& unnamed_param_1 : m ) { { do {goto CONTINUE_label; } while (false); c(); } CPP2_CONTINUE_BREAK(label) }
for ( [[maybe_unused]] auto const& unnamed_param_1 : m ) { { bool _for_break = false; do {goto CONTINUE_label; } while (false); if (_for_break) { break; } c(); } CPP2_CONTINUE_BREAK(label) }

#line 47 "pure2-print.cpp2"
if (cpp2::impl::is(!(CPP2_UFCS(empty)(s)), (true))) {cpp2::move(a)(); }
Expand Down
19 changes: 12 additions & 7 deletions source/to_cpp1.h
Original file line number Diff line number Diff line change
Expand Up @@ -2409,18 +2409,17 @@ class cppfront
printer.print_extra("{");
}

// If there's a next-expression, smuggle it in via a nested do/while(false) loop
// (nested "continue" will work, but "break" won't until we do extra work to implement
// that using a flag and implementing "break" as "_for_break = true; continue;")
// If there's a next-expression, smuggle it in via a nested do/while(false) loop.
// We implement break as "_for_break = true; continue;")
if (n.next_expression) {
printer.print_cpp2(" { do ", n.position());
printer.print_cpp2(" { bool _for_break = false; do ", n.position());
}

assert(n.body);
emit(*n.body);

if (n.next_expression) {
printer.print_cpp2(" while (false); ", n.position());
printer.print_cpp2(" while (false); if (_for_break) { break; } ", n.position());
emit(*n.next_expression);
printer.print_cpp2("; }", n.position());
}
Expand Down Expand Up @@ -2684,8 +2683,14 @@ class cppfront
);
}
else {
emit(*n.keyword);
printer.print_cpp2(";", n.position());
if (*n.keyword == "break" && iteration_statements.back().stmt->next_expression
&& *(iteration_statements.back().stmt->identifier) == "for") {
// if we have a next expression there is an nested "do/while(false) loop
printer.print_cpp2("_for_break = true; continue;", n.position());
} else {
emit(*n.keyword);
printer.print_cpp2(";", n.position());
}
}
}

Expand Down