Skip to content

Commit 4bdd9b4

Browse files
committed
Don't emit { } around a single-anon-type deduced return, fixes #408
1 parent 80af754 commit 4bdd9b4

File tree

3 files changed

+33
-12
lines changed

3 files changed

+33
-12
lines changed

regression-tests/test-results/version

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11

2-
cppfront compiler v0.2.1 Build 8809:1848
2+
cppfront compiler v0.2.1 Build 8809:1933
33
Copyright(c) Herb Sutter All rights reserved
44

55
SPDX-License-Identifier: CC-BY-NC-ND-4.0

source/build.info

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
"8809:1848"
1+
"8809:1933"

source/cppfront.cpp

Lines changed: 31 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1073,13 +1073,16 @@ class cppfront
10731073
struct function_return {
10741074
parameter_declaration_list_node* param_list;
10751075
passing_style pass;
1076+
bool is_deduced;
10761077

10771078
function_return(
10781079
parameter_declaration_list_node* param_list_,
1079-
passing_style pass_ = passing_style::invalid
1080+
passing_style pass_ = passing_style::invalid,
1081+
bool is_deduced_ = false
10801082
)
10811083
: param_list{param_list_}
10821084
, pass{pass_}
1085+
, is_deduced{is_deduced_}
10831086
{ }
10841087
};
10851088
std::vector<function_return> function_returns;
@@ -2217,12 +2220,17 @@ class cppfront
22172220
if (n.expression)
22182221
{
22192222
assert(!current_functions.empty());
2220-
2221-
// If we're doing a forward return
2223+
auto is_forward_return =
2224+
!function_returns.empty()
2225+
&& function_returns.back().pass == passing_style::forward;
2226+
auto is_deduced_return =
2227+
!function_returns.empty()
2228+
&& function_returns.back().is_deduced;
2229+
2230+
// If we're doing a forward return of a single-token name
22222231
if (auto tok = n.expression->expr->get_postfix_expression_node()->expr->get_token();
22232232
tok
2224-
&& !function_returns.empty()
2225-
&& function_returns.back().pass == passing_style::forward
2233+
&& is_forward_return
22262234
)
22272235
{
22282236
// Ensure we're not returning a local or an in/move parameter
@@ -2267,9 +2275,13 @@ class cppfront
22672275
// take over direct control of emitting it without needing to
22682276
// go through the whole grammar, and surround it with braces
22692277
if (n.expression->is_expression_list()) {
2270-
printer.print_cpp2( "{ ", n.position() );
2278+
if (!is_deduced_return) {
2279+
printer.print_cpp2( "{ ", n.position() );
2280+
}
22712281
emit(*n.expression->get_expression_list(), false);
2272-
printer.print_cpp2( " }", n.position() );
2282+
if (!is_deduced_return) {
2283+
printer.print_cpp2( " }", n.position() );
2284+
}
22732285
}
22742286
// Otherwise, just emit the general expression as usual
22752287
else {
@@ -3670,9 +3682,17 @@ class cppfront
36703682
&& n.expr->is_expression_list()
36713683
)
36723684
{
3673-
printer.print_cpp2( "{ ", n.position() );
3685+
auto is_deduced_return =
3686+
!function_returns.empty()
3687+
&& function_returns.back().is_deduced;
3688+
3689+
if (!is_deduced_return) {
3690+
printer.print_cpp2( "{ ", n.position() );
3691+
}
36743692
emit(*n.expr->get_expression_list(), false);
3675-
printer.print_cpp2( " }", n.position() );
3693+
if (!is_deduced_return) {
3694+
printer.print_cpp2( " }", n.position() );
3695+
}
36763696
}
36773697
// Otherwise, just emit the general expression as usual
36783698
else {
@@ -5634,7 +5654,8 @@ class cppfront
56345654
else if (func->returns.index() == function_type_node::id) {
56355655
function_returns.emplace_back(
56365656
&single_anon, // use special value as a note
5637-
std::get<function_type_node::id>(func->returns).pass
5657+
std::get<function_type_node::id>(func->returns).pass,
5658+
std::get<function_type_node::id>(func->returns).type->is_wildcard()
56385659
);
56395660
}
56405661
else {

0 commit comments

Comments
 (0)