From e414b54f681567adb65acc30f9bfdb2f549428c6 Mon Sep 17 00:00:00 2001 From: yuyi Date: Thu, 31 Aug 2023 17:15:35 +0800 Subject: [PATCH] parser: check struct embed with wrong position (#19245) --- vlib/v/fmt/tests/structs_input.vv | 2 +- vlib/v/parser/struct.v | 2 +- .../tests/struct_embed_wrong_pos_in_pub_err.out | 7 +++++++ .../parser/tests/struct_embed_wrong_pos_in_pub_err.vv | 11 +++++++++++ 4 files changed, 20 insertions(+), 2 deletions(-) create mode 100644 vlib/v/parser/tests/struct_embed_wrong_pos_in_pub_err.out create mode 100644 vlib/v/parser/tests/struct_embed_wrong_pos_in_pub_err.vv diff --git a/vlib/v/fmt/tests/structs_input.vv b/vlib/v/fmt/tests/structs_input.vv index c17259a310..976eeb6c4e 100644 --- a/vlib/v/fmt/tests/structs_input.vv +++ b/vlib/v/fmt/tests/structs_input.vv @@ -8,8 +8,8 @@ struct User { } struct FamousUser { -pub: User +pub: aka string } diff --git a/vlib/v/parser/struct.v b/vlib/v/parser/struct.v index e646a052e0..912c70213b 100644 --- a/vlib/v/parser/struct.v +++ b/vlib/v/parser/struct.v @@ -197,7 +197,7 @@ fn (mut p Parser) struct_decl(is_anon bool) ast.StructDecl { && (p.peek_tok.line_nr != p.tok.line_nr || p.peek_tok.kind !in [.name, .amp]) && (p.peek_tok.kind != .lsbr || p.peek_token(2).kind != .rsbr)) || p.peek_tok.kind == .dot) && language == .v && p.peek_tok.kind != .key_fn - is_on_top := ast_fields.len == 0 && !(is_field_mut || is_field_global) + is_on_top := ast_fields.len == 0 && !(is_field_pub || is_field_mut || is_field_global) mut field_name := '' mut typ := ast.Type(0) mut type_pos := token.Pos{} diff --git a/vlib/v/parser/tests/struct_embed_wrong_pos_in_pub_err.out b/vlib/v/parser/tests/struct_embed_wrong_pos_in_pub_err.out new file mode 100644 index 0000000000..23b9088f26 --- /dev/null +++ b/vlib/v/parser/tests/struct_embed_wrong_pos_in_pub_err.out @@ -0,0 +1,7 @@ +vlib/v/parser/tests/struct_embed_wrong_pos_in_pub_err.vv:6:5: error: struct embedding must be declared at the beginning of the struct body + 4 | pub struct ThreadSafeLog { + 5 | pub: + 6 | Log + | ~~~ + 7 | pub mut: + 8 | mu Xyz diff --git a/vlib/v/parser/tests/struct_embed_wrong_pos_in_pub_err.vv b/vlib/v/parser/tests/struct_embed_wrong_pos_in_pub_err.vv new file mode 100644 index 0000000000..54e6398738 --- /dev/null +++ b/vlib/v/parser/tests/struct_embed_wrong_pos_in_pub_err.vv @@ -0,0 +1,11 @@ +struct Log {} +struct Xyz {} + +pub struct ThreadSafeLog { +pub: + Log +pub mut: + mu Xyz +} + +fn main() {}