Skip to content

Commit

Permalink
Don't emit { } around a single-anon-type deduced return, fixes hs…
Browse files Browse the repository at this point in the history
  • Loading branch information
hsutter authored and zaucy committed Dec 5, 2023
1 parent 027ee6e commit 925206e
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 12 deletions.
2 changes: 1 addition & 1 deletion regression-tests/test-results/version
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

cppfront compiler v0.2.1 Build 8809:1848
cppfront compiler v0.2.1 Build 8809:1933
Copyright(c) Herb Sutter All rights reserved

SPDX-License-Identifier: CC-BY-NC-ND-4.0
Expand Down
2 changes: 1 addition & 1 deletion source/build.info
Original file line number Diff line number Diff line change
@@ -1 +1 @@
"8809:1848"
"8809:1933"
41 changes: 31 additions & 10 deletions source/cppfront.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1073,13 +1073,16 @@ class cppfront
struct function_return {
parameter_declaration_list_node* param_list;
passing_style pass;
bool is_deduced;

function_return(
parameter_declaration_list_node* param_list_,
passing_style pass_ = passing_style::invalid
passing_style pass_ = passing_style::invalid,
bool is_deduced_ = false
)
: param_list{param_list_}
, pass{pass_}
, is_deduced{is_deduced_}
{ }
};
std::vector<function_return> function_returns;
Expand Down Expand Up @@ -2217,12 +2220,17 @@ class cppfront
if (n.expression)
{
assert(!current_functions.empty());

// If we're doing a forward return
auto is_forward_return =
!function_returns.empty()
&& function_returns.back().pass == passing_style::forward;
auto is_deduced_return =
!function_returns.empty()
&& function_returns.back().is_deduced;

// If we're doing a forward return of a single-token name
if (auto tok = n.expression->expr->get_postfix_expression_node()->expr->get_token();
tok
&& !function_returns.empty()
&& function_returns.back().pass == passing_style::forward
&& is_forward_return
)
{
// Ensure we're not returning a local or an in/move parameter
Expand Down Expand Up @@ -2267,9 +2275,13 @@ class cppfront
// take over direct control of emitting it without needing to
// go through the whole grammar, and surround it with braces
if (n.expression->is_expression_list()) {
printer.print_cpp2( "{ ", n.position() );
if (!is_deduced_return) {
printer.print_cpp2( "{ ", n.position() );
}
emit(*n.expression->get_expression_list(), false);
printer.print_cpp2( " }", n.position() );
if (!is_deduced_return) {
printer.print_cpp2( " }", n.position() );
}
}
// Otherwise, just emit the general expression as usual
else {
Expand Down Expand Up @@ -3670,9 +3682,17 @@ class cppfront
&& n.expr->is_expression_list()
)
{
printer.print_cpp2( "{ ", n.position() );
auto is_deduced_return =
!function_returns.empty()
&& function_returns.back().is_deduced;

if (!is_deduced_return) {
printer.print_cpp2( "{ ", n.position() );
}
emit(*n.expr->get_expression_list(), false);
printer.print_cpp2( " }", n.position() );
if (!is_deduced_return) {
printer.print_cpp2( " }", n.position() );
}
}
// Otherwise, just emit the general expression as usual
else {
Expand Down Expand Up @@ -5634,7 +5654,8 @@ class cppfront
else if (func->returns.index() == function_type_node::id) {
function_returns.emplace_back(
&single_anon, // use special value as a note
std::get<function_type_node::id>(func->returns).pass
std::get<function_type_node::id>(func->returns).pass,
std::get<function_type_node::id>(func->returns).type->is_wildcard()
);
}
else {
Expand Down

0 comments on commit 925206e

Please sign in to comment.