Skip to content

Commit 76c2941

Browse files
committed
Support _ unnamed type parameters - closes #812
Also require an `operator=` second parameter of the same type to be named `that` - see #475 discussion thread
1 parent 61550a5 commit 76c2941

14 files changed

+95
-56
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ auto add_42_to_subrange(auto& rng, cpp2::in<int> start, cpp2::in<int> end) -> vo
4343
auto count {0};
4444
for (
4545

46-
auto& i : rng ) { do
46+
auto& i : rng ) { do
4747
if ([_0 = start, _1 = count, _2 = end]{ return cpp2::cmp_less_eq(_0,_1) && cpp2::cmp_less_eq(_1,_2); }()) {
4848
i += 42;
4949
} while (false); ++count; }

regression-tests/test-results/mixed-forwarding.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@ struct X {
2020
};
2121

2222
#line 11 "mixed-forwarding.cpp2"
23-
auto copy_from([[maybe_unused]] auto param1) -> void;
23+
auto copy_from([[maybe_unused]] auto unnamed_param_1) -> void;
2424

25-
auto use([[maybe_unused]] auto const& param1) -> void;
25+
auto use([[maybe_unused]] auto const& unnamed_param_1) -> void;
2626

2727
// invoking each of these with an rvalue std::pair argument ...
2828
auto apply_implicit_forward(auto&& t) -> void
@@ -46,9 +46,9 @@ CPP2_REQUIRES (std::is_same_v<CPP2_TYPEOF(t), std::pair<X,X>>)
4646

4747

4848
#line 11 "mixed-forwarding.cpp2"
49-
auto copy_from([[maybe_unused]] auto param1) -> void{}
49+
auto copy_from([[maybe_unused]] auto unnamed_param_1) -> void{}
5050

51-
auto use([[maybe_unused]] auto const& param1) -> void{}
51+
auto use([[maybe_unused]] auto const& unnamed_param_1) -> void{}
5252

5353
#line 16 "mixed-forwarding.cpp2"
5454
auto apply_implicit_forward(auto&& t) -> void

regression-tests/test-results/mixed-parameter-passing-with-forward.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,12 @@
1515
#include <ctime>
1616

1717
#line 6 "mixed-parameter-passing-with-forward.cpp2"
18-
auto copy_from([[maybe_unused]] auto param1) -> void;
18+
auto copy_from([[maybe_unused]] auto unnamed_param_1) -> void;
1919

2020
auto parameter_styles(
21-
[[maybe_unused]] cpp2::in<std::string> param1, // "in" is default
21+
[[maybe_unused]] cpp2::in<std::string> unnamed_param_1, // "in" is default
2222
std::string b,
23-
[[maybe_unused]] std::string& param3,
23+
[[maybe_unused]] std::string& unnamed_param_3,
2424
std::string&& d,
2525
auto&& e
2626
) -> void
@@ -36,12 +36,12 @@ CPP2_REQUIRES (std::is_same_v<CPP2_TYPEOF(e), std::string>)
3636

3737

3838
#line 6 "mixed-parameter-passing-with-forward.cpp2"
39-
auto copy_from([[maybe_unused]] auto param1) -> void{}
39+
auto copy_from([[maybe_unused]] auto unnamed_param_1) -> void{}
4040

4141
auto parameter_styles(
42-
[[maybe_unused]] cpp2::in<std::string> param1,
42+
[[maybe_unused]] cpp2::in<std::string> unnamed_param_1,
4343
std::string b,
44-
[[maybe_unused]] std::string& param3,
44+
[[maybe_unused]] std::string& unnamed_param_3,
4545
std::string&& d,
4646
auto&& e
4747
) -> void

regression-tests/test-results/mixed-parameter-passing.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,12 @@
1515
#include <ctime>
1616

1717
#line 6 "mixed-parameter-passing.cpp2"
18-
auto copy_from([[maybe_unused]] auto param1) -> void;
18+
auto copy_from([[maybe_unused]] auto unnamed_param_1) -> void;
1919

2020
auto parameter_styles(
21-
[[maybe_unused]] cpp2::in<std::string> param1, // "in" is default
21+
[[maybe_unused]] cpp2::in<std::string> unnamed_param_1, // "in" is default
2222
std::string b,
23-
[[maybe_unused]] std::string& param3,
23+
[[maybe_unused]] std::string& unnamed_param_3,
2424
std::string&& d
2525
) -> void;
2626

@@ -32,12 +32,12 @@ auto parameter_styles(
3232

3333

3434
#line 6 "mixed-parameter-passing.cpp2"
35-
auto copy_from([[maybe_unused]] auto param1) -> void{}
35+
auto copy_from([[maybe_unused]] auto unnamed_param_1) -> void{}
3636

3737
auto parameter_styles(
38-
[[maybe_unused]] cpp2::in<std::string> param1,
38+
[[maybe_unused]] cpp2::in<std::string> unnamed_param_1,
3939
std::string b,
40-
[[maybe_unused]] std::string& param3,
40+
[[maybe_unused]] std::string& unnamed_param_3,
4141
std::string&& d
4242
) -> void
4343
{

regression-tests/test-results/mixed-postfix-expression-custom-formatting.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212

1313
#line 2 "mixed-postfix-expression-custom-formatting.cpp2"
14-
auto call([[maybe_unused]] auto const& param1, [[maybe_unused]] auto const& param2, [[maybe_unused]] auto const& param3, [[maybe_unused]] auto const& param4, [[maybe_unused]] auto const& param5) -> void;
14+
auto call([[maybe_unused]] auto const& unnamed_param_1, [[maybe_unused]] auto const& unnamed_param_2, [[maybe_unused]] auto const& unnamed_param_3, [[maybe_unused]] auto const& unnamed_param_4, [[maybe_unused]] auto const& unnamed_param_5) -> void;
1515

1616
[[nodiscard]] auto test(auto const& a) -> std::string;
1717

@@ -24,7 +24,7 @@ auto call([[maybe_unused]] auto const& param1, [[maybe_unused]] auto const& para
2424

2525

2626
#line 2 "mixed-postfix-expression-custom-formatting.cpp2"
27-
auto call([[maybe_unused]] auto const& param1, [[maybe_unused]] auto const& param2, [[maybe_unused]] auto const& param3, [[maybe_unused]] auto const& param4, [[maybe_unused]] auto const& param5) -> void{}
27+
auto call([[maybe_unused]] auto const& unnamed_param_1, [[maybe_unused]] auto const& unnamed_param_2, [[maybe_unused]] auto const& unnamed_param_3, [[maybe_unused]] auto const& unnamed_param_4, [[maybe_unused]] auto const& unnamed_param_5) -> void{}
2828

2929
[[nodiscard]] auto test(auto const& a) -> std::string{
3030
return call(a,

regression-tests/test-results/pure2-bugfix-for-memberwise-base-assignment.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,9 @@ class Base {
2727

2828
#line 3 "pure2-bugfix-for-memberwise-base-assignment.cpp2"
2929
public: auto operator=([[maybe_unused]] Base&& that) noexcept -> Base& ;
30-
public: Base([[maybe_unused]] auto const& param2);
30+
public: Base([[maybe_unused]] auto const& unnamed_param_2);
3131
#line 4 "pure2-bugfix-for-memberwise-base-assignment.cpp2"
32-
public: auto operator=([[maybe_unused]] auto const& param2) -> Base& ;
32+
public: auto operator=([[maybe_unused]] auto const& unnamed_param_2) -> Base& ;
3333
};
3434

3535
class Derived: public Base {
@@ -60,9 +60,9 @@ auto main() -> int;
6060
return *this;
6161
#line 3 "pure2-bugfix-for-memberwise-base-assignment.cpp2"
6262
}
63-
Base::Base([[maybe_unused]] auto const& param2) { std::cout << "(implicit out this, _)\n"; }
63+
Base::Base([[maybe_unused]] auto const& unnamed_param_2) { std::cout << "(implicit out this, _)\n"; }
6464
#line 4 "pure2-bugfix-for-memberwise-base-assignment.cpp2"
65-
auto Base::operator=([[maybe_unused]] auto const& param2) -> Base& { std::cout << "(implicit out this, _)\n";
65+
auto Base::operator=([[maybe_unused]] auto const& unnamed_param_2) -> Base& { std::cout << "(implicit out this, _)\n";
6666
return *this;
6767
#line 4 "pure2-bugfix-for-memberwise-base-assignment.cpp2"
6868
}

regression-tests/test-results/pure2-print.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,15 +38,15 @@ CPP2_REQUIRES_ (true)
3838

3939

4040
#line 54 "pure2-print.cpp2"
41-
public: template<typename T> [[nodiscard]] auto values([[maybe_unused]] T const& param2) const& -> values_ret;
41+
public: template<typename T> [[nodiscard]] auto values([[maybe_unused]] T const& unnamed_param_2) const& -> values_ret;
4242

4343

4444
#line 59 "pure2-print.cpp2"
4545
public: explicit mytype();
4646

4747
public: mytype([[maybe_unused]] mytype const& that);
4848

49-
public: mytype([[maybe_unused]] cpp2::in<int> param2);
49+
public: mytype([[maybe_unused]] cpp2::in<int> unnamed_param_2);
5050

5151
public: static auto variadic(auto const& ...x) -> void
5252
CPP2_REQUIRES_ ((std::is_convertible_v<CPP2_TYPEOF(x), int> && ...))
@@ -133,7 +133,7 @@ requires (true)
133133

134134
do {} while ( CPP2_UFCS_0(empty, s) && [&]{ b() ; return true; }() );
135135

136-
for ( [[maybe_unused]] auto const& param1 : m ) {
136+
for ( [[maybe_unused]] auto const& unnamed_param_1 : m ) {
137137
#line 43 "pure2-print.cpp2"
138138
{ do {goto CONTINUE_43_13; } while (false); c(); } CPP2_CONTINUE_BREAK(43_13) }
139139

@@ -147,7 +147,7 @@ requires (true)
147147
return [_0 = (s + cpp2::assert_in_bounds(m, 0))]() -> std::string { return _0; }();
148148
}
149149

150-
template<typename T> [[nodiscard]] auto outer::mytype::values([[maybe_unused]] T const& param2) const& -> values_ret{
150+
template<typename T> [[nodiscard]] auto outer::mytype::values([[maybe_unused]] T const& unnamed_param_2) const& -> values_ret{
151151
cpp2::deferred_init<int> offset;
152152
cpp2::deferred_init<std::string> name;
153153
#line 55 "pure2-print.cpp2"
@@ -159,7 +159,7 @@ requires (true)
159159

160160
outer::mytype::mytype([[maybe_unused]] mytype const& that){}
161161

162-
outer::mytype::mytype([[maybe_unused]] cpp2::in<int> param2){}
162+
outer::mytype::mytype([[maybe_unused]] cpp2::in<int> unnamed_param_2){}
163163

164164
auto outer::mytype::variadic(auto const& ...x) -> void
165165
requires ((std::is_convertible_v<CPP2_TYPEOF(x), int> && ...))

regression-tests/test-results/pure2-types-down-upcast.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@ class B: public A {
3737
#line 11 "pure2-types-down-upcast.cpp2"
3838
};
3939

40-
auto func_mut(A& a) -> void;
41-
auto func_mut(B& b) -> void;
40+
auto func_mut(A& a) -> void;
41+
auto func_mut(B& b) -> void;
4242
auto func_const(cpp2::in<A> a) -> void;
4343
auto func_const(cpp2::in<B> b) -> void;
4444

@@ -69,8 +69,8 @@ auto test_down() -> void;
6969
auto A::mut_foo() & -> void{std::cout << "foo \n"; }
7070

7171
#line 13 "pure2-types-down-upcast.cpp2"
72-
auto func_mut(A& a) -> void {std::cout << "Call A mut: " + cpp2::to_string(a.i) << std::endl;}
73-
auto func_mut(B& b) -> void {std::cout << "Call B mut: " + cpp2::to_string(b.d) << std::endl;}
72+
auto func_mut(A& a) -> void {std::cout << "Call A mut: " + cpp2::to_string(a.i) << std::endl;}
73+
auto func_mut(B& b) -> void {std::cout << "Call B mut: " + cpp2::to_string(b.d) << std::endl;}
7474
auto func_const(cpp2::in<A> a) -> void{std::cout << "Call A const: " + cpp2::to_string(a.i) << std::endl;}
7575
auto func_const(cpp2::in<B> b) -> void{std::cout << "Call B const: " + cpp2::to_string(b.d) << std::endl;}
7676

regression-tests/test-results/pure2-types-inheritance.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public: virtual ~Human() noexcept;
3939

4040
namespace N {
4141
template<int I> class Machine {
42-
public: explicit Machine([[maybe_unused]] cpp2::in<std::string> param2);
42+
public: explicit Machine([[maybe_unused]] cpp2::in<std::string> unnamed_param_2);
4343
public: virtual auto work() const -> void = 0;
4444

4545
public: virtual ~Machine() noexcept;
@@ -97,7 +97,7 @@ auto main() -> int;
9797
#line 6 "pure2-types-inheritance.cpp2"
9898
namespace N {
9999

100-
template <int I> Machine<I>::Machine([[maybe_unused]] cpp2::in<std::string> param2){}
100+
template <int I> Machine<I>::Machine([[maybe_unused]] cpp2::in<std::string> unnamed_param_2){}
101101

102102
template <int I> Machine<I>::~Machine() noexcept{}
103103

regression-tests/test-results/pure2-ufcs-member-access-and-chaining.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515

1616
#line 24 "pure2-ufcs-member-access-and-chaining.cpp2"
17-
auto no_return([[maybe_unused]] auto const& param1) -> void;
17+
auto no_return([[maybe_unused]] auto const& unnamed_param_1) -> void;
1818

1919
[[nodiscard]] auto ufcs(cpp2::in<int> i) -> int;
2020
struct fun_ret { int i; };
@@ -31,7 +31,7 @@ auto no_return([[maybe_unused]] auto const& param1) -> void;
3131

3232
#line 39 "pure2-ufcs-member-access-and-chaining.cpp2"
3333
// And a test for non-local UFCS, which shouldn't do a [&] capture
34-
[[nodiscard]] auto f([[maybe_unused]] auto const& param1) -> int;
34+
[[nodiscard]] auto f([[maybe_unused]] auto const& unnamed_param_1) -> int;
3535
extern int y;
3636

3737
//=== Cpp2 function definitions =================================================
@@ -59,7 +59,7 @@ extern int y;
5959
CPP2_UFCS_0(no_return, 42);
6060
}
6161

62-
auto no_return([[maybe_unused]] auto const& param1) -> void{}
62+
auto no_return([[maybe_unused]] auto const& unnamed_param_1) -> void{}
6363

6464
[[nodiscard]] auto ufcs(cpp2::in<int> i) -> int{
6565
return i + 2;
@@ -77,6 +77,6 @@ auto no_return([[maybe_unused]] auto const& param1) -> void{}
7777
}
7878

7979
#line 40 "pure2-ufcs-member-access-and-chaining.cpp2"
80-
[[nodiscard]] auto f([[maybe_unused]] auto const& param1) -> int { return 0; }
80+
[[nodiscard]] auto f([[maybe_unused]] auto const& unnamed_param_1) -> int { return 0; }
8181
int y {CPP2_UFCS_0_NONLOCAL(f, 0)};
8282

regression-tests/test-results/version

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11

2-
cppfront compiler v0.3.0 Build 8B07:1638
2+
cppfront compiler v0.3.0 Build 8B09:1419
33
Copyright(c) Herb Sutter All rights reserved
44

55
SPDX-License-Identifier: CC-BY-NC-ND-4.0

source/build.info

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
"8B07:1638"
1+
"8B09:1419"

source/sema.h

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1000,6 +1000,34 @@ class sema
10001000
}
10011001

10021002

1003+
auto check(parameter_declaration_node const& n)
1004+
-> bool
1005+
{
1006+
auto type_name = std::string{};
1007+
if (n.declaration->has_declared_return_type()) {
1008+
type_name = n.declaration->get_object_type()->to_string();
1009+
}
1010+
1011+
if (
1012+
n.ordinal == 2
1013+
&& !n.has_name("that")
1014+
&& n.declaration->parent_declaration
1015+
&& n.declaration->parent_declaration->has_name("operator=")
1016+
&& n.declaration->parent_declaration->parent_declaration
1017+
&& n.declaration->parent_declaration->parent_declaration->name()
1018+
&& type_name == *n.declaration->parent_declaration->parent_declaration->name()
1019+
)
1020+
{
1021+
errors.emplace_back(
1022+
n.position(),
1023+
"if an 'operator=' second parameter is of the same type (here '" + type_name + "'), it must be named 'that'"
1024+
);
1025+
return false;
1026+
}
1027+
1028+
return true;
1029+
}
1030+
10031031
auto check(declaration_node const& n)
10041032
-> bool
10051033
{

0 commit comments

Comments
 (0)