Skip to content

Commit d42f55c

Browse files
cdlearycopybara-github
authored andcommitted
[DSLX:fmt] Fix double-comma when ellipsis are used in array expr.
Fixes #1329 PiperOrigin-RevId: 612900025
1 parent b258839 commit d42f55c

File tree

2 files changed

+29
-8
lines changed

2 files changed

+29
-8
lines changed

xls/dslx/fmt/ast_fmt.cc

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -370,12 +370,13 @@ static DocRef FmtFlat(const Array& n, const Comments& comments,
370370
flat_pieces.push_back(FmtJoin<const Expr*>(n.members(), Joiner::kCommaSpace,
371371
FmtExprPtr, comments, arena));
372372
if (n.has_ellipsis()) {
373-
// Note: zero members with ellipsis is invalid at type checking, we may
374-
// choose not to flag it as a parse-time error, in which case we could have
375-
// it in the AST.
373+
// Note: while zero members with ellipsis is invalid at type checking, we
374+
// may choose not to flag it as a parse-time error, in which case we could
375+
// have it in the AST.
376376
if (!n.members().empty()) {
377377
flat_pieces.push_back(arena.comma());
378378
}
379+
379380
flat_pieces.push_back(arena.space());
380381
flat_pieces.push_back(arena.MakeText("..."));
381382
}
@@ -401,11 +402,6 @@ DocRef Fmt(const Array& n, const Comments& comments, DocArena& arena) {
401402
comments, arena));
402403

403404
if (n.has_ellipsis()) {
404-
// Subtle implementation note: The Joiner::CommaBreak1AsGroup puts a
405-
// trailing comma when we're in break mode, so we only insert the comma for
406-
// ellipsis when we're in flat mode.
407-
member_pieces.push_back(arena.MakeFlatChoice(arena.comma(), arena.empty()));
408-
409405
member_pieces.push_back(
410406
ConcatNGroup(arena, {arena.break1(), arena.MakeText("...")}));
411407
}

xls/dslx/fmt/ast_fmt_test.cc

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1802,5 +1802,30 @@ proc csr_8_32_14 {
18021802
)");
18031803
}
18041804

1805+
TEST_F(ModuleFmtTest, GithubIssue1329) {
1806+
Run(R"(struct StructA { data: u64[8] }
1807+
struct StructB { a: StructA }
1808+
struct StructC { b: StructB }
1809+
1810+
#[test]
1811+
fn struct_c_test() {
1812+
assert_eq(
1813+
zero!<StructA>(),
1814+
StructC {
1815+
b:
1816+
StructB {
1817+
a:
1818+
StructA {
1819+
data:
1820+
u64[8]:[
1821+
0x002698ad4b48ead0, 0x1bfb1e0316f2d5de, 0x173a623c9725b477, 0x0, ...
1822+
]
1823+
}
1824+
}
1825+
});
1826+
}
1827+
)");
1828+
}
1829+
18051830
} // namespace
18061831
} // namespace xls::dslx

0 commit comments

Comments
 (0)