Skip to content

Commit ce8bbbc

Browse files
committed
fix: lower nested _braced-init-list_
1 parent 896fe85 commit ce8bbbc

File tree

4 files changed

+54
-19
lines changed

4 files changed

+54
-19
lines changed

regression-tests/pure2-bugfix-for-nested-lists.cpp2

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,14 @@ main: () = {
1313
assert(check((17, 29)).x == 17);
1414
assert(check((17, 29)).y == 29);
1515

16-
//board: std::array<std::array<u8, 3>, 3> = ((
17-
// ('O', 'X', 'O'),
18-
// (' ', 'X', 'X'),
19-
// ('X', 'O', 'O')
20-
//));
21-
//assert(board[0] == :std::array<u8, 3> = ('O', 'X', 'O'));
22-
//assert(board[1] == :std::array<u8, 3> = (' ', 'X', 'X'));
23-
//assert(board[2] == :std::array<u8, 3> = ('X', 'O', 'O'));
16+
board: std::array<std::array<u8, 3>, 3> = ((
17+
('O', 'X', 'O'),
18+
(' ', ('X'), 'X'),
19+
('X', 'O', 'O')
20+
));
21+
assert(board[0] == :std::array<u8, 3> = ('O', 'X', 'O'));
22+
assert(board[1] == :std::array<u8, 3> = (' ', 'X', 'X'));
23+
assert(board[2] == :std::array<u8, 3> = ('X', 'O', 'O'));
2424

2525
// Still parentheses
2626
assert((:std::vector = (17, 29)).size() == 2);

regression-tests/test-results/pure2-bugfix-for-nested-lists.cpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -70,14 +70,14 @@ auto main() -> int{
7070
if (cpp2::Default.has_handler() && !(check({17, 29}).x == 17) ) { cpp2::Default.report_violation(""); }
7171
if (cpp2::Default.has_handler() && !(check({17, 29}).y == 29) ) { cpp2::Default.report_violation(""); }
7272

73-
//board: std::array<std::array<u8, 3>, 3> = ((
74-
// ('O', 'X', 'O'),
75-
// (' ', 'X', 'X'),
76-
// ('X', 'O', 'O')
77-
//));
78-
//assert(board[0] == :std::array<u8, 3> = ('O', 'X', 'O'));
79-
//assert(board[1] == :std::array<u8, 3> = (' ', 'X', 'X'));
80-
//assert(board[2] == :std::array<u8, 3> = ('X', 'O', 'O'));
73+
std::array<std::array<cpp2::u8,3>,3> board {{ {
74+
'O', 'X', 'O' }, {
75+
' ', { 'X' }, 'X' }, {
76+
'X', 'O', 'O' } }};
77+
78+
if (cpp2::Default.has_handler() && !(CPP2_ASSERT_IN_BOUNDS(board, 0) == std::array<cpp2::u8,3>{'O', 'X', 'O'}) ) { cpp2::Default.report_violation(""); }
79+
if (cpp2::Default.has_handler() && !(CPP2_ASSERT_IN_BOUNDS(board, 1) == std::array<cpp2::u8,3>{' ', 'X', 'X'}) ) { cpp2::Default.report_violation(""); }
80+
if (cpp2::Default.has_handler() && !(CPP2_ASSERT_IN_BOUNDS(std::move(board), 2) == std::array<cpp2::u8,3>{'X', 'O', 'O'}) ) { cpp2::Default.report_violation(""); }
8181

8282
// Still parentheses
8383
if (cpp2::Default.has_handler() && !(CPP2_UFCS(size)((std::vector{17, 29})) == 2) ) { cpp2::Default.report_violation(""); }

source/parse.h

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6241,8 +6241,28 @@ class parser
62416241
return n;
62426242
}
62436243

6244+
auto add_expression = [&](expression_list_node::term t) {
6245+
std::function<void(expression_list_node::term&)> mark_nested_inside_initializer{
6246+
[&](expression_list_node::term& u) {
6247+
if (
6248+
inside_initializer
6249+
&& u.expr->is_expression_list()
6250+
)
6251+
{
6252+
auto l = const_cast<expression_list_node*>(u.expr->get_expression_list());
6253+
l->inside_initializer = true;
6254+
for (auto& e : l->expressions) {
6255+
mark_nested_inside_initializer(e);
6256+
}
6257+
}
6258+
}
6259+
};
6260+
mark_nested_inside_initializer(t);
6261+
n->expressions.push_back(std::move(t));
6262+
};
6263+
62446264
// Otherwise remember the first expression
6245-
n->expressions.push_back( { pass, std::move(x) } );
6265+
add_expression( { pass, std::move(x) } );
62466266
// and see if there are more...
62476267
while (curr().type() == lexeme::Comma) {
62486268
next();
@@ -6261,7 +6281,7 @@ class parser
62616281
error("invalid text in expression list", true, {}, true);
62626282
return {};
62636283
}
6264-
n->expressions.push_back( { pass, std::move(expr) } );
6284+
add_expression( { pass, std::move(expr) } );
62656285
}
62666286
return n;
62676287
}

source/to_cpp1.h

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3929,7 +3929,22 @@ class cppfront
39293929

39303930
assert(x.expr);
39313931
current_args.push_back( {x.pass} );
3932-
emit(*x.expr);
3932+
// In a nested expression-list in an initializer, we can
3933+
// take over direct control of emitting it without needing to
3934+
// go through the whole grammar, and surround it with braces
3935+
if (
3936+
n.inside_initializer
3937+
&& x.expr->is_expression_list()
3938+
)
3939+
{
3940+
printer.print_cpp2( "{ ", n.position() );
3941+
emit(*x.expr->get_expression_list(), false);
3942+
printer.print_cpp2( " }", n.position() );
3943+
}
3944+
// Otherwise, just emit the general expression as usual
3945+
else {
3946+
emit(*x.expr);
3947+
}
39333948
current_args.pop_back();
39343949

39353950
if (is_out) {

0 commit comments

Comments
 (0)