Skip to content

Commit de630b9

Browse files
committed
When a for loop variable is in, declare the use of the range as_const too, see #226
1 parent 71842fc commit de630b9

11 files changed

+28
-16
lines changed

regression-tests/test-results/mixed-bounds-safety-with-assert-2.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ auto add_42_to_subrange(auto& rng, cpp2::in<int> start, cpp2::in<int> end) -> vo
2020
std::vector<int> v {1, 2, 3, 4, 5};
2121
add_42_to_subrange(v, 1, 3);
2222

23-
for ( auto&& cpp2_range = v; auto const& i : cpp2_range )
23+
for ( auto&& cpp2_range = std::as_const(v); auto const& i : cpp2_range )
2424
std::cout << i << "\n";
2525
}
2626

regression-tests/test-results/mixed-bounds-safety-with-assert.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ auto print_subrange(auto const& rng, cpp2::in<int> start, cpp2::in<int> end) ->
2626
cpp2::Bounds.expects(cpp2::cmp_less_eq(end,CPP2_UFCS_0(ssize, rng)), "");
2727

2828
auto count {0};
29-
for ( auto&& cpp2_range = rng;
29+
for ( auto&& cpp2_range = std::as_const(rng);
3030

3131
auto const& i : cpp2_range ) { do
3232
if (cpp2::cmp_less_eq(start,count) && cpp2::cmp_less_eq(count,end)) {

regression-tests/test-results/mixed-function-expression-and-std-for-each.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
std::move(callback)
3535
);
3636

37-
for ( auto&& cpp2_range = view; auto const& str : cpp2_range ) {
37+
for ( auto&& cpp2_range = std::as_const(view); auto const& str : cpp2_range ) {
3838
std::cout << str << "\n";
3939
}
4040
}

regression-tests/test-results/mixed-function-expression-and-std-ranges-for-each-with-capture.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
auto callback {[](auto& x) { return x += "-ish"; }};
2828
std::ranges::for_each( view, std::move(callback));
2929

30-
for ( auto&& cpp2_range = view; auto const& str : cpp2_range )
30+
for ( auto&& cpp2_range = std::as_const(view); auto const& str : cpp2_range )
3131
std::cout << str << "\n";
3232
}
3333

regression-tests/test-results/mixed-function-expression-and-std-ranges-for-each.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
auto callback {[](auto& x) { return x += "-ish"; }};
2727
std::ranges::for_each( view, std::move(callback));
2828

29-
for ( auto&& cpp2_range = view; auto const& str : cpp2_range )
29+
for ( auto&& cpp2_range = std::as_const(view); auto const& str : cpp2_range )
3030
std::cout << str << "\n";
3131
}
3232

regression-tests/test-results/mixed-function-expression-with-pointer-capture.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
auto callback {[](auto& x) { return x += "-ish"; }};
2929
std::ranges::for_each( view, std::move(callback));
3030

31-
for ( auto&& cpp2_range = view; auto const& str : cpp2_range )
31+
for ( auto&& cpp2_range = std::as_const(view); auto const& str : cpp2_range )
3232
std::cout << str << "\n";
3333
}
3434

regression-tests/test-results/mixed-function-expression-with-repeated-capture.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
auto callback {[](auto& x) { return x += "-ish"; }};
2828
std::ranges::for_each( view, std::move(callback));
2929

30-
for ( auto&& cpp2_range = view; auto const& str : cpp2_range )
30+
for ( auto&& cpp2_range = std::as_const(view); auto const& str : cpp2_range )
3131
std::cout << str << "\n";
3232
}
3333

regression-tests/test-results/mixed-intro-for-with-counter-include-last.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
{
1919
std::vector<int> v {1, 2, 3, 4, 5};
2020
auto counter {42};
21-
for ( auto&& cpp2_range = v; auto const& i : cpp2_range ) { do {
21+
for ( auto&& cpp2_range = std::as_const(v); auto const& i : cpp2_range ) { do {
2222
std::cout << i << " " << counter << "\n";
2323
} while (false); counter *= 2; }
2424
}

regression-tests/test-results/pure2-break-continue.cpp

+8-8
Original file line numberDiff line numberDiff line change
@@ -196,9 +196,9 @@ auto for_continue_inner() -> void
196196
{
197197
std::vector vi {0, 1, 2};
198198
auto counter {0};
199-
for ( auto&& cpp2_range = vi; auto const& i : cpp2_range ) { do {
199+
for ( auto&& cpp2_range = std::as_const(vi); auto const& i : cpp2_range ) { do {
200200
std::vector vj {0, 1, 2};
201-
for ( auto&& cpp2_range = vj; auto const& j : cpp2_range ) {{
201+
for ( auto&& cpp2_range = std::as_const(vj); auto const& j : cpp2_range ) {{
202202
std::cout << i << j << " ";
203203
if (j == 1) {
204204
goto CONTINUE_166_9;
@@ -214,9 +214,9 @@ auto for_continue_outer() -> void
214214
{
215215
std::vector vi {0, 1, 2};
216216
auto counter {0};
217-
for ( auto&& cpp2_range = vi; auto const& i : cpp2_range ) { { do {
217+
for ( auto&& cpp2_range = std::as_const(vi); auto const& i : cpp2_range ) { { do {
218218
std::vector vj {0, 1, 2};
219-
for ( auto&& cpp2_range = vj; auto const& j : cpp2_range ) {
219+
for ( auto&& cpp2_range = std::as_const(vj); auto const& j : cpp2_range ) {
220220
std::cout << i << j << " ";
221221
if (j == 1) {
222222
goto CONTINUE_182_5;
@@ -232,9 +232,9 @@ auto for_break_inner() -> void
232232
{
233233
std::vector vi {0, 1, 2};
234234
auto counter {0};
235-
for ( auto&& cpp2_range = vi; auto const& i : cpp2_range ) { do {
235+
for ( auto&& cpp2_range = std::as_const(vi); auto const& i : cpp2_range ) { do {
236236
std::vector vj {0, 1, 2};
237-
for ( auto&& cpp2_range = vj; auto const& j : cpp2_range ) {{
237+
for ( auto&& cpp2_range = std::as_const(vj); auto const& j : cpp2_range ) {{
238238
std::cout << i << j << " ";
239239
if (j == 1) {
240240
goto BREAK_202_9;
@@ -250,9 +250,9 @@ auto for_break_outer() -> void
250250
{
251251
std::vector vi {0, 1, 2};
252252
auto counter {0};
253-
for ( auto&& cpp2_range = vi; auto const& i : cpp2_range ) { { do {
253+
for ( auto&& cpp2_range = std::as_const(vi); auto const& i : cpp2_range ) { { do {
254254
std::vector vj {0, 1, 2};
255-
for ( auto&& cpp2_range = vj; auto const& j : cpp2_range ) {
255+
for ( auto&& cpp2_range = std::as_const(vj); auto const& j : cpp2_range ) {
256256
std::cout << i << j << " ";
257257
if (j == 1) {
258258
goto BREAK_218_5;

source/cppfront.cpp

+6
Original file line numberDiff line numberDiff line change
@@ -1545,7 +1545,13 @@ class cppfront
15451545
assert(!n.condition && !n.statement && n.range && n.body);
15461546

15471547
printer.print_cpp2("for ( auto&& cpp2_range = ", n.position());
1548+
if (n.for_with_in) {
1549+
printer.print_cpp2("std::as_const(", n.position());
1550+
}
15481551
emit(*n.range);
1552+
if (n.for_with_in) {
1553+
printer.print_cpp2(")", n.position());
1554+
}
15491555
printer.print_cpp2("; ", n.position());
15501556
emit(*n.get_for_parameter());
15511557
printer.print_cpp2(" : cpp2_range ) ", n.position());

source/parse.h

+6
Original file line numberDiff line numberDiff line change
@@ -765,6 +765,7 @@ struct iteration_statement_node
765765
std::unique_ptr<compound_statement_node> statement; // used for "do" and "while", else null
766766
std::unique_ptr<expression_node> range; // used for "for", else null
767767
std::unique_ptr<declaration_node> body; // used for "for", else null
768+
bool for_with_in = false;// usse for "for," says whether loop variable is 'in'
768769

769770
auto get_for_parameter() const -> parameter_declaration_node const*;
770771

@@ -2709,6 +2710,11 @@ class parser
27092710
error("for..do loop body must be an unnamed function taking a single parameter and returning nothing", false);
27102711
return {};
27112712
}
2713+
// else
2714+
assert(func && *func);
2715+
if ((**func).parameters->parameters.front()->pass == passing_style::in) {
2716+
n->for_with_in = true;
2717+
}
27122718

27132719
return n;
27142720
}

0 commit comments

Comments
 (0)