diff --git a/regression-tests/test-results/mixed-bounds-safety-with-assert-2.cpp b/regression-tests/test-results/mixed-bounds-safety-with-assert-2.cpp index f85ad0cd2..c1b982f51 100644 --- a/regression-tests/test-results/mixed-bounds-safety-with-assert-2.cpp +++ b/regression-tests/test-results/mixed-bounds-safety-with-assert-2.cpp @@ -31,8 +31,9 @@ auto add_42_to_subrange(auto& rng, cpp2::in start, cpp2::in end) -> vo std::vector v {1, 2, 3, 4, 5}; add_42_to_subrange(v, 1, 3); - for ( auto const& cpp2_range = v; auto const& i : cpp2_range ) - std::cout << i << "\n"; + { auto const& cpp2_range = v; for ( auto const& i : cpp2_range ) + std::cout << i << "\n";} +#line 8 "mixed-bounds-safety-with-assert-2.cpp2" } auto add_42_to_subrange(auto& rng, cpp2::in start, cpp2::in end) -> void @@ -41,11 +42,12 @@ auto add_42_to_subrange(auto& rng, cpp2::in start, cpp2::in end) -> vo cpp2::Bounds.expects(cpp2::cmp_less_eq(end,CPP2_UFCS_0(ssize, rng)), ""); auto count {0}; - for ( auto&& cpp2_range = rng; + { auto&& cpp2_range = rng; for ( auto& i : cpp2_range ) { do if ([_0 = start, _1 = count, _2 = end]{ return cpp2::cmp_less_eq(_0,_1) && cpp2::cmp_less_eq(_1,_2); }()) { i += 42; - } while (false); ++count; } + } while (false); ++count; }} +#line 22 "mixed-bounds-safety-with-assert-2.cpp2" } diff --git a/regression-tests/test-results/mixed-bounds-safety-with-assert.cpp b/regression-tests/test-results/mixed-bounds-safety-with-assert.cpp index f1cd6210d..b559f4341 100644 --- a/regression-tests/test-results/mixed-bounds-safety-with-assert.cpp +++ b/regression-tests/test-results/mixed-bounds-safety-with-assert.cpp @@ -38,11 +38,12 @@ auto print_subrange(auto const& rng, cpp2::in start, cpp2::in end) -> cpp2::Bounds.expects(cpp2::cmp_less_eq(end,CPP2_UFCS_0(ssize, rng)), ""); auto count {0}; - for ( auto const& cpp2_range = rng; + { auto const& cpp2_range = rng; for ( auto const& i : cpp2_range ) { do if (cpp2::cmp_less_eq(start,count) && cpp2::cmp_less_eq(count,end)) { std::cout << i << "\n"; - } while (false); ++count; } + } while (false); ++count; }} +#line 18 "mixed-bounds-safety-with-assert.cpp2" } diff --git a/regression-tests/test-results/mixed-fixed-type-aliases.cpp b/regression-tests/test-results/mixed-fixed-type-aliases.cpp index 5df21cdc8..287561427 100644 --- a/regression-tests/test-results/mixed-fixed-type-aliases.cpp +++ b/regression-tests/test-results/mixed-fixed-type-aliases.cpp @@ -44,7 +44,8 @@ auto test(auto const& x) -> void{ cpp2::u16 z {42}; test(std::move(z)); - for ( auto const& cpp2_range = args; auto const& arg : cpp2_range ) - std::cout << arg << "\n"; + { auto const& cpp2_range = args; for ( auto const& arg : cpp2_range ) + std::cout << arg << "\n";} +#line 24 "mixed-fixed-type-aliases.cpp2" } diff --git a/regression-tests/test-results/mixed-function-expression-and-std-for-each.cpp b/regression-tests/test-results/mixed-function-expression-and-std-for-each.cpp index a80c5f010..0c5b0f6d1 100644 --- a/regression-tests/test-results/mixed-function-expression-and-std-for-each.cpp +++ b/regression-tests/test-results/mixed-function-expression-and-std-for-each.cpp @@ -43,8 +43,9 @@ std::move(callback) ); - for ( auto const& cpp2_range = view; auto const& str : cpp2_range ) { + { auto const& cpp2_range = view; for ( auto const& str : cpp2_range ) { std::cout << str << "\n"; - } + }} +#line 30 "mixed-function-expression-and-std-for-each.cpp2" } diff --git a/regression-tests/test-results/mixed-function-expression-and-std-ranges-for-each-with-capture.cpp b/regression-tests/test-results/mixed-function-expression-and-std-ranges-for-each-with-capture.cpp index 676f712c3..e89575ffb 100644 --- a/regression-tests/test-results/mixed-function-expression-and-std-ranges-for-each-with-capture.cpp +++ b/regression-tests/test-results/mixed-function-expression-and-std-ranges-for-each-with-capture.cpp @@ -36,7 +36,8 @@ auto callback {[](auto& x) -> void { x += "-ish"; }}; std::ranges::for_each(view, std::move(callback)); - for ( auto const& cpp2_range = view; auto const& str : cpp2_range ) - std::cout << str << "\n"; + { auto const& cpp2_range = view; for ( auto const& str : cpp2_range ) + std::cout << str << "\n";} +#line 22 "mixed-function-expression-and-std-ranges-for-each-with-capture.cpp2" } diff --git a/regression-tests/test-results/mixed-function-expression-and-std-ranges-for-each.cpp b/regression-tests/test-results/mixed-function-expression-and-std-ranges-for-each.cpp index e22d38389..7772cc828 100644 --- a/regression-tests/test-results/mixed-function-expression-and-std-ranges-for-each.cpp +++ b/regression-tests/test-results/mixed-function-expression-and-std-ranges-for-each.cpp @@ -35,7 +35,8 @@ auto callback {[](auto& x) -> void { x += "-ish"; }}; std::ranges::for_each(view, std::move(callback)); - for ( auto const& cpp2_range = view; auto const& str : cpp2_range ) - std::cout << str << "\n"; + { auto const& cpp2_range = view; for ( auto const& str : cpp2_range ) + std::cout << str << "\n";} +#line 21 "mixed-function-expression-and-std-ranges-for-each.cpp2" } diff --git a/regression-tests/test-results/mixed-function-expression-with-pointer-capture.cpp b/regression-tests/test-results/mixed-function-expression-with-pointer-capture.cpp index 9145c34a1..19666cf8e 100644 --- a/regression-tests/test-results/mixed-function-expression-with-pointer-capture.cpp +++ b/regression-tests/test-results/mixed-function-expression-with-pointer-capture.cpp @@ -37,7 +37,8 @@ auto callback {[](auto& x) -> void { x += "-ish"; }}; std::ranges::for_each(view, std::move(callback)); - for ( auto const& cpp2_range = view; auto const& str : cpp2_range ) - std::cout << str << "\n"; + { auto const& cpp2_range = view; for ( auto const& str : cpp2_range ) + std::cout << str << "\n";} +#line 23 "mixed-function-expression-with-pointer-capture.cpp2" } diff --git a/regression-tests/test-results/mixed-function-expression-with-repeated-capture.cpp b/regression-tests/test-results/mixed-function-expression-with-repeated-capture.cpp index e0203205b..d8a79516a 100644 --- a/regression-tests/test-results/mixed-function-expression-with-repeated-capture.cpp +++ b/regression-tests/test-results/mixed-function-expression-with-repeated-capture.cpp @@ -36,7 +36,8 @@ auto callback {[](auto& x) -> void { x += "-ish"; }}; std::ranges::for_each(view, std::move(callback)); - for ( auto const& cpp2_range = view; auto const& str : cpp2_range ) - std::cout << str << "\n"; + { auto const& cpp2_range = view; for ( auto const& str : cpp2_range ) + std::cout << str << "\n";} +#line 22 "mixed-function-expression-with-repeated-capture.cpp2" } diff --git a/regression-tests/test-results/mixed-intro-for-with-counter-include-last.cpp b/regression-tests/test-results/mixed-intro-for-with-counter-include-last.cpp index 525b2f16c..05bbea99f 100644 --- a/regression-tests/test-results/mixed-intro-for-with-counter-include-last.cpp +++ b/regression-tests/test-results/mixed-intro-for-with-counter-include-last.cpp @@ -27,8 +27,9 @@ { std::vector v {1, 2, 3, 4, 5}; auto counter {42}; - for ( auto const& cpp2_range = v; auto const& i : cpp2_range ) { do { + { auto const& cpp2_range = v; for ( auto const& i : cpp2_range ) { do { std::cout << i << " " << counter << "\n"; - } while (false); counter *= 2; } + } while (false); counter *= 2; }} +#line 9 "mixed-intro-for-with-counter-include-last.cpp2" } diff --git a/regression-tests/test-results/pure2-break-continue.cpp b/regression-tests/test-results/pure2-break-continue.cpp index 0e928e9ba..6742751fc 100644 --- a/regression-tests/test-results/pure2-break-continue.cpp +++ b/regression-tests/test-results/pure2-break-continue.cpp @@ -253,9 +253,9 @@ auto for_continue_inner() -> void { std::vector vi {0, 1, 2}; auto counter {0}; - for ( auto const& cpp2_range = vi; auto const& i : cpp2_range ) { do { + { auto const& cpp2_range = vi; for ( auto const& i : cpp2_range ) { do { std::vector vj {0, 1, 2}; - for ( auto const& cpp2_range = vj; auto const& j : cpp2_range ) { + { auto const& cpp2_range = vj; for ( auto const& j : cpp2_range ) { #line 166 "pure2-break-continue.cpp2" { std::cout << i << j << " "; @@ -263,31 +263,33 @@ auto for_continue_inner() -> void goto CONTINUE_166_9; } std::cout << "inner "; - } CPP2_CONTINUE_BREAK(166_9) } + } CPP2_CONTINUE_BREAK(166_9) }} #line 174 "pure2-break-continue.cpp2" std::cout << "outer "; - } while (false); ++counter; } + } while (false); ++counter; }} +#line 176 "pure2-break-continue.cpp2" } auto for_continue_outer() -> void { std::vector vi {0, 1, 2}; auto counter {0}; - for ( auto const& cpp2_range = vi; auto const& i : cpp2_range ) { + { auto const& cpp2_range = vi; for ( auto const& i : cpp2_range ) { #line 182 "pure2-break-continue.cpp2" { do { std::vector vj {0, 1, 2}; - for ( auto const& cpp2_range = vj; auto const& j : cpp2_range ) { + { auto const& cpp2_range = vj; for ( auto const& j : cpp2_range ) { std::cout << i << j << " "; if (j==1) { goto CONTINUE_182_5; } std::cout << "inner "; - } + }} +#line 192 "pure2-break-continue.cpp2" std::cout << "outer "; - } while (false); ++counter; } CPP2_CONTINUE_BREAK(182_5) } + } while (false); ++counter; } CPP2_CONTINUE_BREAK(182_5) }} #line 194 "pure2-break-continue.cpp2" } @@ -295,9 +297,9 @@ auto for_break_inner() -> void { std::vector vi {0, 1, 2}; auto counter {0}; - for ( auto const& cpp2_range = vi; auto const& i : cpp2_range ) { do { + { auto const& cpp2_range = vi; for ( auto const& i : cpp2_range ) { do { std::vector vj {0, 1, 2}; - for ( auto const& cpp2_range = vj; auto const& j : cpp2_range ) { + { auto const& cpp2_range = vj; for ( auto const& j : cpp2_range ) { #line 202 "pure2-break-continue.cpp2" { std::cout << i << j << " "; @@ -305,31 +307,33 @@ auto for_break_inner() -> void goto BREAK_202_9; } std::cout << "inner "; - } CPP2_CONTINUE_BREAK(202_9) } + } CPP2_CONTINUE_BREAK(202_9) }} #line 210 "pure2-break-continue.cpp2" std::cout << "outer "; - } while (false); ++counter; } + } while (false); ++counter; }} +#line 212 "pure2-break-continue.cpp2" } auto for_break_outer() -> void { std::vector vi {0, 1, 2}; auto counter {0}; - for ( auto const& cpp2_range = vi; auto const& i : cpp2_range ) { + { auto const& cpp2_range = vi; for ( auto const& i : cpp2_range ) { #line 218 "pure2-break-continue.cpp2" { do { std::vector vj {0, 1, 2}; - for ( auto const& cpp2_range = vj; auto const& j : cpp2_range ) { + { auto const& cpp2_range = vj; for ( auto const& j : cpp2_range ) { std::cout << i << j << " "; if (j==1) { goto BREAK_218_5; } std::cout << "inner "; - } + }} +#line 228 "pure2-break-continue.cpp2" std::cout << "outer "; - } while (false); ++counter; } CPP2_CONTINUE_BREAK(218_5) } + } while (false); ++counter; } CPP2_CONTINUE_BREAK(218_5) }} #line 230 "pure2-break-continue.cpp2" } diff --git a/regression-tests/test-results/pure2-intro-example-hello-2022.cpp b/regression-tests/test-results/pure2-intro-example-hello-2022.cpp index e640dbdf4..ddd0beb98 100644 --- a/regression-tests/test-results/pure2-intro-example-hello-2022.cpp +++ b/regression-tests/test-results/pure2-intro-example-hello-2022.cpp @@ -30,10 +30,11 @@ auto println(auto const& x, auto const& len) -> void; "hello", "2022"}; std::span view {vec}; - for ( auto&& cpp2_range = view; auto& str : cpp2_range ) { + { auto&& cpp2_range = view; for ( auto& str : cpp2_range ) { auto len {decorate(str)}; println(str, len); - } + }} +#line 10 "pure2-intro-example-hello-2022.cpp2" } [[nodiscard]] auto decorate(auto& thing) -> int{ diff --git a/regression-tests/test-results/pure2-intro-example-three-loops.cpp b/regression-tests/test-results/pure2-intro-example-three-loops.cpp index 02b13bfce..1a6760655 100644 --- a/regression-tests/test-results/pure2-intro-example-three-loops.cpp +++ b/regression-tests/test-results/pure2-intro-example-three-loops.cpp @@ -52,9 +52,10 @@ auto decorate_and_print(auto& thing) -> void{ } while ( cpp2::cmp_greater(*cpp2::assert_not_null(i),1) && [&]{ --*cpp2::assert_not_null(i) ; return true; }() ); std::cout << "\n"; - for ( auto&& cpp2_range = words; auto& word : cpp2_range ) - decorate_and_print(word); + { auto&& cpp2_range = words; for ( auto& word : cpp2_range ) + decorate_and_print(word);} +#line 28 "pure2-intro-example-three-loops.cpp2" print(std::string{"end of program"}); } diff --git a/regression-tests/test-results/pure2-statement-scope-parameters.cpp b/regression-tests/test-results/pure2-statement-scope-parameters.cpp index 91434b829..70801aefb 100644 --- a/regression-tests/test-results/pure2-statement-scope-parameters.cpp +++ b/regression-tests/test-results/pure2-statement-scope-parameters.cpp @@ -28,9 +28,9 @@ auto const& i = local_int; // 'in' (read-only) statement scope variable #line 6 "pure2-statement-scope-parameters.cpp2" - for ( auto const& cpp2_range = args; auto const& arg : cpp2_range ) { + { auto const& cpp2_range = args; for ( auto const& arg : cpp2_range ) { std::cout << i << "\n"; // prints 42 - } + }} } { auto& i = local_int; diff --git a/regression-tests/test-results/pure2-type-and-namespace-aliases.cpp b/regression-tests/test-results/pure2-type-and-namespace-aliases.cpp index 036e4bd96..dd74f40c0 100644 --- a/regression-tests/test-results/pure2-type-and-namespace-aliases.cpp +++ b/regression-tests/test-results/pure2-type-and-namespace-aliases.cpp @@ -61,8 +61,9 @@ auto myfunc() -> void{ auto const& v2 = std::move(v); - for ( auto const& cpp2_range = v2; auto const& s : cpp2_range ) - std::cout << cpp2::to_string(s) + "\n"; + { auto const& cpp2_range = v2; for ( auto const& s : cpp2_range ) + std::cout << cpp2::to_string(s) + "\n";} +#line 24 "pure2-type-and-namespace-aliases.cpp2" } auto main() -> int{ diff --git a/regression-tests/test-results/version b/regression-tests/test-results/version index 44889af33..b57bca7d8 100644 --- a/regression-tests/test-results/version +++ b/regression-tests/test-results/version @@ -1,5 +1,5 @@ -cppfront compiler v0.2.1 Build 8505:0902 +cppfront compiler v0.2.1 Build 8506:1618 Copyright(c) Herb Sutter All rights reserved SPDX-License-Identifier: CC-BY-NC-ND-4.0 diff --git a/source/cppfront.cpp b/source/cppfront.cpp index 46080d24c..d8893b917 100644 --- a/source/cppfront.cpp +++ b/source/cppfront.cpp @@ -2116,14 +2116,18 @@ class cppfront && n.body ); + // Note: This used to emit cpp2_range as a range-for-loop scope variable, + // but some major compilers seem to have random troubles with that; + // the workaround to avoid their bugs for now is to emit a { } block + // around the Cpp1 range-for and make the scope variable a normal local if (n.for_with_in) { - printer.print_cpp2("for ( auto const& cpp2_range = ", n.position()); + printer.print_cpp2("{ auto const& cpp2_range = ", n.position()); } else { - printer.print_cpp2("for ( auto&& cpp2_range = ", n.position()); + printer.print_cpp2("{ auto&& cpp2_range = ", n.position()); } emit(*n.range); - printer.print_cpp2("; ", n.position()); + printer.print_cpp2("; for ( ", n.position()); emit(*n.parameter); printer.print_cpp2(" : cpp2_range ) ", n.position()); if (!labelname.empty()) { @@ -2151,6 +2155,8 @@ class cppfront printer.print_extra(" CPP2_CONTINUE_BREAK("+labelname+") }"); } + printer.print_extra("}"); + in_non_rvalue_context.pop_back(); } @@ -5410,7 +5416,7 @@ class cppfront { printer.print_cpp2( prefix, n.position() ); printer.print_cpp2( "auto " + type_qualification_if_any_for(n), n.position() ); - printer.print_cpp2( *n.name(), n.identifier->position() ); + emit( *n.name() ); emit( *func, n.name(), is_main, false, suffix1 ); printer.print_cpp2( suffix2, n.position() ); }