From a8e0ced3449a4c282de61f7213aff73d8d45a041 Mon Sep 17 00:00:00 2001 From: yuyi Date: Mon, 18 Dec 2023 21:39:10 +0800 Subject: [PATCH] parser: fix formatting struct decl with comments (#20207) --- vlib/v/fmt/tests/struct_decl_with_comments_expected.vv | 3 +++ vlib/v/fmt/tests/struct_decl_with_comments_input.vv | 4 ++++ vlib/v/parser/struct.v | 4 ++-- 3 files changed, 9 insertions(+), 2 deletions(-) create mode 100644 vlib/v/fmt/tests/struct_decl_with_comments_expected.vv create mode 100644 vlib/v/fmt/tests/struct_decl_with_comments_input.vv diff --git a/vlib/v/fmt/tests/struct_decl_with_comments_expected.vv b/vlib/v/fmt/tests/struct_decl_with_comments_expected.vv new file mode 100644 index 00000000000000..a8d595a7fc3aea --- /dev/null +++ b/vlib/v/fmt/tests/struct_decl_with_comments_expected.vv @@ -0,0 +1,3 @@ +struct ABC { // size: 0x1=1 (source) (123 45 67) 20B + ff [0x20]u8 +} diff --git a/vlib/v/fmt/tests/struct_decl_with_comments_input.vv b/vlib/v/fmt/tests/struct_decl_with_comments_input.vv new file mode 100644 index 00000000000000..f7ce4dc71f6ce8 --- /dev/null +++ b/vlib/v/fmt/tests/struct_decl_with_comments_input.vv @@ -0,0 +1,4 @@ +struct ABC //size: 0x1=1 (source) (123 45 67) 20B +{ + ff[0x20] u8 +} diff --git a/vlib/v/parser/struct.v b/vlib/v/parser/struct.v index 071419adcc6d8f..999312c3fda56a 100644 --- a/vlib/v/parser/struct.v +++ b/vlib/v/parser/struct.v @@ -52,6 +52,7 @@ fn (mut p Parser) struct_decl(is_anon bool) ast.StructDecl { p.table.reset_parsing_type() } generic_types, _ := p.parse_generic_types() + mut pre_comments := p.eat_comments() no_body := p.tok.kind != .lcbr if language == .v && no_body { p.error('`${p.tok.lit}` lacks body') @@ -93,11 +94,10 @@ fn (mut p Parser) struct_decl(is_anon bool) ast.StructDecl { mut is_field_pub := false mut is_field_global := false mut last_line := p.prev_tok.pos().line_nr + 1 - mut pre_comments := []ast.Comment{} mut end_comments := []ast.Comment{} if !no_body { p.check(.lcbr) - pre_comments = p.eat_comments() + pre_comments << p.eat_comments() mut i := 0 for p.tok.kind != .rcbr { mut comments := []ast.Comment{}