Skip to content

fix(cpp1): emit body of non local function expression #601

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Sep 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
v: <T> concept = :() -> bool = true;();

u: type == std::type_identity_t<decltype(:() = {})>;

t: @struct type = {
this: std::type_identity_t<decltype(:() = {})>;
}

main: () = { }
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@

#define CPP2_USE_MODULES Yes

//=== Cpp2 type declarations ====================================================


#include "cpp2util.h"


#line 5 "pure2-bugfix-for-non-local-function-expression.cpp2"
class t;


//=== Cpp2 type definitions and function declarations ===========================

#line 1 "pure2-bugfix-for-non-local-function-expression.cpp2"
template<typename T> concept v = []() -> bool { return true; }();

using u = std::type_identity_t<decltype([]() -> void{})>;

class t: public std::type_identity_t<decltype([]() -> void{})> {

};

auto main() -> int;


//=== Cpp2 function definitions =================================================


#line 9 "pure2-bugfix-for-non-local-function-expression.cpp2"
auto main() -> int{}

Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
pure2-bugfix-for-non-local-function-expression.cpp2... ok (all Cpp2, passes safety checks)

5 changes: 4 additions & 1 deletion source/cppfront.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5662,7 +5662,10 @@ class cppfront
};

// If we're only emitting declarations, end the function declaration
if (printer.get_phase() == printer.phase1_type_defs_func_decls)
if (
printer.get_phase() == printer.phase1_type_defs_func_decls
&& !n.is_function_expression()
)
{
emit_requires_clause();
printer.print_cpp2( ";\n", n.position() );
Expand Down
11 changes: 7 additions & 4 deletions source/parse.h
Original file line number Diff line number Diff line change
Expand Up @@ -2598,6 +2598,9 @@ struct declaration_node
auto is_alias() const -> bool
{ return type.index() == an_alias; }

auto is_function_expression () const -> bool
{ return is_function() && !identifier; }

auto is_polymorphic() const // has base types or virtual functions
-> bool
{
Expand Down Expand Up @@ -5031,20 +5034,20 @@ class parser
}
return expression(false); // false == disallow unparenthesized relational comparisons in template args
}()
)
)
{
term.arg = std::move(e);
}

// Else try parsing it as a type id
else if (auto i = type_id()) {
term.arg = std::move(i);
}

else {
break;
}

n->template_args.push_back( std::move(term) );
}
// Use the lambda trick to jam in a "next" clause
Expand Down