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

parser: fix fn attrs with anon struct param (#17266)

This commit is contained in:
yuyi 2023-02-09 20:10:28 +08:00 committed by GitHub
parent 4c33a92aac
commit 19ba7c5ceb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 17 additions and 2 deletions

View File

@ -0,0 +1,14 @@
// foo.v
[params]
fn hello(person struct { name string }) string {
if person.name == '' {
return 'Hello World!'
} else {
return 'Hello ${person.name}'
}
}
fn main() {
println(hello())
}

View File

@ -11,7 +11,6 @@ fn (mut p Parser) struct_decl(is_anon bool) ast.StructDecl {
p.top_level_statement_start()
// save attributes, they will be changed later in fields
attrs := p.attrs
p.attrs = []
start_pos := p.tok.pos()
mut is_pub := p.tok.kind == .key_pub
if is_pub {
@ -267,6 +266,8 @@ fn (mut p Parser) struct_decl(is_anon bool) ast.StructDecl {
}
// Comments after type (same line)
comments << p.eat_comments()
prev_attrs := p.attrs
p.attrs = []
if p.tok.kind == .lsbr {
p.inside_struct_attr_decl = true
// attrs are stored in `p.attrs`
@ -331,7 +332,7 @@ fn (mut p Parser) struct_decl(is_anon bool) ast.StructDecl {
is_volatile: is_field_volatile
is_deprecated: is_field_deprecated
}
p.attrs = []
p.attrs = prev_attrs
i++
}
p.top_level_statement_end()