Skip to content

Commit f2f70ed

Browse files
committed
Add warnings for names reserved for metafunction implementations
Closes #720
1 parent 0b380c5 commit f2f70ed

15 files changed

+297
-207
lines changed

include/cpp2util.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,7 @@
260260

261261
#define CPP2_TYPEOF(x) std::remove_cvref_t<decltype(x)>
262262
#define CPP2_FORWARD(x) std::forward<decltype(x)>(x)
263+
#define CPP2_PACK_EMPTY(x) (sizeof...(x) == 0)
263264
#define CPP2_CONTINUE_BREAK(NAME) goto CONTINUE_##NAME; CONTINUE_##NAME: continue; goto BREAK_##NAME; BREAK_##NAME: break;
264265
// these redundant goto's to avoid 'unused label' warnings
265266

regression-tests/pure2-print.cpp2

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ outer: @print type = {
4949
return :() -> std::string = (s + m[0])$; ();
5050
}
5151

52-
values: <T> (this, t: T) throws -> (offset: int, name: std::string) = {
52+
values: <T> (this, _: T) throws -> (offset: int, name: std::string) = {
5353
offset = 53;
5454
name = "plugh";
5555
}
@@ -60,7 +60,7 @@ outer: @print type = {
6060

6161
operator=: (implicit out this, _: int) = { }
6262

63-
variadic: (x...: int) = { }
63+
variadic: (x...: int) = { (std::cout << ... << x); }
6464
}
6565

6666
test: () = {

regression-tests/pure2-variadics.cpp2

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ x: <Ts...: type> type = {
44
tup: std::tuple<Ts...> = ();
55
}
66

7-
print: <Args...: type> (inout out: std::ostream, args...: Args) = {
7+
left_fold_print: <Args...: type> (inout out: std::ostream, args...: Args) = {
88
// Binary left fold expression
99
(out << ... << args);
1010
}
@@ -25,7 +25,7 @@ main: ()
2525
std::cout << make_string("plugh", :u8=3) << "\n";
2626
std::cout << make<std::string>("abracadabra", :u8=3) << "\n";
2727

28-
print( std::cout, 3.14, "word", -1500 );
28+
left_fold_print( std::cout, 3.14, "word", -1500 );
2929

3030
std::cout << "\nfirst all() returned (all(true, true, true, false))$";
3131
std::cout << "\nsecond all() returned " << all(true, true, true, true) as std::string;
Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +0,0 @@
1-
pure2-print.cpp2:52:80: warning: unused parameter 't' [-Wunused-parameter]
2-
template<typename T> [[nodiscard]] auto outer::mytype::values(T const& t) const& -> values_ret{
3-
^
4-
pure2-print.cpp2:63:53: warning: unused parameter 'x' [-Wunused-parameter]
5-
auto outer::mytype::variadic(auto const& ...x) -> void{}
6-
^
7-
2 warnings generated.
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,17 @@
11
In file included from pure2-print.cpp:7:
22
../../../include/cpp2util.h:10005:33: error: expected unqualified-id before ‘static_assert’
33
pure2-print.cpp2:7:1: note: in expansion of macro ‘CPP2_REQUIRES_’
4+
pure2-print.cpp2:63:59: error: expected ‘;’ at end of member declaration
5+
In file included from pure2-print.cpp:7:
6+
../../../include/cpp2util.h:10005:47: error: static assertion failed: GCC 11 or higher is required to support variables and type-scope functions that have a 'requires' clause. This includes a type-scope 'forward' parameter of non-wildcard type, such as 'func: (this, forward s: std::string)', which relies on being able to add a 'requires' clause - in that case, use 'forward s: _' instead if you need the result to compile with GCC 10.
7+
pure2-print.cpp2:64:1: note: in expansion of macro ‘CPP2_REQUIRES_’
48
../../../include/cpp2util.h:10005:33: error: expected initializer before ‘static_assert’
59
pure2-print.cpp2:89:1: note: in expansion of macro ‘CPP2_REQUIRES_’
610
pure2-print.cpp2:6:29: error: ‘constexpr const T outer::object_alias’ is not a static data member of ‘class outer’
711
pure2-print.cpp2:6:36: error: template definition of non-template ‘constexpr const T outer::object_alias’
12+
pure2-print.cpp2:63:14: error: no declaration matches ‘void outer::mytype::variadic(const auto:89& ...) requires (is_convertible_v<typename std::remove_cv<typename std::remove_reference<decltype(outer::mytype::variadic::x)>::type>::type, int> && ...)’
13+
pure2-print.cpp2:63:29: note: candidate is: ‘template<class ... auto:80> static void outer::mytype::variadic(const auto:80& ...)’
14+
pure2-print.cpp2:8:19: note: ‘class outer::mytype’ defined here
815
pure2-print.cpp2:88:37: error: no declaration matches ‘void outer::print(std::ostream&, const Args& ...) requires cpp2::cmp_greater_eq(sizeof (Args)..., 0)’
916
pure2-print.cpp2:88:37: note: no functions named ‘void outer::print(std::ostream&, const Args& ...) requires cpp2::cmp_greater_eq(sizeof (Args)..., 0)’
1017
pure2-print.cpp2:4:7: note: ‘class outer’ defined here

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

Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,9 @@ class file_attributes;
2424

2525
#line 2 "pure2-enum.cpp2"
2626
class skat_game {
27-
private: cpp2::i8 _value; private: constexpr skat_game(cpp2::in<cpp2::i64> val);
27+
private: cpp2::i8 _value; private: constexpr skat_game(cpp2::in<cpp2::i64> _val);
2828

29-
private: constexpr auto operator=(cpp2::in<cpp2::i64> val) -> skat_game& ;
29+
private: constexpr auto operator=(cpp2::in<cpp2::i64> _val) -> skat_game& ;
3030
public: [[nodiscard]] constexpr auto get_raw_value() const& -> cpp2::i8;
3131
public: constexpr skat_game(skat_game const& that);
3232
public: constexpr auto operator=(skat_game const& that) -> skat_game& ;
@@ -54,9 +54,9 @@ class janus {
5454
#line 15 "pure2-enum.cpp2"
5555
public: constexpr auto flip() & -> void;
5656

57-
private: cpp2::i8 _value; private: constexpr janus(cpp2::in<cpp2::i64> val);
57+
private: cpp2::i8 _value; private: constexpr janus(cpp2::in<cpp2::i64> _val);
5858

59-
private: constexpr auto operator=(cpp2::in<cpp2::i64> val) -> janus& ;
59+
private: constexpr auto operator=(cpp2::in<cpp2::i64> _val) -> janus& ;
6060
public: [[nodiscard]] constexpr auto get_raw_value() const& -> cpp2::i8;
6161
public: constexpr janus(janus const& that);
6262
public: constexpr auto operator=(janus const& that) -> janus& ;
@@ -71,9 +71,9 @@ public: [[nodiscard]] auto to_string() const& -> std::string;
7171
};
7272

7373
class file_attributes {
74-
private: cpp2::u8 _value; private: constexpr file_attributes(cpp2::in<cpp2::i64> val);
74+
private: cpp2::u8 _value; private: constexpr file_attributes(cpp2::in<cpp2::i64> _val);
7575

76-
private: constexpr auto operator=(cpp2::in<cpp2::i64> val) -> file_attributes& ;
76+
private: constexpr auto operator=(cpp2::in<cpp2::i64> _val) -> file_attributes& ;
7777
public: [[nodiscard]] constexpr auto get_raw_value() const& -> cpp2::u8;
7878
public: constexpr file_attributes(file_attributes const& that);
7979
public: constexpr auto operator=(file_attributes const& that) -> file_attributes& ;
@@ -111,11 +111,11 @@ auto main() -> int;
111111

112112

113113

114-
constexpr skat_game::skat_game(cpp2::in<cpp2::i64> val)
115-
: _value{ cpp2::unsafe_narrow<cpp2::i8>(val) } { }
116-
constexpr auto skat_game::operator=(cpp2::in<cpp2::i64> val) -> skat_game& {
117-
_value = cpp2::unsafe_narrow<cpp2::i8>(val);
118-
return *this; }
114+
constexpr skat_game::skat_game(cpp2::in<cpp2::i64> _val)
115+
: _value{ cpp2::unsafe_narrow<cpp2::i8>(_val) } { }
116+
constexpr auto skat_game::operator=(cpp2::in<cpp2::i64> _val) -> skat_game& {
117+
_value = cpp2::unsafe_narrow<cpp2::i8>(_val);
118+
return *this; }
119119
[[nodiscard]] constexpr auto skat_game::get_raw_value() const& -> cpp2::i8 { return _value; }
120120
constexpr skat_game::skat_game(skat_game const& that)
121121
: _value{ that._value }{}
@@ -155,12 +155,12 @@ inline CPP2_CONSTEXPR skat_game skat_game::null = 23;
155155
}
156156

157157

158-
constexpr janus::janus(cpp2::in<cpp2::i64> val)
159-
: _value{ cpp2::unsafe_narrow<cpp2::i8>(val) } { }
158+
constexpr janus::janus(cpp2::in<cpp2::i64> _val)
159+
: _value{ cpp2::unsafe_narrow<cpp2::i8>(_val) } { }
160160

161-
constexpr auto janus::operator=(cpp2::in<cpp2::i64> val) -> janus& {
162-
_value = cpp2::unsafe_narrow<cpp2::i8>(val);
163-
return *this; }
161+
constexpr auto janus::operator=(cpp2::in<cpp2::i64> _val) -> janus& {
162+
_value = cpp2::unsafe_narrow<cpp2::i8>(_val);
163+
return *this; }
164164
[[nodiscard]] constexpr auto janus::get_raw_value() const& -> cpp2::i8 { return _value; }
165165
constexpr janus::janus(janus const& that)
166166
: _value{ that._value }{}
@@ -182,11 +182,11 @@ inline CPP2_CONSTEXPR janus janus::future = 1;
182182
return "invalid janus value";
183183
}
184184

185-
constexpr file_attributes::file_attributes(cpp2::in<cpp2::i64> val)
186-
: _value{ cpp2::unsafe_narrow<cpp2::u8>(val) } { }
187-
constexpr auto file_attributes::operator=(cpp2::in<cpp2::i64> val) -> file_attributes& {
188-
_value = cpp2::unsafe_narrow<cpp2::u8>(val);
189-
return *this; }
185+
constexpr file_attributes::file_attributes(cpp2::in<cpp2::i64> _val)
186+
: _value{ cpp2::unsafe_narrow<cpp2::u8>(_val) } { }
187+
constexpr auto file_attributes::operator=(cpp2::in<cpp2::i64> _val) -> file_attributes& {
188+
_value = cpp2::unsafe_narrow<cpp2::u8>(_val);
189+
return *this; }
190190
[[nodiscard]] constexpr auto file_attributes::get_raw_value() const& -> cpp2::u8 { return _value; }
191191
constexpr file_attributes::file_attributes(file_attributes const& that)
192192
: _value{ that._value }{}
@@ -219,15 +219,15 @@ inline CPP2_CONSTEXPR file_attributes file_attributes::none = 0;
219219

220220
[[nodiscard]] auto file_attributes::to_string() const& -> std::string{
221221

222-
std::string ret {"("};
222+
std::string _ret {"("};
223223

224-
std::string comma {};
224+
std::string _comma {};
225225
if ((*this) == none) {return "(none)"; }
226-
if (((*this) & cached) == cached) {ret += comma + "cached";comma = ", ";}
227-
if (((*this) & current) == current) {ret += comma + "current";comma = ", ";}
228-
if (((*this) & obsolete) == obsolete) {ret += comma + "obsolete";comma = ", ";}
229-
if (((*this) & cached_and_current) == cached_and_current) {ret += comma + "cached_and_current";comma = ", ";}
230-
return ret + ")";
226+
if (((*this) & cached) == cached) {_ret += _comma + "cached";_comma = ", ";}
227+
if (((*this) & current) == current) {_ret += _comma + "current";_comma = ", ";}
228+
if (((*this) & obsolete) == obsolete) {_ret += _comma + "obsolete";_comma = ", ";}
229+
if (((*this) & cached_and_current) == cached_and_current) {_ret += _comma + "cached_and_current";_comma = ", ";}
230+
return _ret + ")";
231231
}
232232
#line 28 "pure2-enum.cpp2"
233233
auto main() -> int{

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

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

3939

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

4343

4444
#line 57 "pure2-print.cpp2"
@@ -48,7 +48,10 @@ CPP2_REQUIRES_ (true)
4848

4949
public: mytype([[maybe_unused]] cpp2::in<int> param2);
5050

51-
public: static auto variadic(auto const& ...x) -> void;
51+
public: static auto variadic(auto const& ...x) -> void
52+
CPP2_REQUIRES_ ((std::is_convertible_v<CPP2_TYPEOF(x), int> && ...))
53+
#line 63 "pure2-print.cpp2"
54+
;
5255
};
5356

5457
public: static auto test() -> void;
@@ -142,7 +145,7 @@ requires (true)
142145
return [_0 = (s + cpp2::assert_in_bounds(m, 0))]() -> std::string { return _0; }();
143146
}
144147

145-
template<typename T> [[nodiscard]] auto outer::mytype::values(T const& t) const& -> values_ret{
148+
template<typename T> [[nodiscard]] auto outer::mytype::values([[maybe_unused]] T const& param2) const& -> values_ret{
146149
cpp2::deferred_init<int> offset;
147150
cpp2::deferred_init<std::string> name;
148151
#line 53 "pure2-print.cpp2"
@@ -156,7 +159,10 @@ requires (true)
156159

157160
outer::mytype::mytype([[maybe_unused]] cpp2::in<int> param2){}
158161

159-
auto outer::mytype::variadic(auto const& ...x) -> void{}
162+
auto outer::mytype::variadic(auto const& ...x) -> void
163+
requires ((std::is_convertible_v<CPP2_TYPEOF(x), int> && ...))
164+
#line 63 "pure2-print.cpp2"
165+
{(std::cout << ... << x); }
160166

161167
#line 66 "pure2-print.cpp2"
162168
auto outer::test() -> void{

regression-tests/test-results/pure2-print.cpp2.output

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ outer: type =
8080

8181
values: <T: type>(
8282
in this,
83-
in t: T
83+
in _: T
8484
) throws -> (
8585
out offset: int,
8686
out name: std::string
@@ -111,6 +111,7 @@ outer: type =
111111

112112
variadic:(in x...: int) =
113113
{
114+
(std::cout << ... << x);
114115
}
115116
}
116117

0 commit comments

Comments
 (0)