Skip to content

Commit

Permalink
Add warnings for names reserved for metafunction implementations
Browse files Browse the repository at this point in the history
Closes #720
  • Loading branch information
hsutter committed Oct 8, 2023
1 parent 0b380c5 commit f2f70ed
Show file tree
Hide file tree
Showing 15 changed files with 297 additions and 207 deletions.
1 change: 1 addition & 0 deletions include/cpp2util.h
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,7 @@

#define CPP2_TYPEOF(x) std::remove_cvref_t<decltype(x)>
#define CPP2_FORWARD(x) std::forward<decltype(x)>(x)
#define CPP2_PACK_EMPTY(x) (sizeof...(x) == 0)

This comment has been minimized.

Copy link
@JohelEGP

JohelEGP Oct 8, 2023

Contributor

I have used something like

template<class... T> constexpr bool is_empty = sizeof...(T) == 0;

before.
To allow a pack of non-types, perhaps this could also work:

template<class... T> constexpr bool is_empty(auto const&... x) {
  return sizeof...(T) == 0 && sizeof...(x) == 0;
}

Those can be written in Cpp2 now, so you might want to restrict it to reflect.h2.

#define CPP2_CONTINUE_BREAK(NAME) goto CONTINUE_##NAME; CONTINUE_##NAME: continue; goto BREAK_##NAME; BREAK_##NAME: break;
// these redundant goto's to avoid 'unused label' warnings

Expand Down
4 changes: 2 additions & 2 deletions regression-tests/pure2-print.cpp2
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ outer: @print type = {
return :() -> std::string = (s + m[0])$; ();
}

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

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

variadic: (x...: int) = { }
variadic: (x...: int) = { (std::cout << ... << x); }
}

test: () = {
Expand Down
4 changes: 2 additions & 2 deletions regression-tests/pure2-variadics.cpp2
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ x: <Ts...: type> type = {
tup: std::tuple<Ts...> = ();
}

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

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

std::cout << "\nfirst all() returned (all(true, true, true, false))$";
std::cout << "\nsecond all() returned " << all(true, true, true, true) as std::string;
Expand Down
7 changes: 0 additions & 7 deletions regression-tests/test-results/clang-12/pure2-print.cpp.output
Original file line number Diff line number Diff line change
@@ -1,7 +0,0 @@
pure2-print.cpp2:52:80: warning: unused parameter 't' [-Wunused-parameter]
template<typename T> [[nodiscard]] auto outer::mytype::values(T const& t) const& -> values_ret{
^
pure2-print.cpp2:63:53: warning: unused parameter 'x' [-Wunused-parameter]
auto outer::mytype::variadic(auto const& ...x) -> void{}
^
2 warnings generated.
7 changes: 7 additions & 0 deletions regression-tests/test-results/gcc-10/pure2-print.cpp.output
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
In file included from pure2-print.cpp:7:
../../../include/cpp2util.h:10005:33: error: expected unqualified-id before ‘static_assert’
pure2-print.cpp2:7:1: note: in expansion of macro ‘CPP2_REQUIRES_’
pure2-print.cpp2:63:59: error: expected ‘;’ at end of member declaration
In file included from pure2-print.cpp:7:
../../../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.
pure2-print.cpp2:64:1: note: in expansion of macro ‘CPP2_REQUIRES_’
../../../include/cpp2util.h:10005:33: error: expected initializer before ‘static_assert’
pure2-print.cpp2:89:1: note: in expansion of macro ‘CPP2_REQUIRES_’
pure2-print.cpp2:6:29: error: ‘constexpr const T outer::object_alias’ is not a static data member of ‘class outer’
pure2-print.cpp2:6:36: error: template definition of non-template ‘constexpr const T outer::object_alias’
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> && ...)’
pure2-print.cpp2:63:29: note: candidate is: ‘template<class ... auto:80> static void outer::mytype::variadic(const auto:80& ...)’
pure2-print.cpp2:8:19: note: ‘class outer::mytype’ defined here
pure2-print.cpp2:88:37: error: no declaration matches ‘void outer::print(std::ostream&, const Args& ...) requires cpp2::cmp_greater_eq(sizeof (Args)..., 0)’
pure2-print.cpp2:88:37: note: no functions named ‘void outer::print(std::ostream&, const Args& ...) requires cpp2::cmp_greater_eq(sizeof (Args)..., 0)’
pure2-print.cpp2:4:7: note: ‘class outer’ defined here
56 changes: 28 additions & 28 deletions regression-tests/test-results/pure2-enum.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ class file_attributes;

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

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

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

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

class file_attributes {
private: cpp2::u8 _value; private: constexpr file_attributes(cpp2::in<cpp2::i64> val);
private: cpp2::u8 _value; private: constexpr file_attributes(cpp2::in<cpp2::i64> _val);

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



constexpr skat_game::skat_game(cpp2::in<cpp2::i64> val)
: _value{ cpp2::unsafe_narrow<cpp2::i8>(val) } { }
constexpr auto skat_game::operator=(cpp2::in<cpp2::i64> val) -> skat_game& {
_value = cpp2::unsafe_narrow<cpp2::i8>(val);
return *this; }
constexpr skat_game::skat_game(cpp2::in<cpp2::i64> _val)
: _value{ cpp2::unsafe_narrow<cpp2::i8>(_val) } { }
constexpr auto skat_game::operator=(cpp2::in<cpp2::i64> _val) -> skat_game& {
_value = cpp2::unsafe_narrow<cpp2::i8>(_val);
return *this; }
[[nodiscard]] constexpr auto skat_game::get_raw_value() const& -> cpp2::i8 { return _value; }
constexpr skat_game::skat_game(skat_game const& that)
: _value{ that._value }{}
Expand Down Expand Up @@ -155,12 +155,12 @@ inline CPP2_CONSTEXPR skat_game skat_game::null = 23;
}


constexpr janus::janus(cpp2::in<cpp2::i64> val)
: _value{ cpp2::unsafe_narrow<cpp2::i8>(val) } { }
constexpr janus::janus(cpp2::in<cpp2::i64> _val)
: _value{ cpp2::unsafe_narrow<cpp2::i8>(_val) } { }

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

constexpr file_attributes::file_attributes(cpp2::in<cpp2::i64> val)
: _value{ cpp2::unsafe_narrow<cpp2::u8>(val) } { }
constexpr auto file_attributes::operator=(cpp2::in<cpp2::i64> val) -> file_attributes& {
_value = cpp2::unsafe_narrow<cpp2::u8>(val);
return *this; }
constexpr file_attributes::file_attributes(cpp2::in<cpp2::i64> _val)
: _value{ cpp2::unsafe_narrow<cpp2::u8>(_val) } { }
constexpr auto file_attributes::operator=(cpp2::in<cpp2::i64> _val) -> file_attributes& {
_value = cpp2::unsafe_narrow<cpp2::u8>(_val);
return *this; }
[[nodiscard]] constexpr auto file_attributes::get_raw_value() const& -> cpp2::u8 { return _value; }
constexpr file_attributes::file_attributes(file_attributes const& that)
: _value{ that._value }{}
Expand Down Expand Up @@ -219,15 +219,15 @@ inline CPP2_CONSTEXPR file_attributes file_attributes::none = 0;

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

std::string ret {"("};
std::string _ret {"("};

std::string comma {};
std::string _comma {};
if ((*this) == none) {return "(none)"; }
if (((*this) & cached) == cached) {ret += comma + "cached";comma = ", ";}
if (((*this) & current) == current) {ret += comma + "current";comma = ", ";}
if (((*this) & obsolete) == obsolete) {ret += comma + "obsolete";comma = ", ";}
if (((*this) & cached_and_current) == cached_and_current) {ret += comma + "cached_and_current";comma = ", ";}
return ret + ")";
if (((*this) & cached) == cached) {_ret += _comma + "cached";_comma = ", ";}
if (((*this) & current) == current) {_ret += _comma + "current";_comma = ", ";}
if (((*this) & obsolete) == obsolete) {_ret += _comma + "obsolete";_comma = ", ";}
if (((*this) & cached_and_current) == cached_and_current) {_ret += _comma + "cached_and_current";_comma = ", ";}
return _ret + ")";
}
#line 28 "pure2-enum.cpp2"
auto main() -> int{
Expand Down
14 changes: 10 additions & 4 deletions regression-tests/test-results/pure2-print.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ CPP2_REQUIRES_ (true)


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


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

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

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

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

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

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

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

#line 66 "pure2-print.cpp2"
auto outer::test() -> void{
Expand Down
3 changes: 2 additions & 1 deletion regression-tests/test-results/pure2-print.cpp2.output
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ outer: type =

values: <T: type>(
in this,
in t: T
in _: T
) throws -> (
out offset: int,
out name: std::string
Expand Down Expand Up @@ -111,6 +111,7 @@ outer: type =

variadic:(in x...: int) =
{
(std::cout << ... << x);
}
}

Expand Down
Loading

0 comments on commit f2f70ed

Please sign in to comment.