1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

checker, parser, fmt: fix visibility of anon struct in different modules.(fix #15763) (#15787)

This commit is contained in:
shove 2022-09-18 23:08:33 +08:00 committed by GitHub
parent 0ff74dae63
commit 202585e175
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 28 additions and 4 deletions

View File

@ -290,8 +290,9 @@ pub fn (mut c Checker) struct_init(mut node ast.StructInit) ast.Type {
&& c.table.cur_concrete_types.len == 0 {
pos := type_sym.name.last_index('.') or { -1 }
first_letter := type_sym.name[pos + 1]
if !first_letter.is_capital() && type_sym.kind != .placeholder
&& !type_sym.name.starts_with('main._VAnonStruct') {
if !first_letter.is_capital()
&& (type_sym.kind != .struct_ || !(type_sym.info as ast.Struct).is_anon)
&& type_sym.kind != .placeholder {
c.error('cannot initialize builtin type `$type_sym.name`', node.pos)
}
}

View File

@ -0,0 +1 @@
foo.bar.baz == 1

View File

@ -0,0 +1,9 @@
module amod
pub struct Foo {
pub mut:
bar struct {
pub mut:
baz int
}
}

View File

@ -0,0 +1,10 @@
import v.checker.tests.anon_structs_visibility.amod
fn main() {
foo := amod.Foo{
bar: struct {
baz: 1
}
}
println('foo.bar.baz == $foo.bar.baz')
}

View File

@ -8,7 +8,7 @@ import v.ast
pub fn (mut f Fmt) struct_decl(node ast.StructDecl, is_anon bool) {
f.attrs(node.attrs)
if node.is_pub {
if node.is_pub && !is_anon {
f.write('pub ')
}
if node.is_union {

View File

@ -14,10 +14,13 @@ fn (mut p Parser) struct_decl(is_anon bool) ast.StructDecl {
attrs := p.attrs
p.attrs = []
start_pos := p.tok.pos()
is_pub := p.tok.kind == .key_pub
mut is_pub := p.tok.kind == .key_pub
if is_pub {
p.next()
}
if is_anon {
is_pub = true
}
is_union := p.tok.kind == .key_union
if p.tok.kind == .key_struct {
p.next()