Skip to content

Commit 2e23597

Browse files
authored
Fix default argument lowering (#1262)
There should now be enough test cases to exercise all the places you can put default arguments Also, only emit Cpp1 lambdas as 'mutable' if there are captures (when it matters) so that pure function expressions are not 'mutable' Closes #1235 Signed-off-by: Herb Sutter <[email protected]>
1 parent c599f41 commit 2e23597

File tree

48 files changed

+302
-156
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

48 files changed

+302
-156
lines changed

regression-tests/pure2-default-arguments.cpp2

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,19 +11,52 @@ my_function_name: (
1111

1212
f: (x: i32 = 0) = { std::cout << x; }
1313

14+
combine_maps:
15+
<
16+
AssocContainer,
17+
Func: type = std::plus<>
18+
>
19+
(
20+
inout map1: AssocContainer,
21+
map2: AssocContainer,
22+
func: Func = ()
23+
)
24+
= {
25+
for map2 do(kv) {
26+
map1[kv.first] = func(map1[kv.first], kv.second);
27+
}
28+
}
29+
30+
myclass: <T: type = int, N: int = 42> type = {
31+
memfunc: <TT: type = int, NN: int = 42> (MM: int = 43) = { _ = MM; }
32+
}
33+
myfunc: <T: type = int, N: int = 42> (M: int = 43) = {
34+
_ = M;
35+
: <TT: type = int, NN: int = 42> (MM: int = 43) = { _ = MM; };
36+
}
37+
1438
main: (args) = {
1539
my_function_name();
1640
f();
1741
f(1);
1842
f(2);
1943

20-
: <V: bool = gcc_clang_msvc_min_versions( 1400, 1600, 1920)> () = {
44+
: <V: bool = gcc_clang_msvc_min_versions( 1400, 1600, 1920 )> () = {
2145
if constexpr V {
22-
std::cout << "a newer compiler\n";
46+
std::cout << "\na newer compiler\n";
2347
}
2448
else {
25-
std::cout << "an older compiler\n";
49+
std::cout << "\nan older compiler\n";
2650
}
2751
} ();
2852

53+
m1: std::map<int, int> = ();
54+
m1[1] = 11;
55+
56+
m2: std::map<int, int> = ();
57+
m2[1] = 22;
58+
59+
combine_maps( m1, m2, :(x,y) x+y+33 );
60+
61+
std::cout << "(m1.size())$, (m2.size())$, (m1[1])$\n";
2962
}

regression-tests/test-results/clang-12-c++20/mixed-bugfix-for-ufcs-non-local.cpp.output

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ mixed-bugfix-for-ufcs-non-local.cpp2:27:29: error: a lambda expression cannot ap
107107
#define CPP2_UFCS_(LAMBDADEFCAPT,SFINAE,MVFWD,QUALID,TEMPKW,...) \
108108
^
109109
mixed-bugfix-for-ufcs-non-local.cpp2:41:84: error: lambda expression in an unevaluated operand
110-
inline CPP2_CONSTEXPR bool u::c{ [](cpp2::impl::in<std::type_identity_t<decltype(CPP2_UFCS_NONLOCAL(f)(o))>> x) mutable -> auto { return x; }(true) };// Fails on Clang 12 (lambda in unevaluated context).
110+
inline CPP2_CONSTEXPR bool u::c{ [](cpp2::impl::in<std::type_identity_t<decltype(CPP2_UFCS_NONLOCAL(f)(o))>> x) -> auto { return x; }(true) };// Fails on Clang 12 (lambda in unevaluated context).
111111
^
112112
../../../include/cpp2util.h:2137:59: note: expanded from macro 'CPP2_UFCS_NONLOCAL'
113113
#define CPP2_UFCS_NONLOCAL(...) CPP2_UFCS_(,CPP2_UFCS_IDENTITY,CPP2_UFCS_IDENTITY,(),,__VA_ARGS__)
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
pure2-bugfix-for-non-local-function-expression.cpp2:5:34: error: lambda expression in an unevaluated operand
2-
template<typename T> concept v = []() mutable -> bool { return true; }();
2+
template<typename T> concept v = []() -> bool { return true; }();
33
^
44
pure2-bugfix-for-non-local-function-expression.cpp2:7:41: error: lambda expression in an unevaluated operand
5-
using u = std::type_identity_t<decltype([]() mutable -> void{})>;
5+
using u = std::type_identity_t<decltype([]() -> void{})>;
66
^
77
pure2-bugfix-for-non-local-function-expression.cpp2:9:47: error: lambda expression in an unevaluated operand
8-
class t: public std::type_identity_t<decltype([]() mutable -> void{})> {
8+
class t: public std::type_identity_t<decltype([]() -> void{})> {
99
^
1010
3 errors generated.
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
pure2-last-use.cpp2:945:44: error: a lambda expression cannot appear in this context
2-
static_cast<void>([_0 = std::array<int,[](auto const& x) mutable -> auto { return identity(x); }(0)>()]() mutable -> auto { return _0; });// Fails on Clang 12 (lambda in unevaluated context).
2+
static_cast<void>([_0 = std::array<int,[](auto const& x) -> auto { return identity(x); }(0)>()]() mutable -> auto { return _0; });// Fails on Clang 12 (lambda in unevaluated context).
33
^
44
1 error generated.
Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
11
calling: int main(int, char**)
2-
012an older compiler
2+
012
3+
a newer compiler
4+
1, 1, 66

regression-tests/test-results/mixed-bugfix-for-ufcs-non-local.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ auto g() -> void{
9595

9696
#line 40 "mixed-bugfix-for-ufcs-non-local.cpp2"
9797
inline CPP2_CONSTEXPR bool u::b{ CPP2_UFCS_NONLOCAL(f)(o) };
98-
inline CPP2_CONSTEXPR bool u::c{ [](cpp2::impl::in<std::type_identity_t<decltype(CPP2_UFCS_NONLOCAL(f)(o))>> x) mutable -> auto { return x; }(true) };// Fails on Clang 12 (lambda in unevaluated context).
98+
inline CPP2_CONSTEXPR bool u::c{ [](cpp2::impl::in<std::type_identity_t<decltype(CPP2_UFCS_NONLOCAL(f)(o))>> x) -> auto { return x; }(true) };// Fails on Clang 12 (lambda in unevaluated context).
9999
auto u::g(auto const& s, auto const& sz) -> void{
100100
if (cpp2::cpp2_default.is_active() && !(CPP2_UFCS(sz)(s) != 0) ) { cpp2::cpp2_default.report_violation(""); }}
101101

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,11 @@
3232
// Passing a function expression
3333
std::ranges::for_each(
3434
vec,
35-
[](auto& x) mutable -> void { x += "-ish"; }
35+
[](auto& x) -> void { x += "-ish"; }
3636
);
3737

3838
// Initializing from a function expression
39-
auto callback {[](auto& x) mutable -> void { x += " maybe"; }};
39+
auto callback {[](auto& x) -> void { x += " maybe"; }};
4040
std::ranges::for_each(
4141
vec,
4242
cpp2::move(callback)

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
std::ranges::for_each
3535
(vec, [_0 = cpp2::move(y)](auto const& x) mutable -> void { std::cout << x << _0; });
3636

37-
auto callback {[](auto& x) mutable -> void { x += "-ish"; }};
37+
auto callback {[](auto& x) -> void { x += "-ish"; }};
3838
std::ranges::for_each(vec, cpp2::move(callback));
3939

4040
for ( auto const& str : cpp2::move(vec) )

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,9 @@
3131
"hello", "2022"};
3232

3333
std::ranges::for_each
34-
(vec, [](auto const& x) mutable -> void { std::cout << x << "\n"; });
34+
(vec, [](auto const& x) -> void { std::cout << x << "\n"; });
3535

36-
auto callback {[](auto& x) mutable -> void { x += "-ish"; }};
36+
auto callback {[](auto& x) -> void { x += "-ish"; }};
3737
std::ranges::for_each(vec, cpp2::move(callback));
3838

3939
for ( auto const& str : cpp2::move(vec) )

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
std::cout << CPP2_UFCS(c_str)((*cpp2::impl::assert_not_null(_0))) << x << *cpp2::impl::assert_not_null(_0); }
3636
);
3737

38-
auto callback {[](auto& x) mutable -> void { x += "-ish"; }};
38+
auto callback {[](auto& x) -> void { x += "-ish"; }};
3939
std::ranges::for_each(vec, cpp2::move(callback));
4040

4141
for ( auto const& str : cpp2::move(vec) )

0 commit comments

Comments
 (0)