Skip to content

Commit 8f15d65

Browse files
authored
cgen, x.json2: fix anon struct encode (fix #24836) (#24852)
1 parent 52ae3f2 commit 8f15d65

File tree

2 files changed

+26
-1
lines changed

2 files changed

+26
-1
lines changed

vlib/v/gen/c/fn.v

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2767,7 +2767,8 @@ fn (mut g Gen) ref_or_deref_arg(arg ast.CallArg, expected_type ast.Type, lang as
27672767
g.expr_with_cast(arg.expr, arg_typ, expected_type)
27682768
g.write('.data')
27692769
return
2770-
} else if arg.expr is ast.Ident && arg_sym.info is ast.Struct && arg_sym.info.is_anon {
2770+
} else if arg.expr is ast.Ident && arg_sym.info is ast.Struct && arg_sym.info.is_anon
2771+
&& !expected_type.has_flag(.generic) {
27712772
// make anon struct struct compatible with another anon struct declaration
27722773
g.write('*(${g.cc_type(expected_type, false)}*)&')
27732774
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import x.json2
2+
3+
struct Disk {
4+
dev string
5+
size ?struct {
6+
value u64
7+
}
8+
}
9+
10+
fn test_main() {
11+
disk := Disk{
12+
size: struct {
13+
value: 123
14+
}
15+
}
16+
disk_str := json2.encode[Disk](disk)
17+
assert disk_str == '{"dev":"","size":{"value":123}}'
18+
}
19+
20+
fn test_none() {
21+
disk := Disk{}
22+
disk_str := json2.encode[Disk](disk)
23+
assert disk_str == '{"dev":""}'
24+
}

0 commit comments

Comments
 (0)