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

parser: disallow using attributes on embedded structs (#15901)

This commit is contained in:
Swastik Baranwal 2022-09-28 19:45:33 +05:30 committed by GitHub
parent 1ac3f3d8dc
commit 1ff1f23d9a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 24 additions and 0 deletions

View File

@ -224,6 +224,9 @@ fn (mut p Parser) struct_decl(is_anon bool) ast.StructDecl {
p.error_with_pos('duplicate field `$field_name`', type_pos)
return ast.StructDecl{}
}
if p.tok.kind == .lsbr {
p.error('cannot use attributes on embedded structs')
}
embed_field_names << field_name
embed_types << typ
embeds << ast.Embed{

View File

@ -0,0 +1,7 @@
vlib/v/parser/tests/embeded_struct_attribute_err.vv:8:7: error: cannot use attributes on embedded structs
6 |
7 | struct Button {
8 | Size [json: size] // fmt removes [json: size]
| ^
9 | }
10 |

View File

@ -0,0 +1,14 @@
import json
struct Size {
width int
}
struct Button {
Size [json: size] // fmt removes [json: size]
}
fn main() {
a := Button{}
println(json.encode(a)) // {"size":{"width":0}}
}