-
Notifications
You must be signed in to change notification settings - Fork 249
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow function parameter default arguments
Closes #1189 I had been experimenting with not allowing default arguments for function parameters, in part because of the potential for creating order-dependent code; the way to find out whether they're necessary is to not support them and see if that leaves a usability hole. The result of the experiment is that it does leave a hole: There's persistent feedback that default arguments are often useful, and are actually necessary for a few cases including particularly `std::source_location` parameters. As for order independence, there are already ways to opt into creating potentially order-dependent code (such as by deduced return types which depend on function bodies). So I think it's time to enable default arguments, and Cpp2 is still order-independent by default. This example now works: my_function_name: ( fn: *const char = std::source_location::current().function_name() ) = { std::cout << "calling: (fn)$\n"; } main: (args) = { my_function_name(); } // On MSVC 2022, prints: // calling: int __cdecl main(const int,char **) // On GCC 14, prints: // calling: int main(int, char**)
- Loading branch information
Showing
12 changed files
with
172 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
|
||
// Note: Using source_location requires GCC 11 or higher, | ||
// Clang 16 or higher, MSVC 2019 16.10 or higher. | ||
// Older compilers will emit failures for this test case. | ||
my_function_name: ( | ||
fn: *const char = std::source_location::current().function_name() | ||
) | ||
= { | ||
std::cout << "calling: (fn)$\n"; | ||
} | ||
|
||
f: (x: i32 = 0) = { std::cout << x; } | ||
|
||
main: (args) = { | ||
my_function_name(); | ||
f(); | ||
f(1); | ||
f(2); | ||
} |
4 changes: 4 additions & 0 deletions
4
regression-tests/test-results/clang-12-c++20/pure2-default-arguments.cpp.output
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
pure2-default-arguments.cpp2:6:61: error: no member named 'source_location' in namespace 'std' | ||
char const* fn = CPP2_UFCS_NONLOCAL(function_name)(std::source_location::current()) | ||
~~~~~^ | ||
1 error generated. |
66 changes: 66 additions & 0 deletions
66
regression-tests/test-results/gcc-10-c++20/pure2-default-arguments.cpp.output
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
In file included from pure2-default-arguments.cpp:7: | ||
../../../include/cpp2util.h:2086:28: error: local variable ‘obj’ may not appear in this context | ||
2086 | if constexpr (std::is_same_v< CPP2_TYPEOF(operator_as<17>(x)), T >) { if (x.index() == 17) return operator_as<17>(x); } | ||
| ^~~ | ||
../../../include/cpp2util.h:2047:34: note: in definition of macro ‘CPP2_UFCS_IDENTITY’ | ||
2047 | if constexpr (std::is_same_v< CPP2_TYPEOF(operator_as< 3>(x)), T >) { if (x.index() == 3) return operator_as<3>(x); } | ||
| ^~~~~~~~~~~ | ||
../../../include/cpp2util.h:2086:15: note: in expansion of macro ‘CPP2_FORWARD’ | ||
2086 | if constexpr (std::is_same_v< CPP2_TYPEOF(operator_as<17>(x)), T >) { if (x.index() == 17) return operator_as<17>(x); } | ||
| ^~~~~~~~~~~~ | ||
../../../include/cpp2util.h:2107:22: note: in expansion of macro ‘CPP2_UFCS_CONSTRAINT_ARG’ | ||
2107 | { return !x.has_value(); } | ||
| ^~~~~~~~~ | ||
../../../include/cpp2util.h:2137:59: note: in expansion of macro ‘CPP2_UFCS_’ | ||
2137 | { return std::any_cast<T>( x ); } | ||
| ^ | ||
pure2-default-arguments.cpp2:6:22: note: in expansion of macro ‘CPP2_UFCS_NONLOCAL’ | ||
../../../include/cpp2util.h:2086:92: error: local variable ‘params’ may not appear in this context | ||
2086 | if constexpr (std::is_same_v< CPP2_TYPEOF(operator_as<17>(x)), T >) { if (x.index() == 17) return operator_as<17>(x); } | ||
| ^~~~~~ | ||
../../../include/cpp2util.h:2047:34: note: in definition of macro ‘CPP2_UFCS_IDENTITY’ | ||
2047 | if constexpr (std::is_same_v< CPP2_TYPEOF(operator_as< 3>(x)), T >) { if (x.index() == 3) return operator_as<3>(x); } | ||
| ^~~~~~~~~~~ | ||
../../../include/cpp2util.h:2086:79: note: in expansion of macro ‘CPP2_FORWARD’ | ||
2086 | if constexpr (std::is_same_v< CPP2_TYPEOF(operator_as<17>(x)), T >) { if (x.index() == 17) return operator_as<17>(x); } | ||
| ^~~~~~~~~~~~ | ||
../../../include/cpp2util.h:2107:22: note: in expansion of macro ‘CPP2_UFCS_CONSTRAINT_ARG’ | ||
2107 | { return !x.has_value(); } | ||
| ^~~~~~~~~ | ||
../../../include/cpp2util.h:2137:59: note: in expansion of macro ‘CPP2_UFCS_’ | ||
2137 | { return std::any_cast<T>( x ); } | ||
| ^ | ||
pure2-default-arguments.cpp2:6:22: note: in expansion of macro ‘CPP2_UFCS_NONLOCAL’ | ||
../../../include/cpp2util.h:2087:74: error: local variable ‘obj’ may not appear in this context | ||
2087 | if constexpr (std::is_same_v< CPP2_TYPEOF(operator_as<18>(x)), T >) { if (x.index() == 18) return operator_as<18>(x); } | ||
| ^~~ | ||
../../../include/cpp2util.h:2047:34: note: in definition of macro ‘CPP2_UFCS_IDENTITY’ | ||
2047 | if constexpr (std::is_same_v< CPP2_TYPEOF(operator_as< 3>(x)), T >) { if (x.index() == 3) return operator_as<3>(x); } | ||
| ^~~~~~~~~~~ | ||
../../../include/cpp2util.h:2087:61: note: in expansion of macro ‘CPP2_FORWARD’ | ||
2087 | if constexpr (std::is_same_v< CPP2_TYPEOF(operator_as<18>(x)), T >) { if (x.index() == 18) return operator_as<18>(x); } | ||
| ^~~~~~~~~~~~ | ||
../../../include/cpp2util.h:2107:22: note: in expansion of macro ‘CPP2_UFCS_CONSTRAINT_ARG’ | ||
2107 | { return !x.has_value(); } | ||
| ^~~~~~~~~ | ||
../../../include/cpp2util.h:2137:59: note: in expansion of macro ‘CPP2_UFCS_’ | ||
2137 | { return std::any_cast<T>( x ); } | ||
| ^ | ||
pure2-default-arguments.cpp2:6:22: note: in expansion of macro ‘CPP2_UFCS_NONLOCAL’ | ||
../../../include/cpp2util.h:2087:93: error: local variable ‘params’ may not appear in this context | ||
2087 | if constexpr (std::is_same_v< CPP2_TYPEOF(operator_as<18>(x)), T >) { if (x.index() == 18) return operator_as<18>(x); } | ||
| ^~~~~~ | ||
../../../include/cpp2util.h:2047:34: note: in definition of macro ‘CPP2_UFCS_IDENTITY’ | ||
2047 | if constexpr (std::is_same_v< CPP2_TYPEOF(operator_as< 3>(x)), T >) { if (x.index() == 3) return operator_as<3>(x); } | ||
| ^~~~~~~~~~~ | ||
../../../include/cpp2util.h:2087:80: note: in expansion of macro ‘CPP2_FORWARD’ | ||
2087 | if constexpr (std::is_same_v< CPP2_TYPEOF(operator_as<18>(x)), T >) { if (x.index() == 18) return operator_as<18>(x); } | ||
| ^~~~~~~~~~~~ | ||
../../../include/cpp2util.h:2107:22: note: in expansion of macro ‘CPP2_UFCS_CONSTRAINT_ARG’ | ||
2107 | { return !x.has_value(); } | ||
| ^~~~~~~~~ | ||
../../../include/cpp2util.h:2137:59: note: in expansion of macro ‘CPP2_UFCS_’ | ||
2137 | { return std::any_cast<T>( x ); } | ||
| ^ | ||
pure2-default-arguments.cpp2:6:22: note: in expansion of macro ‘CPP2_UFCS_NONLOCAL’ | ||
pure2-default-arguments.cpp2:6:61: error: ‘std::source_location’ has not been declared |
2 changes: 2 additions & 0 deletions
2
regression-tests/test-results/gcc-14-c++2b/pure2-default-arguments.cpp.execution
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
calling: int main(int, char**) | ||
012 |
2 changes: 2 additions & 0 deletions
2
regression-tests/test-results/msvc-2022-c++latest/pure2-default-arguments.cpp.execution
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
calling: int __cdecl main(const int,char **) | ||
012 |
1 change: 1 addition & 0 deletions
1
regression-tests/test-results/msvc-2022-c++latest/pure2-default-arguments.cpp.output
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
pure2-default-arguments.cpp |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
|
||
#define CPP2_IMPORT_STD Yes | ||
|
||
//=== Cpp2 type declarations ==================================================== | ||
|
||
|
||
#include "cpp2util.h" | ||
|
||
#line 1 "pure2-default-arguments.cpp2" | ||
|
||
|
||
//=== Cpp2 type definitions and function declarations =========================== | ||
|
||
#line 1 "pure2-default-arguments.cpp2" | ||
|
||
// Note: Using source_location requires GCC 11 or higher, | ||
// Clang 16 or higher, MSVC 2019 16.10 or higher. | ||
// Older compilers will emit failures for this test case. | ||
#line 5 "pure2-default-arguments.cpp2" | ||
auto my_function_name( | ||
char const* fn = CPP2_UFCS_NONLOCAL(function_name)(std::source_location::current()) | ||
) -> void; | ||
|
||
#line 12 "pure2-default-arguments.cpp2" | ||
auto f(cpp2::impl::in<cpp2::i32> x = 0) -> void; | ||
|
||
auto main(int const argc_, char** argv_) -> int; | ||
|
||
//=== Cpp2 function definitions ================================================= | ||
|
||
#line 1 "pure2-default-arguments.cpp2" | ||
|
||
#line 5 "pure2-default-arguments.cpp2" | ||
auto my_function_name( | ||
char const* fn | ||
) -> void | ||
{ | ||
std::cout << "calling: " + cpp2::to_string(fn) + "\n"; | ||
} | ||
|
||
#line 12 "pure2-default-arguments.cpp2" | ||
auto f(cpp2::impl::in<cpp2::i32> x) -> void{std::cout << x; } | ||
|
||
#line 14 "pure2-default-arguments.cpp2" | ||
auto main(int const argc_, char** argv_) -> int{ | ||
auto const args = cpp2::make_args(argc_, argv_); | ||
#line 15 "pure2-default-arguments.cpp2" | ||
my_function_name(); | ||
f(); | ||
f(1); | ||
f(2); | ||
} | ||
|
2 changes: 2 additions & 0 deletions
2
regression-tests/test-results/pure2-default-arguments.cpp2.output
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
pure2-default-arguments.cpp2... ok (all Cpp2, passes safety checks) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
"9804:1033" | ||
"9809:1046" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters