Skip to content

Commit 8a93460

Browse files
committed
Re-enable move from last use in initializers, closes #749
I was trying out suppressing move-from-last-use if the last use was in an initializer, because of examples like `view: std::span = local_variable;` when that was the last use of `local_variable`, which is an error for an rvalue. But that also suppresses a righteous move in examples like `result := local_variable;`. So I now agree those should be treated as rvalues too, and if that causes an error for an rvalue because there is no further us of that variable in scope, then the programmer must write an explicit discard of the variable.
1 parent d34baf4 commit 8a93460

17 files changed

+46
-67
lines changed

regression-tests/mixed-function-expression-and-std-for-each.cpp2

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,24 +7,21 @@
77
main: () -> int = {
88
vec: std::vector<std::string>
99
= ("hello", "2022");
10-
view: std::span = vec;
1110

1211
// Passing a function expression
13-
std::for_each(
14-
view.begin(),
15-
view.end(),
16-
:(inout x) = x += "-ish";
12+
std::ranges::for_each(
13+
vec,
14+
:(inout x) = x += "-ish"
1715
);
1816

1917
// Initializing from a function expression
2018
callback := :(inout x) = x += " maybe";
21-
std::for_each(
22-
view.begin(),
23-
view.end(),
19+
std::ranges::for_each(
20+
vec,
2421
callback
2522
);
2623

27-
for view do (str) {
24+
for vec do (str) {
2825
std::cout << str << "\n";
2926
}
3027
}

regression-tests/mixed-function-expression-and-std-ranges-for-each-with-capture.cpp2

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,14 @@
88
main: () -> int = {
99
vec: std::vector<std::string>
1010
= ("hello", "2022");
11-
view: std::span = vec;
1211

1312
y := "\n";
1413
std::ranges::for_each
15-
( view, :(x) = std::cout << x << y$ );
14+
( vec, :(x) = std::cout << x << y$ );
1615

1716
callback := :(inout x) = x += "-ish";
18-
std::ranges::for_each( view, callback );
17+
std::ranges::for_each( vec, callback );
1918

20-
for view do (str)
19+
for vec do (str)
2120
std::cout << str << "\n";
2221
}

regression-tests/mixed-function-expression-and-std-ranges-for-each.cpp2

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,13 @@
88
main: () -> int = {
99
vec: std::vector<std::string>
1010
= ("hello", "2022");
11-
view: std::span = vec;
1211

1312
std::ranges::for_each
14-
( view, :(x) = std::cout << x << "\n" );
13+
( vec, :(x) = std::cout << x << "\n" );
1514

1615
callback := :(inout x) = x += "-ish";
17-
std::ranges::for_each( view, callback );
16+
std::ranges::for_each( vec, callback );
1817

19-
for view do (str)
18+
for vec do (str)
2019
std::cout << str << "\n";
2120
}

regression-tests/mixed-function-expression-with-pointer-capture.cpp2

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,15 @@
88
main: () -> int = {
99
vec: std::vector<std::string>
1010
= ("hello", "2023");
11-
view: std::span = vec;
1211

1312
y: std::string = "\n";
14-
std::ranges::for_each( view, :(x) =
15-
std::cout << y&$*.c_str() << x << y&$*;
13+
std::ranges::for_each( vec, :(x) =
14+
std::cout << y&$*.c_str() << x << y&$*
1615
);
1716

1817
callback := :(inout x) = x += "-ish";
19-
std::ranges::for_each( view, callback );
18+
std::ranges::for_each( vec, callback );
2019

21-
for view do (str)
20+
for vec do (str)
2221
std::cout << str << "\n";
2322
}

regression-tests/mixed-function-expression-with-repeated-capture.cpp2

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,14 @@
88
main: () -> int = {
99
vec: std::vector<std::string>
1010
= ("hello", "2022");
11-
view: std::span = vec;
1211

1312
y := "\n";
1413
std::ranges::for_each
15-
( view, :(x) = std::cout << y$ << x << y$ );
14+
( vec, :(x) = std::cout << y$ << x << y$ );
1615

1716
callback := :(inout x) = x += "-ish";
18-
std::ranges::for_each( view, callback );
17+
std::ranges::for_each( vec, callback );
1918

20-
for view do (str)
19+
for vec do (str)
2120
std::cout << str << "\n";
2221
}

regression-tests/pure2-bounds-safety-span.cpp2

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ main: () -> int
44
words: std::vector<std::string> = ( "decorated", "hello", "world" );
55

66
s: std::span<std::string> = words;
7+
_ = words;
78

89
i := 0;
910
while i < s.ssize() next i++ {

regression-tests/pure2-intro-example-hello-2022.cpp2

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
main: () -> int = {
22
vec: std::vector<std::string>
33
= ("hello", "2022");
4-
view: std::span = vec;
54

6-
for view do (inout str) {
5+
for vec do (inout str) {
76
len := decorate(str);
87
print_it(str, len);
98
}

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

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,24 +26,21 @@
2626
[[nodiscard]] auto main() -> int{
2727
std::vector<std::string> vec {
2828
"hello", "2022"};
29-
std::span view {vec};
3029

3130
// Passing a function expression
32-
std::for_each(
33-
CPP2_UFCS_0(begin, view),
34-
CPP2_UFCS_0(end, view),
31+
std::ranges::for_each(
32+
vec,
3533
[](auto& x) -> void { x += "-ish"; }
3634
);
3735

3836
// Initializing from a function expression
3937
auto callback {[](auto& x) -> void { x += " maybe"; }};
40-
std::for_each(
41-
CPP2_UFCS_0(begin, view),
42-
CPP2_UFCS_0(end, view),
38+
std::ranges::for_each(
39+
vec,
4340
std::move(callback)
4441
);
4542

46-
for ( auto const& str : view ) {
43+
for ( auto const& str : vec ) {
4744
std::cout << str << "\n";
4845
}
4946
}

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

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,16 +27,15 @@
2727
[[nodiscard]] auto main() -> int{
2828
std::vector<std::string> vec {
2929
"hello", "2022"};
30-
std::span view {vec};
3130

3231
auto y {"\n"};
3332
std::ranges::for_each
34-
(view, [_0 = std::move(y)](auto const& x) -> void { std::cout << x << _0; });
33+
(vec, [_0 = std::move(y)](auto const& x) -> void { std::cout << x << _0; });
3534

3635
auto callback {[](auto& x) -> void { x += "-ish"; }};
37-
std::ranges::for_each(view, std::move(callback));
36+
std::ranges::for_each(vec, std::move(callback));
3837

39-
for ( auto const& str : view )
38+
for ( auto const& str : vec )
4039
std::cout << str << "\n";
4140
}
4241

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

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,15 +27,14 @@
2727
[[nodiscard]] auto main() -> int{
2828
std::vector<std::string> vec {
2929
"hello", "2022"};
30-
std::span view {vec};
3130

3231
std::ranges::for_each
33-
(view, [](auto const& x) -> void { std::cout << x << "\n"; });
32+
(vec, [](auto const& x) -> void { std::cout << x << "\n"; });
3433

3534
auto callback {[](auto& x) -> void { x += "-ish"; }};
36-
std::ranges::for_each(view, std::move(callback));
35+
std::ranges::for_each(vec, std::move(callback));
3736

38-
for ( auto const& str : view )
37+
for ( auto const& str : vec )
3938
std::cout << str << "\n";
4039
}
4140

0 commit comments

Comments
 (0)