From e4edc5925a526c1a1779e102911d1f3115005889 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20D=C3=A4schle?= Date: Wed, 30 Dec 2020 20:17:48 +0100 Subject: [PATCH] parser: embed always public and mutable (#7722) --- vlib/v/parser/struct.v | 14 +++++++++++--- vlib/v/parser/tests/embed_pub_mut_err.out | 7 +++++++ vlib/v/parser/tests/embed_pub_mut_err.vv | 9 +++++++++ vlib/v/tests/field_publicity/embed.v | 10 ++++++++++ vlib/v/tests/struct_embed_test.v | 11 +++++++++++ 5 files changed, 48 insertions(+), 3 deletions(-) create mode 100644 vlib/v/parser/tests/embed_pub_mut_err.out create mode 100644 vlib/v/parser/tests/embed_pub_mut_err.vv create mode 100644 vlib/v/tests/field_publicity/embed.v diff --git a/vlib/v/parser/struct.v b/vlib/v/parser/struct.v index f2fd7a9eef..b35ed195f9 100644 --- a/vlib/v/parser/struct.v +++ b/vlib/v/parser/struct.v @@ -158,7 +158,7 @@ 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 + language == .v && 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{} @@ -250,8 +250,16 @@ fn (mut p Parser) struct_decl() ast.StructDecl { typ: typ default_expr: ast.ex2fe(default_expr) has_default_expr: has_default_expr - is_pub: is_field_pub - is_mut: is_field_mut + is_pub: if is_embed { + true + } else { + is_field_pub + } + is_mut: if is_embed { + true + } else { + is_field_mut + } is_global: is_field_global attrs: p.attrs } diff --git a/vlib/v/parser/tests/embed_pub_mut_err.out b/vlib/v/parser/tests/embed_pub_mut_err.out new file mode 100644 index 0000000000..59f3c27532 --- /dev/null +++ b/vlib/v/parser/tests/embed_pub_mut_err.out @@ -0,0 +1,7 @@ +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 new file mode 100644 index 0000000000..19563ef2a8 --- /dev/null +++ b/vlib/v/parser/tests/embed_pub_mut_err.vv @@ -0,0 +1,9 @@ +struct Context {} + +struct App { +pub mut: + Context +} + +fn main() { +} diff --git a/vlib/v/tests/field_publicity/embed.v b/vlib/v/tests/field_publicity/embed.v new file mode 100644 index 0000000000..d04fcf51f8 --- /dev/null +++ b/vlib/v/tests/field_publicity/embed.v @@ -0,0 +1,10 @@ +module field_publicity + +pub struct Context { +pub: + name string +} + +pub struct App { + Context +} diff --git a/vlib/v/tests/struct_embed_test.v b/vlib/v/tests/struct_embed_test.v index 1a0e137d01..cf18f2b02b 100644 --- a/vlib/v/tests/struct_embed_test.v +++ b/vlib/v/tests/struct_embed_test.v @@ -1,4 +1,5 @@ import flag +import field_publicity struct Foo { x int @@ -80,6 +81,11 @@ fn test_assign() { assert h.x == 5 } +fn test_embed_is_public() { + a := field_publicity.App{} + assert a.Context.name == '' +} + struct Eggs {} fn (f &Eggs) test(x int) int { @@ -94,3 +100,8 @@ fn test_embed_method_receiver_ptr() { b := Breakfast{} assert b.test(5) == 5 } + +fn test_embed_mutable() { + mut a := field_publicity.App{} + a.Context = field_publicity.Context{} +}