From 8a0fc2e3c21ba3779380d74dbbafde05a675ef02 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20D=C3=A4schle?= Date: Wed, 6 Jan 2021 15:46:36 +0100 Subject: [PATCH] parser: proper error when declaring struct embedding at the wrong pos (#7920) --- vlib/v/parser/struct.v | 8 +++++++- vlib/v/parser/tests/embed_pub_mut_err.out | 7 ------- vlib/v/parser/tests/embed_pub_mut_err.vv | 9 --------- vlib/v/parser/tests/struct_embed_wrong_pos_long_err.out | 7 +++++++ vlib/v/parser/tests/struct_embed_wrong_pos_long_err.vv | 6 ++++++ vlib/v/parser/tests/struct_embed_wrong_pos_short_err.out | 7 +++++++ vlib/v/parser/tests/struct_embed_wrong_pos_short_err.vv | 8 ++++++++ 7 files changed, 35 insertions(+), 17 deletions(-) delete mode 100644 vlib/v/parser/tests/embed_pub_mut_err.out delete mode 100644 vlib/v/parser/tests/embed_pub_mut_err.vv create mode 100644 vlib/v/parser/tests/struct_embed_wrong_pos_long_err.out create mode 100644 vlib/v/parser/tests/struct_embed_wrong_pos_long_err.vv create mode 100644 vlib/v/parser/tests/struct_embed_wrong_pos_short_err.out create mode 100644 vlib/v/parser/tests/struct_embed_wrong_pos_short_err.vv diff --git a/vlib/v/parser/struct.v b/vlib/v/parser/struct.v index 26279c5e25..bc5c6e5098 100644 --- a/vlib/v/parser/struct.v +++ b/vlib/v/parser/struct.v @@ -158,7 +158,8 @@ fn (mut p Parser) struct_decl() ast.StructDecl { field_start_pos := p.tok.position() is_embed := ((p.tok.lit.len > 1 && p.tok.lit[0].is_capital()) || p.peek_tok.kind == .dot) && - language == .v && ast_fields.len == 0 && !(is_field_mut || is_field_mut || is_field_global) + language == .v + is_on_top := ast_fields.len == 0 && !(is_field_mut || is_field_mut || is_field_global) mut field_name := '' mut typ := table.Type(0) mut type_pos := token.Position{} @@ -174,6 +175,11 @@ fn (mut p Parser) struct_decl() ast.StructDecl { } } type_pos = type_pos.extend(p.prev_tok.position()) + if !is_on_top { + p.error_with_pos('struct embedding must be declared at the beginning of the struct body', + type_pos) + return ast.StructDecl{} + } sym := p.table.get_type_symbol(typ) if typ in embed_types { p.error_with_pos('cannot embed `$sym.name` more than once', type_pos) diff --git a/vlib/v/parser/tests/embed_pub_mut_err.out b/vlib/v/parser/tests/embed_pub_mut_err.out deleted file mode 100644 index 59f3c27532..0000000000 --- a/vlib/v/parser/tests/embed_pub_mut_err.out +++ /dev/null @@ -1,7 +0,0 @@ -vlib/v/parser/tests/embed_pub_mut_err.vv:6:1: error: expecting type declaration - 4 | pub mut: - 5 | Context - 6 | } - | ^ - 7 | - 8 | fn main() { \ No newline at end of file diff --git a/vlib/v/parser/tests/embed_pub_mut_err.vv b/vlib/v/parser/tests/embed_pub_mut_err.vv deleted file mode 100644 index 19563ef2a8..0000000000 --- a/vlib/v/parser/tests/embed_pub_mut_err.vv +++ /dev/null @@ -1,9 +0,0 @@ -struct Context {} - -struct App { -pub mut: - Context -} - -fn main() { -} diff --git a/vlib/v/parser/tests/struct_embed_wrong_pos_long_err.out b/vlib/v/parser/tests/struct_embed_wrong_pos_long_err.out new file mode 100644 index 0000000000..a4bc579e13 --- /dev/null +++ b/vlib/v/parser/tests/struct_embed_wrong_pos_long_err.out @@ -0,0 +1,7 @@ +vlib/v/parser/tests/struct_embed_wrong_pos_long_err.vv:4:2: error: struct embedding must be declared at the beginning of the struct body + 2 | struct Foo2 { + 3 | mut: + 4 | cli.Command + | ~~~~~~~~~~~ + 5 | } + 6 | fn main() {} \ No newline at end of file diff --git a/vlib/v/parser/tests/struct_embed_wrong_pos_long_err.vv b/vlib/v/parser/tests/struct_embed_wrong_pos_long_err.vv new file mode 100644 index 0000000000..8dcc62ef17 --- /dev/null +++ b/vlib/v/parser/tests/struct_embed_wrong_pos_long_err.vv @@ -0,0 +1,6 @@ +import cli +struct Foo2 { +mut: + cli.Command +} +fn main() {} \ No newline at end of file diff --git a/vlib/v/parser/tests/struct_embed_wrong_pos_short_err.out b/vlib/v/parser/tests/struct_embed_wrong_pos_short_err.out new file mode 100644 index 0000000000..04240f82c0 --- /dev/null +++ b/vlib/v/parser/tests/struct_embed_wrong_pos_short_err.out @@ -0,0 +1,7 @@ +vlib/v/parser/tests/struct_embed_wrong_pos_short_err.vv:6:2: error: struct embedding must be declared at the beginning of the struct body + 4 | struct Foo2 { + 5 | mut: + 6 | Foo + | ~~~ + 7 | } + 8 | fn main() {} \ No newline at end of file diff --git a/vlib/v/parser/tests/struct_embed_wrong_pos_short_err.vv b/vlib/v/parser/tests/struct_embed_wrong_pos_short_err.vv new file mode 100644 index 0000000000..cc270fa664 --- /dev/null +++ b/vlib/v/parser/tests/struct_embed_wrong_pos_short_err.vv @@ -0,0 +1,8 @@ +struct Foo { + foo string +} +struct Foo2 { +mut: + Foo +} +fn main() {} \ No newline at end of file