parser: support `module:` for immutable private struct fields (#8140)

pull/8159/head
Nick Treleaven 2021-01-17 04:39:44 +00:00 committed by GitHub
parent d16721e5b8
commit df39e7001c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 51 additions and 4 deletions

View File

@ -190,13 +190,15 @@ pub mut:
pub struct StructDecl {
pub:
pos token.Position
name string
gen_types []table.Type
is_pub bool
pos token.Position
name string
gen_types []table.Type
is_pub bool
// _pos fields for vfmt
mut_pos int // mut:
pub_pos int // pub:
pub_mut_pos int // pub mut:
module_pos int // module:
language table.Language
is_union bool
attrs []table.Attr

View File

@ -695,6 +695,8 @@ pub fn (mut f Fmt) struct_decl(node ast.StructDecl) {
f.writeln('pub:')
} else if i == node.pub_mut_pos {
f.writeln('pub mut:')
} else if i == node.module_pos {
f.writeln('module:')
}
end_pos := field.pos.pos + field.pos.len
comments := field.comments

View File

@ -92,6 +92,7 @@ fn (mut p Parser) struct_decl() ast.StructDecl {
mut pub_pos := -1
mut pub_mut_pos := -1
mut global_pos := -1
mut module_pos := -1
mut is_field_mut := false
mut is_field_pub := false
mut is_field_global := false
@ -156,6 +157,17 @@ fn (mut p Parser) struct_decl() ast.StructDecl {
is_field_pub = true
is_field_mut = true
is_field_global = true
} else if p.tok.kind == .key_module {
if module_pos != -1 {
p.error('redefinition of `module` section')
return {}
}
p.next()
p.check(.colon)
module_pos = fields.len
is_field_pub = false
is_field_mut = false
is_field_global = false
}
for p.tok.kind == .comment {
comments << p.comment()
@ -319,6 +331,7 @@ fn (mut p Parser) struct_decl() ast.StructDecl {
mut_pos: mut_pos - embeds.len
pub_pos: pub_pos - embeds.len
pub_mut_pos: pub_mut_pos - embeds.len
module_pos: module_pos - embeds.len
language: language
is_union: is_union
attrs: attrs

View File

@ -0,0 +1,12 @@
vlib/v/parser/tests/struct_module_section.vv:16:3: error: field `i` of struct `S1` is immutable
14 |
15 | mut s := S1{}
16 | s.i++
| ^
17 | mut s2 := S2{}
18 | s2.j++
vlib/v/parser/tests/struct_module_section.vv:18:4: error: field `j` of struct `S2` is immutable
16 | s.i++
17 | mut s2 := S2{}
18 | s2.j++
| ^

View File

@ -0,0 +1,18 @@
struct S1 {
pub mut:
v byte
module:
i int
}
struct S2 {
module:
j int
mut:
v byte
}
mut s := S1{}
s.i++
mut s2 := S2{}
s2.j++