Skip to content

Commit ce5bd83

Browse files
authored
fix(cpp1): emit body of non local function expression (#601)
* test: add unit test for non local function expressions * fix(cpp1): emit body of non local function expression * test: add missing result
1 parent 8ade56b commit ce5bd83

7 files changed

+51
-1
lines changed
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
v: <T> concept = :() -> bool = true;();
2+
3+
u: type == std::type_identity_t<decltype(:() = {})>;
4+
5+
t: @struct type = {
6+
this: std::type_identity_t<decltype(:() = {})>;
7+
}
8+
9+
main: () = { }

regression-tests/test-results/clang-18/pure2-bugfix-for-non-local-function-expression.cpp.execution

Whitespace-only changes.

regression-tests/test-results/clang-18/pure2-bugfix-for-non-local-function-expression.cpp.output

Whitespace-only changes.
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
2+
#define CPP2_USE_MODULES Yes
3+
4+
//=== Cpp2 type declarations ====================================================
5+
6+
7+
#include "cpp2util.h"
8+
9+
10+
#line 5 "pure2-bugfix-for-non-local-function-expression.cpp2"
11+
class t;
12+
13+
14+
//=== Cpp2 type definitions and function declarations ===========================
15+
16+
#line 1 "pure2-bugfix-for-non-local-function-expression.cpp2"
17+
template<typename T> concept v = []() -> bool { return true; }();
18+
19+
using u = std::type_identity_t<decltype([]() -> void{})>;
20+
21+
class t: public std::type_identity_t<decltype([]() -> void{})> {
22+
23+
};
24+
25+
auto main() -> int;
26+
27+
28+
//=== Cpp2 function definitions =================================================
29+
30+
31+
#line 9 "pure2-bugfix-for-non-local-function-expression.cpp2"
32+
auto main() -> int{}
33+
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
pure2-bugfix-for-non-local-function-expression.cpp2... ok (all Cpp2, passes safety checks)
2+

source/cppfront.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5671,7 +5671,10 @@ class cppfront
56715671
}
56725672

56735673
// If we're only emitting declarations, end the function declaration
5674-
if (printer.get_phase() == printer.phase1_type_defs_func_decls)
5674+
if (
5675+
printer.get_phase() == printer.phase1_type_defs_func_decls
5676+
&& !n.is_function_expression()
5677+
)
56755678
{
56765679
emit_requires_clause();
56775680
printer.print_cpp2( ";\n", n.position() );

source/parse.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2598,6 +2598,9 @@ struct declaration_node
25982598
auto is_alias() const -> bool
25992599
{ return type.index() == an_alias; }
26002600

2601+
auto is_function_expression () const -> bool
2602+
{ return is_function() && !identifier; }
2603+
26012604
auto is_polymorphic() const // has base types or virtual functions
26022605
-> bool
26032606
{

0 commit comments

Comments
 (0)