Skip to content

Commit 43822ae

Browse files
committed
Add initial support for x: my_enum_type = /*value*/;, closes #670
1 parent d43a1f6 commit 43822ae

16 files changed

+201
-146
lines changed

include/cpp2util.h

+20-2
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,18 @@ using __schar = signed char; // normally use i8 instead
279279
using __uchar = unsigned char; // normally use u8 instead
280280

281281

282+
//-----------------------------------------------------------------------
283+
//
284+
// General helpers
285+
//
286+
//-----------------------------------------------------------------------
287+
//
288+
289+
auto max(auto... values) {
290+
return std::max( { values... } );
291+
}
292+
293+
282294
//-----------------------------------------------------------------------
283295
//
284296
// String: A helper workaround for passing a string literal as a
@@ -1797,8 +1809,14 @@ inline constexpr auto as_() -> decltype(auto)
17971809

17981810
using cpp2::cpp2_new;
17991811

1800-
// Workaround GCC 10 not supporting requires in forward declarations in some cases.
1801-
// See commit 5a0d77f8e297902c0b9712c5aafb6208cfa4c139.
1812+
1813+
// Stabilize line numbers for "compatibility" static assertions that we know
1814+
// will fire for some compilers, to keep regression test outputs cleaner
1815+
#line 9999
1816+
1817+
// GCC 10 doesn't support 'requires' in forward declarations in some cases
1818+
// Workaround: Disable the requires clause where that gets reasonable behavior
1819+
// Diagnostic: static_assert the other cases that can't be worked around
18021820
#if !defined(__clang__) && defined(__GNUC__) && __GNUC__ == 10
18031821
#define CPP2_REQUIRES(...) /* empty */
18041822
#define CPP2_REQUIRES_(...) static_assert(false, "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.")

regression-tests/pure2-enum.cpp2

+6-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11

2+
23
skat_game: @enum type = {
34
diamonds := 9;
45
hearts; // 10
@@ -24,12 +25,15 @@ file_attributes: @flag_enum type = {
2425
main: () = {
2526
// x : skat_game = 9; // error, can't construct skat_game from integer
2627

27-
x := skat_game::clubs;
28+
x: skat_game = skat_game::clubs;
29+
x2 := skat_game::diamonds;
30+
x2 = x;
2831

2932
// if x == 9 { } // error, can't compare skat_game and integer
3033
// if x == rgb::red { } // error, can't compare skat_game and rgb color
3134

3235
std::cout << "x.to_string() is (x.to_string())$\n";
36+
std::cout << "x2.to_string() is (x2.to_string())$\n";
3337
std::cout << "using << prints " << x << "\n";
3438

3539
std::cout << "with if else: ";
@@ -62,7 +66,7 @@ main: () = {
6266

6367
x = skat_game::diamonds; // ok, can assign one skat_game from another
6468

65-
f := file_attributes::cached_and_current;
69+
f: file_attributes = file_attributes::cached_and_current;
6670
f &= file_attributes::cached | file_attributes::obsolete;
6771

6872
f2 := file_attributes::cached;

regression-tests/test-results/clang-12/pure2-enum.cpp.execution

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
x.to_string() is clubs
2+
x2.to_string() is clubs
23
using << prints clubs
34
with if else: clubs
45
with inspect: clubs

regression-tests/test-results/gcc-10/pure2-bugfix-for-requires-clause-in-forward-declaration.cpp.output

+6-10
Original file line numberDiff line numberDiff line change
@@ -3,30 +3,26 @@ pure2-bugfix-for-requires-clause-in-forward-declaration.cpp:16:36: error: expect
33
| ^
44
| ;
55
In file included from pure2-bugfix-for-requires-clause-in-forward-declaration.cpp:7:
6-
../../../include/cpp2util.h:1804: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-
1804 | #define CPP2_REQUIRES_(...) static_assert(false, "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.")
8-
| ^~~~~
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.
97
pure2-bugfix-for-requires-clause-in-forward-declaration.cpp:17:1: note: in expansion of macro ‘CPP2_REQUIRES_’
108
17 | CPP2_REQUIRES_ (std::is_same_v<CPP2_TYPEOF(n), std::string>)
119
| ^~~~~~~~~~~~~~
1210
pure2-bugfix-for-requires-clause-in-forward-declaration.cpp2:3:46: error: expected ‘;’ at end of member declaration
1311
In file included from pure2-bugfix-for-requires-clause-in-forward-declaration.cpp:7:
14-
../../../include/cpp2util.h:1804: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.
15-
1804 | #define CPP2_REQUIRES_(...) static_assert(false, "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.")
16-
| ^~~~~
12+
../../../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.
1713
pure2-bugfix-for-requires-clause-in-forward-declaration.cpp2:4:1: note: in expansion of macro ‘CPP2_REQUIRES_’
18-
pure2-bugfix-for-requires-clause-in-forward-declaration.cpp2:3:3: error: no declaration matches ‘element::element(auto:80&&) requires is_same_v<typename std::remove_cv<typename std::remove_reference<decltype(element::__ct ::n)>::type>::type, std::__cxx11::string>’
14+
pure2-bugfix-for-requires-clause-in-forward-declaration.cpp2:3:3: error: no declaration matches ‘element::element(auto:81&&) requires is_same_v<typename std::remove_cv<typename std::remove_reference<decltype(element::__ct ::n)>::type>::type, std::__cxx11::string>’
1915
pure2-bugfix-for-requires-clause-in-forward-declaration.cpp2:5:11: note: candidates are: ‘element::element(const element&)’
20-
pure2-bugfix-for-requires-clause-in-forward-declaration.cpp:16:20: note: ‘template<class auto:78> element::element(auto:78&&)’
16+
pure2-bugfix-for-requires-clause-in-forward-declaration.cpp:16:20: note: ‘template<class auto:79> element::element(auto:79&&)’
2117
16 | public: explicit element(auto&& n)
2218
| ^~~~~~~
2319
pure2-bugfix-for-requires-clause-in-forward-declaration.cpp:14:7: note: ‘class element’ defined here
2420
14 | class element {
2521
| ^~~~~~~
2622
pure2-bugfix-for-requires-clause-in-forward-declaration.cpp2:3:3: error: expected unqualified-id before ‘{’ token
27-
pure2-bugfix-for-requires-clause-in-forward-declaration.cpp2:3:8: error: no declaration matches ‘element& element::operator=(auto:81&&) requires is_same_v<typename std::remove_cv<typename std::remove_reference<decltype(element::operator=::n)>::type>::type, std::__cxx11::string>’
23+
pure2-bugfix-for-requires-clause-in-forward-declaration.cpp2:3:8: error: no declaration matches ‘element& element::operator=(auto:82&&) requires is_same_v<typename std::remove_cv<typename std::remove_reference<decltype(element::operator=::n)>::type>::type, std::__cxx11::string>’
2824
pure2-bugfix-for-requires-clause-in-forward-declaration.cpp2:6:16: note: candidates are: ‘void element::operator=(const element&)’
29-
pure2-bugfix-for-requires-clause-in-forward-declaration.cpp2:3:16: note: ‘template<class auto:79> element& element::operator=(auto:79&&)’
25+
pure2-bugfix-for-requires-clause-in-forward-declaration.cpp2:3:16: note: ‘template<class auto:80> element& element::operator=(auto:80&&)’
3026
pure2-bugfix-for-requires-clause-in-forward-declaration.cpp:14:7: note: ‘class element’ defined here
3127
14 | class element {
3228
| ^~~~~~~

regression-tests/test-results/gcc-10/pure2-enum.cpp.execution

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
x.to_string() is clubs
2+
x2.to_string() is clubs
23
using << prints clubs
34
with if else: clubs
45
with inspect: clubs
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
11
In file included from pure2-requires-clauses.cpp:7:
2-
../../../include/cpp2util.h:1804:33: error: expected unqualified-id before ‘static_assert’
3-
1804 | #define CPP2_REQUIRES_(...) static_assert(false, "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.")
4-
| ^~~~~~~~~~~~~
2+
../../../include/cpp2util.h:10005:33: error: expected unqualified-id before ‘static_assert’
53
pure2-requires-clauses.cpp2:19:1: note: in expansion of macro ‘CPP2_REQUIRES_’

regression-tests/test-results/gcc-13/pure2-enum.cpp.execution

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
x.to_string() is clubs
2+
x2.to_string() is clubs
23
using << prints clubs
34
with if else: clubs
45
with inspect: clubs

regression-tests/test-results/msvc-2022/pure2-enum.cpp.execution

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
x.to_string() is clubs
2+
x2.to_string() is clubs
23
using << prints clubs
34
with if else: clubs
45
with inspect: clubs

0 commit comments

Comments
 (0)