parser: embed always public and mutable (#7722)

pull/7724/head
Daniel Däschle 2020-12-30 20:17:48 +01:00 committed by GitHub
parent b8af81240a
commit e4edc5925a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 48 additions and 3 deletions

View File

@ -158,7 +158,7 @@ fn (mut p Parser) struct_decl() ast.StructDecl {
field_start_pos := p.tok.position() field_start_pos := p.tok.position()
is_embed := ((p.tok.lit.len > 1 && p.tok.lit[0].is_capital()) || is_embed := ((p.tok.lit.len > 1 && p.tok.lit[0].is_capital()) ||
p.peek_tok.kind == .dot) && 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 field_name := ''
mut typ := table.Type(0) mut typ := table.Type(0)
mut type_pos := token.Position{} mut type_pos := token.Position{}
@ -250,8 +250,16 @@ fn (mut p Parser) struct_decl() ast.StructDecl {
typ: typ typ: typ
default_expr: ast.ex2fe(default_expr) default_expr: ast.ex2fe(default_expr)
has_default_expr: has_default_expr has_default_expr: has_default_expr
is_pub: is_field_pub is_pub: if is_embed {
is_mut: is_field_mut true
} else {
is_field_pub
}
is_mut: if is_embed {
true
} else {
is_field_mut
}
is_global: is_field_global is_global: is_field_global
attrs: p.attrs attrs: p.attrs
} }

View File

@ -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() {

View File

@ -0,0 +1,9 @@
struct Context {}
struct App {
pub mut:
Context
}
fn main() {
}

View File

@ -0,0 +1,10 @@
module field_publicity
pub struct Context {
pub:
name string
}
pub struct App {
Context
}

View File

@ -1,4 +1,5 @@
import flag import flag
import field_publicity
struct Foo { struct Foo {
x int x int
@ -80,6 +81,11 @@ fn test_assign() {
assert h.x == 5 assert h.x == 5
} }
fn test_embed_is_public() {
a := field_publicity.App{}
assert a.Context.name == ''
}
struct Eggs {} struct Eggs {}
fn (f &Eggs) test(x int) int { fn (f &Eggs) test(x int) int {
@ -94,3 +100,8 @@ fn test_embed_method_receiver_ptr() {
b := Breakfast{} b := Breakfast{}
assert b.test(5) == 5 assert b.test(5) == 5
} }
fn test_embed_mutable() {
mut a := field_publicity.App{}
a.Context = field_publicity.Context{}
}