From e6f243eaf7abf0e0c94e8119c285455e8518dfa0 Mon Sep 17 00:00:00 2001 From: Chris Leary Date: Wed, 22 Nov 2023 13:44:07 -0800 Subject: [PATCH] [DSLX:fmt] Re-enable autofmt tests with explicit struct instantiations. PiperOrigin-RevId: 584710766 --- xls/dslx/fmt/ast_fmt.cc | 42 +++++++++++--------- xls/dslx/fmt/ast_fmt_test.cc | 15 +++++++ xls/dslx/tests/BUILD | 4 -- xls/dslx/tests/explicit_parametric.x | 25 +++++------- xls/dslx/tests/explicit_parametric_reduced.x | 19 +++------ 5 files changed, 54 insertions(+), 51 deletions(-) diff --git a/xls/dslx/fmt/ast_fmt.cc b/xls/dslx/fmt/ast_fmt.cc index bcb8440e16..90cf539bab 100644 --- a/xls/dslx/fmt/ast_fmt.cc +++ b/xls/dslx/fmt/ast_fmt.cc @@ -54,6 +54,28 @@ DocRef Fmt(const NameDefTree& n, const Comments& comments, DocArena& arena); DocRef FmtExpr(const Expr& n, const Comments& comments, DocArena& arena, bool suppress_parens); +// A parametric argument, as in parametric instantiation: +// +// f() +// ^^^^^^^^^^~~~ parametric argument, expr or types can go here generally +DocRef FmtParametricArg(const ExprOrType& n, const Comments& comments, + DocArena& arena) { + return absl::visit( + Visitor{ + [&](const Expr* n) { + DocRef guts = Fmt(*n, comments, arena); + if (dynamic_cast(n) != nullptr || + dynamic_cast(n) != nullptr || + dynamic_cast(n) != nullptr) { + return guts; // No need for enclosing curlies. + } + return ConcatN(arena, {arena.ocurl(), guts, arena.ccurl()}); + }, + [&](const TypeAnnotation* n) { return Fmt(*n, comments, arena); }, + }, + n); +} + static DocRef FmtExprPtr(const Expr* n, const Comments& comments, DocArena& arena) { XLS_CHECK(n != nullptr); @@ -205,7 +227,7 @@ DocRef Fmt(const TypeRefTypeAnnotation& n, const Comments& comments, if (!n.parametrics().empty()) { pieces.push_back(arena.oangle()); pieces.push_back(FmtJoin(absl::MakeConstSpan(n.parametrics()), - Joiner::kCommaSpace, FmtExprOrType, + Joiner::kCommaSpace, FmtParametricArg, comments, arena)); pieces.push_back(arena.cangle()); } @@ -797,24 +819,6 @@ DocRef FmtExprOrType(const ExprOrType& n, const Comments& comments, n); } -static DocRef FmtParametricArg(const ExprOrType& n, const Comments& comments, - DocArena& arena) { - return absl::visit( - Visitor{ - [&](const Expr* n) { - DocRef guts = Fmt(*n, comments, arena); - if (dynamic_cast(n) != nullptr || - dynamic_cast(n) != nullptr || - dynamic_cast(n) != nullptr) { - return guts; // No need for enclosing curlies. - } - return ConcatN(arena, {arena.ocurl(), guts, arena.ccurl()}); - }, - [&](const TypeAnnotation* n) { return Fmt(*n, comments, arena); }, - }, - n); -} - DocRef Fmt(const Invocation& n, const Comments& comments, DocArena& arena) { DocRef callee_doc = Fmt(*n.callee(), comments, arena); diff --git a/xls/dslx/fmt/ast_fmt_test.cc b/xls/dslx/fmt/ast_fmt_test.cc index e210c75091..5795d9e161 100644 --- a/xls/dslx/fmt/ast_fmt_test.cc +++ b/xls/dslx/fmt/ast_fmt_test.cc @@ -1143,6 +1143,21 @@ fn f() -> u8 { p<8>(u8:42) } } } +TEST(ModuleFmtTest, SimpleParametricStructInstantiation) { + const std::string_view kProgram = + R"(import mol + +struct Point { x: bits[N], y: bits[N] } + +fn f() -> Point { Point { x: u8:42, y: u8:64 } } +)"; + std::vector comments; + XLS_ASSERT_OK_AND_ASSIGN(std::unique_ptr m, + ParseModule(kProgram, "fake.x", "fake", &comments)); + std::string got = AutoFmt(*m, Comments::Create(comments)); + EXPECT_EQ(got, kProgram); +} + TEST(ModuleFmtTest, TypeRefTypeAnnotationModuleLevel) { constexpr std::string_view kProgram = R"(type MyU32 = u32; diff --git a/xls/dslx/tests/BUILD b/xls/dslx/tests/BUILD index dc9d97888f..b529ce5b3b 100644 --- a/xls/dslx/tests/BUILD +++ b/xls/dslx/tests/BUILD @@ -286,8 +286,6 @@ dslx_lang_test(name = "constexpr_pad_via_slice") dslx_lang_test( name = "explicit_parametric", dslx_entry = "instantiates_indirect_foo", - # TODO(leary): 2023-10-20 Explicit instantiations of structs get deleted. - test_autofmt = False, ) dslx_lang_test( @@ -300,8 +298,6 @@ dslx_lang_test( name = "explicit_parametric_reduced", # No meaningful entry point to convert. convert_to_ir = False, - # TODO(leary): 2023-10-20 Explicit instantiations of structs get deleted. - test_autofmt = False, ) dslx_lang_test( diff --git a/xls/dslx/tests/explicit_parametric.x b/xls/dslx/tests/explicit_parametric.x index 7aa63d7410..ecf0ec3284 100644 --- a/xls/dslx/tests/explicit_parametric.x +++ b/xls/dslx/tests/explicit_parametric.x @@ -13,33 +13,28 @@ // limitations under the License. // Tests for explicit instantiations of parametric structs and functions. -struct Generic { - a: bits[X], - b: bits[Y] -} +struct Generic { a: bits[X], b: bits[Y] } -pub fn foo(a: bits[4]) -> Generic<4, 8> { - Generic{ a: a, b: bits[8]:0 } -} +pub fn foo(a: bits[4]) -> Generic<4, 8> { Generic { a, b: bits[8]:0 } } pub fn indirect_foo(a: bits[4]) -> Generic { - Generic<{X as u32}, u32:8>{ a: a as bits[X], b: bits[8]:32 } + Generic<{X as u32}, u32:8> { a: a as bits[X], b: bits[8]:32 } } pub fn instantiates_indirect_foo(a: bits[16]) -> Generic<16, 8> { - indirect_foo(a as bits[4]) + indirect_foo(a as bits[4]) } -pub fn parameterized_zero(x: bits[C]) -> Generic { - Generic{ a: bits[C]:0, b: bits[D]:1 } +pub fn parameterized_zero(x: bits[C]) -> Generic { + Generic { a: bits[C]:0, b: bits[D]:1 } } -pub fn two_param_indirect(value: bits[E]) -> Generic { - parameterized_zero(value) +pub fn two_param_indirect(value: bits[E]) -> Generic { + parameterized_zero(value) } #[test] fn generic() { - let actual = two_param_indirect(u1:0); - assert_eq(actual, Generic<1, 2>{a: u1:0, b: u2:1}); + let actual = two_param_indirect(u1:0); + assert_eq(actual, Generic<1, 2> { a: u1:0, b: u2:1 }); } diff --git a/xls/dslx/tests/explicit_parametric_reduced.x b/xls/dslx/tests/explicit_parametric_reduced.x index e91c5dda4c..e53655bb1f 100644 --- a/xls/dslx/tests/explicit_parametric_reduced.x +++ b/xls/dslx/tests/explicit_parametric_reduced.x @@ -12,22 +12,15 @@ // See the License for the specific language governing permissions and // limitations under the License. -struct Generic { - a: bits[X], - b: bits[Y] -} +struct Generic { a: bits[X], b: bits[Y] } -fn zero() -> Generic { - Generic{ a: bits[C]:0, b: bits[D]:0 } -} +fn zero() -> Generic { Generic { a: bits[C]:0, b: bits[D]:0 } } -fn indirection() -> Generic { - zero() -} +fn indirection() -> Generic { zero() } #[test] fn test_generic() { - let got = indirection(); - let want = Generic{ a: u1:0, b: u2:0 }; - assert_eq(want, got) + let got = indirection(); + let want = Generic { a: u1:0, b: u2:0 }; + assert_eq(want, got) }