Skip to content

Commit 19ba7c5

Browse files
authored
parser: fix fn attrs with anon struct param (#17266)
1 parent 4c33a92 commit 19ba7c5

File tree

2 files changed

+17
-2
lines changed

2 files changed

+17
-2
lines changed
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// foo.v
2+
3+
[params]
4+
fn hello(person struct { name string }) string {
5+
if person.name == '' {
6+
return 'Hello World!'
7+
} else {
8+
return 'Hello ${person.name}'
9+
}
10+
}
11+
12+
fn main() {
13+
println(hello())
14+
}

vlib/v/parser/struct.v

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ fn (mut p Parser) struct_decl(is_anon bool) ast.StructDecl {
1111
p.top_level_statement_start()
1212
// save attributes, they will be changed later in fields
1313
attrs := p.attrs
14-
p.attrs = []
1514
start_pos := p.tok.pos()
1615
mut is_pub := p.tok.kind == .key_pub
1716
if is_pub {
@@ -267,6 +266,8 @@ fn (mut p Parser) struct_decl(is_anon bool) ast.StructDecl {
267266
}
268267
// Comments after type (same line)
269268
comments << p.eat_comments()
269+
prev_attrs := p.attrs
270+
p.attrs = []
270271
if p.tok.kind == .lsbr {
271272
p.inside_struct_attr_decl = true
272273
// attrs are stored in `p.attrs`
@@ -331,7 +332,7 @@ fn (mut p Parser) struct_decl(is_anon bool) ast.StructDecl {
331332
is_volatile: is_field_volatile
332333
is_deprecated: is_field_deprecated
333334
}
334-
p.attrs = []
335+
p.attrs = prev_attrs
335336
i++
336337
}
337338
p.top_level_statement_end()

0 commit comments

Comments
 (0)